-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patharray_test.go
133 lines (115 loc) · 2.59 KB
/
array_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
package threadsafe
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestNewArray(t *testing.T) {
arr := NewArray[int](5)
assert.Equal(t, 5, arr.Length())
}
func TestArrayGetSet(t *testing.T) {
arr := NewArray[int](3)
ok := arr.Set(2, 42)
assert.True(t, ok)
value, ok := arr.Get(2)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestArrayGetInvalidIndex(t *testing.T) {
arr := NewArray[int](1)
value, ok := arr.Get(10)
assert.False(t, ok)
assert.Equal(t, 0, value)
}
func TestArraySetInvalidIndex(t *testing.T) {
arr := NewArray[int](1)
ok := arr.Set(10, 42)
assert.False(t, ok)
}
func TestArrayAppend(t *testing.T) {
arr := NewArray[int](0)
arr.Append(42)
assert.Equal(t, 1, arr.Length())
value, ok := arr.Get(0)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestArrayRemove(t *testing.T) {
arr := NewArray[int](3)
arr.Set(0, 1)
arr.Set(1, 2)
arr.Set(2, 3)
ok := arr.Remove(1)
assert.True(t, ok)
assert.Equal(t, 2, arr.Length())
value, ok := arr.Get(1)
assert.True(t, ok)
assert.Equal(t, 3, value)
}
func TestArrayRemoveInvalidIndex(t *testing.T) {
arr := NewArray[int](1)
ok := arr.Remove(10)
assert.False(t, ok)
}
func TestArrayContains(t *testing.T) {
arr := NewArray[int](3)
arr.Set(0, 1)
arr.Set(1, 2)
arr.Set(2, 3)
assert.True(t, arr.Contains(2))
assert.False(t, arr.Contains(42))
}
func TestArrayClear(t *testing.T) {
arr := NewArray[int](3)
arr.Set(0, 1)
arr.Set(1, 2)
arr.Set(2, 3)
arr.Clear()
assert.Equal(t, 0, arr.Length())
}
func TestArrayInsert(t *testing.T) {
arr := NewArray[int](3)
arr.Set(0, 1)
arr.Set(1, 2)
arr.Set(2, 3)
ok := arr.Insert(1, 42)
assert.True(t, ok)
assert.Equal(t, 4, arr.Length())
value, ok := arr.Get(1)
assert.True(t, ok)
assert.Equal(t, 42, value)
}
func TestArrayInsertInvalidIndex(t *testing.T) {
arr := NewArray[int](1)
ok := arr.Insert(10, 42)
assert.False(t, ok)
}
func TestArrayCopy(t *testing.T) {
arr := NewArray[int](3)
arr.Set(0, 1)
arr.Set(1, 2)
arr.Set(2, 3)
copyArr := arr.Copy()
assert.Equal(t, arr.Length(), copyArr.Length())
for i := 0; i < arr.Length(); i++ {
origValue, _ := arr.Get(i)
copyValue, _ := copyArr.Get(i)
assert.Equal(t, origValue, copyValue)
}
}
func TestArrayValues(t *testing.T) {
arr := NewArray[int](10)
// Set values in the array
for i := 0; i < arr.Length(); i++ {
arr.Set(i, i*10)
}
// Get all values from the array
values := arr.Values()
// Check the length of the values slice
assert.Equal(t, arr.Length(), len(values))
// Check each value
for i, v := range values {
expectedValue, _ := arr.Get(i)
assert.Equal(t, expectedValue, v)
}
}