Skip to content

Commit be13981

Browse files
authored
Add tests for 2D array binary search (TheAlgorithms#3892)
1 parent 6b9eb1b commit be13981

1 file changed

Lines changed: 66 additions & 0 deletions

File tree

src/test/java/com/thealgorithms/searches/BinarySearch2dArrayTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package com.thealgorithms.searches;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

56
import java.util.*;
67
import org.junit.jupiter.api.Test;
8+
import org.junit.jupiter.api.AfterAll;
9+
710

811
public class BinarySearch2dArrayTest {
912

@@ -97,4 +100,67 @@ public void BinarySearch2dArrayTestNotFound() {
97100
assertEquals(-1, ans[0]);
98101
assertEquals(-1, ans[1]);
99102
}
103+
104+
/**
105+
* Test if the method works with input arrays consisting only of one row.
106+
*/
107+
@Test
108+
public void BinarySearch2dArrayTestOneRow() {
109+
int[][] arr = { { 1, 2, 3, 4 }};
110+
int target = 2;
111+
112+
// Assert that the requirement, that the array only has one row, is fulfilled.
113+
assertEquals(arr.length, 1);
114+
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
115+
System.out.println(Arrays.toString(ans));
116+
assertEquals(0, ans[0]);
117+
assertEquals(1, ans[1]);
118+
}
119+
120+
/**
121+
* Test if the method works with the target in the middle of the input.
122+
*/
123+
@Test
124+
public void BinarySearch2dArrayTestTargetInMiddle() {
125+
int[][] arr = { { 1, 2, 3, 4, 5 }, { 6, 7, 8, 9, 10 }, { 11, 12, 13, 14, 15} };
126+
int target = 8;
127+
// Assert that the requirement, that the target is in the middle row and middle column, is fulfilled.
128+
assertEquals(arr[arr.length/2][arr[0].length/2], target);
129+
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
130+
System.out.println(Arrays.toString(ans));
131+
assertEquals(1, ans[0]);
132+
assertEquals(2, ans[1]);
133+
}
134+
135+
/**
136+
* Test if the method works with the target in the middle column,
137+
* in the row above the middle row.
138+
*/
139+
@Test
140+
public void BinarySearch2dArrayTestTargetAboveMiddleRowInMiddleColumn() {
141+
int[][] arr = { { 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 } };
142+
int target = 3;
143+
144+
// Assert that the requirement, that he target is in the middle column,
145+
// in an array with an even number of columns, and on the row "above" the middle row.
146+
assertEquals(arr[0].length % 2, 0);
147+
assertEquals(arr[arr.length/2-1][arr[0].length/2], target);
148+
int[] ans = BinarySearch2dArray.BinarySearch(arr, target);
149+
System.out.println(Arrays.toString(ans));
150+
assertEquals(0, ans[0]);
151+
assertEquals(2, ans[1]);
152+
}
153+
154+
/**
155+
* Test if the method works with an empty array.
156+
*/
157+
@Test
158+
public void BinarySearch2dArrayTestEmptyArray() {
159+
int[][] arr = {};
160+
int target = 5;
161+
162+
// Assert that an empty array is not valid input for the method.
163+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.BinarySearch(arr, target));
164+
}
165+
100166
}

0 commit comments

Comments
 (0)