util_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. // Copyright 2017 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 namespace
  15. import (
  16. "bytes"
  17. "testing"
  18. )
  19. func TestPrefixInterval(t *testing.T) {
  20. tests := []struct {
  21. pfx string
  22. key []byte
  23. end []byte
  24. wKey []byte
  25. wEnd []byte
  26. }{
  27. // single key
  28. {
  29. pfx: "pfx/",
  30. key: []byte("a"),
  31. wKey: []byte("pfx/a"),
  32. },
  33. // range
  34. {
  35. pfx: "pfx/",
  36. key: []byte("abc"),
  37. end: []byte("def"),
  38. wKey: []byte("pfx/abc"),
  39. wEnd: []byte("pfx/def"),
  40. },
  41. // one-sided range
  42. {
  43. pfx: "pfx/",
  44. key: []byte("abc"),
  45. end: []byte{0},
  46. wKey: []byte("pfx/abc"),
  47. wEnd: []byte("pfx0"),
  48. },
  49. // one-sided range, end of keyspace
  50. {
  51. pfx: "\xff\xff",
  52. key: []byte("abc"),
  53. end: []byte{0},
  54. wKey: []byte("\xff\xffabc"),
  55. wEnd: []byte{0},
  56. },
  57. }
  58. for i, tt := range tests {
  59. pfxKey, pfxEnd := prefixInterval(tt.pfx, tt.key, tt.end)
  60. if !bytes.Equal(pfxKey, tt.wKey) {
  61. t.Errorf("#%d: expected key=%q, got key=%q", i, tt.wKey, pfxKey)
  62. }
  63. if !bytes.Equal(pfxEnd, tt.wEnd) {
  64. t.Errorf("#%d: expected end=%q, got end=%q", i, tt.wEnd, pfxEnd)
  65. }
  66. }
  67. }