...
1
2
3
4
5
6
7
8
9 package trace
10
11 import "testing"
12
13 func TestQueue(t *testing.T) {
14 var q queue[int]
15 check := func(name string, exp []int) {
16 for _, v := range exp {
17 q.push(v)
18 }
19 for i, want := range exp {
20 if got, ok := q.pop(); !ok {
21 t.Fatalf("check %q: expected to be able to pop after %d pops", name, i+1)
22 } else if got != want {
23 t.Fatalf("check %q: expected value %d after on pop %d, got %d", name, want, i+1, got)
24 }
25 }
26 if _, ok := q.pop(); ok {
27 t.Fatalf("check %q: did not expect to be able to pop more values", name)
28 }
29 if _, ok := q.pop(); ok {
30 t.Fatalf("check %q: did not expect to be able to pop more values a second time", name)
31 }
32 }
33 check("one element", []int{4})
34 check("two elements", []int{64, 12})
35 check("six elements", []int{55, 16423, 2352, 644, 12874, 9372})
36 check("one element again", []int{7})
37 check("two elements again", []int{77, 6336})
38 }
39
View as plain text