registry_test.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. // Copyright (C) 2019 The Syncthing Authors.
  2. //
  3. // This Source Code Form is subject to the terms of the Mozilla Public
  4. // License, v. 2.0. If a copy of the MPL was not distributed with this file,
  5. // You can obtain one at https://mozilla.org/MPL/2.0/.
  6. package registry
  7. import (
  8. "testing"
  9. )
  10. func TestRegistry(t *testing.T) {
  11. r := New()
  12. if res := r.Get("int", intLess); res != nil {
  13. t.Error("unexpected")
  14. }
  15. r.Register("int", 1)
  16. r.Register("int", 11)
  17. r.Register("int4", 4)
  18. r.Register("int4", 44)
  19. r.Register("int6", 6)
  20. r.Register("int6", 66)
  21. if res := r.Get("int", intLess).(int); res != 1 {
  22. t.Error("unexpected", res)
  23. }
  24. // int is prefix of int4, so returns 1
  25. if res := r.Get("int4", intLess).(int); res != 1 {
  26. t.Error("unexpected", res)
  27. }
  28. r.Unregister("int", 1)
  29. // Check that falls through to 11
  30. if res := r.Get("int", intLess).(int); res != 11 {
  31. t.Error("unexpected", res)
  32. }
  33. // 6 is smaller than 11 available in int.
  34. if res := r.Get("int6", intLess).(int); res != 6 {
  35. t.Error("unexpected", res)
  36. }
  37. // Unregister 11, int should be impossible to find
  38. r.Unregister("int", 11)
  39. if res := r.Get("int", intLess); res != nil {
  40. t.Error("unexpected")
  41. }
  42. // Unregister a second time does nothing.
  43. r.Unregister("int", 1)
  44. // Can have multiple of the same
  45. r.Register("int", 1)
  46. r.Register("int", 1)
  47. r.Unregister("int", 1)
  48. if res := r.Get("int4", intLess).(int); res != 1 {
  49. t.Error("unexpected", res)
  50. }
  51. }
  52. func intLess(i, j interface{}) bool {
  53. iInt := i.(int)
  54. jInt := j.(int)
  55. return iInt < jInt
  56. }