1
+ package array .peak_element ;
2
+
3
+ import org .junit .jupiter .api .BeforeEach ;
4
+ import org .junit .jupiter .api .DisplayName ;
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import java .util .Set ;
8
+
9
+ import static org .junit .jupiter .api .Assertions .*;
10
+
11
+ /**
12
+ * Test suite for the BinarySearch class to find a peak element.
13
+ */
14
+ class BinarySearchTest {
15
+
16
+ private BinarySearch binarySearch ;
17
+
18
+ @ BeforeEach
19
+ void setUp () {
20
+ binarySearch = new BinarySearch ();
21
+ }
22
+
23
+ @ Test
24
+ @ DisplayName ("Test with a simple peak in the middle of the array" )
25
+ void testPeakElement_simplePeak () {
26
+ int [] nums = {1 , 2 , 3 , 1 };
27
+ assertEquals (2 , binarySearch .peakElement (nums ), "Should find the peak at index 2" );
28
+ }
29
+
30
+ @ Test
31
+ @ DisplayName ("Test with a peak at the beginning of the array" )
32
+ void testPeakElement_peakAtStart () {
33
+ int [] nums = {5 , 4 , 3 , 2 , 1 };
34
+ assertEquals (0 , binarySearch .peakElement (nums ), "Should find the peak at the start" );
35
+ }
36
+
37
+ @ Test
38
+ @ DisplayName ("Test with a peak at the end of the array" )
39
+ void testPeakElement_peakAtEnd () {
40
+ int [] nums = {1 , 2 , 3 , 4 , 5 };
41
+ assertEquals (4 , binarySearch .peakElement (nums ), "Should find the peak at the end" );
42
+ }
43
+
44
+ @ Test
45
+ @ DisplayName ("Test with multiple peaks, should return any valid peak index" )
46
+ void testPeakElement_multiplePeaks () {
47
+ int [] nums = {1 , 2 , 1 , 3 , 5 , 6 , 4 };
48
+ // Valid peaks are at index 1 (value 2) and index 5 (value 6)
49
+ Set <Integer > validPeakIndices = Set .of (1 , 5 );
50
+ int result = binarySearch .peakElement (nums );
51
+ assertTrue (validPeakIndices .contains (result ), "Result should be one of the valid peak indices" );
52
+ }
53
+
54
+ @ Test
55
+ @ DisplayName ("Test with a single-element array" )
56
+ void testPeakElement_singleElement () {
57
+ int [] nums = {42 };
58
+ assertEquals (0 , binarySearch .peakElement (nums ), "A single element is always a peak" );
59
+ }
60
+
61
+ @ Test
62
+ @ DisplayName ("Test with a two-element ascending array" )
63
+ void testPeakElement_twoElementsAscending () {
64
+ int [] nums = {10 , 20 };
65
+ assertEquals (1 , binarySearch .peakElement (nums ), "Peak should be the larger element at index 1" );
66
+ }
67
+
68
+ @ Test
69
+ @ DisplayName ("Test with a two-element descending array" )
70
+ void testPeakElement_twoElementsDescending () {
71
+ int [] nums = {20 , 10 };
72
+ assertEquals (0 , binarySearch .peakElement (nums ), "Peak should be the larger element at index 0" );
73
+ }
74
+
75
+ @ Test
76
+ @ DisplayName ("Test with an array containing a plateau" )
77
+ void testPeakElement_withPlateau () {
78
+ int [] nums = {1 , 5 , 5 , 2 };
79
+ // The algorithm should find index 1 as a peak because nums[1] >= nums[2]
80
+ assertEquals (1 , binarySearch .peakElement (nums ), "Should handle plateaus correctly" );
81
+ }
82
+
83
+ @ Test
84
+ @ DisplayName ("Test with a zig-zag array" )
85
+ void testPeakElement_zigZag () {
86
+ int [] nums = {1 , 5 , 1 , 5 , 1 , 5 , 1 };
87
+ Set <Integer > validPeakIndices = Set .of (1 , 3 , 5 );
88
+ int result = binarySearch .peakElement (nums );
89
+ assertTrue (validPeakIndices .contains (result ), "Result should be a valid peak in the zig-zag array" );
90
+ }
91
+
92
+ @ Test
93
+ @ DisplayName ("Test with a long, strictly increasing then decreasing array" )
94
+ void testPeakElement_longArray () {
95
+ int [] nums = {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 };
96
+ assertEquals (7 , binarySearch .peakElement (nums ), "Should find the peak in a long array" );
97
+ }
98
+
99
+ @ Test
100
+ @ DisplayName ("Test with null input should throw IllegalArgumentException" )
101
+ void testPeakElement_nullInput () {
102
+ assertThrows (IllegalArgumentException .class , () -> binarySearch .peakElement (null ), "Should throw IllegalArgumentException for null input" );
103
+ }
104
+
105
+ @ Test
106
+ @ DisplayName ("Test with empty input should throw IllegalArgumentException" )
107
+ void testPeakElement_emptyInput () {
108
+ int [] nums = {};
109
+ assertThrows (IllegalArgumentException .class , () -> binarySearch .peakElement (nums ), "Should throw IllegalArgumentException for an empty array" );
110
+ }
111
+ }
0 commit comments