-
Notifications
You must be signed in to change notification settings - Fork 63
/
dict_test.go
107 lines (96 loc) · 2.35 KB
/
dict_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
package gozstd
import (
"fmt"
"math/rand"
"testing"
"time"
)
func TestCDictEmpty(t *testing.T) {
cd, err := NewCDict(nil)
if err == nil {
t.Fatalf("expecting non-nil error")
}
if cd != nil {
t.Fatalf("expecting nil cd")
}
}
func TestDDictEmpty(t *testing.T) {
dd, err := NewDDict(nil)
if err == nil {
t.Fatalf("expecting non-nil error")
}
if dd != nil {
t.Fatalf("expecting nil dd")
}
}
func TestCDictCreateRelease(t *testing.T) {
var samples [][]byte
for i := 0; i < 1000; i++ {
samples = append(samples, []byte(fmt.Sprintf("sample %d", i)))
}
dict := BuildDict(samples, 64*1024)
for i := 0; i < 10; i++ {
cd, err := NewCDict(dict)
if err != nil {
t.Fatalf("cannot create dict: %s", err)
}
cd.Release()
}
}
func TestDDictCreateRelease(t *testing.T) {
var samples [][]byte
for i := 0; i < 1000; i++ {
samples = append(samples, []byte(fmt.Sprintf("sample %d", i)))
}
dict := BuildDict(samples, 64*1024)
for i := 0; i < 10; i++ {
dd, err := NewDDict(dict)
if err != nil {
t.Fatalf("cannot create dict: %s", err)
}
dd.Release()
}
}
func TestBuildDict(t *testing.T) {
for _, samplesCount := range []int{0, 1, 10, 100, 1000} {
t.Run(fmt.Sprintf("samples_%d", samplesCount), func(t *testing.T) {
var samples [][]byte
for i := 0; i < samplesCount; i++ {
sample := []byte(fmt.Sprintf("sample %d, rand num %d, other num %X", i, rand.Intn(100), rand.Intn(100000)))
samples = append(samples, sample)
samples = append(samples, nil) // add empty sample
}
for _, desiredDictLen := range []int{20, 256, 1000, 10000} {
t.Run(fmt.Sprintf("desiredDictLen_%d", desiredDictLen), func(t *testing.T) {
testBuildDict(t, samples, desiredDictLen)
})
}
})
}
}
func testBuildDict(t *testing.T, samples [][]byte, desiredDictLen int) {
t.Helper()
// Serial test.
dictOrig := BuildDict(samples, desiredDictLen)
// Concurrent test.
ch := make(chan error, 3)
for i := 0; i < cap(ch); i++ {
go func() {
dict := BuildDict(samples, desiredDictLen)
if string(dict) != string(dictOrig) {
ch <- fmt.Errorf("unexpected dict; got\n%X; want\n%X", dict, dictOrig)
}
ch <- nil
}()
}
for i := 0; i < cap(ch); i++ {
select {
case err := <-ch:
if err != nil {
t.Fatalf("error in concurrent test: %s", err)
}
case <-time.After(time.Second):
t.Fatalf("timeout in concurrent test")
}
}
}