-
Notifications
You must be signed in to change notification settings - Fork 5
/
xz_test.go
91 lines (78 loc) · 1.33 KB
/
xz_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
package xz
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)
func TestDecompress(T *testing.T) {
orig, err := os.Open("testdata/spec")
if err != nil {
T.Fatal(err)
}
defer orig.Close()
expected := &bytes.Buffer{}
_, err = io.Copy(expected, orig)
if err != nil {
T.Fatal(err)
}
source, err := os.Open("testdata/spec.xz")
if err != nil {
T.Fatal(err)
}
defer source.Close()
r, err := NewReader(source)
if err != nil {
T.Fatal(err)
}
buf := &bytes.Buffer{}
n, err := io.Copy(buf, r)
if n != int64(expected.Len()) {
T.Fail()
}
if bytes.Compare(buf.Bytes(), expected.Bytes()) != 0 {
T.Fail()
}
err = r.Close()
if err != nil {
T.Fail()
}
}
func TestEarlyClose(T *testing.T) {
source, err := os.Open("testdata/spec.xz")
if err != nil {
T.Fatal(err)
}
defer source.Close()
r, err := NewReader(source)
if err != nil {
T.Fatal(err)
}
buf := make([]byte, 10)
n, err := r.Read(buf)
if n != len(buf) {
T.Fail()
}
err = r.Close()
if err != nil {
T.Fail()
}
}
func BenchmarkDecompress(b *testing.B) {
data, err := ioutil.ReadFile("testdata/spec.xz")
if err != nil {
b.Fatal(err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
r, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatal(err)
}
_, err = io.Copy(ioutil.Discard, r)
if err != nil {
b.Fatal(err)
}
}
}