forked from jaekwon/go-wire
-
Notifications
You must be signed in to change notification settings - Fork 0
/
int_test.go
193 lines (179 loc) · 4.52 KB
/
int_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package wire
import (
"bytes"
"fmt"
"testing"
)
const arch64 = uint64(^uint(0)) == ^uint64(0)
func TestInt16(t *testing.T) {
buf := new(bytes.Buffer)
n, err := new(int), new(error)
WriteUint16(11, buf, n, err)
if *n != 2 {
t.Fatalf("Encoded uint16 expected 2 bytes got %d", *n)
}
}
// Returns true of overflow or underflow
func checkIntSpill(i int64) bool {
return i == (int64)((int)(i))
}
// Returns true of overflow or underflow
func checkUintSpill(i uint64) bool {
return i == (uint64)((uint)(i))
}
func TestVarint(t *testing.T) {
check := func(i64 int64, s string) {
i := int(i64)
// Test with WriteVarint
{
buf := new(bytes.Buffer)
n, err := new(int), new(error)
WriteVarint(i, buf, n, err)
bufBytes := buf.Bytes() // Read before consuming below.
i_ := ReadVarint(buf, n, err)
if i != i_ {
fmt.Println(bufBytes)
t.Fatalf("Encoded %v and got %v", i, i_)
}
if s != "" {
if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
t.Fatalf("Encoded %v, expected %v", bufHex, s)
}
}
}
// Test with PutVarint
{
buf := make([]byte, 100, 100)
n1, err := PutVarint(buf, i)
if err != nil {
t.Errorf("Unexpected error in PutVarint %v (encoding %v)", err.Error(), i)
}
if s != "" {
if n1 != len(s)/2 {
t.Errorf("Unexpected PutVarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
}
if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
}
}
i2, n2, err := GetVarint(buf)
if err != nil {
t.Errorf("Unexpected error in GetVarint %v (encoding %v)", err.Error(), i)
}
if s != "" {
if n2 != len(s)/2 {
t.Errorf("Unexpected GetVarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
}
} else {
if n1 != n2 {
t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
}
}
if i != i2 {
t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
}
}
}
// 123457 is some prime.
var i int64
for i = -(2 << 33); i < (2 << 33); i += 123457 {
if !arch64 && checkIntSpill(i) {
continue
}
check(i, "")
}
// Near zero
check(-1, "F101")
check(0, "00")
check(1, "0101")
// Positives
check(1<<31-1, "047FFFFFFF")
if arch64 {
check(1<<32-1, "04FFFFFFFF")
check(1<<32+0, "050100000000")
check(1<<32+1, "050100000001")
check(1<<53-1, "071FFFFFFFFFFFFF")
check(1<<63-1, "087FFFFFFFFFFFFFFF")
}
// Negatives
check(-1<<31+1, "F47FFFFFFF")
if arch64 {
check(-1<<32+1, "F4FFFFFFFF")
check(-1<<32-0, "F50100000000")
check(-1<<32-1, "F50100000001")
check(-1<<53+1, "F71FFFFFFFFFFFFF")
check(-1<<63, "F88000000000000000")
check(-1<<63+1, "F87FFFFFFFFFFFFFFF")
}
}
func TestUvarint(t *testing.T) {
check := func(i64 uint64, s string) {
i := uint(i64)
// Test with WriteUvarint
{
buf := new(bytes.Buffer)
n, err := new(int), new(error)
WriteUvarint(i, buf, n, err)
bufBytes := buf.Bytes()
i_ := ReadUvarint(buf, n, err)
if i != i_ {
fmt.Println(buf.Bytes())
t.Fatalf("Encoded %v and got %v", i, i_)
}
if s != "" {
if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
t.Fatalf("Encoded %v, expected %v", bufHex, s)
}
}
}
// Test with PutUvarint
{
buf := make([]byte, 100, 100)
n1, err := PutUvarint(buf, i)
if err != nil {
t.Errorf("Unexpected error in PutUvarint %v (encoding %v)", err.Error(), i)
}
if s != "" {
if n1 != len(s)/2 {
t.Errorf("Unexpected PutUvarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
}
if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
}
}
i2, n2, err := GetUvarint(buf)
if err != nil {
t.Errorf("Unexpected error in GetUvarint %v (encoding %v)", err.Error(), i)
}
if s != "" {
if n2 != len(s)/2 {
t.Errorf("Unexpected GetUvarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
}
} else {
if n1 != n2 {
t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
}
}
if i != i2 {
t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
}
}
}
// 123457 is some prime.
var i uint64
for i = 0; i < (2 << 33); i += 123457 {
if !arch64 && checkUintSpill(i) {
continue
}
check(i, "")
}
check(1, "0101")
check(1<<31-1, "047FFFFFFF")
check(1<<32-1, "04FFFFFFFF")
if arch64 {
check(1<<32-1, "04FFFFFFFF")
check(1<<32+0, "050100000000")
check(1<<32+1, "050100000001")
check(1<<53-1, "071FFFFFFFFFFFFF")
}
}