forked from dhowden/tag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
120 lines (111 loc) · 1.87 KB
/
util_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
// Copyright 2015, David Howden
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package tag
import (
"bytes"
"testing"
)
func TestGetBit(t *testing.T) {
for i := uint(0); i < 8; i++ {
b := byte(1 << i)
got := getBit(b, i)
if !got {
t.Errorf("getBit(%v, %v) = %v, expected %v", b, i, got, true)
}
}
}
func TestGet7BitChunkedInt(t *testing.T) {
tests := []struct {
input []byte
output int
}{
{
[]byte{},
0,
},
{
[]byte{0x01},
1,
},
{
[]byte{0x7F, 0x7F},
0x3FFF,
},
}
for ii, tt := range tests {
got := get7BitChunkedInt(tt.input)
if got != tt.output {
t.Errorf("[%d] get7BitChunkedInt(%v) = %v, expected %v", ii, tt.input, got, tt.output)
}
}
}
func TestGetInt(t *testing.T) {
tests := []struct {
input []byte
output int
}{
{
[]byte{},
0,
},
{
[]byte{0x01},
1,
},
{
[]byte{0xF1, 0xF2},
0xF1F2,
},
{
[]byte{0xF1, 0xF2, 0xF3},
0xF1F2F3,
},
{
[]byte{0xF1, 0xF2, 0xF3, 0xF4},
0xF1F2F3F4,
},
}
for ii, tt := range tests {
got := getInt(tt.input)
if got != tt.output {
t.Errorf("[%d] getInt(%v) = %v, expected %v", ii, tt.input, got, tt.output)
}
}
}
func TestGetUintLittleEndian(t *testing.T) {
tests := []struct {
input []byte
output uint64
}{
{
[]byte{0, 0, 0, 0, 0, 0, 0, 0},
0,
},
{
[]byte{0x01, 0, 0, 0, 0, 0, 0, 0},
1,
},
{
[]byte{0xF1, 0xF2, 0, 0, 0, 0, 0, 0},
0xF2F1,
},
{
[]byte{0xF1, 0xF2, 0xF3, 0, 0, 0, 0, 0},
0xF3F2F1,
},
{
[]byte{0xF1, 0xF2, 0xF3, 0xF4, 0, 0, 0, 0},
0xF4F3F2F1,
},
}
for ii, tt := range tests {
got, err := readUint64LittleEndian(bytes.NewReader(tt.input))
if err != nil {
t.Errorf("unexpected error: %v", err)
}
if got != tt.output {
t.Errorf("[%d] getInt(%v) = %v, expected %v", ii, tt.input, got, tt.output)
}
}
}