diff options
| author | Federico Igne <git@federicoigne.com> | 2021-01-02 17:58:32 +0000 |
|---|---|---|
| committer | Federico Igne <git@federicoigne.com> | 2021-11-03 18:55:08 +0000 |
| commit | e7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29 (patch) | |
| tree | 66933e429dbcb0e0dff68cc23cd1ef9430f12eed /rust/binary-search/tests/binary-search.rs | |
| parent | fdde4fcd039210d7378c3f31ec9372396b1464a9 (diff) | |
| download | exercism-e7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29.tar.gz exercism-e7e054e21fd530fa6873b7aedd1fc4d4cfe5eb29.zip | |
[rust] Binary Search
Diffstat (limited to 'rust/binary-search/tests/binary-search.rs')
| -rw-r--r-- | rust/binary-search/tests/binary-search.rs | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/rust/binary-search/tests/binary-search.rs b/rust/binary-search/tests/binary-search.rs new file mode 100644 index 0000000..aa5cc8b --- /dev/null +++ b/rust/binary-search/tests/binary-search.rs | |||
| @@ -0,0 +1,93 @@ | |||
| 1 | use binary_search::find; | ||
| 2 | |||
| 3 | #[test] | ||
| 4 | fn finds_a_value_in_an_array_with_one_element() { | ||
| 5 | assert_eq!(find(&[6], 6), Some(0)); | ||
| 6 | } | ||
| 7 | |||
| 8 | #[test] | ||
| 9 | fn finds_first_value_in_an_array_with_two_element() { | ||
| 10 | assert_eq!(find(&[1, 2], 1), Some(0)); | ||
| 11 | } | ||
| 12 | |||
| 13 | #[test] | ||
| 14 | fn finds_second_value_in_an_array_with_two_element() { | ||
| 15 | assert_eq!(find(&[1, 2], 2), Some(1)); | ||
| 16 | } | ||
| 17 | |||
| 18 | #[test] | ||
| 19 | fn finds_a_value_in_the_middle_of_an_array() { | ||
| 20 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 6), Some(3)); | ||
| 21 | } | ||
| 22 | |||
| 23 | #[test] | ||
| 24 | fn finds_a_value_at_the_beginning_of_an_array() { | ||
| 25 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 1), Some(0)); | ||
| 26 | } | ||
| 27 | |||
| 28 | #[test] | ||
| 29 | fn finds_a_value_at_the_end_of_an_array() { | ||
| 30 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 11), Some(6)); | ||
| 31 | } | ||
| 32 | |||
| 33 | #[test] | ||
| 34 | fn finds_a_value_in_an_array_of_odd_length() { | ||
| 35 | assert_eq!( | ||
| 36 | find(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 634], 144), | ||
| 37 | Some(9) | ||
| 38 | ); | ||
| 39 | } | ||
| 40 | |||
| 41 | #[test] | ||
| 42 | fn finds_a_value_in_an_array_of_even_length() { | ||
| 43 | assert_eq!( | ||
| 44 | find(&[1, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377], 21), | ||
| 45 | Some(5) | ||
| 46 | ); | ||
| 47 | } | ||
| 48 | |||
| 49 | #[test] | ||
| 50 | fn identifies_that_a_value_is_not_included_in_the_array() { | ||
| 51 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 7), None); | ||
| 52 | } | ||
| 53 | |||
| 54 | #[test] | ||
| 55 | fn a_value_smaller_than_the_arrays_smallest_value_is_not_included() { | ||
| 56 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 0), None); | ||
| 57 | } | ||
| 58 | |||
| 59 | #[test] | ||
| 60 | fn a_value_larger_than_the_arrays_largest_value_is_not_included() { | ||
| 61 | assert_eq!(find(&[1, 3, 4, 6, 8, 9, 11], 13), None); | ||
| 62 | } | ||
| 63 | |||
| 64 | #[test] | ||
| 65 | fn nothing_is_included_in_an_empty_array() { | ||
| 66 | assert_eq!(find(&[], 1), None); | ||
| 67 | } | ||
| 68 | |||
| 69 | #[test] | ||
| 70 | fn nothing_is_found_when_the_left_and_right_bounds_cross() { | ||
| 71 | assert_eq!(find(&[1, 2], 0), None); | ||
| 72 | } | ||
| 73 | |||
| 74 | #[test] | ||
| 75 | #[cfg(feature = "generic")] | ||
| 76 | fn works_for_arrays() { | ||
| 77 | assert_eq!(find([6], 6), Some(0)); | ||
| 78 | } | ||
| 79 | |||
| 80 | #[test] | ||
| 81 | #[cfg(feature = "generic")] | ||
| 82 | fn works_for_vec() { | ||
| 83 | let vector = vec![6]; | ||
| 84 | assert_eq!(find(&vector, 6), Some(0)); | ||
| 85 | assert_eq!(find(vector, 6), Some(0)); | ||
| 86 | } | ||
| 87 | |||
| 88 | #[test] | ||
| 89 | #[cfg(feature = "generic")] | ||
| 90 | fn works_for_str_elements() { | ||
| 91 | assert_eq!(find(["a"], "a"), Some(0)); | ||
| 92 | assert_eq!(find(["a", "b"], "b"), Some(1)); | ||
| 93 | } | ||
