-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
74 lines (62 loc) · 1.58 KB
/
utils_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
package quicklist
import (
"bytes"
"encoding/binary"
"math"
"slices"
"testing"
)
func TestUtils(t *testing.T) {
t.Run("appendVarint", func(t *testing.T) {
for i := 0; i < math.MaxUint16; i++ {
// append varint
b1 := binary.AppendUvarint(nil, uint64(i))
b2 := appendUvarint(nil, i, false)
b3 := appendUvarint(nil, i, true)
b4 := slices.Clone(b3)
slices.Reverse(b4)
equalBytes(t, b1, b2)
equalBytes(t, b1, b2)
equalBytes(t, b1, b4)
// read uvarint
x1, s1 := binary.Uvarint(b1)
x2, s2 := uvarintReverse(b3)
x3, s3 := uvarintReverse(append([]byte("something"), b3...))
equal(t, x1, x2)
equal(t, x1, x3)
equal(t, s1, s2)
equal(t, s1, s3)
}
})
}
// for test
func lessOrEqual[T int](t *testing.T, expected, actual T) {
if expected > actual {
t.Fatalf("[lessOrEqual] expected: %v, actual: %v", expected, actual)
}
}
func notEqual[T comparable](t *testing.T, expected, actual T) {
if expected == actual {
t.Fatalf("[notEqual] expected: %v, actual: %v", expected, actual)
}
}
func equal[T comparable](t *testing.T, expected, actual T) {
if expected != actual {
t.Fatalf("[equal] expected: %v, actual: %v", expected, actual)
}
}
func equalBytes(t *testing.T, expected, actual []byte) {
if !bytes.Equal(expected, actual) {
t.Fatalf("[equalBytes] expected: %v, actual: %v", expected, actual)
}
}
func isNil(t *testing.T, expected any) {
if expected != nil {
t.Fatalf("[isNil] expected nil: %v", expected)
}
}
func isNotNil(t *testing.T, expected any) {
if expected == nil {
t.Fatalf("[isNotNil] expected not nil: %v", expected)
}
}