-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathintslice_test.go
139 lines (118 loc) · 2.72 KB
/
intslice_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
package villa
import (
"fmt"
"testing"
"github.com/golangplus/testing/assert"
)
func TestIntSlice(t *testing.T) {
defer __(o_(t))
var s IntSlice
for i := 0; i < 1000; i++ {
s.Add(i)
} // for i
assert.Equal(t, "len", len(s), 1000)
//fmt.Println(s)
s.Clear()
assert.Equal(t, "len", len(s), 0)
s = IntSlice{}
s.Add(1)
s.Insert(0, 2)
s.Insert(1, 3)
t.Logf("%v", s)
assert.Equal(t, "len", len(s), 3)
assert.StringEqual(t, "s", s, "[2 3 1]")
}
func TestIntSliceRemove(t *testing.T) {
defer __(o_(t))
var s IntSlice
s.Add(1, 2, 3, 4, 5, 6, 7)
assert.Equal(t, "len", len(s), 7)
assert.StringEqual(t, "s", s, "[1 2 3 4 5 6 7]")
s.Fill(2, 5, 9)
assert.StringEqual(t, "s", s, "[1 2 9 9 9 6 7]")
s.RemoveRange(2, 5)
assert.Equal(t, "len", len(s), 4)
assert.StringEqual(t, "s", s, "[1 2 6 7]")
s.Remove(2)
assert.Equal(t, "len", len(s), 3)
assert.StringEqual(t, "s", s, "[1 2 7]")
}
func TestIntSliceEquals(t *testing.T) {
s := IntSlice([]int{1, 2, 3, 4})
assert.Equal(t, "s.Equals(nil)", s.Equals(nil), false)
assert.Equal(t, "s.Equals([1, 2, 3, 4])", s.Equals([]int{1, 2, 3, 4}), true)
assert.Equal(t, "s.Equals([1, 2, 5, 4])", s.Equals([]int{1, 2, 5, 4}), false)
assert.Equal(t, "s.Equals([1, 2, 3, 4, 5])", s.Equals([]int{1, 2, 3, 4, 5}), false)
assert.Equal(t, "nil.Equals([]int{})", IntSlice(nil).Equals(s[:0]), true)
assert.Equal(t, "nil.Equals([]int{1, 2})", IntSlice(nil).Equals([]int{1, 2}), false)
}
func ExampleIntSlice_direct() {
var s IntSlice
s.Add(10, 20, 30)
fmt.Println(s)
s.Insert(1, 40, 50)
fmt.Println(s)
s.Swap(1, len(s)-1)
fmt.Println(s)
s.RemoveRange(1, 3)
fmt.Println(s)
s.Fill(0, len(s), 55)
fmt.Println(s)
s.Clear()
fmt.Println(s)
// Output:
// [10 20 30]
// [10 40 50 20 30]
// [10 30 50 20 40]
// [10 20 40]
// [55 55 55]
// []
}
func ExampleIntSlice_typecnv() {
var s []int
s = append(s, 10, 20, 30)
fmt.Println(s)
(*IntSlice)(&s).Insert(1, 40, 50)
fmt.Println(s)
IntSlice(s).Swap(1, len(s)-1)
fmt.Println(s)
(*IntSlice)(&s).RemoveRange(1, 3)
fmt.Println(s)
IntSlice(s).Fill(0, len(s), 55)
fmt.Println(s)
s = s[:0]
fmt.Println(s)
// Output:
// [10 20 30]
// [10 40 50 20 30]
// [10 30 50 20 40]
// [10 20 40]
// [55 55 55]
// []
}
func BenchmarkIntSliceInsert(b *testing.B) {
b.StopTimer()
s := make(IntSlice, 100000)
b.StartTimer()
for i := 0; i < b.N; i++ {
s.Insert(1, i)
}
}
func BenchmarkIntSliceInsertByAppend(b *testing.B) {
b.StopTimer()
s := make([]int, 100000)
b.StartTimer()
for i := 0; i < b.N; i++ {
s = append(s[:1], append([]int{i}, s[1:]...)...)
}
}
func BenchmarkIntSliceInsertByCopy(b *testing.B) {
b.StopTimer()
s := make([]int, 100000)
b.StartTimer()
for i := 0; i < b.N; i++ {
s = append(s, 0)
copy(s[2:], s[1:])
s[1] = i
}
}