-
Notifications
You must be signed in to change notification settings - Fork 15
/
list_test.go
115 lines (108 loc) · 1.9 KB
/
list_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
package fenwick
import (
"testing"
)
func TestGet(t *testing.T) {
n := 100
a := make([]int64, n)
for i := range a {
a[i] = int64(i)
}
l := New(a...)
for i := range a {
if a[i] != l.Get(i) {
t.Errorf("Get(%d) = %d; want %d", i, l.Get(i), a[i])
}
}
}
func TestSet(t *testing.T) {
n := 100
a := make([]int64, n)
for i := range a {
a[i] = int64(i)
}
l := New(a...)
for i := range a {
l.Set(i, 100)
if l.Get(i) != 100 {
t.Errorf("Get(%d) = %d; want %d", i, l.Get(i), 100)
}
}
}
func TestAdd(t *testing.T) {
n := 100
a := make([]int64, n)
for i := range a {
a[i] = int64(i)
}
l := New(a...)
for i := range a {
l.Add(i, 100)
if l.Get(i) != a[i]+100 {
t.Errorf("Get(%d) = %d; want %d", i, l.Get(i), a[i]+100)
}
}
}
func TestSum(t *testing.T) {
n := 100
a := make([]int64, n)
for i := range a {
a[i] = int64(i)
}
l := New(a...)
for i := range a {
var res int64
for j := 0; j < i; j++ {
res += a[j]
}
if l.Sum(i) != res {
t.Errorf("Sum(%d) = %d; want %d", i, l.Get(i), res)
}
}
}
func TestSumRange(t *testing.T) {
n := 100
a := make([]int64, n)
for i := range a {
a[i] = int64(i)
}
l := New(a...)
for i := range a {
for j := i; j < n; j++ {
var res int64
for k := i; k < j; k++ {
res += a[k]
}
if l.SumRange(i, j) != res {
t.Errorf("SumRange(%d, %d) = %d; want %d", i, j, l.SumRange(i, j), res)
}
}
}
}
func TestAppend(t *testing.T) {
n := 100
a := make([]int64, n)
l := new(List)
if l.Len() != 0 {
t.Errorf("Len() = %d; want %d", l.Len(), 0)
}
for i := range a {
a[i] = int64(i)
l.Append(int64(i))
}
if l.Len() != n {
t.Errorf("Len() = %d; want %d", l.Len(), n)
}
for i := range a {
var res int64
for j := 0; j < i; j++ {
res += a[j]
}
if l.Get(i) != a[i] {
t.Errorf("Get(%d) = %d; want %d", i, l.Get(i), a[i])
}
if l.Sum(i) != res {
t.Errorf("Sum(%d) = %d; want %d", i, l.Sum(i), res)
}
}
}