-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslist_test.go
130 lines (95 loc) · 2.54 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
package Slist
import (
"fmt"
"github.com/stretchr/testify/assert"
"testing"
)
func TestNew(t *testing.T) {
l := New()
assert.Equal(t, l.Size(), 0)
l1 := New(1, 2, 3)
assert.Equal(t, l1.Size(), 3)
}
func TestSlist_Swap(t *testing.T) {
l0 := New()
lpos := New(1, 2, 3, 4, 5)
lneg := New(-5, -4, -3, -2, -1)
result := l0.Swap(lpos) // test empty swap non-empty
assert.Equal(t, l0.Size(), 5)
assert.Equal(t, result, l0)
assert.Equal(t, lpos.Size(), 0)
result = l0.Swap(lpos) // test non-empty swap empty
assert.Equal(t, l0.Size(), 0)
assert.Equal(t, lpos.Size(), 5)
assert.Equal(t, result, l0)
result = lpos.Swap(lneg)
assert.Equal(t, lpos.Size(), 5)
assert.Equal(t, lneg.Size(), 5)
assert.Equal(t, result, lpos)
assert.True(t, lpos.Traverse(func(key interface{}) bool {
return key.(int) >= -5 && key.(int) <= -1
}))
assert.True(t, lneg.Traverse(func(key interface{}) bool {
return key.(int) >= 1 && key.(int) <= 5
}))
}
func Test_misc(t *testing.T) {
l := New(1, 2, 3)
assert.Equal(t, l.First().(int), 1)
assert.Equal(t, l.Last().(int), 3)
}
func TestSlist_RemoveFirst(t *testing.T) {
assert.Nil(t, New().RemoveFirst())
l := New(1, 2, 3)
for i := 1; !l.IsEmpty(); i++ {
item := l.RemoveFirst()
assert.NotNil(t, item)
assert.Equal(t, item.(int), i)
assert.LessOrEqual(t, i, 3)
}
}
func TestSlist_ToSlice(t *testing.T) {
l := New(1, 2, 3)
s := l.ToSlice()
for i, it := 0, NewIterator(l); it.HasCurr(); it.Next() {
assert.Equal(t, it.GetCurr().(int), s[i].(int))
fmt.Printf("%d == %d\n", it.GetCurr().(int), s[i].(int))
i++
}
}
func TestSlist_clone(t *testing.T) {
l1 := New(1, 2, 3)
l2 := l1.clone()
for it1, it2 := NewIterator(l1), NewIterator(l2); it1.HasCurr() && it2.HasCurr(); {
assert.Equal(t, it1.GetCurr().(int), it2.GetCurr().(int))
it1.Next()
it2.Next()
}
}
func TestSlist_ReverseInPlace(t *testing.T) {
l := New(1, 2, 3)
l.ReverseInPlace()
assert.Equal(t, l.Size(), 3)
assert.Equal(t, l.First(), 3)
assert.Equal(t, l.Last(), 1)
}
func TestSlist_Reverse(t *testing.T) {
l := New(1, 2, 3)
r := l.Reverse()
assert.Equal(t, l.Size(), 3)
assert.Equal(t, l.First(), 1)
assert.Equal(t, l.Last(), 3)
assert.Equal(t, r.Size(), 3)
assert.Equal(t, r.First(), 3)
assert.Equal(t, r.Last(), 1)
}
func TestSlist_RotateLeftInPlace(t *testing.T) {
l := New(1, 2, 3, 4, 5)
lp := l.RotateLeft(2)
l.RotateLeftInPlace(2)
for it1, it2 := NewIterator(l), NewIterator(lp); it1.HasCurr() && it2.HasCurr(); {
assert.Equal(t, it1.GetCurr().(int), it2.GetCurr().(int))
it1.Next()
it2.Next()
}
}