-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathSaddlebackSearchTest.java
85 lines (68 loc) · 2.83 KB
/
SaddlebackSearchTest.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
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");
}
}