namesmanager_test.go 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  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 TestNamesManager(t *testing.T) {
  13. nm := NewNamesManager(make(map[string]string))
  14. if res := nm.newCode16(); res != string([]byte{0x01, 0x00}) {
  15. t.Error("Unexpected result:", res)
  16. return
  17. }
  18. if res := nm.newCode16(); res != string([]byte{0x02, 0x00}) {
  19. t.Error("Unexpected result:", res)
  20. return
  21. }
  22. if res := nm.newCode16(); res != string([]byte{0x03, 0x00}) {
  23. t.Error("Unexpected result:", res)
  24. return
  25. }
  26. if res := nm.newCode32(); res != string([]byte{0x01, 0x00, 0x00, 0x00}) {
  27. t.Error("Unexpected result:", res)
  28. return
  29. }
  30. if res := nm.newCode32(); res != string([]byte{0x02, 0x00, 0x00, 0x00}) {
  31. t.Error("Unexpected result:", res)
  32. return
  33. }
  34. if res := nm.newCode32(); res != string([]byte{0x03, 0x00, 0x00, 0x00}) {
  35. t.Error("Unexpected result:", res)
  36. return
  37. }
  38. code := nm.encode("bb", "myentry", true)
  39. if name := nm.decode("bb", code); name != "myentry" {
  40. t.Error("Unexpected result:", name)
  41. return
  42. }
  43. if res := nm.decode("b123b", "123"); res != "" {
  44. t.Error("Unexpected result:", res)
  45. return
  46. }
  47. if nm.Decode16(nm.Encode16("mykind", true)) != "mykind" {
  48. t.Error("Unexpected result")
  49. return
  50. }
  51. if nm.Decode32(nm.Encode32("myrole", true)) != "myrole" {
  52. t.Error("Unexpected result")
  53. return
  54. }
  55. if res := nm.Encode32("mynonexistentstring", false); res != "" {
  56. t.Error("Unexpected lookup result:", res)
  57. return
  58. }
  59. }