-
Notifications
You must be signed in to change notification settings - Fork 49
/
slist_test.go
152 lines (131 loc) · 2.69 KB
/
slist_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
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
package stl4go
import (
"testing"
)
func Test_SList_Interface(t *testing.T) {
sl := SList[int]{}
_ = Container(&sl)
}
func Test_SList_Clean(t *testing.T) {
sl := SList[int]{}
sl.PushFront(1)
sl.Clear()
expectTrue(t, sl.IsEmpty())
expectEq(t, sl.Len(), 0)
}
func Test_SList_Front(t *testing.T) {
t.Run("empty", func(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.Front() })
})
t.Run("normal", func(t *testing.T) {
sl := SList[int]{}
sl.PushFront(1)
expectEq(t, sl.Front(), 1)
sl.PushBack(2)
expectEq(t, sl.Front(), 1)
sl.PushFront(3)
expectEq(t, sl.Front(), 3)
})
}
func Test_SList_Back(t *testing.T) {
t.Run("empty", func(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.Back() })
})
t.Run("normal", func(t *testing.T) {
sl := SList[int]{}
sl.PushBack(1)
expectEq(t, sl.Back(), 1)
sl.PushFront(2)
expectEq(t, sl.Back(), 1)
sl.PushBack(3)
expectEq(t, sl.Back(), 3)
})
}
func Test_SList_PushFront(t *testing.T) {
sl := SList[int]{}
for i := 1; i < 10; i++ {
sl.PushFront(i)
expectEq(t, sl.Front(), i)
expectEq(t, sl.Len(), i)
}
}
func Test_SList_PushBack(t *testing.T) {
sl := SList[int]{}
for i := 1; i < 10; i++ {
sl.PushBack(i)
expectEq(t, sl.Back(), i)
expectEq(t, sl.Len(), i)
expectFalse(t, sl.IsEmpty())
}
}
func Test_SList_PopFront(t *testing.T) {
sl := SList[int]{}
expectPanic(t, func() { sl.PopFront() })
sl.PushFront(1)
sl.PushFront(2)
expectEq(t, sl.PopFront(), 2)
expectEq(t, sl.PopFront(), 1)
expectPanic(t, func() { sl.PopFront() })
}
func Test_SList_Reverse(t *testing.T) {
sl := SListOf(1, 2, 3, 4)
sl.Reverse()
expectTrue(t, Equal(sl.Values(), []int{4, 3, 2, 1}))
}
func Test_SList_ForEach(t *testing.T) {
sl := SListOf(1, 2, 3, 4)
i := 0
sl.ForEach(func(v int) {
i++
expectEq(t, v, i)
})
expectEq(t, i, sl.Len())
}
func Test_SList_ForEachIf(t *testing.T) {
sl := SListOf(1, 2, 3, 4)
i := 0
sl.ForEachIf(func(v int) bool {
i++
expectEq(t, v, i)
return i < 3
})
expectEq(t, i, 3)
}
func Test_SList_ForEachMutable(t *testing.T) {
sl := SListOf(1, 2, 3, 4)
i := 0
sl.ForEachMutable(func(v *int) {
i++
expectEq(t, *v, i)
*v = -*v
})
expectEq(t, i, sl.Len())
sl.ForEachMutable(func(v *int) {
expectLt(t, *v, 0)
})
}
func Test_SList_ForEachMutableIf(t *testing.T) {
sl := SListOf(1, 2, 3, 4)
i := 0
sl.ForEachMutableIf(func(v *int) bool {
i++
expectEq(t, *v, i)
return i < 3
})
expectEq(t, i, 3)
}
func Test_SList_Iterate(t *testing.T) {
sl := SList[int]{}
sl.PushBack(1)
sl.PushBack(2)
sl.PushBack(3)
i := 0
for it := sl.Iterate(); it.IsNotEnd(); it.MoveToNext() {
i++
expectEq(t, it.Value(), i)
expectEq(t, *it.Pointer(), i)
}
expectEq(t, i, 3)
}