-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathreader_test.go
137 lines (130 loc) · 3.11 KB
/
reader_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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright 2014-2022 Ulrich Kunitz. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package xz
import (
"bytes"
"io"
"io/ioutil"
"os"
"testing"
)
func TestReaderSimple(t *testing.T) {
const file = "fox.xz"
xz, err := os.Open(file)
if err != nil {
t.Fatalf("os.Open(%q) error %s", file, err)
}
r, err := NewReader(xz)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, r); err != nil {
t.Fatalf("io.Copy error %s", err)
}
}
func TestReaderSingleStream(t *testing.T) {
data, err := ioutil.ReadFile("fox.xz")
if err != nil {
t.Fatalf("ReadFile error %s", err)
}
xz := bytes.NewReader(data)
rc := ReaderConfig{SingleStream: true}
r, err := rc.NewReader(xz)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, r); err != nil {
t.Fatalf("io.Copy error %s", err)
}
buf.Reset()
data = append(data, 0)
xz = bytes.NewReader(data)
r, err = rc.NewReader(xz)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
if _, err = io.Copy(&buf, r); err != errUnexpectedData {
t.Fatalf("io.Copy returned %v; want %v", err, errUnexpectedData)
}
}
func TestReaderMultipleStreams(t *testing.T) {
data, err := ioutil.ReadFile("fox.xz")
if err != nil {
t.Fatalf("ReadFile error %s", err)
}
m := make([]byte, 0, 4*len(data)+4*4)
m = append(m, data...)
m = append(m, data...)
m = append(m, 0, 0, 0, 0)
m = append(m, data...)
m = append(m, 0, 0, 0, 0)
m = append(m, 0, 0, 0, 0)
m = append(m, data...)
m = append(m, 0, 0, 0, 0)
xz := bytes.NewReader(m)
r, err := NewReader(xz)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, r); err != nil {
t.Fatalf("io.Copy error %s", err)
}
}
func TestCheckNone(t *testing.T) {
const file = "fox-check-none.xz"
xz, err := os.Open(file)
if err != nil {
t.Fatalf("os.Open(%q) error %s", file, err)
}
r, err := NewReader(xz)
if err != nil {
t.Fatalf("NewReader error %s", err)
}
var buf bytes.Buffer
if _, err = io.Copy(&buf, r); err != nil {
t.Fatalf("io.Copy error %s", err)
}
}
func BenchmarkReader(b *testing.B) {
const testFile = "testdata/enwik7"
data, err := os.ReadFile(testFile)
if err != nil {
b.Fatalf("os.ReadFile(%q) error %s", testFile, err)
}
buf := new(bytes.Buffer)
uncompressedLen := int64(len(data))
b.SetBytes(int64(uncompressedLen))
b.ReportAllocs()
buf.Reset()
w, err := NewWriter(buf)
if err != nil {
b.Fatalf("NewWriter(buf) error %s", err)
}
if _, err = w.Write(data); err != nil {
b.Fatalf("w.Write(data) error %s", err)
}
if err = w.Close(); err != nil {
b.Fatalf("w.Write(data)")
}
data = make([]byte, buf.Len())
copy(data, buf.Bytes())
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf.Reset()
r, err := NewReader(bytes.NewReader(data))
if err != nil {
b.Fatalf("NewReader(data) error %s", err)
}
n, err := io.Copy(buf, r)
if err != nil {
b.Fatalf("io.Copy(buf, r) error %s", err)
}
if n != uncompressedLen {
b.Fatalf("io.Copy got %d; want %d", n, uncompressedLen)
}
}
}