forked from creachadair/mds
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmlink_test.go
71 lines (59 loc) · 1.41 KB
/
mlink_test.go
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
package mlink_test
import (
"testing"
"github.com/creachadair/mds/internal/mdtest"
"github.com/creachadair/mds/mlink"
)
var (
_ mdtest.Shared[any] = (*mlink.Queue[any])(nil)
_ mdtest.Shared[any] = (*mlink.List[any])(nil)
)
func TestQueue(t *testing.T) {
var q mlink.Queue[int]
check := func(want ...int) { mdtest.CheckContents(t, &q, want) }
// Front and Pop of an empty queue report no value.
if v := q.Front(); v != 0 {
t.Errorf("Front: got %v, want 0", v)
}
if v, ok := q.Pop(); ok {
t.Errorf("Pop: got (%v, %v), want (0, false)", v, ok)
}
check()
if !q.IsEmpty() {
t.Error("IsEmpty is incorrectly false")
}
if n := q.Len(); n != 0 {
t.Errorf("Len: got %d, want 0", n)
}
q.Add(1)
if q.IsEmpty() {
t.Error("IsEmpty is incorrectly true")
}
check(1)
q.Add(2)
check(1, 2)
q.Add(3)
check(1, 2, 3)
if n := q.Len(); n != 3 {
t.Errorf("Len: got %d, want 3", n)
}
front := q.Front()
if front != 1 {
t.Errorf("Front: got %v, want 1", front)
}
if v, ok := q.Peek(0); !ok || v != front {
t.Errorf("Peek(0): got (%v, %v), want (%v, true)", v, ok, front)
}
if v, ok := q.Peek(1); !ok || v != 2 {
t.Errorf("Peek(1): got (%v, %v), want (2, true)", v, ok)
}
if v, ok := q.Peek(10); ok {
t.Errorf("Peek(10): got (%v, %v), want (0, false)", v, ok)
}
if v, ok := q.Pop(); !ok || v != front {
t.Errorf("Pop: got (%v, %v), want (%v, true)", v, ok, front)
}
check(2, 3)
q.Clear()
check()
}