- Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathBinarySearch2dArrayTest.java
157 lines (133 loc) · 5.07 KB
/
BinarySearch2dArrayTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
packagecom.thealgorithms.searches;
importstaticorg.junit.jupiter.api.Assertions.assertEquals;
importstaticorg.junit.jupiter.api.Assertions.assertThrows;
importjava.util.Arrays;
importorg.junit.jupiter.api.Test;
publicclassBinarySearch2dArrayTest {
@Test
// valid test case
publicvoidbinarySearch2dArrayTestMiddle() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 6;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]);
assertEquals(1, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestMiddleSide() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 8;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]);
assertEquals(3, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestUpper() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 2;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(1, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestUpperSide() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 1;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(0, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestLower() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 10;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(2, ans[0]);
assertEquals(1, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestLowerSide() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 11;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(2, ans[0]);
assertEquals(2, ans[1]);
}
@Test
// valid test case
publicvoidbinarySearch2dArrayTestNotFound() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 101;
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(-1, ans[0]);
assertEquals(-1, ans[1]);
}
/**
* Test if the method works with input arrays consisting only of one row.
*/
@Test
publicvoidbinarySearch2dArrayTestOneRow() {
int[][] arr = {{1, 2, 3, 4}};
inttarget = 2;
// Assert that the requirement, that the array only has one row, is fulfilled.
assertEquals(arr.length, 1);
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(1, ans[1]);
}
/**
* Test if the method works with the target in the middle of the input.
*/
@Test
publicvoidbinarySearch2dArrayTestTargetInMiddle() {
int[][] arr = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}, {11, 12, 13, 14, 15}};
inttarget = 8;
// Assert that the requirement, that the target is in the middle row and middle column, is
// fulfilled.
assertEquals(arr[arr.length / 2][arr[0].length / 2], target);
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(1, ans[0]);
assertEquals(2, ans[1]);
}
/**
* Test if the method works with the target in the middle column,
* in the row above the middle row.
*/
@Test
publicvoidbinarySearch2dArrayTestTargetAboveMiddleRowInMiddleColumn() {
int[][] arr = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
inttarget = 3;
// Assert that the requirement, that he target is in the middle column,
// in an array with an even number of columns, and on the row "above" the middle row.
assertEquals(arr[0].length % 2, 0);
assertEquals(arr[arr.length / 2 - 1][arr[0].length / 2], target);
int[] ans = BinarySearch2dArray.binarySearch(arr, target);
System.out.println(Arrays.toString(ans));
assertEquals(0, ans[0]);
assertEquals(2, ans[1]);
}
/**
* Test if the method works with an empty array.
*/
@Test
publicvoidbinarySearch2dArrayTestEmptyArray() {
int[][] arr = {};
inttarget = 5;
// Assert that an empty array is not valid input for the method.
assertThrows(ArrayIndexOutOfBoundsException.class, () -> BinarySearch2dArray.binarySearch(arr, target));
}
}