forked from peterbourgon/diskv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compression_test.go
73 lines (60 loc) · 1.59 KB
/
compression_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
package diskv
import (
"compress/flate"
"fmt"
"math/rand"
"os"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func testCompressionWith(t *testing.T, c Compression, name string) {
d := New(Options{
BasePath: "compression-test",
Transform: func(string) []string { return []string{""} },
CacheSizeMax: 0,
Compression: c,
})
defer d.EraseAll()
sz := 4096
val := make([]byte, sz)
for i := 0; i < sz; i++ {
val[i] = byte('a' + rand.Intn(26)) // {a-z}; should compress some
}
key := "a"
if err := d.Write(key, val); err != nil {
t.Fatalf("write failed: %s", err)
}
targetFile := fmt.Sprintf("%s%c%s", d.BasePath, os.PathSeparator, key)
fi, err := os.Stat(targetFile)
if err != nil {
t.Fatalf("%s: %s", targetFile, err)
}
if fi.Size() >= int64(sz) {
t.Fatalf("%s: size=%d, expected smaller", targetFile, fi.Size())
}
t.Logf("%s compressed %d to %d", name, sz, fi.Size())
readVal, err := d.Read(key)
if len(readVal) != sz {
t.Fatalf("read: expected size=%d, got size=%d", sz, len(readVal))
}
for i := 0; i < sz; i++ {
if readVal[i] != val[i] {
t.Fatalf("i=%d: expected %v, got %v", i, val[i], readVal[i])
}
}
}
func TestGzipDefault(t *testing.T) {
testCompressionWith(t, NewGzipCompression(), "gzip")
}
func TestGzipBestCompression(t *testing.T) {
testCompressionWith(t, NewGzipCompressionLevel(flate.BestCompression), "gzip-max")
}
func TestGzipBestSpeed(t *testing.T) {
testCompressionWith(t, NewGzipCompressionLevel(flate.BestSpeed), "gzip-min")
}
func TestZlib(t *testing.T) {
testCompressionWith(t, NewZlibCompression(), "zlib")
}