Skip to content

Commit

Permalink
Sync pythagorean-triplet with problem-specifications (#1788)
Browse files Browse the repository at this point in the history
[no important files changed]
  • Loading branch information
senekor authored Nov 20, 2023
1 parent a6688ba commit 41fd16e
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 44 deletions.
14 changes: 14 additions & 0 deletions exercises/practice/pythagorean-triplet/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
use std::collections::HashSet;
{% for test in cases %}
#[test]
{% if loop.index != 1 -%}
#[ignore]
{% endif -%}
fn {{ test.description | slugify | replace(from="-", to="_") }}() {
let input = {{ test.input.n | json_encode() }};
let output = {{ crate_name }}::{{ fn_names[0] }}(input);
let expected = {{ test.expected | json_encode() }};
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}
{% endfor -%}
34 changes: 31 additions & 3 deletions exercises/practice/pythagorean-triplet/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,31 @@
# 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.

[a19de65d-35b8-4480-b1af-371d9541e706]
description = "triplets whose sum is 12"

[48b21332-0a3d-43b2-9a52-90b2a6e5c9f5]
description = "triplets whose sum is 108"

[dffc1266-418e-4daa-81af-54c3e95c3bb5]
description = "triplets whose sum is 1000"

[5f86a2d4-6383-4cce-93a5-e4489e79b186]
description = "no matching triplets for 1001"

[bf17ba80-1596-409a-bb13-343bdb3b2904]
description = "returns all matching triplets"

[9d8fb5d5-6c6f-42df-9f95-d3165963ac57]
description = "several matching triplets"

[f5be5734-8aa0-4bd1-99a2-02adcc4402b4]
description = "triplets for large number"
91 changes: 50 additions & 41 deletions exercises/practice/pythagorean-triplet/tests/pythagorean-triplet.rs
Original file line number Diff line number Diff line change
@@ -1,76 +1,85 @@
use pythagorean_triplet::find;
use std::collections::HashSet;

fn process_tripletswithsum_case(sum: u32, expected: &[[u32; 3]]) {
let triplets = find(sum);

if !expected.is_empty() {
let expected: HashSet<_> = expected.iter().cloned().collect();

assert_eq!(expected, triplets);
} else {
assert!(triplets.is_empty());
}
}

#[test]
fn triplets_whose_sum_is_12() {
process_tripletswithsum_case(12, &[[3, 4, 5]]);
let input = 12;
let output = pythagorean_triplet::find(input);
let expected = [[3, 4, 5]];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn triplets_whose_sum_is_108() {
process_tripletswithsum_case(108, &[[27, 36, 45]]);
let input = 108;
let output = pythagorean_triplet::find(input);
let expected = [[27, 36, 45]];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn triplets_whose_sum_is_1000() {
process_tripletswithsum_case(1000, &[[200, 375, 425]]);
let input = 1000;
let output = pythagorean_triplet::find(input);
let expected = [[200, 375, 425]];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn no_matching_triplets_for_1001() {
process_tripletswithsum_case(1001, &[]);
let input = 1001;
let output = pythagorean_triplet::find(input);
let expected = [];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn returns_all_matching_triplets() {
process_tripletswithsum_case(90, &[[9, 40, 41], [15, 36, 39]]);
let input = 90;
let output = pythagorean_triplet::find(input);
let expected = [[9, 40, 41], [15, 36, 39]];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn several_matching_triplets() {
process_tripletswithsum_case(
840,
&[
[40, 399, 401],
[56, 390, 394],
[105, 360, 375],
[120, 350, 370],
[140, 336, 364],
[168, 315, 357],
[210, 280, 350],
[240, 252, 348],
],
);
let input = 840;
let output = pythagorean_triplet::find(input);
let expected = [
[40, 399, 401],
[56, 390, 394],
[105, 360, 375],
[120, 350, 370],
[140, 336, 364],
[168, 315, 357],
[210, 280, 350],
[240, 252, 348],
];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn triplets_for_large_number() {
process_tripletswithsum_case(
30_000,
&[
[1200, 14_375, 14_425],
[1875, 14_000, 14_125],
[5000, 12_000, 13_000],
[6000, 11_250, 12_750],
[7500, 10_000, 12_500],
],
);
let input = 30000;
let output = pythagorean_triplet::find(input);
let expected = [
[1200, 14375, 14425],
[1875, 14000, 14125],
[5000, 12000, 13000],
[6000, 11250, 12750],
[7500, 10000, 12500],
];
let expected: HashSet<_> = expected.iter().cloned().collect();
assert_eq!(output, expected);
}

0 comments on commit 41fd16e

Please sign in to comment.