-
Notifications
You must be signed in to change notification settings - Fork 29
/
compress_test.go
71 lines (63 loc) · 1.69 KB
/
compress_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
package kdb
import (
"bufio"
"bytes"
//"fmt"
"math/rand"
"reflect"
"testing"
)
// -18!2000#1b
var bytes2KTrue = []byte{0x01, 0x00, 0x01, 0x00, 0x26, 0x00, 0x00, 0x00, 0xde, 0x07, 0x00, 0x00, 0x00, 0x01, 0x00, 0xd0, 0x07, 0x00, 0x00, 0x01, 0x01, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x00, 0xc5}
func TestCompress(t *testing.T) {
true2K := make([]bool, 2000)
for i := 0; i < len(true2K); i++ {
true2K[i] = true
}
buf := new(bytes.Buffer)
_ = Encode(buf, ASYNC, &K{KB, NONE, true2K})
bc := buf.Bytes()
if !bytes.Equal(bc, bytes2KTrue) {
t.Errorf("Compress failed expected/got: \n%v\n%v\n", bytes2KTrue, bc)
}
}
func TestUncompress(t *testing.T) {
true2K := make([]bool, 2000)
for i := 0; i < len(true2K); i++ {
true2K[i] = true
}
buf := new(bytes.Buffer)
_ = Encode(buf, ASYNC, &K{KB, NONE, true2K})
uc2 := Uncompress(bytes2KTrue[8:])
uc1 := Uncompress(buf.Bytes()[8:])
if !bytes.Equal(uc1, uc2) {
t.Errorf("Uncompress failed expected/got: \n%v\n%v\n", buf.Bytes(), bytes2KTrue)
}
}
func generateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
func TestCompressRoundtrip(t *testing.T) {
true2K := make([]bool, 2000)
for i := 0; i < len(true2K); i++ {
true2K[i] = true
}
k1 := &K{KB, NONE, true2K}
buf := new(bytes.Buffer)
Encode(buf, ASYNC, k1)
k2, _, _ := Decode(bufio.NewReader(buf))
if !reflect.DeepEqual(k1, k2) {
t.Errorf("Roundtrip failed expected/got: \n%v\n%v\n", k1, k2)
}
}
func BenchmarkUncompress(b *testing.B) {
for i := 0; i < b.N; i++ {
Uncompress(bytes2KTrue[8:])
}
}