@@ -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