reversion_test.go 940 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package storage
  2. import (
  3. "bytes"
  4. "math"
  5. "reflect"
  6. "testing"
  7. )
  8. // TestReversion tests that reversion could be encoded to and decoded from
  9. // bytes slice. Moreover, the lexicograph order of its byte slice representation
  10. // follows the order of (main, sub).
  11. func TestReversion(t *testing.T) {
  12. tests := []reversion{
  13. // order in (main, sub)
  14. reversion{},
  15. reversion{main: 1, sub: 0},
  16. reversion{main: 1, sub: 1},
  17. reversion{main: 2, sub: 0},
  18. reversion{main: math.MaxInt64, sub: math.MaxInt64},
  19. }
  20. bs := make([][]byte, len(tests))
  21. for i, tt := range tests {
  22. b := newRevBytes()
  23. revToBytes(tt, b)
  24. bs[i] = b
  25. if grev := bytesToRev(b); !reflect.DeepEqual(grev, tt) {
  26. t.Errorf("#%d: reversion = %+v, want %+v", i, grev, tt)
  27. }
  28. }
  29. for i := 0; i < len(tests)-1; i++ {
  30. if bytes.Compare(bs[i], bs[i+1]) >= 0 {
  31. t.Errorf("#%d: %v (%+v) should be smaller than %v (%+v)", i, bs[i], tests[i], bs[i+1], tests[i+1])
  32. }
  33. }
  34. }