package varint import ( "bytes" "testing" ) // TestRoundTrip covers the boundary values where the terminal-byte encoding // changes width. func TestRoundTrip(t *testing.T) { vals := []uint64{0, 1, 126, 127, 128, 254, 255, 16383, 16384, 1 << 31, 1 << 62} for _, v := range vals { var buf bytes.Buffer Encode(&buf, v) got, err := Decode(&buf) if err != nil { t.Fatal(err) } if got != v { t.Fatalf("roundtrip: got %d want %d", got, v) } if buf.Len() != 0 { t.Fatalf("Decode left %d bytes", buf.Len()) } } } // TestStream decodes values written back to back: the reader must stop at the // terminal byte and leave the next value in place. func TestStream(t *testing.T) { var buf bytes.Buffer Encode(&buf, uint64(300)) Encode(&buf, uint64(7)) a, err := Decode(&buf) if err != nil || a != 300 { t.Fatal("first value") } b, err := Decode(&buf) if err != nil || b != 7 { t.Fatal("second value") } } func TestDecodeEmpty(t *testing.T) { var buf bytes.Buffer if _, err := Decode(&buf); err == nil { t.Fatal("Decode of an empty reader should fail") } }