@@ -15,6 +15,16 @@ func checkVarint(t *testing.T, x uint64) {
15
15
if size != expected {
16
16
t .Fatalf ("expected varintsize of %d to be %d, got %d" , x , expected , size )
17
17
}
18
+ xi , n , err := FromUvarint (buf )
19
+ if err != nil {
20
+ t .Fatal ("decoding error" , err )
21
+ }
22
+ if n != size {
23
+ t .Fatal ("read the wrong size" )
24
+ }
25
+ if xi != x {
26
+ t .Fatal ("expected a different result" )
27
+ }
18
28
}
19
29
20
30
func TestVarintSize (t * testing.T ) {
@@ -44,7 +54,8 @@ func TestOverflow(t *testing.T) {
44
54
}
45
55
46
56
func TestNotMinimal (t * testing.T ) {
47
- i , n , err := FromUvarint ([]byte {0x80 , 0x01 })
57
+ varint := []byte {0x81 , 0x00 }
58
+ i , n , err := FromUvarint (varint )
48
59
if err != ErrNotMinimal {
49
60
t .Error ("expected an error" )
50
61
}
@@ -54,6 +65,29 @@ func TestNotMinimal(t *testing.T) {
54
65
if i != 0 {
55
66
t .Error ("expected i = 0" )
56
67
}
68
+ i , n = binary .Uvarint (varint )
69
+ if n != len (varint ) {
70
+ t .Error ("expected to read entire buffer" )
71
+ }
72
+ if i != 1 {
73
+ t .Error ("expected varint 1" )
74
+ }
75
+ }
76
+
77
+ func TestNotMinimalRead (t * testing.T ) {
78
+ varint := bytes .NewBuffer ([]byte {0x81 , 0x00 })
79
+ i , err := ReadUvarint (varint )
80
+ if err != ErrNotMinimal {
81
+ t .Error ("expected an error" )
82
+ }
83
+ if i != 0 {
84
+ t .Error ("expected i = 0" )
85
+ }
86
+ varint = bytes .NewBuffer ([]byte {0x81 , 0x00 })
87
+ i , err = binary .ReadUvarint (varint )
88
+ if i != 1 {
89
+ t .Error ("expected varint 1" )
90
+ }
57
91
}
58
92
59
93
func TestUnderflow (t * testing.T ) {
0 commit comments