9 const arch64 = uint64(^uint(0)) == ^uint64(0)
11 // Returns true of overflow or underflow
12 func checkIntSpill(i int64) bool {
13 return i == (int64)((int)(i))
16 // Returns true of overflow or underflow
17 func checkUintSpill(i uint64) bool {
18 return i == (uint64)((uint)(i))
21 func TestVarint(t *testing.T) {
23 check := func(i64 int64, s string) {
25 // Test with WriteVarint
27 buf := new(bytes.Buffer)
28 n, err := new(int), new(error)
29 WriteVarint(i, buf, n, err)
30 bufBytes := buf.Bytes() // Read before consuming below.
31 i_ := ReadVarint(buf, n, err)
34 t.Fatalf("Encoded %v and got %v", i, i_)
37 if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
38 t.Fatalf("Encoded %v, expected %v", bufHex, s)
42 // Test with PutVarint
44 buf := make([]byte, 100, 100)
45 n1, err := PutVarint(buf, i)
47 t.Errorf("Unexpected error in PutVarint %v (encoding %v)", err.Error(), i)
51 t.Errorf("Unexpected PutVarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
53 if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
54 t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
57 i2, n2, err := GetVarint(buf)
59 t.Errorf("Unexpected error in GetVarint %v (encoding %v)", err.Error(), i)
63 t.Errorf("Unexpected GetVarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
67 t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
71 t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
76 // 123457 is some prime.
78 for i = -(2 << 33); i < (2 << 33); i += 123457 {
79 if !arch64 && checkIntSpill(i) {
90 check(1<<31-1, "047FFFFFFF")
92 check(1<<32-1, "04FFFFFFFF")
93 check(1<<32+0, "050100000000")
94 check(1<<32+1, "050100000001")
95 check(1<<53-1, "071FFFFFFFFFFFFF")
98 check(-1<<31+1, "F47FFFFFFF")
100 check(-1<<32+1, "F4FFFFFFFF")
101 check(-1<<32-0, "F50100000000")
102 check(-1<<32-1, "F50100000001")
103 check(-1<<53+1, "F71FFFFFFFFFFFFF")
107 func TestUvarint(t *testing.T) {
109 check := func(i64 uint64, s string) {
111 // Test with WriteUvarint
113 buf := new(bytes.Buffer)
114 n, err := new(int), new(error)
115 WriteUvarint(i, buf, n, err)
116 bufBytes := buf.Bytes()
117 i_ := ReadUvarint(buf, n, err)
119 fmt.Println(buf.Bytes())
120 t.Fatalf("Encoded %v and got %v", i, i_)
123 if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
124 t.Fatalf("Encoded %v, expected %v", bufHex, s)
128 // Test with PutUvarint
130 buf := make([]byte, 100, 100)
131 n1, err := PutUvarint(buf, i)
133 t.Errorf("Unexpected error in PutUvarint %v (encoding %v)", err.Error(), i)
137 t.Errorf("Unexpected PutUvarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
139 if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
140 t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
143 i2, n2, err := GetUvarint(buf)
145 t.Errorf("Unexpected error in GetUvarint %v (encoding %v)", err.Error(), i)
149 t.Errorf("Unexpected GetUvarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
153 t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
157 t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
162 // 123457 is some prime.
164 for i = 0; i < (2 << 33); i += 123457 {
165 if !arch64 && checkUintSpill(i) {
172 check(1<<31-1, "047FFFFFFF")
173 check(1<<32-1, "04FFFFFFFF")
175 check(1<<32-1, "04FFFFFFFF")
176 check(1<<32+0, "050100000000")
177 check(1<<32+1, "050100000001")
178 check(1<<53-1, "071FFFFFFFFFFFFF")