Skip to content

Instantly share code, notes, and snippets.

@quux00
Created June 30, 2015 02:06
Show Gist options
  • Save quux00/6e62f44f19663ca29848 to your computer and use it in GitHub Desktop.
Save quux00/6e62f44f19663ca29848 to your computer and use it in GitHub Desktop.
Test all possible int64's for variable int encoding/decoding algorithm
type testrange struct {
start int64
end int64
}
func zigzagExhaustiveTest() {
runtime.GOMAXPROCS(8)
var wg sync.WaitGroup
fnRangeTester := func(fnum int, tr testrange, chfailures chan string) {
fmt.Printf("FN %d STARTED\n", fnum)
defer wg.Done()
buf := new(bytes.Buffer)
for i := tr.start; i != tr.end; i++ {
buf.Reset()
in := i
zzin := varint.ZigzagEncodeUInt64(in)
err := varint.VarintEncode(buf, zzin)
if err != nil {
chfailures <- fmt.Sprintf("Failed on %d: varintEncode err: %v", i, err)
}
zzout, err := varint.ReadVarIntToUint(buf)
if err != nil {
chfailures <- fmt.Sprintf("Failed on %d: ReadVarIntToUint err: %v", i, err)
}
if zzin != zzout {
chfailures <- fmt.Sprintf("Failed on %d: ReadVarIntToUint zzin != zzout: %d != %d", i, zzin, zzout)
}
out := varint.ZigzagDecodeInt64(zzout)
if in != out {
chfailures <- fmt.Sprintf("Failed on %d: ReadVarIntToUint in != out: %d != %d", i, in, out)
}
}
fmt.Printf("FN %d is DONE\n", fnum)
}
ranges := []testrange{
{-9223372036854775808, -6917529027641081856},
{-6917529027641081855, -4611686018427387904},
{-4611686018427387903, -2305843009213693952},
{-2305843009213693951, -1},
{0, 2305843009213693952},
{2305843009213693953, 4611686018427387904},
{4611686018427387905, 6917529027641081855},
{6917529027641081856, 9223372036854775807},
}
wg.Add(len(ranges))
failchan := make(chan string, 10)
for i, trange := range ranges {
go fnRangeTester(i, trange, failchan)
}
go func() {
wg.Wait()
close(failchan)
}()
// main thread monitors for failures, waiting for the failchan to be closed
for failmsg := range failchan {
fmt.Println(failmsg)
}
fmt.Println("DONE")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment