Skip to content

Commit 0e96548

Browse files
committed
test: add edge-case tests for search_rotated_sorted_array
1 parent a7cca54 commit 0e96548

File tree

2 files changed

+36
-2
lines changed

2 files changed

+36
-2
lines changed

src/searching/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ mod linear_search;
1010
mod moore_voting;
1111
mod quick_select;
1212
mod saddleback_search;
13+
mod search_rotated_sorted_array;
1314
mod ternary_search;
1415
mod ternary_search_min_max;
1516
mod ternary_search_min_max_recursive;
1617
mod ternary_search_recursive;
17-
mod search_rotated_sorted_array;
1818

1919
pub use self::binary_search::binary_search;
2020
pub use self::binary_search_recursive::binary_search_rec;
@@ -28,10 +28,10 @@ pub use self::linear_search::linear_search;
2828
pub use self::moore_voting::moore_voting;
2929
pub use self::quick_select::quick_select;
3030
pub use self::saddleback_search::saddleback_search;
31+
pub use self::search_rotated_sorted_array::search_rotated_sorted_array;
3132
pub use self::ternary_search::ternary_search;
3233
pub use self::ternary_search_min_max::ternary_search_max;
3334
pub use self::ternary_search_min_max::ternary_search_min;
3435
pub use self::ternary_search_min_max_recursive::ternary_search_max_rec;
3536
pub use self::ternary_search_min_max_recursive::ternary_search_min_rec;
3637
pub use self::ternary_search_recursive::ternary_search_rec;
37-
pub use self::search_rotated_sorted_array::search_rotated_sorted_array;

src/searching/search_rotated_sorted_array.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,38 @@ mod tests {
7171
assert_eq!(search_rotated_sorted_array(&single, &1), Some(0));
7272
assert_eq!(search_rotated_sorted_array(&single, &2), None);
7373
}
74+
75+
#[test]
76+
fn non_rotated_array() {
77+
// already sorted ascending
78+
let arr = vec![0, 1, 2, 3, 4, 5];
79+
assert_eq!(search_rotated_sorted_array(&arr, &0), Some(0));
80+
assert_eq!(search_rotated_sorted_array(&arr, &5), Some(5));
81+
assert_eq!(search_rotated_sorted_array(&arr, &3), Some(3));
82+
assert_eq!(search_rotated_sorted_array(&arr, &6), None);
83+
}
84+
85+
#[test]
86+
fn small_rotations_and_edges() {
87+
// rotation by 1
88+
let arr1 = vec![5, 0, 1, 2, 3, 4];
89+
assert_eq!(search_rotated_sorted_array(&arr1, &5), Some(0));
90+
assert_eq!(search_rotated_sorted_array(&arr1, &4), Some(5));
91+
92+
// rotation by len-1 (same as rotation by -1)
93+
let arr2 = vec![1, 2, 3, 4, 5, 0];
94+
assert_eq!(search_rotated_sorted_array(&arr2, &0), Some(5));
95+
assert_eq!(search_rotated_sorted_array(&arr2, &1), Some(0));
96+
}
97+
98+
#[test]
99+
fn two_elements_varieties() {
100+
let a = vec![1, 2];
101+
assert_eq!(search_rotated_sorted_array(&a, &1), Some(0));
102+
assert_eq!(search_rotated_sorted_array(&a, &2), Some(1));
103+
104+
let b = vec![2, 1];
105+
assert_eq!(search_rotated_sorted_array(&b, &1), Some(1));
106+
assert_eq!(search_rotated_sorted_array(&b, &2), Some(0));
107+
}
74108
}

0 commit comments

Comments
 (0)