revision_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. // Copyright 2015 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package mvcc
  15. import (
  16. "bytes"
  17. "math"
  18. "reflect"
  19. "testing"
  20. )
  21. // TestRevision tests that revision could be encoded to and decoded from
  22. // bytes slice. Moreover, the lexicographical order of its byte slice representation
  23. // follows the order of (main, sub).
  24. func TestRevision(t *testing.T) {
  25. tests := []revision{
  26. // order in (main, sub)
  27. {},
  28. {main: 1, sub: 0},
  29. {main: 1, sub: 1},
  30. {main: 2, sub: 0},
  31. {main: math.MaxInt64, sub: math.MaxInt64},
  32. }
  33. bs := make([][]byte, len(tests))
  34. for i, tt := range tests {
  35. b := newRevBytes()
  36. revToBytes(tt, b)
  37. bs[i] = b
  38. if grev := bytesToRev(b); !reflect.DeepEqual(grev, tt) {
  39. t.Errorf("#%d: revision = %+v, want %+v", i, grev, tt)
  40. }
  41. }
  42. for i := 0; i < len(tests)-1; i++ {
  43. if bytes.Compare(bs[i], bs[i+1]) >= 0 {
  44. t.Errorf("#%d: %v (%+v) should be smaller than %v (%+v)", i, bs[i], tests[i], bs[i+1], tests[i+1])
  45. }
  46. }
  47. }