1
+ package com .borrelunde .leetcodesolutions .problem0035 .searchinsertposition ;
2
+
3
+ import org .junit .jupiter .api .DisplayName ;
4
+ import org .junit .jupiter .api .Nested ;
5
+ import org .junit .jupiter .api .Test ;
6
+
7
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
8
+
9
+ /**
10
+ * This is the test to the LeetCode problem: 35. Search Insert Position
11
+ *
12
+ * @author Børre A. Opedal Lunde
13
+ * @since 2024年01月28日
14
+ */
15
+ @ DisplayName ("Search Insert Position" )
16
+ class SolutionTest {
17
+
18
+ private final Solution solution = new Solution ();
19
+
20
+ @ Test
21
+ @ DisplayName ("Example one" )
22
+ void exampleOne () {
23
+ int [] nums = {1 , 3 , 5 , 6 };
24
+ int target = 5 ;
25
+
26
+ int expected = 2 ;
27
+ int actual = solution .searchInsert (nums , target );
28
+
29
+ assertEquals (expected , actual );
30
+ }
31
+
32
+ @ Test
33
+ @ DisplayName ("Example two" )
34
+ void exampleTwo () {
35
+ int [] nums = {1 , 3 , 5 , 6 };
36
+ int target = 2 ;
37
+
38
+ int expected = 1 ;
39
+ int actual = solution .searchInsert (nums , target );
40
+
41
+ assertEquals (expected , actual );
42
+ }
43
+
44
+ @ Test
45
+ @ DisplayName ("Example three" )
46
+ void exampleThree () {
47
+ int [] nums = {1 , 3 , 5 , 6 };
48
+ int target = 7 ;
49
+
50
+ int expected = 4 ;
51
+ int actual = solution .searchInsert (nums , target );
52
+
53
+ assertEquals (expected , actual );
54
+ }
55
+
56
+ @ Nested
57
+ @ DisplayName ("Given array with one element" )
58
+ class GivenArrayWithOneElement {
59
+
60
+ final int [] nums = {1 };
61
+
62
+ @ Test
63
+ @ DisplayName ("Should return index 0 when target is less than the element" )
64
+ void shouldReturnIndex0WhenTargetIsLessThanTheElement () {
65
+ int expected = 0 ;
66
+ int actual = solution .searchInsert (nums , Integer .MIN_VALUE );
67
+
68
+ assertEquals (expected , actual );
69
+ }
70
+
71
+ @ Test
72
+ @ DisplayName ("Should return its index when target is equal to the element" )
73
+ void shouldReturnItsIndexWhenTargetIsEqualToTheElement () {
74
+ int expected = 0 ;
75
+ int actual = solution .searchInsert (nums , 1 );
76
+
77
+ assertEquals (expected , actual );
78
+ }
79
+
80
+ @ Test
81
+ @ DisplayName ("Should return index 1 when target is greater than the element" )
82
+ void shouldReturnIndex1WhenTargetIsGreaterThanTheElement () {
83
+ int expected = 1 ;
84
+ int actual = solution .searchInsert (nums , Integer .MAX_VALUE );
85
+
86
+ assertEquals (expected , actual );
87
+ }
88
+ }
89
+
90
+ @ Nested
91
+ @ DisplayName ("Given array with two elements" )
92
+ class GivenArrayWithTwoElements {
93
+
94
+ final int [] nums = {1 , 3 };
95
+
96
+ @ Test
97
+ @ DisplayName ("Should return index 0 when target is less than the first element" )
98
+ void shouldReturnIndex0WhenTargetIsLessThanTheFirstElement () {
99
+ int expected = 0 ;
100
+ int actual = solution .searchInsert (nums , Integer .MIN_VALUE );
101
+
102
+ assertEquals (expected , actual );
103
+ }
104
+
105
+ @ Test
106
+ @ DisplayName ("Should return index 0 when target is equal to the first element" )
107
+ void shouldReturnIndex0WhenTargetIsEqualToTheFirstElement () {
108
+ int expected = 0 ;
109
+ int actual = solution .searchInsert (nums , 1 );
110
+
111
+ assertEquals (expected , actual );
112
+ }
113
+
114
+ @ Test
115
+ @ DisplayName ("Should return index 1 when target is greater than the first element and less than the second element" )
116
+ void shouldReturnIndex1WhenTargetIsGreaterThanTheFirstElementAndLessThanTheSecondElement () {
117
+ int expected = 1 ;
118
+ int actual = solution .searchInsert (nums , 2 );
119
+
120
+ assertEquals (expected , actual );
121
+ }
122
+
123
+ @ Test
124
+ @ DisplayName ("Should return index 1 when target is equal to the second element" )
125
+ void shouldReturnIndex1WhenTargetIsEqualToTheSecondElement () {
126
+ int expected = 1 ;
127
+ int actual = solution .searchInsert (nums , 3 );
128
+
129
+ assertEquals (expected , actual );
130
+ }
131
+
132
+ @ Test
133
+ @ DisplayName ("Should return index 2 when target is greater than the second element" )
134
+ void shouldReturnIndex2WhenTargetIsGreaterThanTheSecondElement () {
135
+ int expected = 2 ;
136
+ int actual = solution .searchInsert (nums , Integer .MAX_VALUE );
137
+
138
+ assertEquals (expected , actual );
139
+ }
140
+ }
141
+
142
+ @ Nested
143
+ @ DisplayName ("Given array with only the same element" )
144
+ class GivenArrayWithOnlyTheSameElement {
145
+
146
+ final int [] nums = {1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 , 1 };
147
+
148
+ @ Test
149
+ @ DisplayName ("Should return index 0 when target is less than the element" )
150
+ void shouldReturnIndex0WhenTargetIsLessThanTheElement () {
151
+ int expected = 0 ;
152
+ int actual = solution .searchInsert (nums , Integer .MIN_VALUE );
153
+
154
+ assertEquals (expected , actual );
155
+ }
156
+
157
+ @ Test
158
+ @ DisplayName ("Should return index 9 when target is greater than the element" )
159
+ void shouldReturnIndex9WhenTargetIsGreaterThanTheElement () {
160
+ int expected = 9 ;
161
+ int actual = solution .searchInsert (nums , Integer .MAX_VALUE );
162
+
163
+ assertEquals (expected , actual );
164
+ }
165
+ }
166
+ }
0 commit comments