Repository navigation
Expand file tree
/
Copy pathSaddlebackSearchTest.java
More file actions
134 lines (112 loc) · 4.58 KB
/
Copy pathSaddlebackSearchTest.java
File metadata and controls
134 lines (112 loc) · 4.58 KB
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
package com.thealgorithms.searches;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import org.junit.jupiter.api.Test;
class SaddlebackSearchTest {
/**
* Test searching for an element that exists in the array.
*/
@Test
void testFindElementExists() {
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 4);
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3)");
}
/**
* Test searching for an element that does not exist in the array.
*/
@Test
void testFindElementNotExists() {
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 1000);
assertArrayEquals(new int[] {-1, -1}, result, "Element 1000 should not be found");
}
/**
* Test searching for the smallest element in the array.
*/
@Test
void testFindSmallestElement() {
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, -10);
assertArrayEquals(new int[] {0, 0}, result, "Element -10 should be found at (0, 0)");
}
/**
* Test searching for the largest element in the array.
*/
@Test
void testFindLargestElement() {
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
int[] result = SaddlebackSearch.find(arr, arr.length - 1, 0, 150);
assertArrayEquals(new int[] {4, 4}, result, "Element 150 should be found at (4, 4)");
}
/**
* Test searching in an empty array.
*/
@Test
void testFindInEmptyArray() {
int[][] arr = {};
assertThrows(IllegalArgumentException.class, () -> { SaddlebackSearch.find(arr, 0, 0, 4); });
}
/**
* Test searching in a single element array that matches the search key.
*/
@Test
void testFindSingleElementExists() {
int[][] arr = {{5}};
int[] result = SaddlebackSearch.find(arr, 0, 0, 5);
assertArrayEquals(new int[] {0, 0}, result, "Element 5 should be found at (0, 0)");
}
/**
* Test searching in a single element array that does not match the search key.
*/
@Test
void testFindSingleElementNotExists() {
int[][] arr = {{5}};
int[] result = SaddlebackSearch.find(arr, 0, 0, 10);
assertArrayEquals(new int[] {-1, -1}, result, "Element 10 should not be found in single element array");
}
/**
* Test convenience method searching from the bottom-left corner automatically.
*/
@Test
void testFindWithConvenienceMethod() {
int[][] arr = {{-10, -5, -3, 4, 9}, {-6, -2, 0, 5, 10}, {-4, -1, 1, 6, 12}, {2, 3, 7, 8, 13}, {100, 120, 130, 140, 150}};
int[] result = SaddlebackSearch.find(arr, 4);
assertArrayEquals(new int[] {0, 3}, result, "Element 4 should be found at (0, 3) using convenience overload");
}
/**
* Test searching in a null array throws IllegalArgumentException.
*/
@Test
void testFindWithNullArray() {
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 4));
assertThrows(IllegalArgumentException.class, () -> SaddlebackSearch.find(null, 0, 0, 4));
}
/**
* Test row index out of bounds returns {-1, -1}.
*/
@Test
void testFindWithRowOutOfBounds() {
int[][] arr = {{1, 2}, {3, 4}};
int[] result = SaddlebackSearch.find(arr, 5, 0, 3);
assertArrayEquals(new int[] {-1, -1}, result, "Row index out of bounds should return {-1, -1}");
}
/**
* Test negative column index returns {-1, -1}.
*/
@Test
void testFindWithNegativeColumn() {
int[][] arr = {{1, 2}, {3, 4}};
int[] result = SaddlebackSearch.find(arr, 1, -1, 3);
assertArrayEquals(new int[] {-1, -1}, result, "Negative column index should return {-1, -1}");
}
/**
* Test null row in matrix returns {-1, -1} safely.
*/
@Test
void testFindWithNullRowInMatrix() {
int[][] arr = {{1, 2}, null, {5, 6}};
int[] result = SaddlebackSearch.find(arr, 1, 0, 5);
assertArrayEquals(new int[] {-1, -1}, result, "Null row in matrix should safely return {-1, -1}");
}
}