forked from DiceDB/dice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeque_test.go
140 lines (120 loc) · 3.67 KB
/
deque_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
package eval_test
import (
"fmt"
"github.com/dicedb/dice/internal/eval"
"math/rand"
"strconv"
"testing"
"time"
"gotest.tools/v3/assert"
)
var deqRandGenerator *rand.Rand
func deqTestInit() {
randSeed := time.Now().UnixNano()
deqRandGenerator = rand.New(rand.NewSource(randSeed))
fmt.Printf("rand seed: %v", randSeed)
}
var deqRunes = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_!@#$%^&*()-=+[]\\;':,.<>/?~.|")
func deqRandStr(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = deqRunes[deqRandGenerator.Intn(len(deqRunes))]
}
return string(b)
}
func TestDeqEncodeEntryString(t *testing.T) {
deqTestInit()
testCases := []string{
deqRandStr(1), // min 6 bit string
deqRandStr(10), // 6 bit string
deqRandStr((1 << 6) - 1), // max 6 bit string
deqRandStr(1 << 6), // min 12 bit string
deqRandStr(2024), // 12 bit string
deqRandStr((1 << 12) - 1), // max 12 bit string
deqRandStr(1 << 12), // min 32 bit string
deqRandStr((1 << 20) - 1000), // 32 bit string
// randStr((1 << 32) - 1), // max 32 bit string, maybe too huge to test..
"0", // min 7 bit uint
"28", // 7 bit uint
"127", // max 7 bit uint
"-4096", // min 13 bit int
"2024", // + 13 bit int
"-2024", // - 13 bit int
"4095", // max 13 bit int
"-32768", // min 16 bit int
"15384", // + 16 bit int
"-15384", // - 16 bit int
"32767", // max 16 bit int
"-8388608", // min 24 bit int
"4193301", // + 24 bit int
"-4193301", // - 24 bit int
"8388607", // max 24 bit int
"-2147483648", // min 32 bit int
"1073731765", // + 32 bit int
"-1073731765", // - 32 bit int
"2147483647", // max 32 bit int
"-9223372036854775808", // min 64 bit int
"4611686018427287903", // + 64 bit int
"-4611686018427287903", // - 64 bit int
"9223372036854775807", // max 64 bit int
}
for _, tc := range testCases {
x, _ := eval.DecodeDeqEntry(eval.EncodeDeqEntry(tc))
assert.Equal(t, tc, x)
}
}
func dequeRPushIntStrMany(howmany int, deq eval.DequeI, b *testing.B) {
for i := 0; i < howmany; i++ {
deq.RPush(strconv.FormatInt(int64(i), 10))
}
}
func dequeLPushIntStrMany(howmany int, deq eval.DequeI, b *testing.B) {
for i := 0; i < howmany; i++ {
deq.LPush(strconv.FormatInt(int64(i), 10))
}
}
func BenchmarkBasicDequeRPush20(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(20, eval.NewBasicDeque(), b)
}
}
func BenchmarkBasicDequeRPush200(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(200, eval.NewBasicDeque(), b)
}
}
func BenchmarkBasicDequeRPush2000(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(2000, eval.NewBasicDeque(), b)
}
}
func BenchmarkDequeRPush20(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(20, eval.NewDeque(), b)
}
}
func BenchmarkDequeRPush200(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(200, eval.NewDeque(), b)
}
}
func BenchmarkDequeRPush2000(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeRPushIntStrMany(2000, eval.NewDeque(), b)
}
}
func BenchmarkDequeLPush20(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeLPushIntStrMany(20, eval.NewDeque(), b)
}
}
func BenchmarkDequeLPush200(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeLPushIntStrMany(200, eval.NewDeque(), b)
}
}
func BenchmarkDequeLPush2000(b *testing.B) {
for n := 0; n < b.N; n++ {
dequeLPushIntStrMany(2000, eval.NewDeque(), b)
}
}