-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathencoder_test.go
162 lines (152 loc) · 4.97 KB
/
encoder_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package bencode_test
import (
"bytes"
"fmt"
"math"
"runtime"
"strings"
"testing"
"github.com/stefanovazzocell/bencode"
)
// Testing helper
func GenericEncoderTester(t *testing.T, v interface{}, expected string) {
t.Logf("Test case (%T): %v", v, v)
// Fetch encoder
encoder, err := bencode.NewEncoderFromInterface(v)
if err != nil {
t.Fatalf("NewEncoderFromInterface returned error: %v", err)
}
// Retrieve result
buf := bytes.Buffer{}
if n, err := encoder.WriteTo(&buf); err != nil {
t.Fatalf("Failed to write to a buffer: %v", err)
} else if n != int64(buf.Len()) {
t.Fatalf("Wrote %d, but only has %d Len()", n, buf.Len())
}
actualReader := buf.Bytes()
actualStr := encoder.String()
actualBytes := encoder.Bytes()
if !bytes.Equal([]byte(actualStr), actualBytes) {
t.Fatalf("Return values don't match between bytes %q (%x) and string %q (%s)", actualBytes, actualBytes, actualStr, actualStr)
}
if !bytes.Equal(actualReader, actualBytes) {
t.Fatalf("Return values don't match between the reader output %q (%x) and bytes %q (%s)", actualReader, actualReader, actualBytes, actualBytes)
}
// Compare result
if expected != actualStr {
t.Fatalf("Expected %q (%x) doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
func TestInvalidEncoding(t *testing.T) {
for _, invalid := range invalidTestCases {
t.Logf("Testing with type %T: %v", invalid, invalid)
if encoder, err := bencode.NewEncoderFromInterface(invalid); err == nil {
t.Fatalf("Encoded invalid type without error as %v", encoder)
}
}
}
func TestStringEncoding(t *testing.T) {
for _, str := range stringsTestCases {
// Generate expected
expected := fmt.Sprintf("%d:%s", len(str), str)
// Run Test
GenericEncoderTester(t, str, expected)
// Check string-specific encoding
encoder := bencode.NewEncoderFromString(str)
actualStr := encoder.String()
if actualStr != expected {
t.Fatalf("Expected %q (%x) from string-specific encoder doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
}
func TestIntEncoding(t *testing.T) {
for i, expected := range int64TestCases {
t.Logf("Testing %d with expected value %q", i, expected)
// Run Tests
if strings.Contains(runtime.GOARCH, "64") {
// Poor man's way to check if it's 64bit
GenericEncoderTester(t, int(i), expected)
}
GenericEncoderTester(t, int64(i), expected)
if math.MinInt32 <= i && i <= math.MaxInt32 {
GenericEncoderTester(t, int32(i), expected)
}
if math.MinInt16 <= i && i <= math.MaxInt16 {
GenericEncoderTester(t, int16(i), expected)
}
if math.MinInt8 <= i && i <= math.MaxInt8 {
GenericEncoderTester(t, int8(i), expected)
}
// Check string-specific encoding
encoder := bencode.NewEncoderFromInt(i)
actualStr := encoder.String()
if actualStr != expected {
t.Fatalf("Expected %q (%x) from int-specific encoder doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
}
func TestUintEncoding(t *testing.T) {
for i, expected := range uintsTestCases {
t.Logf("Testing %d with expected value %q", i, expected)
// Run Tests
if math.MaxInt64 == (1<<63 - 1) {
// Poor man's way to check if it's 64bit
GenericEncoderTester(t, uint(i), expected)
}
GenericEncoderTester(t, uint64(i), expected)
if i <= math.MaxUint32 {
GenericEncoderTester(t, uint32(i), expected)
}
if i <= math.MaxUint16 {
GenericEncoderTester(t, uint16(i), expected)
}
if i <= math.MaxUint8 {
GenericEncoderTester(t, uint8(i), expected)
}
// Check string-specific encoding
encoder := bencode.NewEncoderFromUint(i)
actualStr := encoder.String()
if actualStr != expected {
t.Fatalf("Expected %q (%x) from uint-specific encoder doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
}
func TestSliceEncoding(t *testing.T) {
for expected, list := range slicesTestCases {
// Run Test
GenericEncoderTester(t, list, expected)
// Check string-specific encoding
encoder, err := bencode.NewEncoderFromSlice(list)
if err != nil {
t.Fatalf("Got unexpected error from slice-specific encoder: %v", err)
}
actualStr := encoder.String()
if actualStr != expected {
t.Fatalf("Expected %q (%x) from string-specific encoder doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
}
func TestMapEncoding(t *testing.T) {
for expected, m := range mapTestCases {
// Run Test
GenericEncoderTester(t, m, expected)
// Check string-specific encoding
encoder, err := bencode.NewEncoderFromMap(m)
if err != nil {
t.Fatalf("Got unexpected error from slice-specific encoder: %v", err)
}
actualStr := encoder.String()
if actualStr != expected {
t.Fatalf("Expected %q (%x) from string-specific encoder doesn't match actual %q (%x)", expected, expected, actualStr, actualStr)
}
}
}
func BenchmarkEncoder(b *testing.B) {
for benchName, testInterface := range encoderBenchmarks {
b.Run(benchName, func(b *testing.B) {
for i := 0; i < b.N; i++ {
bencode.NewEncoderFromInterface(testInterface)
}
})
}
}