Skip to content

Commit

Permalink
Make number literals more human readable (#1963)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
senekor authored Aug 14, 2024
1 parent 08386e5 commit f29b58c
Show file tree
Hide file tree
Showing 21 changed files with 88 additions and 83 deletions.
1 change: 1 addition & 0 deletions .ignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!problem-specifications
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use collatz_conjecture::*;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let output = collatz({{ test.input.number | json_encode() }});

let output = collatz({{ test.input.number | fmt_num }});
let expected = {% if test.expected is object %}
None
{% else %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use collatz_conjecture::*;
#[test]
fn zero_steps_for_one() {
let output = collatz(1);

let expected = Some(0);
assert_eq!(output, expected);
}
Expand All @@ -12,7 +11,6 @@ fn zero_steps_for_one() {
#[ignore]
fn divide_if_even() {
let output = collatz(16);

let expected = Some(4);
assert_eq!(output, expected);
}
Expand All @@ -21,16 +19,14 @@ fn divide_if_even() {
#[ignore]
fn even_and_odd_steps() {
let output = collatz(12);

let expected = Some(9);
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn large_number_of_even_and_odd_steps() {
let output = collatz(1000000);

let output = collatz(1_000_000);
let expected = Some(152);
assert_eq!(output, expected);
}
Expand All @@ -39,7 +35,6 @@ fn large_number_of_even_and_odd_steps() {
#[ignore]
fn zero_is_an_error() {
let output = collatz(0);

let expected = None;
assert_eq!(output, expected);
}
2 changes: 1 addition & 1 deletion exercises/practice/eliuds-eggs/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use eliuds_eggs::*;
#[test]
#[ignore]
fn test_{{ test.description | snake_case }}() {
let input = {{ test.input.number }};
let input = {{ test.input.number | fmt_num }};
let output = egg_count(input);
let expected = {{ test.expected | json_encode() }};
assert_eq!(output, expected);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/eliuds-eggs/tests/eliuds-eggs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ fn test_4_eggs() {
#[test]
#[ignore]
fn test_13_eggs() {
let input = 2000000000;
let input = 2_000_000_000;
let output = egg_count(input);
let expected = 13;
assert_eq!(output, expected);
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/nth-prime/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use nth_prime::*;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let output = nth({{ test.input.number - 1 }});
let expected = {{ test.expected | json_encode() }};
let output = nth({{ test.input.number - 1 | fmt_num }});
let expected = {{ test.expected | fmt_num }};
assert_eq!(output, expected);
}
{% endfor -%}
4 changes: 2 additions & 2 deletions exercises/practice/nth-prime/tests/nth-prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn sixth_prime() {
#[test]
#[ignore]
fn big_prime() {
let output = nth(10000);
let expected = 104743;
let output = nth(10_000);
let expected = 104_743;
assert_eq!(output, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use perfect_numbers::*;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let input = {{ test.input.number | json_encode() }};
let input = {{ test.input.number | fmt_num }};
let output = classify(input);
{%- if test.expected is object %}
assert!(output.is_none());
Expand Down
6 changes: 3 additions & 3 deletions exercises/practice/perfect-numbers/tests/perfect-numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ fn medium_perfect_number_is_classified_correctly() {
#[test]
#[ignore]
fn large_perfect_number_is_classified_correctly() {
let input = 33550336;
let input = 33_550_336;
let output = classify(input);
let expected = Some(Classification::Perfect);
assert_eq!(output, expected);
Expand All @@ -47,7 +47,7 @@ fn medium_abundant_number_is_classified_correctly() {
#[test]
#[ignore]
fn large_abundant_number_is_classified_correctly() {
let input = 33550335;
let input = 33_550_335;
let output = classify(input);
let expected = Some(Classification::Abundant);
assert_eq!(output, expected);
Expand Down Expand Up @@ -83,7 +83,7 @@ fn medium_deficient_number_is_classified_correctly() {
#[test]
#[ignore]
fn large_deficient_number_is_classified_correctly() {
let input = 33550337;
let input = 33_550_337;
let output = classify(input);
let expected = Some(Classification::Deficient);
assert_eq!(output, expected);
Expand Down
6 changes: 4 additions & 2 deletions exercises/practice/prime-factors/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ use prime_factors::*;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let factors = factors({{ test.input.value }});
let expected = {{ test.expected | json_encode() }};
let factors = factors({{ test.input.value | fmt_num }});
let expected = [{% for factor in test.expected -%}
{{ factor | fmt_num }},
{%- endfor %}];
assert_eq!(factors, expected);
}
{% endfor -%}
6 changes: 3 additions & 3 deletions exercises/practice/prime-factors/tests/prime-factors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ fn product_of_primes_and_non_primes() {
#[test]
#[ignore]
fn product_of_primes() {
let factors = factors(901255);
let factors = factors(901_255);
let expected = [5, 17, 23, 461];
assert_eq!(factors, expected);
}

#[test]
#[ignore]
fn factors_include_a_large_prime() {
let factors = factors(93819012551);
let expected = [11, 9539, 894119];
let factors = factors(93_819_012_551);
let expected = [11, 9_539, 894_119];
assert_eq!(factors, expected);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@ use std::collections::HashSet;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let input = {{ test.input.n | json_encode() }};
let input = {{ test.input.n | fmt_num }};
let output = find(input);
let expected = {{ test.expected | json_encode() }};
let expected = [{% for triple in test.expected -%}
[{% for side in triple -%}
{{ side | fmt_num }},
{%- endfor %}],
{%- endfor %}];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ fn triplets_whose_sum_is_108() {
#[test]
#[ignore]
fn triplets_whose_sum_is_1000() {
let input = 1000;
let input = 1_000;
let output = find(input);
let expected = [[200, 375, 425]];
let expected: HashSet<_> = expected.iter().cloned().collect();
Expand All @@ -34,7 +34,7 @@ fn triplets_whose_sum_is_1000() {
#[test]
#[ignore]
fn no_matching_triplets_for_1001() {
let input = 1001;
let input = 1_001;
let output = find(input);
let expected = [];
let expected: HashSet<_> = expected.iter().cloned().collect();
Expand Down Expand Up @@ -73,14 +73,14 @@ fn several_matching_triplets() {
#[test]
#[ignore]
fn triplets_for_large_number() {
let input = 30000;
let input = 30_000;
let output = find(input);
let expected = [
[1200, 14375, 14425],
[1875, 14000, 14125],
[5000, 12000, 13000],
[6000, 11250, 12750],
[7500, 10000, 12500],
[1_200, 14_375, 14_425],
[1_875, 14_000, 14_125],
[5_000, 12_000, 13_000],
[6_000, 11_250, 12_750],
[7_500, 10_000, 12_500],
];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/say/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use say::*;
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let input = {{ test.input.number | json_encode() }};
let input = {{ test.input.number | fmt_num }};
let output = encode(input);
let expected = {{ test.expected | json_encode() }};
assert_eq!(output, expected);
Expand Down
16 changes: 8 additions & 8 deletions exercises/practice/say/tests/say.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ fn nine_hundred_ninety_nine() {
#[test]
#[ignore]
fn one_thousand() {
let input = 1000;
let input = 1_000;
let output = encode(input);
let expected = "one thousand";
assert_eq!(output, expected);
Expand All @@ -110,7 +110,7 @@ fn one_thousand() {
#[test]
#[ignore]
fn one_thousand_two_hundred_thirty_four() {
let input = 1234;
let input = 1_234;
let output = encode(input);
let expected = "one thousand two hundred thirty-four";
assert_eq!(output, expected);
Expand All @@ -119,7 +119,7 @@ fn one_thousand_two_hundred_thirty_four() {
#[test]
#[ignore]
fn one_million() {
let input = 1000000;
let input = 1_000_000;
let output = encode(input);
let expected = "one million";
assert_eq!(output, expected);
Expand All @@ -128,7 +128,7 @@ fn one_million() {
#[test]
#[ignore]
fn one_million_two_thousand_three_hundred_forty_five() {
let input = 1002345;
let input = 1_002_345;
let output = encode(input);
let expected = "one million two thousand three hundred forty-five";
assert_eq!(output, expected);
Expand All @@ -137,7 +137,7 @@ fn one_million_two_thousand_three_hundred_forty_five() {
#[test]
#[ignore]
fn one_billion() {
let input = 1000000000;
let input = 1_000_000_000;
let output = encode(input);
let expected = "one billion";
assert_eq!(output, expected);
Expand All @@ -146,7 +146,7 @@ fn one_billion() {
#[test]
#[ignore]
fn a_big_number() {
let input = 987654321123;
let input = 987_654_321_123;
let output = encode(input);
let expected = "nine hundred eighty-seven billion six hundred fifty-four million three hundred twenty-one thousand one hundred twenty-three";
assert_eq!(output, expected);
Expand All @@ -155,7 +155,7 @@ fn a_big_number() {
#[test]
#[ignore]
fn max_i64() {
let input = 9223372036854775807;
let input = 9_223_372_036_854_775_807;
let output = encode(input);
let expected = "nine quintillion two hundred twenty-three quadrillion three hundred seventy-two trillion thirty-six billion eight hundred fifty-four million seven hundred seventy-five thousand eight hundred seven";
assert_eq!(output, expected);
Expand All @@ -164,7 +164,7 @@ fn max_i64() {
#[test]
#[ignore]
fn max_u64() {
let input = 18446744073709551615;
let input = 18_446_744_073_709_551_615;
let output = encode(input);
let expected = "eighteen quintillion four hundred forty-six quadrillion seven hundred forty-four trillion seventy-three billion seven hundred nine million five hundred fifty-one thousand six hundred fifteen";
assert_eq!(output, expected);
Expand Down
2 changes: 1 addition & 1 deletion exercises/practice/space-age/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ fn assert_in_delta(expected: f64, actual: f64) {
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
let seconds = {{ test.input.seconds | json_encode() }};
let seconds = {{ test.input.seconds | fmt_num }};
let duration = Duration::from(seconds);
let output = {{ test.input.planet }}::years_during(&duration);
let expected = {{ test.expected | json_encode() }};
Expand Down
16 changes: 8 additions & 8 deletions exercises/practice/space-age/tests/space-age.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ fn assert_in_delta(expected: f64, actual: f64) {

#[test]
fn age_on_earth() {
let seconds = 1000000000;
let seconds = 1_000_000_000;
let duration = Duration::from(seconds);
let output = Earth::years_during(&duration);
let expected = 31.69;
Expand All @@ -20,7 +20,7 @@ fn age_on_earth() {
#[test]
#[ignore]
fn age_on_mercury() {
let seconds = 2134835688;
let seconds = 2_134_835_688;
let duration = Duration::from(seconds);
let output = Mercury::years_during(&duration);
let expected = 280.88;
Expand All @@ -30,7 +30,7 @@ fn age_on_mercury() {
#[test]
#[ignore]
fn age_on_venus() {
let seconds = 189839836;
let seconds = 189_839_836;
let duration = Duration::from(seconds);
let output = Venus::years_during(&duration);
let expected = 9.78;
Expand All @@ -40,7 +40,7 @@ fn age_on_venus() {
#[test]
#[ignore]
fn age_on_mars() {
let seconds = 2129871239;
let seconds = 2_129_871_239;
let duration = Duration::from(seconds);
let output = Mars::years_during(&duration);
let expected = 35.88;
Expand All @@ -50,7 +50,7 @@ fn age_on_mars() {
#[test]
#[ignore]
fn age_on_jupiter() {
let seconds = 901876382;
let seconds = 901_876_382;
let duration = Duration::from(seconds);
let output = Jupiter::years_during(&duration);
let expected = 2.41;
Expand All @@ -60,7 +60,7 @@ fn age_on_jupiter() {
#[test]
#[ignore]
fn age_on_saturn() {
let seconds = 2000000000;
let seconds = 2_000_000_000;
let duration = Duration::from(seconds);
let output = Saturn::years_during(&duration);
let expected = 2.15;
Expand All @@ -70,7 +70,7 @@ fn age_on_saturn() {
#[test]
#[ignore]
fn age_on_uranus() {
let seconds = 1210123456;
let seconds = 1_210_123_456;
let duration = Duration::from(seconds);
let output = Uranus::years_during(&duration);
let expected = 0.46;
Expand All @@ -80,7 +80,7 @@ fn age_on_uranus() {
#[test]
#[ignore]
fn age_on_neptune() {
let seconds = 1821023456;
let seconds = 1_821_023_456;
let duration = Duration::from(seconds);
let output = Neptune::years_during(&duration);
let expected = 0.35;
Expand Down
4 changes: 2 additions & 2 deletions exercises/practice/sum-of-multiples/.meta/test_template.tera
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use sum_of_multiples::*;
#[ignore]
fn {{ test.description | snake_case }}() {
let factors = &{{ test.input.factors | json_encode() }};
let limit = {{ test.input.limit | json_encode() }};
let limit = {{ test.input.limit | fmt_num }};
let output = sum_of_multiples(limit, factors);
let expected = {{ test.expected | json_encode() }};
let expected = {{ test.expected | fmt_num }};
assert_eq!(output, expected);
}
{% endfor -%}
Loading

0 comments on commit f29b58c

Please sign in to comment.