Skip to content

Commit

Permalink
Variable length encoding: Test encoding size
Browse files Browse the repository at this point in the history
  • Loading branch information
Ngo-The-Trung authored and petertseng committed Oct 2, 2016
1 parent ae98780 commit a5d91a0
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions exercises/variable-length-quantity/variable_length_quantity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@ func TestEncodeDecodeVarint(t *testing.T) {
testCases := []struct {
input []byte
output uint32
size int
}{
0: {[]byte{0x7F}, 127},
1: {[]byte{0x81, 0x00}, 128},
2: {[]byte{0xC0, 0x00}, 8192},
3: {[]byte{0xFF, 0x7F}, 16383},
4: {[]byte{0x81, 0x80, 0x00}, 16384},
5: {[]byte{0xFF, 0xFF, 0x7F}, 2097151},
6: {[]byte{0x81, 0x80, 0x80, 0x00}, 2097152},
7: {[]byte{0xC0, 0x80, 0x80, 0x00}, 134217728},
8: {[]byte{0xFF, 0xFF, 0xFF, 0x7F}, 268435455},
0: {[]byte{0x7F}, 127, 1},
1: {[]byte{0x81, 0x00}, 128, 1},
2: {[]byte{0xC0, 0x00}, 8192, 1},
3: {[]byte{0xFF, 0x7F}, 16383, 1},
4: {[]byte{0x81, 0x80, 0x00}, 16384, 2},
5: {[]byte{0xFF, 0xFF, 0x7F}, 2097151, 2},
6: {[]byte{0x81, 0x80, 0x80, 0x00}, 2097152, 3},
7: {[]byte{0xC0, 0x80, 0x80, 0x00}, 134217728, 3},
8: {[]byte{0xFF, 0xFF, 0xFF, 0x7F}, 268435455, 3},

9: {[]byte{0x82, 0x00}, 256},
10: {[]byte{0x81, 0x10}, 144},
9: {[]byte{0x82, 0x00}, 256, 1},
10: {[]byte{0x81, 0x10}, 144, 1},
}

for i, tc := range testCases {
t.Logf("test case %d - %#v\n", i, tc.input)
if o, _ := DecodeVarint(tc.input); o != tc.output {
o, size := DecodeVarint(tc.input)
if o != tc.output {
t.Fatalf("expected %d\ngot\n%d\n", tc.output, o)
}
if size != tc.size {
t.Fatalf("expected encoding size of %d bytes\ngot %d bytes\n", tc.size, size)
}
if encoded := EncodeVarint(tc.output); bytes.Compare(encoded, tc.input) != 0 {
t.Fatalf("%d - expected %#v\ngot\n%#v\n", tc.output, tc.input, encoded)
}
Expand Down

0 comments on commit a5d91a0

Please sign in to comment.