forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNearestElementTest.java
More file actions
58 lines (47 loc) · 1.77 KB
/
NearestElementTest.java
File metadata and controls
58 lines (47 loc) · 1.77 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
package com.thealgorithms.datastructures.stacks;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
public class NearestElementTest {
@Test
public void testNearestGreaterToRight() {
int[] arr = {4, 5, 2, 10, 8};
int[] expected = {5, 10, 10, -1, -1};
assertArrayEquals(expected, NearestElement.nearestGreaterToRight(arr));
}
@Test
public void testNearestGreaterToLeft() {
int[] arr = {4, 5, 2, 10, 8};
int[] expected = {-1, -1, 5, -1, 10};
assertArrayEquals(expected, NearestElement.nearestGreaterToLeft(arr));
}
@Test
public void testNearestSmallerToRight() {
int[] arr = {4, 5, 2, 10, 8};
int[] expected = {2, 2, -1, 8, -1};
assertArrayEquals(expected, NearestElement.nearestSmallerToRight(arr));
}
@Test
public void testNearestSmallerToLeft() {
int[] arr = {4, 5, 2, 10, 8};
int[] expected = {-1, 4, -1, 2, 2};
assertArrayEquals(expected, NearestElement.nearestSmallerToLeft(arr));
}
@Test
void testEmptyArray() {
int[] arr = {};
assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToRight(arr));
assertArrayEquals(new int[]{}, NearestElement.nearestGreaterToLeft(arr));
}
@Test
void testAllEqualElements() {
int[] arr = {5, 5, 5, 5};
assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToRight(arr));
assertArrayEquals(new int[]{-1, -1, -1, -1}, NearestElement.nearestGreaterToLeft(arr));
}
@Test
void testPrivateConstructor() throws Exception {
var constructor = NearestElement.class.getDeclaredConstructor();
constructor.setAccessible(true);
constructor.newInstance();
}
}