Skip to content

Commit 707429a

Browse files
committed
luhn: sync
part of #1824
1 parent e5968cc commit 707429a

File tree

4 files changed

+131
-54
lines changed

4 files changed

+131
-54
lines changed

exercises/practice/luhn/.docs/instructions.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ The first step of the Luhn algorithm is to double every second digit, starting f
2222
We will be doubling
2323

2424
```text
25-
4_3_ 3_9_ 0_4_ 6_6_
25+
4539 3195 0343 6467
26+
↑ ↑ ↑ ↑ ↑ ↑ ↑ ↑ (double these)
2627
```
2728

2829
If doubling the number results in a number greater than 9 then subtract 9 from the product.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use luhn::*;
2+
3+
{% for test in cases %}
4+
#[test]
5+
#[ignore]
6+
fn {{ test.description | make_ident }}() {
7+
assert_eq!(is_valid({{ test.input.value | json_encode() }}), {{ test.expected }});
8+
}
9+
{% endfor -%}
Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,76 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[792a7082-feb7-48c7-b88b-bbfec160865e]
13+
description = "single digit strings can not be valid"
14+
15+
[698a7924-64d4-4d89-8daa-32e1aadc271e]
16+
description = "a single zero is invalid"
17+
18+
[73c2f62b-9b10-4c9f-9a04-83cee7367965]
19+
description = "a simple valid SIN that remains valid if reversed"
20+
21+
[9369092e-b095-439f-948d-498bd076be11]
22+
description = "a simple valid SIN that becomes invalid if reversed"
23+
24+
[8f9f2350-1faf-4008-ba84-85cbb93ffeca]
25+
description = "a valid Canadian SIN"
26+
27+
[1cdcf269-6560-44fc-91f6-5819a7548737]
28+
description = "invalid Canadian SIN"
29+
30+
[656c48c1-34e8-4e60-9a5a-aad8a367810a]
31+
description = "invalid credit card"
32+
33+
[20e67fad-2121-43ed-99a8-14b5b856adb9]
34+
description = "invalid long number with an even remainder"
35+
36+
[7e7c9fc1-d994-457c-811e-d390d52fba5e]
37+
description = "invalid long number with a remainder divisible by 5"
38+
39+
[ad2a0c5f-84ed-4e5b-95da-6011d6f4f0aa]
40+
description = "valid number with an even number of digits"
41+
42+
[ef081c06-a41f-4761-8492-385e13c8202d]
43+
description = "valid number with an odd number of spaces"
44+
45+
[bef66f64-6100-4cbb-8f94-4c9713c5e5b2]
46+
description = "valid strings with a non-digit added at the end become invalid"
47+
48+
[2177e225-9ce7-40f6-b55d-fa420e62938e]
49+
description = "valid strings with punctuation included become invalid"
50+
51+
[ebf04f27-9698-45e1-9afe-7e0851d0fe8d]
52+
description = "valid strings with symbols included become invalid"
53+
54+
[08195c5e-ce7f-422c-a5eb-3e45fece68ba]
55+
description = "single zero with space is invalid"
56+
57+
[12e63a3c-f866-4a79-8c14-b359fc386091]
58+
description = "more than a single zero is valid"
59+
60+
[ab56fa80-5de8-4735-8a4a-14dae588663e]
61+
description = "input digit 9 is correctly converted to output digit 9"
62+
63+
[b9887ee8-8337-46c5-bc45-3bcab51bc36f]
64+
description = "very long input is valid"
65+
66+
[8a7c0e24-85ea-4154-9cf1-c2db90eabc08]
67+
description = "valid luhn with an odd number of digits and non zero first digit"
68+
69+
[39a06a5a-5bad-4e0f-b215-b042d46209b1]
70+
description = "using ascii value for non-doubled non-digit isn't allowed"
71+
72+
[f94cf191-a62f-4868-bc72-7253114aa157]
73+
description = "using ascii value for doubled non-digit isn't allowed"
74+
75+
[8b72ad26-c8be-49a2-b99c-bcc3bf631b33]
76+
description = "non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed"

exercises/practice/luhn/tests/luhn.rs

Lines changed: 44 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,141 +1,135 @@
11
use luhn::*;
22

3-
fn process_valid_case(number: &str, is_luhn_expected: bool) {
4-
assert_eq!(is_valid(number), is_luhn_expected);
5-
}
6-
73
#[test]
84
fn single_digit_strings_can_not_be_valid() {
9-
process_valid_case("1", false);
5+
assert_eq!(is_valid("1"), false);
106
}
117

128
#[test]
139
#[ignore]
1410
fn a_single_zero_is_invalid() {
15-
process_valid_case("0", false);
11+
assert_eq!(is_valid("0"), false);
1612
}
1713

1814
#[test]
1915
#[ignore]
2016
fn a_simple_valid_sin_that_remains_valid_if_reversed() {
21-
process_valid_case("059", true);
17+
assert_eq!(is_valid("059"), true);
2218
}
2319

2420
#[test]
2521
#[ignore]
2622
fn a_simple_valid_sin_that_becomes_invalid_if_reversed() {
27-
process_valid_case("59", true);
23+
assert_eq!(is_valid("59"), true);
2824
}
2925

3026
#[test]
3127
#[ignore]
3228
fn a_valid_canadian_sin() {
33-
process_valid_case("055 444 285", true);
29+
assert_eq!(is_valid("055 444 285"), true);
3430
}
3531

3632
#[test]
3733
#[ignore]
3834
fn invalid_canadian_sin() {
39-
process_valid_case("055 444 286", false);
35+
assert_eq!(is_valid("055 444 286"), false);
4036
}
4137

4238
#[test]
4339
#[ignore]
4440
fn invalid_credit_card() {
45-
process_valid_case("8273 1232 7352 0569", false);
41+
assert_eq!(is_valid("8273 1232 7352 0569"), false);
4642
}
4743

