-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreference_test.go
More file actions
92 lines (82 loc) · 1.85 KB
/
reference_test.go
File metadata and controls
92 lines (82 loc) · 1.85 KB
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
package pdf
import (
"reflect"
"testing"
)
func TestBytesToInt(t *testing.T) {
type test struct {
b []byte
v uint
}
tests := []test{
test{[]byte{0x0, 0x0, 0x0}, 0},
test{[]byte{0x0, 0x0, 0x0, 0x01}, 1},
test{[]byte{0x0, 0x0, 0x01, 0x0}, 256},
test{[]byte{0x0, 0x01, 0x0, 0x0}, 65536},
test{[]byte{0x0, 0x01, 0x01, 0x0}, 65792},
}
for i, test := range tests {
result := bytesToInt(test.b)
if result != test.v {
t.Errorf("%d: expected %v for %#v, got %v", i, test.v, test.b, result)
}
}
}
func TestNBytesForInt(t *testing.T) {
type test struct {
b []byte
v int
}
tests := []test{
test{[]byte{0x0}, 0},
test{[]byte{0x01}, 1},
test{[]byte{0x01, 0x0}, 256},
test{[]byte{0x01, 0x0, 0x0}, 65536},
test{[]byte{0x01, 0x01, 0x0}, 65792},
}
for i, test := range tests {
result := nBytesForInt(test.v)
if result != len(test.b) {
t.Errorf("%d: expected %v for %#v, got %v", i, len(test.b), test.v, result)
}
}
}
func TestIntToBytes(t *testing.T) {
type test struct {
b []byte
v uint
}
tests := []test{
test{[]byte{0x0}, 0},
test{[]byte{0x01}, 1},
test{[]byte{0x01, 0x0}, 256},
test{[]byte{0x01, 0x0, 0x0}, 65536},
test{[]byte{0x01, 0x01, 0x0}, 65792},
}
for i, test := range tests {
result := intToBytes(test.v, len(test.b))
if !reflect.DeepEqual(result, test.b) {
t.Errorf("%d: expected %v for %#v, got %v", i, test.b, test.v, result)
}
}
}
func TestIntToBytesAndBack(t *testing.T) {
type test struct {
b []byte
v uint
}
tests := []test{
test{[]byte{0x0}, 0},
test{[]byte{0x01}, 1},
test{[]byte{0x01, 0x0}, 256},
test{[]byte{0x01, 0x0, 0x0}, 65536},
test{[]byte{0x01, 0x01, 0x0}, 65792},
}
for i, test := range tests {
asBytes := intToBytes(test.v, 8)
asInt := bytesToInt(asBytes)
if asInt != test.v {
t.Errorf("%d: expected %v for %#v, got %v", i, test.v, test.b, asInt)
}
}
}