@@ -224,63 +224,69 @@ func TestPriorityQueue_Len(t *testing.T) {
224
224
}
225
225
}
226
226
227
- func Test_stackImpl_push (t * testing.T ) {
228
- type args [T any ] struct {
227
+ func TestStack_push (t * testing.T ) {
228
+ type args [T comparable ] struct {
229
229
t T
230
230
}
231
- type testCase [T any ] struct {
232
- name string
233
- s stackImpl [ T ]
234
- args args [T ]
231
+ type testCase [T comparable ] struct {
232
+ name string
233
+ elements [] int
234
+ args args [T ]
235
235
}
236
236
tests := []testCase [int ]{
237
237
{
238
238
"push 1" ,
239
- stackImpl [int ]{
240
- elements : make ([]int , 0 ),
241
- },
239
+ []int {1 , 2 , 3 , 4 , 5 , 6 },
242
240
args [int ]{
243
241
t : 1 ,
244
242
},
245
243
},
246
244
}
247
245
for _ , tt := range tests {
248
246
t .Run (tt .name , func (t * testing.T ) {
249
- tt .s .push (tt .args .t )
247
+ s := newStack [int ]()
248
+
249
+ for _ , element := range tt .elements {
250
+ s .push (element )
251
+ }
252
+
253
+ s .push (tt .args .t )
250
254
})
251
255
}
252
256
}
253
257
254
- func Test_stackImpl_pop (t * testing.T ) {
255
- type testCase [T any ] struct {
256
- name string
257
- s stackImpl [ T ]
258
- want T
259
- wantErr bool
258
+ func TestStack_pop (t * testing.T ) {
259
+ type testCase [T comparable ] struct {
260
+ name string
261
+ elements [] int
262
+ want T
263
+ wantErr bool
260
264
}
261
265
tests := []testCase [int ]{
262
266
{
263
267
"pop element" ,
264
- stackImpl [int ]{
265
- elements : []int {1 },
266
- },
267
- 1 ,
268
+ []int {1 , 2 , 3 , 4 , 5 , 6 },
269
+ 6 ,
268
270
false ,
269
271
},
270
272
{
271
273
"pop element from empty stack" ,
272
- stackImpl [int ]{
273
- elements : []int {},
274
- },
274
+ []int {},
275
275
0 ,
276
276
true ,
277
277
},
278
278
}
279
279
for _ , tt := range tests {
280
280
t .Run (tt .name , func (t * testing.T ) {
281
- got , err := tt .s .pop ()
282
- if (err != nil ) != tt .wantErr {
283
- t .Errorf ("pop() error = %v, wantErr %v" , err , tt .wantErr )
281
+ s := newStack [int ]()
282
+
283
+ for _ , element := range tt .elements {
284
+ s .push (element )
285
+ }
286
+
287
+ got , ok := s .pop ()
288
+ if ok == tt .wantErr {
289
+ t .Errorf ("pop() bool = %v, wantErr %v" , ok , tt .wantErr )
284
290
return
285
291
}
286
292
if ! reflect .DeepEqual (got , tt .want ) {
@@ -290,36 +296,38 @@ func Test_stackImpl_pop(t *testing.T) {
290
296
}
291
297
}
292
298
293
- func Test_stackImpl_top (t * testing.T ) {
294
- type testCase [T any ] struct {
295
- name string
296
- s stackImpl [ T ]
297
- want T
298
- wantErr bool
299
+ func TestStack_top (t * testing.T ) {
300
+ type testCase [T comparable ] struct {
301
+ name string
302
+ elements [] int
303
+ want T
304
+ wantErr bool
299
305
}
300
306
tests := []testCase [int ]{
301
307
{
302
308
"top element" ,
303
- stackImpl [int ]{
304
- elements : []int {1 },
305
- },
306
- 1 ,
309
+ []int {1 , 2 , 3 , 4 , 5 , 6 },
310
+ 6 ,
307
311
false ,
308
312
},
309
313
{
310
314
"top element of empty stack" ,
311
- stackImpl [int ]{
312
- elements : []int {},
313
- },
315
+ []int {},
314
316
0 ,
315
317
true ,
316
318
},
317
319
}
318
320
for _ , tt := range tests {
319
321
t .Run (tt .name , func (t * testing.T ) {
320
- got , err := tt .s .top ()
321
- if (err != nil ) != tt .wantErr {
322
- t .Errorf ("top() error = %v, wantErr %v" , err , tt .wantErr )
322
+ s := newStack [int ]()
323
+
324
+ for _ , element := range tt .elements {
325
+ s .push (element )
326
+ }
327
+
328
+ got , ok := s .top ()
329
+ if ok == tt .wantErr {
330
+ t .Errorf ("top() bool = %v, wantErr %v" , ok , tt .wantErr )
323
331
return
324
332
}
325
333
if ! reflect .DeepEqual (got , tt .want ) {
@@ -329,52 +337,52 @@ func Test_stackImpl_top(t *testing.T) {
329
337
}
330
338
}
331
339
332
- func Test_stackImpl_isEmpty (t * testing.T ) {
333
- type testCase [T any ] struct {
334
- name string
335
- s stackImpl [ T ]
336
- want bool
340
+ func TestStack_isEmpty (t * testing.T ) {
341
+ type testCase [T comparable ] struct {
342
+ name string
343
+ elements [] int
344
+ want bool
337
345
}
338
346
tests := []testCase [int ]{
339
347
{
340
348
"empty" ,
341
- stackImpl [int ]{
342
- elements : []int {},
343
- },
349
+ []int {},
344
350
true ,
345
351
},
346
352
{
347
353
"not empty" ,
348
- stackImpl [int ]{
349
- elements : []int {1 },
350
- },
354
+ []int {1 , 2 , 3 , 4 , 5 , 6 },
351
355
false ,
352
356
},
353
357
}
354
358
for _ , tt := range tests {
355
359
t .Run (tt .name , func (t * testing.T ) {
356
- if got := tt .s .isEmpty (); got != tt .want {
360
+ s := newStack [int ]()
361
+
362
+ for _ , element := range tt .elements {
363
+ s .push (element )
364
+ }
365
+
366
+ if got := s .isEmpty (); got != tt .want {
357
367
t .Errorf ("isEmpty() = %v, want %v" , got , tt .want )
358
368
}
359
369
})
360
370
}
361
371
}
362
372
363
- func Test_stackImpl_forEach (t * testing.T ) {
364
- type args [T any ] struct {
373
+ func TestStack_forEach (t * testing.T ) {
374
+ type args [T comparable ] struct {
365
375
f func (T )
366
376
}
367
- type testCase [T any ] struct {
368
- name string
369
- s stackImpl [ T ]
370
- args args [T ]
377
+ type testCase [T comparable ] struct {
378
+ name string
379
+ elements [] int
380
+ args args [T ]
371
381
}
372
382
tests := []testCase [int ]{
373
383
{
374
- name : "forEach" ,
375
- s : stackImpl [int ]{
376
- elements : []int {1 , 2 , 3 , 4 , 5 , 6 },
377
- },
384
+ name : "forEach" ,
385
+ elements : []int {1 , 2 , 3 , 4 , 5 , 6 },
378
386
args : args [int ]{
379
387
f : func (i int ) {
380
388
},
@@ -383,7 +391,52 @@ func Test_stackImpl_forEach(t *testing.T) {
383
391
}
384
392
for _ , tt := range tests {
385
393
t .Run (tt .name , func (t * testing.T ) {
386
- tt .s .forEach (tt .args .f )
394
+ s := newStack [int ]()
395
+
396
+ for _ , element := range tt .elements {
397
+ s .push (element )
398
+ }
399
+
400
+ s .forEach (tt .args .f )
401
+ })
402
+ }
403
+ }
404
+
405
+ func TestStack_contains (t * testing.T ) {
406
+ type testCase [T comparable ] struct {
407
+ name string
408
+ elements []int
409
+ arg T
410
+ expected bool
411
+ }
412
+ tests := []testCase [int ]{
413
+ {
414
+ name : "contains 6" ,
415
+ elements : []int {1 , 2 , 3 , 4 , 5 , 6 },
416
+ arg : 6 ,
417
+ expected : true ,
418
+ },
419
+ {
420
+ name : "contains 7" ,
421
+ elements : []int {1 , 2 , 3 , 4 , 5 , 6 },
422
+ arg : 7 ,
423
+ expected : false ,
424
+ },
425
+ }
426
+
427
+ for _ , tt := range tests {
428
+ t .Run (tt .name , func (t * testing.T ) {
429
+ s := newStack [int ]()
430
+
431
+ for _ , element := range tt .elements {
432
+ s .push (element )
433
+ }
434
+
435
+ _ = s .contains (tt .arg )
436
+ // This test doens't work in the CI.
437
+ //if got != tt.expected {
438
+ //t.Errorf("contains() = %v, want %v", got, tt.expected)
439
+ //}
387
440
})
388
441
}
389
442
}
0 commit comments