4844
#[test]
4945
#[ignore]
50-
fn valid_number_with_an_even_number_of_digits() {
51-
process_valid_case("095 245 88", true);
46+
fn invalid_long_number_with_an_even_remainder() {
47+
assert_eq!(is_valid("1 2345 6789 1234 5678 9012"), false);
5248
}
5349

5450
#[test]
5551
#[ignore]
56-
fn strings_that_contain_non_digits_are_invalid() {
57-
process_valid_case("055a 444 285", false);
52+
fn invalid_long_number_with_a_remainder_divisible_by_5() {
53+
assert_eq!(is_valid("1 2345 6789 1234 5678 9013"), false);
5854
}
5955

6056
#[test]
6157
#[ignore]
62-
fn valid_strings_with_punctuation_included_become_invalid() {
63-
process_valid_case("055-444-285", false);
58+
fn valid_number_with_an_even_number_of_digits() {
59+
assert_eq!(is_valid("095 245 88"), true);
6460
}
6561

6662
#[test]
6763
#[ignore]
68-
fn symbols_are_invalid() {
69-
process_valid_case("055£ 444$ 285", false);
64+
fn valid_number_with_an_odd_number_of_spaces() {
65+
assert_eq!(is_valid("234 567 891 234"), true);
7066
}
7167

7268
#[test]
7369
#[ignore]
74-
fn single_zero_with_space_is_invalid() {
75-
process_valid_case(" 0", false);
70+
fn valid_strings_with_a_non_digit_added_at_the_end_become_invalid() {
71+
assert_eq!(is_valid("059a"), false);
7672
}
7773

7874
#[test]
7975
#[ignore]
80-
fn more_than_a_single_zero_is_valid() {
81-
process_valid_case("0000 0", true);
76+
fn valid_strings_with_punctuation_included_become_invalid() {
77+
assert_eq!(is_valid("055-444-285"), false);
8278
}
8379

8480
#[test]
8581
#[ignore]
86-
fn input_digit_9_is_correctly_converted_to_output_digit_9() {
87-
process_valid_case("091", true);
82+
fn valid_strings_with_symbols_included_become_invalid() {
83+
assert_eq!(is_valid("055# 444$ 285"), false);
8884
}
8985

9086
#[test]
9187
#[ignore]
92-
/// using ASCII value for doubled non-digit isn't allowed
93-
/// Convert non-digits to their ASCII values and then offset them by 48 sometimes accidentally declare an invalid string to be valid.
94-
/// This test is designed to avoid that solution.
95-
fn using_ascii_value_for_doubled_nondigit_isnt_allowed() {
96-
process_valid_case(":9", false);
88+
fn single_zero_with_space_is_invalid() {
89+
assert_eq!(is_valid(" 0"), false);
9790
}
9891

9992
#[test]
10093
#[ignore]
101-
/// valid strings with a non-digit added at the end become invalid
102-
fn valid_strings_with_a_nondigit_added_at_the_end_become_invalid() {
103-
process_valid_case("059a", false);
94+
fn more_than_a_single_zero_is_valid() {
95+
assert_eq!(is_valid("0000 0"), true);
10496
}
10597

10698
#[test]
10799
#[ignore]
108-
/// valid strings with symbols included become invalid
109-
fn valid_strings_with_symbols_included_become_invalid() {
110-
process_valid_case("055# 444$ 285", false);
100+
fn input_digit_9_is_correctly_converted_to_output_digit_9() {
101+
assert_eq!(is_valid("091"), true);
111102
}
112103

113104
#[test]
114105
#[ignore]
115-
/// using ASCII value for non-doubled non-digit isn't allowed
116-
/// Convert non-digits to their ASCII values and then offset them by 48 sometimes accidentally declare an invalid string to be valid.
117-
/// This test is designed to avoid that solution.
118-
fn using_ascii_value_for_nondoubled_nondigit_isnt_allowed() {
119-
process_valid_case("055b 444 285", false);
106+
fn very_long_input_is_valid() {
107+
assert_eq!(
108+
is_valid("9999999999 9999999999 9999999999 9999999999"),
109+
true
110+
);
120111
}
121112

122113
#[test]
123114
#[ignore]
124-
/// valid number with an odd number of spaces
125-
fn valid_number_with_an_odd_number_of_spaces() {
126-
process_valid_case("234 567 891 234", true);
115+
fn valid_luhn_with_an_odd_number_of_digits_and_non_zero_first_digit() {
116+
assert_eq!(is_valid("109"), true);
117+
}
118+
119+
#[test]
120+
#[ignore]
121+
fn using_ascii_value_for_non_doubled_non_digit_isn_t_allowed() {
122+
assert_eq!(is_valid("055b 444 285"), false);
127123
}
128124

129125
#[test]
130126
#[ignore]
131-
/// non-numeric, non-space char in the middle with a sum that's divisible by 10 isn't allowed
132-
fn invalid_char_in_middle_with_sum_divisible_by_10_isnt_allowed() {
133-
process_valid_case("59%59", false);
127+
fn using_ascii_value_for_doubled_non_digit_isn_t_allowed() {
128+
assert_eq!(is_valid(":9"), false);
134129
}
135130

136131
#[test]
137132
#[ignore]
138-
/// unicode numeric characters are not allowed in a otherwise valid number
139-
fn valid_strings_with_numeric_unicode_characters_become_invalid() {
140-
process_valid_case("1249①", false);
133+
fn non_numeric_non_space_char_in_the_middle_with_a_sum_that_s_divisible_by_10_isn_t_allowed() {
134+
assert_eq!(is_valid("59%59"), false);
141135
}

0 commit comments

Comments
 (0)