-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbigendianreader_test.go
102 lines (93 loc) · 2.62 KB
/
bigendianreader_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
package byteio
import (
"strings"
"testing"
)
func TestReadBigEndian(t *testing.T) {
const testData = "\x0a\x0b\x0c\x0d\x0c\x0d\x01\x02\x03\x04\x05\x06\x07\x08A"
r := BigEndianReader{Reader: strings.NewReader(testData)}
if i, n, err := r.ReadUint32(); err != nil {
t.Errorf("test 1: unexpected error: %s", err)
} else if n != 4 {
t.Errorf("test 1: expecting to read 4 bytes, read %d", n)
} else if i != 168496141 {
t.Errorf("test 1: expecting 168496141, got %d", i)
}
if i, n, err := r.ReadUint16(); err != nil {
t.Errorf("test 2: unexpected error: %s", err)
} else if n != 2 {
t.Errorf("test 2: expecting to read 2 bytes, read %d", n)
} else if i != 3085 {
t.Errorf("test 2: expecting 3085, got %d", i)
}
if i, n, err := r.ReadUint64(); err != nil {
t.Errorf("test 3: unexpected error: %s", err)
} else if n != 8 {
t.Errorf("test 3: expecting to read 8 bytes, read %d", n)
} else if i != 72623859790382856 {
t.Errorf("test 3: expecting 72623859790382856 got %d", i)
}
if i, n, err := r.ReadUint8(); err != nil {
t.Errorf("test 4: unexpected error: %s", err)
} else if n != 1 {
t.Errorf("test 4: expecting to read 1 byte, read %d", n)
} else if i != 65 {
t.Errorf("test 4: expecting 65, got %d", i)
}
}
func BenchmarkReadBigEndianUint8(b *testing.B) {
r := BigEndianReader{Reader: neverending{1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadUint8()
}
}
func BenchmarkReadBigEndianInt8(b *testing.B) {
r := BigEndianReader{Reader: neverending{1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadInt8()
}
}
func BenchmarkReadBigEndianUint16(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadUint16()
}
}
func BenchmarkReadBigEndianInt16(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadInt16()
}
}
func BenchmarkReadBigEndianUint32(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1, 1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadUint32()
}
}
func BenchmarkReadBigEndianInt32(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1, 1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadInt32()
}
}
func BenchmarkReadBigEndianUint64(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1, 1, 1, 1, 1, 1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadUint64()
}
}
func BenchmarkReadBigEndianInt64(b *testing.B) {
r := BigEndianReader{Reader: neverending{1, 1, 1, 1, 1, 1, 1, 1}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _, _ = r.ReadInt64()
}
}