Skip to content

Commit

Permalink
Sync sum-of-multiples with problem-specifications (#1765)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
senekor authored Nov 14, 2023
1 parent ec0862b commit dbb64c9
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 22 deletions.
2 changes: 1 addition & 1 deletion exercises/practice/sum-of-multiples/.meta/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
},
"blurb": "Given a number, find the sum of all the multiples of particular numbers up to but not including that number.",
"source": "A variation on Problem 1 at Project Euler",
"source_url": "http://projecteuler.net/problem=1"
"source_url": "https://projecteuler.net/problem=1"
}
13 changes: 13 additions & 0 deletions exercises/practice/sum-of-multiples/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let factors = &{{ test.input.factors | json_encode() }};
let limit = {{ test.input.limit | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(limit, factors);
let expected = {{ test.expected | json_encode() }};
assert_eq!(output, expected);
}
{% endfor -%}
16 changes: 13 additions & 3 deletions exercises/practice/sum-of-multiples/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# This is an auto-generated file. Regular comments will be removed when this
# file is regenerated. Regenerating will not touch any manually added keys,
# so comments can be added in a "comment" key.
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[54aaab5a-ce86-4edc-8b40-d3ab2400a279]
description = "no multiples within limit"
Expand Down Expand Up @@ -46,3 +53,6 @@ description = "the only multiple of 0 is 0"

[c423ae21-a0cb-4ec7-aeb1-32971af5b510]
description = "the factor 0 does not affect the sum of multiples of other factors"

[17053ba9-112f-4ac0-aadb-0519dd836342]
description = "solutions using include-exclude must extend to cardinality greater than 3"
98 changes: 80 additions & 18 deletions exercises/practice/sum-of-multiples/tests/sum-of-multiples.rs
Original file line number Diff line number Diff line change
@@ -1,96 +1,158 @@
use sum_of_multiples::*;

#[test]
fn no_multiples_within_limit() {
assert_eq!(0, sum_of_multiples(1, &[3, 5]))
let factors = &[3, 5];
let limit = 1;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 0;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn one_factor_has_multiples_within_limit() {
assert_eq!(3, sum_of_multiples(4, &[3, 5]))
let factors = &[3, 5];
let limit = 4;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 3;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn more_than_one_multiple_within_limit() {
assert_eq!(9, sum_of_multiples(7, &[3]))
let factors = &[3];
let limit = 7;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 9;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn more_than_one_factor_with_multiples_within_limit() {
assert_eq!(23, sum_of_multiples(10, &[3, 5]))
let factors = &[3, 5];
let limit = 10;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 23;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn each_multiple_is_only_counted_once() {
assert_eq!(2318, sum_of_multiples(100, &[3, 5]))
let factors = &[3, 5];
let limit = 100;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 2318;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn a_much_larger_limit() {
assert_eq!(233_168, sum_of_multiples(1000, &[3, 5]))
let factors = &[3, 5];
let limit = 1000;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 233168;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn three_factors() {
assert_eq!(51, sum_of_multiples(20, &[7, 13, 17]))
let factors = &[7, 13, 17];
let limit = 20;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 51;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn factors_not_relatively_prime() {
assert_eq!(30, sum_of_multiples(15, &[4, 6]))
let factors = &[4, 6];
let limit = 15;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 30;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn some_pairs_of_factors_relatively_prime_and_some_not() {
assert_eq!(4419, sum_of_multiples(150, &[5, 6, 8]))
let factors = &[5, 6, 8];
let limit = 150;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 4419;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn one_factor_is_a_multiple_of_another() {
assert_eq!(275, sum_of_multiples(51, &[5, 25]))
let factors = &[5, 25];
let limit = 51;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 275;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn much_larger_factors() {
assert_eq!(2_203_160, sum_of_multiples(10_000, &[43, 47]))
let factors = &[43, 47];
let limit = 10000;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 2203160;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn all_numbers_are_multiples_of_1() {
assert_eq!(4950, sum_of_multiples(100, &[1]))
let factors = &[1];
let limit = 100;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 4950;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn no_factors_means_an_empty_sum() {
assert_eq!(0, sum_of_multiples(10_000, &[]))
let factors = &[];
let limit = 10000;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 0;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn the_only_multiple_of_0_is_0() {
assert_eq!(0, sum_of_multiples(1, &[0]))
let factors = &[0];
let limit = 1;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 0;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn the_factor_0_does_not_affect_the_sum_of_multiples_of_other_factors() {
assert_eq!(3, sum_of_multiples(4, &[3, 0]))
let factors = &[3, 0];
let limit = 4;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 3;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn solutions_using_include_exclude_must_extend_to_cardinality_greater_than_3() {
assert_eq!(39_614_537, sum_of_multiples(10_000, &[2, 3, 5, 7, 11]))
let factors = &[2, 3, 5, 7, 11];
let limit = 10000;
let output = sum_of_multiples::sum_of_multiples(limit, factors);
let expected = 39614537;
assert_eq!(output, expected);
}

0 comments on commit dbb64c9

Please sign in to comment.