-
Notifications
You must be signed in to change notification settings - Fork 2
/
slilce_test.go
128 lines (113 loc) · 2.59 KB
/
slilce_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
package threadsafe
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewSlice(t *testing.T) {
slice := NewSlice[int]()
assert.Equal(t, 0, slice.Length())
}
func TestSliceAppend(t *testing.T) {
slice := NewSlice[int]()
slice.Append(42)
assert.Equal(t, 1, slice.Length())
value, ok := slice.Get(0)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestSliceGetSet(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
ok := slice.Set(1, 42)
assert.True(t, ok)
value, ok := slice.Get(1)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestSliceGetInvalidIndex(t *testing.T) {
slice := NewSlice[int]()
value, ok := slice.Get(10)
assert.False(t, ok)
assert.Equal(t, 0, value)
}
func TestSliceSetInvalidIndex(t *testing.T) {
slice := NewSlice[int]()
ok := slice.Set(10, 42)
assert.False(t, ok)
}
func TestSliceRemove(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
ok := slice.Remove(1)
assert.True(t, ok)
assert.Equal(t, 2, slice.Length())
value, ok := slice.Get(1)
assert.True(t, ok)
assert.Equal(t, 3, value)
}
func TestSliceRemoveInvalidIndex(t *testing.T) {
slice := NewSlice[int]()
ok := slice.Remove(10)
assert.False(t, ok)
}
func TestSliceContains(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
assert.True(t, slice.Contains(2))
assert.False(t, slice.Contains(42))
}
func TestSliceClear(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
slice.Clear()
assert.Equal(t, 0, slice.Length())
}
func TestSliceInsert(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
ok := slice.Insert(1, 42)
assert.True(t, ok)
assert.Equal(t, 4, slice.Length())
value, ok := slice.Get(1)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestSliceInsertInvalidIndex(t *testing.T) {
slice := NewSlice[int]()
ok := slice.Insert(10, 42)
assert.False(t, ok)
}
func TestSliceCopy(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
copySlice := slice.Copy()
assert.Equal(t, slice.Length(), copySlice.Length())
for i := 0; i < slice.Length(); i++ {
origValue, _ := slice.Get(i)
copyValue, _ := copySlice.Get(i)
assert.Equal(t, origValue, copyValue)
}
}
func TestSliceValues(t *testing.T) {
slice := NewSlice[int]()
slice.Append(1)
slice.Append(2)
slice.Append(3)
values := slice.Values()
assert.Equal(t, 3, len(values))
assert.Equal(t, 1, values[0])
assert.Equal(t, 2, values[1])
assert.Equal(t, 3, values[2])
}