varint_test.go raw

   1  package varint
   2  
   3  import (
   4  	"bytes"
   5  	"math"
   6  	"testing"
   7  
   8  	"next.orly.dev/pkg/lol/chk"
   9  	"lukechampine.com/frand"
  10  )
  11  
  12  func TestEncode_Decode(t *testing.T) {
  13  	var v uint64
  14  	for range 10000000 {
  15  		v = uint64(frand.Intn(math.MaxInt64))
  16  		buf1 := new(bytes.Buffer)
  17  		Encode(buf1, v)
  18  		buf2 := bytes.NewBuffer(buf1.Bytes())
  19  		u, err := Decode(buf2)
  20  		if chk.E(err) {
  21  			t.Fatal(err)
  22  		}
  23  		if u != v {
  24  			t.Fatalf("expected %d got %d", v, u)
  25  		}
  26  
  27  	}
  28  }
  29