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

palindrome-products: sync #1910

Merged
merged 1 commit into from
Apr 24, 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
42 changes: 42 additions & 0 deletions exercises/practice/palindrome-products/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use palindrome_products::*;

{#
These first two custom test cases are for the object-oriented design of the exercise.
They don't fit the structure of the upstream tests, so they're implemented here.
#}

#[test]
#[ignore]
/// test `Palindrome::new` with valid input
fn palindrome_new_return_some() {
for v in [1, 11, 121, 12321, 1234321, 123454321, 543212345] {
assert_eq!(Palindrome::new(v).expect("is a palindrome").into_inner(), v);
}
}

#[test]
#[ignore]
/// test `Palindrome::new` with invalid input
fn palindrome_new_return_none() {
for v in [12, 2322, 23443, 1233211, 8932343] {
assert_eq!(Palindrome::new(v), None);
}
}

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
{%- if test.property == "smallest" %}
let output = palindrome_products({{ test.input.min }}, {{ test.input.max }}).map(|(min, _)| min.into_inner());
{%- else %}
let output = palindrome_products({{ test.input.min }}, {{ test.input.max }}).map(|(_, max)| max.into_inner());
{%- endif%}
{%- if test.expected.error is defined or not test.expected.value %}
let expected = None;
{%- else %}
let expected = Some({{ test.expected.value }});
{%- endif%}
assert_eq!(output, expected);
}
{% endfor -%}
52 changes: 49 additions & 3 deletions exercises/practice/palindrome-products/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
# 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.

[5cff78fe-cf02-459d-85c2-ce584679f887]
description = "find the smallest palindrome from single digit factors"

[0853f82c-5fc4-44ae-be38-fadb2cced92d]
description = "find the largest palindrome from single digit factors"

[66c3b496-bdec-4103-9129-3fcb5a9063e1]
description = "find the smallest palindrome from double digit factors"

[a10682ae-530a-4e56-b89d-69664feafe53]
description = "find the largest palindrome from double digit factors"

[cecb5a35-46d1-4666-9719-fa2c3af7499d]
description = "find the smallest palindrome from triple digit factors"

[edab43e1-c35f-4ea3-8c55-2f31dddd92e5]
description = "find the largest palindrome from triple digit factors"

[4f802b5a-9d74-4026-a70f-b53ff9234e4e]
description = "find the smallest palindrome from four digit factors"

[787525e0-a5f9-40f3-8cb2-23b52cf5d0be]
description = "find the largest palindrome from four digit factors"

[58fb1d63-fddb-4409-ab84-a7a8e58d9ea0]
description = "empty result for smallest if no palindrome in the range"

[9de9e9da-f1d9-49a5-8bfc-3d322efbdd02]
description = "empty result for largest if no palindrome in the range"

[12e73aac-d7ee-4877-b8aa-2aa3dcdb9f8a]
description = "error result for smallest if min is more than max"

[eeeb5bff-3f47-4b1e-892f-05829277bd74]
description = "error result for largest if min is more than max"

[16481711-26c4-42e0-9180-e2e4e8b29c23]
description = "smallest product does not use the smallest factor"
99 changes: 49 additions & 50 deletions exercises/practice/palindrome-products/tests/palindrome-products.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,4 @@
//! This test suite was generated by the rust exercise tool, which can be found at
//! https://github.com/exercism/rust/tree/main/util/exercise

use palindrome_products::{palindrome_products, Palindrome};

/// Process a single test case for the property `smallest`
///
/// All cases for the `smallest` property are implemented
/// in terms of this function.
fn process_smallest_case((from, to): (u64, u64), expected: Option<u64>) {
let min = palindrome_products(from, to).map(|(min, _)| min);
assert_eq!(min.map(|newtype| newtype.into_inner()), expected);
}

/// Process a single test case for the property `largest`
///
/// All cases for the `largest` property are implemented
/// in terms of this function.
fn process_largest_case((from, to): (u64, u64), expected: Option<u64>) {
let max = palindrome_products(from, to).map(|(_, max)| max);
assert_eq!(max.map(|newtype| newtype.into_inner()), expected);
}
use palindrome_products::*;

