This repository has been archived by the owner on Jan 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
skiptake_test.go
148 lines (118 loc) · 3.23 KB
/
skiptake_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
package skiptake
import (
"bytes"
"testing"
)
func equalUint64(a, b []uint64) bool {
if len(a) != len(b) {
return false
}
for i := 0; i < len(a); i++ {
if a[i] != b[i] {
return false
}
}
return true
}
type intrv [2]uint
func makeRange(args ...intrv) List {
b := Build(&List{})
for _, p := range args {
b.Next(uint64(p[0]))
b.Take(uint64(p[1] - p[0]))
}
return b.Finish()
}
// Test the testers
func Test_makeRange(t *testing.T) {
subject := makeRange(intrv{0, 2}, intrv{4, 5})
expected := []uint64{0, 1, 2, 4, 5}
result := subject.Expand()
if !equalUint64(expected, result) {
t.Fatalf("Test function makeRange() Does not work: %v != %v", subject, Create(expected...))
}
}
func Test_SkipTake_Create(t *testing.T) {
subject := []uint64{533252, 2120193, 3173236, 3875580}
expected := FromRaw(533252, 1,
2120193-533252-1, 1,
3173236-2120193-1, 1,
3875580-3173236-1, 1)
result := Create(subject...)
if bytes.Compare(result, expected) != 0 {
t.Errorf("%s != %s", result.String(), expected.String())
}
t.Logf("%#v", []byte(result))
expanded := result.Expand()
if !equalUint64(subject, expanded) {
t.Errorf("%v != %v", subject, expanded)
}
}
func Test_SkipTake_Expand(t *testing.T) {
subject := FromRaw(5, 3, 10, 1, 1, 2)
expected := []uint64{5, 6, 7, 18, 20, 21}
result := subject.Expand()
t.Logf("%v -> %v", subject.GetRaw(), result)
if !equalUint64(expected, result) {
t.Errorf("%v != %v", result, expected)
}
}
func Test_SkipTake_Compress(t *testing.T) {
subject := []uint64{2, 3, 4, 5, 9, 11, 13, 15, 16}
expected := FromRaw(2, 4, 3, 1, 1, 1, 1, 1, 1, 2)
result := Create(subject...)
t.Logf("%v -> %v", subject, result)
if !Equal(expected, result) {
t.Errorf("Encode %v != %v", result, expected)
}
}
func Test_SkipTake_CompressExpand(t *testing.T) {
subject := []uint64{2, 3, 4, 5, 9, 22, 23, 24, 100, 200, 201}
list := Create(subject...)
t.Logf("%v -> %v", subject, list)
result := list.Expand()
t.Logf("%v -> %v", list, result)
if !equalUint64(subject, result) {
t.Errorf("%v != %v", subject, result)
}
}
func expectUint64(t *testing.T, result, expected uint64) {
if result != expected {
t.Errorf("%d != %d", result, expected)
}
}
func Test_SkipTake_LargeSkip(t *testing.T) {
subject := []uint64{0x200000000, 0x200000001, 0xaaaabbbbccccddd0, 0xaaaabbbbccccddd1, 0xaaaabbbbccccddd2}
list := Create(subject...)
t.Logf("%v -> %v", subject, list)
if int(list.Len()) != len(subject) {
t.Errorf("Len() != %d", len(subject))
}
result := list.Expand()
if !equalUint64(subject, result) {
t.Errorf("%v != %v", result, subject)
}
}
func Test_SkipTake_LargeTake(t *testing.T) {
list := FromRaw(0x10, 0xffffffff, 0, 1)
if list.Len() != 0x100000000 {
t.Error("Bad big skip")
}
iter := list.Iterate()
expectUint64(t, iter.Next(), 0x10)
skip, take := iter.Seek(0xffffffff)
expectUint64(t, skip, 0x10000000f)
expectUint64(t, take, 1)
}
func Test_SkipTake_InclusiveMaxValue(t *testing.T) {
subject := []uint64{0x100000000000, 0xfffffffffffffffe, 0xffffffffffffffff}
list := Create(subject...)
if int(list.Len()) != len(subject) {
t.Errorf("Len() != %d", len(subject))
return
}
result := list.Expand()
if !equalUint64(subject, result) {
t.Errorf("%#v != %#v", result, subject)
}
}