-
Notifications
You must be signed in to change notification settings - Fork 1
/
reader_test.go
101 lines (79 loc) · 1.85 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
package yenc
import (
"bytes"
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDecoder(t *testing.T) {
asrt := assert.New(t)
encoded, err := ioutil.ReadFile("./test/00000005.ntx")
asrt.NoError(err)
unencoded, err := ioutil.ReadFile("./test/testfile.txt")
asrt.NoError(err)
decBytes, err := ioutil.ReadAll(NewReader(bytes.NewReader(encoded)))
asrt.NoError(err)
if !asrt.Equal(decBytes, unencoded) {
fmt.Println("predecoded")
fmt.Println(hex.Dump(encoded))
fmt.Println("decoded")
fmt.Println(hex.Dump(decBytes))
fmt.Println("expected")
fmt.Println(hex.Dump(unencoded))
diff := getDiff(unencoded, decBytes)
t.Errorf("Decoded bytes did not equal unencoded bytes. Diff was %d long; dec was %d long", len(diff), len(decBytes))
}
}
func TestReader(t *testing.T) {
asrt := assert.New(t)
fs, _ := filepath.Glob("./test/examples/*raw")
for _, e := range fs {
enc, err := os.Open(e)
asrt.NoError(err)
r := NewReader(enc)
b := &bytes.Buffer{}
_, err = io.Copy(b, r)
asrt.NoError(err)
}
}
func BenchmarkReader(b *testing.B) {
encoded, err := ioutil.ReadFile("./test/00000005.ntx")
if err != nil {
b.Fatalf("could not begin test: ntx file not readable")
}
b.SetBytes(int64(len(encoded)))
b.RunParallel(func(pb *testing.PB) {
empty := make([]byte, 1024)
for pb.Next() {
r := NewReader(bytes.NewReader(encoded))
n, err := r.Read(empty)
if err != nil && err != io.EOF {
b.Fatal(err)
}
if n == 0 {
log.Println(empty)
b.Fatal("Didn't read anything")
}
}
})
}
func getDiff(b1, b2 []byte) []byte {
b1c := make([]byte, len(b1))
copy(b1c, b1)
b2c := make([]byte, len(b2))
copy(b2c, b2)
for len(b1c) > 0 && len(b2c) > 0 && b1c[0] == b2c[0] {
b1c = b1c[1:]
b2c = b2c[1:]
}
if len(b1c) > 0 {
return b1c
}
return b2c
}