location_test.go 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /*
  2. * EliasDB
  3. *
  4. * Copyright 2016 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the Mozilla Public
  7. * License, v. 2.0. If a copy of the MPL was not distributed with this
  8. * file, You can obtain one at http://mozilla.org/MPL/2.0/.
  9. */
  10. package util
  11. import "testing"
  12. func TestLocation(t *testing.T) {
  13. var i uint16
  14. var j uint64
  15. for i = 0; i < MaxOffsetValue; i++ {
  16. location := PackLocation(1, uint16(i))
  17. recID := LocationRecord(location)
  18. if recID != 1 {
  19. t.Error("Unexpected record for location", location, " i:", i, " recId:", recID)
  20. return
  21. }
  22. off := LocationOffset(location)
  23. if off != i {
  24. t.Error("Unexpected record for location", location, " i:", i, " off:", off)
  25. return
  26. }
  27. }
  28. testPackLocationPanic(t)
  29. for j = 0; j < MaxRecordValue; j++ {
  30. location := PackLocation(j, 1)
  31. recID := LocationRecord(location)
  32. if recID != j {
  33. t.Error("Unexpected record for location", location, " i:", i, " recId:", recID)
  34. return
  35. }
  36. off := LocationOffset(location)
  37. if off != 1 {
  38. t.Error("Unexpected record for location", location, " i:", i, " off:", off)
  39. return
  40. }
  41. }
  42. if PackLocation(0xFFFFFF, 0xFFFF) != 0xFFFFFFFF {
  43. t.Error("Unexpected max location")
  44. }
  45. }
  46. func testPackLocationPanic(t *testing.T) {
  47. defer func() {
  48. if r := recover(); r == nil {
  49. t.Error("Packing location with invalid record id.")
  50. }
  51. }()
  52. PackLocation(MaxRecordValue+1, 0)
  53. }