11package com .thealgorithms .searches ;
22
3+ import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
34import static org .junit .jupiter .api .Assertions .assertEquals ;
45import static org .junit .jupiter .api .Assertions .assertThrows ;
56
@@ -84,7 +85,7 @@ public void binarySearch2dArrayTestLowerSide() {
8485 // valid test case
8586 public void binarySearch2dArrayTestNotFound () {
8687 int [][] arr = {{1 , 2 , 3 , 4 }, {5 , 6 , 7 , 8 }, {9 , 10 , 11 , 12 }};
87- int target = 101 ;
88+ int target = 100 ;
8889
8990 int [] ans = BinarySearch2dArray .binarySearch (arr , target );
9091 System .out .println (Arrays .toString (ans ));
@@ -93,7 +94,7 @@ public void binarySearch2dArrayTestNotFound() {
9394 }
9495
9596 /**
96- * Test if the method works with input arrays consisting only of one row.
97+ * Test if the method works with an array that only has one row.
9798 */
9899 @ Test
99100 public void binarySearch2dArrayTestOneRow () {
@@ -154,4 +155,52 @@ public void binarySearch2dArrayTestEmptyArray() {
154155 // Assert that an empty array is not valid input for the method.
155156 assertThrows (ArrayIndexOutOfBoundsException .class , () -> BinarySearch2dArray .binarySearch (arr , target ));
156157 }
158+
159+ @ Test
160+ public void binarySearch2dArrayTestSingleRowTargetNotFoundGreater () {
161+ int [][] arr = {{1 , 2 , 3 , 4 }};
162+ int target = 10 ;
163+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
164+ assertArrayEquals (new int [] {-1 , -1 }, ans );
165+ }
166+
167+ @ Test
168+ public void binarySearch2dArrayTestSingleRowTargetNotFoundSmaller () {
169+ int [][] arr = {{1 , 2 , 3 , 4 }};
170+ int target = 0 ;
171+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
172+ assertArrayEquals (new int [] {-1 , -1 }, ans );
173+ }
174+
175+ @ Test
176+ public void binarySearch2dArrayTestSingleColumnPresent () {
177+ int [][] arr = {{1 }, {3 }, {5 }};
178+ int target = 3 ;
179+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
180+ assertArrayEquals (new int [] {1 , 0 }, ans );
181+ }
182+
183+ @ Test
184+ public void binarySearch2dArrayTestSingleColumnNotFound () {
185+ int [][] arr = {{1 }, {3 }, {5 }};
186+ int target = 4 ;
187+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
188+ assertArrayEquals (new int [] {-1 , -1 }, ans );
189+ }
190+
191+ @ Test
192+ public void binarySearch2dArrayTestSingleElementPresent () {
193+ int [][] arr = {{42 }};
194+ int target = 42 ;
195+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
196+ assertArrayEquals (new int [] {0 , 0 }, ans );
197+ }
198+
199+ @ Test
200+ public void binarySearch2dArrayTestSingleElementNotFound () {
201+ int [][] arr = {{42 }};
202+ int target = 10 ;
203+ int [] ans = BinarySearch2dArray .binarySearch (arr , target );
204+ assertArrayEquals (new int [] {-1 , -1 }, ans );
205+ }
157206}
0 commit comments