-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils_test.go
More file actions
284 lines (226 loc) · 5.58 KB
/
utils_test.go
File metadata and controls
284 lines (226 loc) · 5.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package polygfgo
import (
"reflect"
"testing"
)
func TestExpand(t *testing.T) {
t.Run("expand slice to larger size", func(t *testing.T) {
s := []int{1, 2, 3}
n := 6
got := expand(s, n)
want := []int{1, 2, 3, 0, 0, 0}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("no expansion needed when n equals slice length", func(t *testing.T) {
s := []int{4, 5, 6}
n := 3
got := expand(s, n)
want := []int{4, 5, 6}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("no expansion needed when n is smaller than slice length", func(t *testing.T) {
s := []int{7, 8, 9, 10}
n := 2
got := expand(s, n)
want := []int{7, 8, 9, 10}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("expand an empty slice", func(t *testing.T) {
s := []int{}
n := 4
got := expand(s, n)
want := []int{0, 0, 0, 0}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("no expansion for empty slice when n is 0", func(t *testing.T) {
s := []int{}
n := 0
got := expand(s, n)
want := []int{}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
}
func TestReverse(t *testing.T) {
t.Run("reverse a slice with odd number of elements", func(t *testing.T) {
input := []int{1, 2, 3, 4, 5}
got := reverse(input)
want := []int{5, 4, 3, 2, 1}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("reverse a slice with even number of elements", func(t *testing.T) {
input := []int{1, 2, 3, 4}
got := reverse(input)
want := []int{4, 3, 2, 1}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("reverse a single-element slice", func(t *testing.T) {
input := []int{1}
got := reverse(input)
want := []int{1}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("reverse an empty slice", func(t *testing.T) {
input := []int{}
got := reverse(input)
want := []int{}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("reverse a slice with duplicate elements", func(t *testing.T) {
input := []int{1, 2, 3, 2, 1}
got := reverse(input)
want := []int{1, 2, 3, 2, 1}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
t.Run("reverse a slice with negative numbers", func(t *testing.T) {
input := []int{-1, -2, -3, -4, -5}
got := reverse(input)
want := []int{-5, -4, -3, -2, -1}
if !reflect.DeepEqual(got, want) {
t.Errorf("Expected %v but got %v", want, got)
}
})
}
func TestNextPOT(t *testing.T) {
t.Run("number is already a power of two", func(t *testing.T) {
n := 8
got := nextPOT(n)
want := 8
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is less than the next power of two", func(t *testing.T) {
n := 7
got := nextPOT(n)
want := 8
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is greater than the previous power of two", func(t *testing.T) {
n := 33
got := nextPOT(n)
want := 64
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is zero", func(t *testing.T) {
n := 0
got := nextPOT(n)
want := 1
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is negative", func(t *testing.T) {
n := -5
got := nextPOT(n)
want := 1
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is already a large power of two", func(t *testing.T) {
n := 1024
got := nextPOT(n)
want := 1024
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is just below a large power of two", func(t *testing.T) {
n := 1023
got := nextPOT(n)
want := 1024
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
t.Run("number is between two large powers of two", func(t *testing.T) {
n := 3000
got := nextPOT(n)
want := 4096
if got != want {
t.Errorf("Expected %d but got %d", want, got)
}
})
}
func TestIsPOT(t *testing.T) {
t.Run("number is a power of two", func(t *testing.T) {
n := 8
got := isPOT(n)
want := true
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("number is not a power of two", func(t *testing.T) {
n := 10
got := isPOT(n)
want := false
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("number is zero", func(t *testing.T) {
n := 0
got := isPOT(n)
want := false
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("number is negative", func(t *testing.T) {
n := -8
got := isPOT(n)
want := false
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("number is 1", func(t *testing.T) {
n := 1
got := isPOT(n)
want := true
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("large power of two", func(t *testing.T) {
n := 1024
got := isPOT(n)
want := true
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
t.Run("large number that is not a power of two", func(t *testing.T) {
n := 3000
got := isPOT(n)
want := false
if got != want {
t.Errorf("Expected %t but got %t for input %d", want, got, n)
}
})
}