Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grains: sync #1977

Merged
merged 1 commit into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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