pointer_test.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Copyright (c) 2018 Arista Networks, Inc.
  2. // Use of this source code is governed by the Apache License 2.0
  3. // that can be found in the COPYING file.
  4. package key_test
  5. import (
  6. "encoding/json"
  7. "fmt"
  8. "testing"
  9. "notabug.org/themusicgod1/goarista/key"
  10. "notabug.org/themusicgod1/goarista/path"
  11. )
  12. func TestPointer(t *testing.T) {
  13. p := key.NewPointer(path.New("foo"))
  14. if expected, actual := path.New("foo"), p.Pointer(); !path.Equal(expected, actual) {
  15. t.Errorf("Expected %#v but got %#v", expected, actual)
  16. }
  17. if expected, actual := "{/foo}", fmt.Sprintf("%s", p); actual != expected {
  18. t.Errorf("Expected %q but got %q", expected, actual)
  19. }
  20. if js, err := json.Marshal(p); err != nil {
  21. t.Errorf("JSON marshaling failed: %s", err)
  22. } else if expected, actual := `{"_ptr":"/foo"}`, string(js); actual != expected {
  23. t.Errorf("Expected %q but got %q", expected, actual)
  24. }
  25. }
  26. func TestPointerAsKey(t *testing.T) {
  27. a := key.NewPointer(path.New("foo", path.Wildcard, map[string]interface{}{
  28. "bar": map[key.Key]interface{}{
  29. // Should be able to embed pointer key.
  30. key.New(key.NewPointer(path.New("baz"))):
  31. // Should be able to embed pointer value.
  32. key.NewPointer(path.New("baz")),
  33. },
  34. }))
  35. m := map[key.Key]string{
  36. key.New(a): "a",
  37. }
  38. if s, ok := m[key.New(a)]; !ok {
  39. t.Error("pointer to path not keyed in map")
  40. } else if s != "a" {
  41. t.Errorf("pointer to path not mapped to correct value in map: %s", s)
  42. }
  43. }
  44. func BenchmarkPointer(b *testing.B) {
  45. benchmarks := []key.Path{
  46. path.New(),
  47. path.New("foo"),
  48. path.New("foo", "bar"),
  49. path.New("foo", "bar", "baz"),
  50. path.New("foo", "bar", "baz", "qux"),
  51. }
  52. for i, benchmark := range benchmarks {
  53. b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
  54. for i := 0; i < b.N; i++ {
  55. key.NewPointer(benchmark)
  56. }
  57. })
  58. }
  59. }
  60. func BenchmarkPointerAsKey(b *testing.B) {
  61. benchmarks := []key.Pointer{
  62. key.NewPointer(path.New()),
  63. key.NewPointer(path.New("foo")),
  64. key.NewPointer(path.New("foo", "bar")),
  65. key.NewPointer(path.New("foo", "bar", "baz")),
  66. key.NewPointer(path.New("foo", "bar", "baz", "qux")),
  67. }
  68. for i, benchmark := range benchmarks {
  69. b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
  70. for i := 0; i < b.N; i++ {
  71. key.New(benchmark)
  72. }
  73. })
  74. }
  75. }
  76. func BenchmarkEmbeddedPointerAsKey(b *testing.B) {
  77. benchmarks := [][]interface{}{
  78. []interface{}{key.NewPointer(path.New())},
  79. []interface{}{key.NewPointer(path.New("foo"))},
  80. []interface{}{key.NewPointer(path.New("foo", "bar"))},
  81. []interface{}{key.NewPointer(path.New("foo", "bar", "baz"))},
  82. []interface{}{key.NewPointer(path.New("foo", "bar", "baz", "qux"))},
  83. }
  84. for i, benchmark := range benchmarks {
  85. b.Run(fmt.Sprintf("%d", i), func(b *testing.B) {
  86. for i := 0; i < b.N; i++ {
  87. key.New(benchmark)
  88. }
  89. })
  90. }
  91. }