Skip to content

Commit

Permalink
grains: sync
Browse files Browse the repository at this point in the history
part of #1824
  • Loading branch information
senekor committed Aug 16, 2024
1 parent e5968cc commit 6ea3610
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 24 deletions.
22 changes: 22 additions & 0 deletions exercises/practice/grains/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use grains::*;

{% for test in cases.0.cases %}
#[test]
#[ignore]
{% if test.expected is object -%}
#[should_panic]
{% endif -%}
fn {{ test.description | make_ident }}() {
{% if test.expected is number -%}
assert_eq!(square({{ test.input.square }}), {{ test.expected | fmt_num }});
{% else %}
square({{ test.input.square }});
{% endif -%}
}
{% endfor -%}

#[test]
#[ignore]
fn returns_the_total_number_of_grains_on_the_board() {
assert_eq!(grains::total(), 18_446_744_073_709_551_615);
}
2 changes: 2 additions & 0 deletions exercises/practice/grains/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ description = "square 0 raises an exception"

[61974483-eeb2-465e-be54-ca5dde366453]
description = "negative square raises an exception"
include = false
comment = "u64 cannot be negative"

[a95e4374-f32c-45a7-a10d-ffec475c012f]
description = "square greater than 64 raises an exception"
Expand Down
45 changes: 21 additions & 24 deletions exercises/practice/grains/tests/grains.rs
Original file line number Diff line number Diff line change
@@ -1,62 +1,59 @@
fn process_square_case(input: u32, expected: u64) {
assert_eq!(grains::square(input), expected);
}
use grains::*;

#[test]
fn one() {
process_square_case(1, 1);
fn grains_on_square_1() {
assert_eq!(square(1), 1);
}

#[test]
#[ignore]
fn two() {
process_square_case(2, 2);
fn grains_on_square_2() {
assert_eq!(square(2), 2);
}

#[test]
#[ignore]
fn three() {
process_square_case(3, 4);
fn grains_on_square_3() {
assert_eq!(square(3), 4);
}

#[test]
#[ignore]
fn four() {
process_square_case(4, 8);
fn grains_on_square_4() {
assert_eq!(square(4), 8);
}

#[test]
#[ignore]
fn sixteen() {
process_square_case(16, 32_768);
fn grains_on_square_16() {
assert_eq!(square(16), 32_768);
}

#[test]
#[ignore]
fn thirty_two() {
process_square_case(32, 2_147_483_648);
fn grains_on_square_32() {
assert_eq!(square(32), 2_147_483_648);
}

#[test]
#[ignore]
fn sixty_four() {
process_square_case(64, 9_223_372_036_854_775_808);
fn grains_on_square_64() {
assert_eq!(square(64), 9_223_372_036_854_775_808);
}

#[test]
#[ignore]
#[should_panic(expected = "Square must be between 1 and 64")]
fn square_0_raises_an_exception() {
grains::square(0);
#[should_panic]
fn square_0_is_invalid() {
square(0);
}

#[test]
#[ignore]
#[should_panic(expected = "Square must be between 1 and 64")]
fn square_greater_than_64_raises_an_exception() {
grains::square(65);
#[should_panic]
fn square_greater_than_64_is_invalid() {
square(65);
}

#[test]
#[ignore]
fn returns_the_total_number_of_grains_on_the_board() {
Expand Down

0 comments on commit 6ea3610

Please sign in to comment.