@@ -10,39 +10,39 @@ import (
10
10
var _ = Describe ("Varint encoding / decoding" , func () {
11
11
Context ("decoding" , func () {
12
12
It ("reads a 1 byte number" , func () {
13
- b := bytes .NewReader ([]byte {25 }) // 00011001
13
+ b := bytes .NewReader ([]byte {0b00011001 })
14
14
val , err := ReadVarInt (b )
15
15
Expect (err ).ToNot (HaveOccurred ())
16
16
Expect (val ).To (Equal (uint64 (25 )))
17
17
Expect (b .Len ()).To (BeZero ())
18
18
})
19
19
20
20
It ("reads a number that is encoded too long" , func () {
21
- b := bytes .NewReader ([]byte {0x40 , 0x25 }) // first byte: 01000000
21
+ b := bytes .NewReader ([]byte {0b01000000 , 0x25 })
22
22
val , err := ReadVarInt (b )
23
23
Expect (err ).ToNot (HaveOccurred ())
24
24
Expect (val ).To (Equal (uint64 (37 )))
25
25
Expect (b .Len ()).To (BeZero ())
26
26
})
27
27
28
28
It ("reads a 2 byte number" , func () {
29
- b := bytes .NewReader ([]byte {0x7b , 0xbd }) // first byte: 01111011
29
+ b := bytes .NewReader ([]byte {0b01111011 , 0xbd })
30
30
val , err := ReadVarInt (b )
31
31
Expect (err ).ToNot (HaveOccurred ())
32
32
Expect (val ).To (Equal (uint64 (15293 )))
33
33
Expect (b .Len ()).To (BeZero ())
34
34
})
35
35
36
36
It ("reads a 4 byte number" , func () {
37
- b := bytes .NewReader ([]byte {0x9d , 0x7f , 0x3e , 0x7d }) // first byte: 10011011
37
+ b := bytes .NewReader ([]byte {0b10011101 , 0x7f , 0x3e , 0x7d })
38
38
val , err := ReadVarInt (b )
39
39
Expect (err ).ToNot (HaveOccurred ())
40
40
Expect (val ).To (Equal (uint64 (494878333 )))
41
41
Expect (b .Len ()).To (BeZero ())
42
42
})
43
43
44
44
It ("reads an 8 byte number" , func () {
45
- b := bytes .NewReader ([]byte {0xc2 , 0x19 , 0x7c , 0x5e , 0xff , 0x14 , 0xe8 , 0x8c }) // first byte: 10000010
45
+ b := bytes .NewReader ([]byte {0b11000010 , 0x19 , 0x7c , 0x5e , 0xff , 0x14 , 0xe8 , 0x8c })
46
46
val , err := ReadVarInt (b )
47
47
Expect (err ).ToNot (HaveOccurred ())
48
48
Expect (val ).To (Equal (uint64 (151288809941952652 )))
@@ -51,81 +51,135 @@ var _ = Describe("Varint encoding / decoding", func() {
51
51
})
52
52
53
53
Context ("encoding" , func () {
54
- It ("writes a 1 byte number" , func () {
55
- b := & bytes.Buffer {}
56
- WriteVarInt (b , 37 )
57
- Expect (b .Bytes ()).To (Equal ([]byte {0x25 }))
58
- })
59
-
60
- It ("writes the maximum 1 byte number in 1 byte" , func () {
61
- b := & bytes.Buffer {}
62
- WriteVarInt (b , maxVarInt1 )
63
- Expect (b .Bytes ()).To (Equal ([]byte {0x3f /* 00111111 */ }))
64
- })
65
-
66
- It ("writes the minimum 2 byte number in 2 bytes" , func () {
67
- b := & bytes.Buffer {}
68
- WriteVarInt (b , maxVarInt1 + 1 )
69
- Expect (b .Bytes ()).To (Equal ([]byte {0x40 , maxVarInt1 + 1 }))
70
- })
71
-
72
- It ("writes a 2 byte number" , func () {
73
- b := & bytes.Buffer {}
74
- WriteVarInt (b , 15293 )
75
- Expect (b .Bytes ()).To (Equal ([]byte {0x7b , 0xbd }))
76
- })
77
-
78
- It ("writes the maximum 2 byte number in 2 bytes" , func () {
79
- b := & bytes.Buffer {}
80
- WriteVarInt (b , maxVarInt2 )
81
- Expect (b .Bytes ()).To (Equal ([]byte {0x7f /* 01111111 */ , 0xff }))
82
- })
83
-
84
- It ("writes the minimum 4 byte number in 4 bytes" , func () {
85
- b := & bytes.Buffer {}
86
- WriteVarInt (b , maxVarInt2 + 1 )
87
- Expect (b .Len ()).To (Equal (4 ))
88
- num , err := ReadVarInt (b )
89
- Expect (err ).ToNot (HaveOccurred ())
90
- Expect (num ).To (Equal (uint64 (maxVarInt2 + 1 )))
91
- })
92
-
93
- It ("writes a 4 byte number" , func () {
94
- b := & bytes.Buffer {}
95
- WriteVarInt (b , 494878333 )
96
- Expect (b .Bytes ()).To (Equal ([]byte {0x9d , 0x7f , 0x3e , 0x7d }))
97
- })
98
-
99
- It ("writes the maximum 4 byte number in 4 bytes" , func () {
100
- b := & bytes.Buffer {}
101
- WriteVarInt (b , maxVarInt4 )
102
- Expect (b .Bytes ()).To (Equal ([]byte {0xbf /* 10111111 */ , 0xff , 0xff , 0xff }))
103
- })
104
-
105
- It ("writes the minimum 8 byte number in 8 bytes" , func () {
106
- b := & bytes.Buffer {}
107
- WriteVarInt (b , maxVarInt4 + 1 )
108
- Expect (b .Len ()).To (Equal (8 ))
109
- num , err := ReadVarInt (b )
110
- Expect (err ).ToNot (HaveOccurred ())
111
- Expect (num ).To (Equal (uint64 (maxVarInt4 + 1 )))
112
- })
113
-
114
- It ("writes an 8 byte number" , func () {
115
- b := & bytes.Buffer {}
116
- WriteVarInt (b , 151288809941952652 )
117
- Expect (b .Bytes ()).To (Equal ([]byte {0xc2 , 0x19 , 0x7c , 0x5e , 0xff , 0x14 , 0xe8 , 0x8c }))
118
- })
119
-
120
- It ("writes the maximum 8 byte number in 8 bytes" , func () {
121
- b := & bytes.Buffer {}
122
- WriteVarInt (b , maxVarInt8 )
123
- Expect (b .Bytes ()).To (Equal ([]byte {0xff /* 11111111 */ , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff }))
124
- })
125
-
126
- It ("panics when given a too large number (> 62 bit)" , func () {
127
- b := & bytes.Buffer {}
128
- Expect (func () { WriteVarInt (b , maxVarInt8 + 1 ) }).Should (Panic ())
54
+ Context ("with minimal length" , func () {
55
+ It ("writes a 1 byte number" , func () {
56
+ b := & bytes.Buffer {}
57
+ WriteVarInt (b , 37 )
58
+ Expect (b .Bytes ()).To (Equal ([]byte {0x25 }))
59
+ })
60
+
61
+ It ("writes the maximum 1 byte number in 1 byte" , func () {
62
+ b := & bytes.Buffer {}
63
+ WriteVarInt (b , maxVarInt1 )
64
+ Expect (b .Bytes ()).To (Equal ([]byte {0b00111111 }))
65
+ })
66
+
67
+ It ("writes the minimum 2 byte number in 2 bytes" , func () {
68
+ b := & bytes.Buffer {}
69
+ WriteVarInt (b , maxVarInt1 + 1 )
70
+ Expect (b .Bytes ()).To (Equal ([]byte {0x40 , maxVarInt1 + 1 }))
71
+ })
72
+
73
+ It ("writes a 2 byte number" , func () {
74
+ b := & bytes.Buffer {}
75
+ WriteVarInt (b , 15293 )
76
+ Expect (b .Bytes ()).To (Equal ([]byte {0b01000000 ^ 0x3b , 0xbd }))
77
+ })
78
+
79
+ It ("writes the maximum 2 byte number in 2 bytes" , func () {
80
+ b := & bytes.Buffer {}
81
+ WriteVarInt (b , maxVarInt2 )
82
+ Expect (b .Bytes ()).To (Equal ([]byte {0b01111111 , 0xff }))
83
+ })
84
+
85
+ It ("writes the minimum 4 byte number in 4 bytes" , func () {
86
+ b := & bytes.Buffer {}
87
+ WriteVarInt (b , maxVarInt2 + 1 )
88
+ Expect (b .Len ()).To (Equal (4 ))
89
+ num , err := ReadVarInt (b )
90
+ Expect (err ).ToNot (HaveOccurred ())
91
+ Expect (num ).To (Equal (uint64 (maxVarInt2 + 1 )))
92
+ })
93
+
94
+ It ("writes a 4 byte number" , func () {
95
+ b := & bytes.Buffer {}
96
+ WriteVarInt (b , 494878333 )
97
+ Expect (b .Bytes ()).To (Equal ([]byte {0b10000000 ^ 0x1d , 0x7f , 0x3e , 0x7d }))
98
+ })
99
+
100
+ It ("writes the maximum 4 byte number in 4 bytes" , func () {
101
+ b := & bytes.Buffer {}
102
+ WriteVarInt (b , maxVarInt4 )
103
+ Expect (b .Bytes ()).To (Equal ([]byte {0b10111111 , 0xff , 0xff , 0xff }))
104
+ })
105
+
106
+ It ("writes the minimum 8 byte number in 8 bytes" , func () {
107
+ b := & bytes.Buffer {}
108
+ WriteVarInt (b , maxVarInt4 + 1 )
109
+ Expect (b .Len ()).To (Equal (8 ))
110
+ num , err := ReadVarInt (b )
111
+ Expect (err ).ToNot (HaveOccurred ())
112
+ Expect (num ).To (Equal (uint64 (maxVarInt4 + 1 )))
113
+ })
114
+
115
+ It ("writes an 8 byte number" , func () {
116
+ b := & bytes.Buffer {}
117
+ WriteVarInt (b , 151288809941952652 )
118
+ Expect (b .Bytes ()).To (Equal ([]byte {0xc2 , 0x19 , 0x7c , 0x5e , 0xff , 0x14 , 0xe8 , 0x8c }))
119
+ })
120
+
121
+ It ("writes the maximum 8 byte number in 8 bytes" , func () {
122
+ b := & bytes.Buffer {}
123
+ WriteVarInt (b , maxVarInt8 )
124
+ Expect (b .Bytes ()).To (Equal ([]byte {0xff /* 11111111 */ , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff , 0xff }))
125
+ })
126
+
127
+ It ("panics when given a too large number (> 62 bit)" , func () {
128
+ Expect (func () { WriteVarInt (& bytes.Buffer {}, maxVarInt8 + 1 ) }).Should (Panic ())
129
+ })
130
+ })
131
+
132
+ Context ("with fixed length" , func () {
133
+ It ("panics when given an invalid length" , func () {
134
+ Expect (func () { WriteVarIntWithLen (& bytes.Buffer {}, 25 , 3 ) }).Should (Panic ())
135
+ })
136
+
137
+ It ("panics when given a too short length" , func () {
138
+ Expect (func () { WriteVarIntWithLen (& bytes.Buffer {}, maxVarInt1 + 1 , 1 ) }).Should (Panic ())
139
+ Expect (func () { WriteVarIntWithLen (& bytes.Buffer {}, maxVarInt2 + 1 , 2 ) }).Should (Panic ())
140
+ Expect (func () { WriteVarIntWithLen (& bytes.Buffer {}, maxVarInt4 + 1 , 4 ) }).Should (Panic ())
141
+ })
142
+
143
+ It ("writes a 1-byte number in minimal encoding" , func () {
144
+ b := & bytes.Buffer {}
145
+ WriteVarIntWithLen (b , 37 , 1 )
146
+ Expect (b .Bytes ()).To (Equal ([]byte {0x25 }))
147
+ })
148
+
149
+ It ("writes a 1-byte number in 2 bytes" , func () {
150
+ b := & bytes.Buffer {}
151
+ WriteVarIntWithLen (b , 37 , 2 )
152
+ Expect (b .Bytes ()).To (Equal ([]byte {0b01000000 , 0x25 }))
153
+ Expect (ReadVarInt (b )).To (BeEquivalentTo (37 ))
154
+ })
155
+
156
+ It ("writes a 1-byte number in 4 bytes" , func () {
157
+ b := & bytes.Buffer {}
158
+ WriteVarIntWithLen (b , 37 , 4 )
159
+ Expect (b .Bytes ()).To (Equal ([]byte {0b10000000 , 0 , 0 , 0x25 }))
160
+ Expect (ReadVarInt (b )).To (BeEquivalentTo (37 ))
161
+ })
162
+
163
+ It ("writes a 1-byte number in 8 bytes" , func () {
164
+ b := & bytes.Buffer {}
165
+ WriteVarIntWithLen (b , 37 , 8 )
166
+ Expect (b .Bytes ()).To (Equal ([]byte {0b11000000 , 0 , 0 , 0 , 0 , 0 , 0 , 0x25 }))
167
+ Expect (ReadVarInt (b )).To (BeEquivalentTo (37 ))
168
+ })
169
+
170
+ It ("writes a 2-byte number in 4 bytes" , func () {
171
+ b := & bytes.Buffer {}
172
+ WriteVarIntWithLen (b , 15293 , 4 )
173
+ Expect (b .Bytes ()).To (Equal ([]byte {0b10000000 , 0 , 0x3b , 0xbd }))
174
+ Expect (ReadVarInt (b )).To (BeEquivalentTo (15293 ))
175
+ })
176
+
177
+ It ("write a 4-byte number in 8 bytes" , func () {
178
+ b := & bytes.Buffer {}
179
+ WriteVarIntWithLen (b , 494878333 , 8 )
180
+ Expect (b .Bytes ()).To (Equal ([]byte {0b11000000 , 0 , 0 , 0 , 0x1d , 0x7f , 0x3e , 0x7d }))
181
+ Expect (ReadVarInt (b )).To (BeEquivalentTo (494878333 ))
182
+ })
129
183
})
130
184
})
131
185
0 commit comments