#[test]
/// test `Palindrome::new` with valid input
Expand All @@ -40,84 +19,104 @@ fn palindrome_new_return_none() {

#[test]
#[ignore]
/// finds the smallest palindrome from single digit factors
fn finds_the_smallest_palindrome_from_single_digit_factors() {
process_smallest_case((1, 9), Some(1));
fn find_the_smallest_palindrome_from_single_digit_factors() {
let output = palindrome_products(1, 9).map(|(min, _)| min.into_inner());
let expected = Some(1);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// finds the largest palindrome from single digit factors
fn finds_the_largest_palindrome_from_single_digit_factors() {
process_largest_case((1, 9), Some(9));
fn find_the_largest_palindrome_from_single_digit_factors() {
let output = palindrome_products(1, 9).map(|(_, max)| max.into_inner());
let expected = Some(9);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find the smallest palindrome from double digit factors
fn find_the_smallest_palindrome_from_double_digit_factors() {
process_smallest_case((10, 99), Some(121));
let output = palindrome_products(10, 99).map(|(min, _)| min.into_inner());
let expected = Some(121);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find the largest palindrome from double digit factors
fn find_the_largest_palindrome_from_double_digit_factors() {
process_largest_case((10, 99), Some(9009));
let output = palindrome_products(10, 99).map(|(_, max)| max.into_inner());
let expected = Some(9009);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find smallest palindrome from triple digit factors
fn find_smallest_palindrome_from_triple_digit_factors() {
process_smallest_case((100, 999), Some(10201));
fn find_the_smallest_palindrome_from_triple_digit_factors() {
let output = palindrome_products(100, 999).map(|(min, _)| min.into_inner());
let expected = Some(10201);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find the largest palindrome from triple digit factors
fn find_the_largest_palindrome_from_triple_digit_factors() {
process_largest_case((100, 999), Some(906609));
let output = palindrome_products(100, 999).map(|(_, max)| max.into_inner());
let expected = Some(906609);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find smallest palindrome from four digit factors
fn find_smallest_palindrome_from_four_digit_factors() {
process_smallest_case((1000, 9999), Some(1002001));
fn find_the_smallest_palindrome_from_four_digit_factors() {
let output = palindrome_products(1000, 9999).map(|(min, _)| min.into_inner());
let expected = Some(1002001);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// find the largest palindrome from four digit factors
fn find_the_largest_palindrome_from_four_digit_factors() {
process_largest_case((1000, 9999), Some(99000099));
let output = palindrome_products(1000, 9999).map(|(_, max)| max.into_inner());
let expected = Some(99000099);
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// empty result for smallest if no palindrome in the range
fn empty_result_for_smallest_if_no_palindrome_in_the_range() {
process_smallest_case((1002, 1003), None);
let output = palindrome_products(1002, 1003).map(|(min, _)| min.into_inner());
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// empty result for largest if no palindrome in the range
fn empty_result_for_largest_if_no_palindrome_in_the_range() {
process_largest_case((15, 15), None);
let output = palindrome_products(15, 15).map(|(_, max)| max.into_inner());
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// error result for smallest if min is more than max
fn error_result_for_smallest_if_min_is_more_than_max() {
process_smallest_case((10000, 1), None);
let output = palindrome_products(10000, 1).map(|(min, _)| min.into_inner());
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
/// error result for largest if min is more than max
fn error_result_for_largest_if_min_is_more_than_max() {
process_largest_case((2, 1), None);
let output = palindrome_products(2, 1).map(|(_, max)| max.into_inner());
let expected = None;
assert_eq!(output, expected);
}

#[test]
#[ignore]
fn smallest_product_does_not_use_the_smallest_factor() {
let output = palindrome_products(3215, 4000).map(|(min, _)| min.into_inner());
let expected = Some(10988901);
assert_eq!(output, expected);
}
senekor marked this conversation as resolved.
Show resolved Hide resolved