-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlzfse_test.go
111 lines (95 loc) · 2.3 KB
/
lzfse_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
package lzfse
import (
"bufio"
"bytes"
"crypto/rand"
"io"
"io/ioutil"
"os"
"strings"
"syscall"
"testing"
)
func allocInStreams(n int) ([]*inStream, error){
ret := make([]*inStream, n)
payload := make([]byte, 64)
rand.Read(payload)
for i := 0; i < n; i++ {
var err error
r := bytes.NewReader(payload)
ret[i], err = newInStream(0, r)
if err != nil {
return nil, err
}
}
return ret, nil
}
func BenchmarkFsePull(b *testing.B) {
inStreams, err := allocInStreams(b.N)
if err != nil {
b.FailNow()
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
s := inStreams[i]
for s.accum_nbits >= 1 {
s.pull(1)
}
}
}
// Run make -C test/ to generate the data.
func TestVariousSizes(t *testing.T) {
if testFile, err := os.Open("test/test.list"); err == nil {
defer testFile.Close()
scanner := bufio.NewScanner(testFile)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
for _, compressedInput := range strings.Fields(scanner.Text()) {
decompressedInput := strings.TrimSuffix(compressedInput, ".cmp")
errorFile := decompressedInput + ".err"
t.Run(compressedInput, func(t *testing.T) {
DoDecomp(compressedInput, decompressedInput, errorFile, t)
})
}
}
}
}
func DoDecomp(compressed, original, errorOutputFile string, t *testing.T) {
ff, err := syscall.Open(compressed, syscall.O_RDONLY, 0)
if err != nil {
t.Errorf("couldn't open file")
}
var stat syscall.Stat_t
err = syscall.Fstat(ff, &stat)
if err != nil {
t.Errorf("Couldn't fstat")
}
fd, err := syscall.Mmap(ff, 0, int(stat.Size), syscall.PROT_READ, syscall.MAP_SHARED)
if err != nil {
t.Errorf("Couldn't mmap test file")
}
cmp := bytes.NewReader(fd)
// cmp, err := os.Open(compressed)
// if err != nil {
// t.Errorf("Couldn't open test file")
// }
// defer cmp.Close()
dec, err := os.Open(original)
if err != nil {
t.Errorf("Couldn't open decompressed file")
}
defer dec.Close()
decBytes, err := ioutil.ReadAll(dec)
if err != nil {
t.Errorf("Couldn't readall original")
}
var buffer bytes.Buffer
d := NewReader(cmp)
if n, err := io.Copy(&buffer, d); err != nil {
t.Errorf("Error decompressing: %v [orig= %d new=%d]", err, len(decBytes), n)
}
if !bytes.Equal(buffer.Bytes(), decBytes) {
t.Errorf("The outputs did not match")
ioutil.WriteFile(errorOutputFile, buffer.Bytes(), 0644)
}
}