OSDN Git Service

Hulk did something
[bytom/vapor.git] / vendor / github.com / tendermint / go-wire / int_test.go
1 package wire
2
3 import (
4         "bytes"
5         "fmt"
6         "testing"
7 )
8
9 const arch64 = uint64(^uint(0)) == ^uint64(0)
10
11 // Returns true of overflow or underflow
12 func checkIntSpill(i int64) bool {
13         return i == (int64)((int)(i))
14 }
15
16 // Returns true of overflow or underflow
17 func checkUintSpill(i uint64) bool {
18         return i == (uint64)((uint)(i))
19 }
20
21 func TestVarint(t *testing.T) {
22
23         check := func(i64 int64, s string) {
24                 i := int(i64)
25                 // Test with WriteVarint
26                 {
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)
32                         if i != i_ {
33                                 fmt.Println(bufBytes)
34                                 t.Fatalf("Encoded %v and got %v", i, i_)
35                         }
36                         if s != "" {
37                                 if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
38                                         t.Fatalf("Encoded %v, expected %v", bufHex, s)
39                                 }
40                         }
41                 }
42                 // Test with PutVarint
43                 {
44                         buf := make([]byte, 100, 100)
45                         n1, err := PutVarint(buf, i)
46                         if err != nil {
47                                 t.Errorf("Unexpected error in PutVarint %v (encoding %v)", err.Error(), i)
48                         }
49                         if s != "" {
50                                 if n1 != len(s)/2 {
51                                         t.Errorf("Unexpected PutVarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
52                                 }
53                                 if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
54                                         t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
55                                 }
56                         }
57                         i2, n2, err := GetVarint(buf)
58                         if err != nil {
59                                 t.Errorf("Unexpected error in GetVarint %v (encoding %v)", err.Error(), i)
60                         }
61                         if s != "" {
62                                 if n2 != len(s)/2 {
63                                         t.Errorf("Unexpected GetVarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
64                                 }
65                         } else {
66                                 if n1 != n2 {
67                                         t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
68                                 }
69                         }
70                         if i != i2 {
71                                 t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
72                         }
73                 }
74         }
75
76         // 123457 is some prime.
77         var i int64
78         for i = -(2 << 33); i < (2 << 33); i += 123457 {
79                 if !arch64 && checkIntSpill(i) {
80                         continue
81                 }
82                 check(i, "")
83         }
84
85         // Near zero
86         check(-1, "F101")
87         check(0, "00")
88         check(1, "0101")
89         // Positives
90         check(1<<31-1, "047FFFFFFF")
91         if arch64 {
92                 check(1<<32-1, "04FFFFFFFF")
93                 check(1<<32+0, "050100000000")
94                 check(1<<32+1, "050100000001")
95                 check(1<<53-1, "071FFFFFFFFFFFFF")
96         }
97         // Negatives
98         check(-1<<31+1, "F47FFFFFFF")
99         if arch64 {
100                 check(-1<<32+1, "F4FFFFFFFF")
101                 check(-1<<32-0, "F50100000000")
102                 check(-1<<32-1, "F50100000001")
103                 check(-1<<53+1, "F71FFFFFFFFFFFFF")
104         }
105 }
106
107 func TestUvarint(t *testing.T) {
108
109         check := func(i64 uint64, s string) {
110                 i := uint(i64)
111                 // Test with WriteUvarint
112                 {
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)
118                         if i != i_ {
119                                 fmt.Println(buf.Bytes())
120                                 t.Fatalf("Encoded %v and got %v", i, i_)
121                         }
122                         if s != "" {
123                                 if bufHex := fmt.Sprintf("%X", bufBytes); bufHex != s {
124                                         t.Fatalf("Encoded %v, expected %v", bufHex, s)
125                                 }
126                         }
127                 }
128                 // Test with PutUvarint
129                 {
130                         buf := make([]byte, 100, 100)
131                         n1, err := PutUvarint(buf, i)
132                         if err != nil {
133                                 t.Errorf("Unexpected error in PutUvarint %v (encoding %v)", err.Error(), i)
134                         }
135                         if s != "" {
136                                 if n1 != len(s)/2 {
137                                         t.Errorf("Unexpected PutUvarint length %v, expected %v (encoding %v)", n1, len(s)/2, i)
138                                 }
139                                 if bufHex := fmt.Sprintf("%X", buf[:n1]); bufHex != s {
140                                         t.Errorf("Got %v, expected %v (encoding %v)", bufHex, s, i)
141                                 }
142                         }
143                         i2, n2, err := GetUvarint(buf)
144                         if err != nil {
145                                 t.Errorf("Unexpected error in GetUvarint %v (encoding %v)", err.Error(), i)
146                         }
147                         if s != "" {
148                                 if n2 != len(s)/2 {
149                                         t.Errorf("Unexpected GetUvarint length %v, expected %v (encoding %v)", n2, len(s)/2, i)
150                                 }
151                         } else {
152                                 if n1 != n2 {
153                                         t.Errorf("Unmatched Put/Get lengths. %v vs %v (encoding %v)", n1, n2, i)
154                                 }
155                         }
156                         if i != i2 {
157                                 t.Errorf("Put/Get expected %v, got %v (encoding %v)", i, i2, i)
158                         }
159                 }
160         }
161
162         // 123457 is some prime.
163         var i uint64
164         for i = 0; i < (2 << 33); i += 123457 {
165                 if !arch64 && checkUintSpill(i) {
166                         continue
167                 }
168                 check(i, "")
169         }
170
171         check(1, "0101")
172         check(1<<31-1, "047FFFFFFF")
173         check(1<<32-1, "04FFFFFFFF")
174         if arch64 {
175                 check(1<<32-1, "04FFFFFFFF")
176                 check(1<<32+0, "050100000000")
177                 check(1<<32+1, "050100000001")
178                 check(1<<53-1, "071FFFFFFFFFFFFF")
179         }
180
181 }