-
Notifications
You must be signed in to change notification settings - Fork 7
/
count_test.go
157 lines (119 loc) · 3.77 KB
/
count_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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package cete
import (
"errors"
"io/ioutil"
"os"
"strconv"
"strings"
"testing"
)
func TestCounting(t *testing.T) {
if testing.Short() {
t.Parallel()
}
testCounting(t, false)
}
func TestCountingCompressed(t *testing.T) {
if testing.Short() {
t.Parallel()
}
testCounting(t, true)
}
func testCounting(t *testing.T, compression bool) {
dir, err := ioutil.TempDir("", "cete_")
panicNotNil(err)
t.Log("testing directory:", dir)
defer func() {
if !t.Failed() {
os.RemoveAll(dir)
}
}()
db, err := Open(dir + "/data")
panicNotNil(err)
defer db.Close()
err = db.NewTable("count_testing", compression)
panicNotNil(err)
err = db.Table("count_testing").NewIndex("Age")
panicNotNil(err)
for i := 1; i <= 1000; i++ {
panicNotNil(db.Table("count_testing").Set(paddedItoa(i), Person{Age: i}))
}
if db.Table("count_testing").CountBetween(MinValue, "0010") != 10 {
t.Fatal("count should be 10, but isn't")
}
if db.Table("count_testing").CountBetween(MinValue, "0100") != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").CountBetween("0901", MaxValue) != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").CountBetween("0101", "0200") != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").CountBetween("0100", "0010") != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween(MinValue, MaxValue) != 1000 {
t.Fatal("count should be 1000, but isn't")
}
if db.Table("count_testing").CountBetween(MaxValue, MinValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween(MinValue, MinValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween(MaxValue, MaxValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween("99999999", MaxValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween(0, MaxValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").CountBetween(MinValue, 0) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MinValue, 10) != 10 {
t.Fatal("count should be 10, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MinValue, 100) != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(901, MaxValue) != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(101, 200) != 100 {
t.Fatal("count should be 100, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(100, 10) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MinValue, MaxValue) != 1000 {
t.Fatal("count should be 1000, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MaxValue, MinValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MinValue, MinValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(MaxValue, MaxValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
if db.Table("count_testing").Index("Age").CountBetween(10000, MaxValue) != 0 {
t.Fatal("count should be 0, but isn't")
}
countError := errors.New("cete testing: count error")
r := newRange(func() (string, []byte, uint64, error) {
return "", nil, 0, countError
}, func() {}, nil)
_, err = r.Count()
if err != countError {
t.Fatal("error should be countError, but isn't")
}
}
func paddedItoa(number int) string {
num := strconv.Itoa(number)
return strings.Repeat("0", 4-len(num)) + num
}