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