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

binary-search: sync #1960

Merged
merged 1 commit into from
Aug 13, 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
2 changes: 1 addition & 1 deletion bin/test_exercise.sh
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ fi
# run tests from within temporary directory
cd "$tmp_dir"
if [ -n "$CLIPPY" ]; then
export RUSTFLAGS="$RUSTFLAGS -D clippy::all"
export RUSTFLAGS="$RUSTFLAGS -D warnings"
# shellcheck disable=SC2086
cargo clippy --tests $cargo_args
else
Expand Down
37 changes: 37 additions & 0 deletions exercises/practice/binary-search/.meta/test_template.tera
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
use binary_search::*;

{% for test in cases %}
#[test]
#[ignore]
fn {{ test.description | snake_case }}() {
assert_eq!(find(&{{ test.input.array | json_encode() }}, {{ test.input.value }}), {% if test.expected is object -%}
None
{%- else -%}
Some({{ test.expected }})
{%- endif -%});
}
{% endfor %}

#[test]
#[ignore]
#[cfg(feature = "generic")]
fn works_for_arrays() {
assert_eq!(find([6], 6), Some(0));
}

#[test]
#[ignore]
#[cfg(feature = "generic")]
fn works_for_vec() {
let vector = vec![6];
assert_eq!(find(&vector, 6), Some(0));
assert_eq!(find(vector, 6), Some(0));
}

#[test]
#[ignore]
#[cfg(feature = "generic")]
fn works_for_str_elements() {
assert_eq!(find(["a"], "a"), Some(0));
assert_eq!(find(["a", "b"], "b"), Some(1));
}
22 changes: 19 additions & 3 deletions exercises/practice/binary-search/.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.

[b55c24a9-a98d-4379-a08c-2adcf8ebeee8]
description = "finds a value in an array with one element"
Expand All @@ -23,5 +30,14 @@ description = "finds a value in an array of even length"
[da7db20a-354f-49f7-a6a1-650a54998aa6]
description = "identifies that a value is not included in the array"

[95d869ff-3daf-4c79-b622-6e805c675f97]
description = "a value smaller than the array's smallest value is not found"

[8b24ef45-6e51-4a94-9eac-c2bf38fdb0ba]
description = "a value larger than the array's largest value is not found"

[f439a0fa-cf42-4262-8ad1-64bf41ce566a]
description = "nothing is found in an empty array"

[2c353967-b56d-40b8-acff-ce43115eed64]
description = "nothing is found when the left and right bounds cross"
3 changes: 3 additions & 0 deletions exercises/practice/binary-search/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ version = "1.3.0"

[features]
generic = []

[lints.clippy]
needless_borrows_for_generic_args = "allow"
27 changes: 4 additions & 23 deletions exercises/practice/binary-search/tests/binary-search.rs
Original file line number Diff line number Diff line change
@@ -1,29 +1,10 @@
// The &[] borrows are required for the base exercise,
// where `find` is not generic. Once `find` is made generic,
// the borrows become needless. Since we want the tests to work
// without clippy warnings for both people who take on the
// additional challenge and people who don't, we disable this lint.
#![allow(clippy::needless_borrows_for_generic_args)]

use binary_search::find;
use binary_search::*;

#[test]
fn finds_a_value_in_an_array_with_one_element() {
assert_eq!(find(&[6], 6), Some(0));
}

#[test]
#[ignore]
fn finds_first_value_in_an_array_with_two_element() {
assert_eq!(find(&[1, 2], 1), Some(0));
}

#[test]
#[ignore]
fn finds_second_value_in_an_array_with_two_element() {
assert_eq!(find(&[1, 2], 2), Some(1));
}

#[test]
#[ignore]
fn finds_a_value_in_the_middle_of_an_array() {
Expand Down Expand Up @@ -68,19 +49,19 @@ fn identifies_that_a_value_is_not_included_in_the_array() {

#[test]
#[ignore]
fn a_value_smaller_than_the_arrays_smallest_value_is_not_included() {
fn a_value_smaller_than_the_array_s_smallest_value_is_not_found() {
assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 0), None);
}

#[test]
#[ignore]
fn a_value_larger_than_the_arrays_largest_value_is_not_included() {
fn a_value_larger_than_the_array_s_largest_value_is_not_found() {
assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 13), None);
}

#[test]
#[ignore]
fn nothing_is_included_in_an_empty_array() {
fn nothing_is_found_in_an_empty_array() {
assert_eq!(find(&[], 1), None);
}

Expand Down