array_test.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package zap
  21. import (
  22. "testing"
  23. "time"
  24. "go.uber.org/zap/zapcore"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. func BenchmarkBoolsArrayMarshaler(b *testing.B) {
  28. // Keep this benchmark here to capture the overhead of the ArrayMarshaler
  29. // wrapper.
  30. bs := make([]bool, 50)
  31. enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{})
  32. b.ResetTimer()
  33. for i := 0; i < b.N; i++ {
  34. Bools("array", bs).AddTo(enc.Clone())
  35. }
  36. }
  37. func BenchmarkBoolsReflect(b *testing.B) {
  38. bs := make([]bool, 50)
  39. enc := zapcore.NewJSONEncoder(zapcore.EncoderConfig{})
  40. b.ResetTimer()
  41. for i := 0; i < b.N; i++ {
  42. Reflect("array", bs).AddTo(enc.Clone())
  43. }
  44. }
  45. func TestArrayWrappers(t *testing.T) {
  46. tests := []struct {
  47. desc string
  48. field Field
  49. expected []interface{}
  50. }{
  51. {"empty bools", Bools("", []bool{}), []interface{}{}},
  52. {"empty byte strings", ByteStrings("", [][]byte{}), []interface{}{}},
  53. {"empty complex128s", Complex128s("", []complex128{}), []interface{}{}},
  54. {"empty complex64s", Complex64s("", []complex64{}), []interface{}{}},
  55. {"empty durations", Durations("", []time.Duration{}), []interface{}{}},
  56. {"empty float64s", Float64s("", []float64{}), []interface{}{}},
  57. {"empty float32s", Float32s("", []float32{}), []interface{}{}},
  58. {"empty ints", Ints("", []int{}), []interface{}{}},
  59. {"empty int64s", Int64s("", []int64{}), []interface{}{}},
  60. {"empty int32s", Int32s("", []int32{}), []interface{}{}},
  61. {"empty int16s", Int16s("", []int16{}), []interface{}{}},
  62. {"empty int8s", Int8s("", []int8{}), []interface{}{}},
  63. {"empty strings", Strings("", []string{}), []interface{}{}},
  64. {"empty times", Times("", []time.Time{}), []interface{}{}},
  65. {"empty uints", Uints("", []uint{}), []interface{}{}},
  66. {"empty uint64s", Uint64s("", []uint64{}), []interface{}{}},
  67. {"empty uint32s", Uint32s("", []uint32{}), []interface{}{}},
  68. {"empty uint16s", Uint16s("", []uint16{}), []interface{}{}},
  69. {"empty uint8s", Uint8s("", []uint8{}), []interface{}{}},
  70. {"empty uintptrs", Uintptrs("", []uintptr{}), []interface{}{}},
  71. {"bools", Bools("", []bool{true, false}), []interface{}{true, false}},
  72. {"byte strings", ByteStrings("", [][]byte{{1, 2}, {3, 4}}), []interface{}{"\x01\x02", "\x03\x04"}},
  73. {"complex128s", Complex128s("", []complex128{1 + 2i, 3 + 4i}), []interface{}{1 + 2i, 3 + 4i}},
  74. {"complex64s", Complex64s("", []complex64{1 + 2i, 3 + 4i}), []interface{}{complex64(1 + 2i), complex64(3 + 4i)}},
  75. {"durations", Durations("", []time.Duration{1, 2}), []interface{}{time.Nanosecond, 2 * time.Nanosecond}},
  76. {"float64s", Float64s("", []float64{1.2, 3.4}), []interface{}{1.2, 3.4}},
  77. {"float32s", Float32s("", []float32{1.2, 3.4}), []interface{}{float32(1.2), float32(3.4)}},
  78. {"ints", Ints("", []int{1, 2}), []interface{}{1, 2}},
  79. {"int64s", Int64s("", []int64{1, 2}), []interface{}{int64(1), int64(2)}},
  80. {"int32s", Int32s("", []int32{1, 2}), []interface{}{int32(1), int32(2)}},
  81. {"int16s", Int16s("", []int16{1, 2}), []interface{}{int16(1), int16(2)}},
  82. {"int8s", Int8s("", []int8{1, 2}), []interface{}{int8(1), int8(2)}},
  83. {"strings", Strings("", []string{"foo", "bar"}), []interface{}{"foo", "bar"}},
  84. {"times", Times("", []time.Time{time.Unix(0, 0), time.Unix(0, 0)}), []interface{}{time.Unix(0, 0), time.Unix(0, 0)}},
  85. {"uints", Uints("", []uint{1, 2}), []interface{}{uint(1), uint(2)}},
  86. {"uint64s", Uint64s("", []uint64{1, 2}), []interface{}{uint64(1), uint64(2)}},
  87. {"uint32s", Uint32s("", []uint32{1, 2}), []interface{}{uint32(1), uint32(2)}},
  88. {"uint16s", Uint16s("", []uint16{1, 2}), []interface{}{uint16(1), uint16(2)}},
  89. {"uint8s", Uint8s("", []uint8{1, 2}), []interface{}{uint8(1), uint8(2)}},
  90. {"uintptrs", Uintptrs("", []uintptr{1, 2}), []interface{}{uintptr(1), uintptr(2)}},
  91. }
  92. for _, tt := range tests {
  93. enc := zapcore.NewMapObjectEncoder()
  94. tt.field.Key = "k"
  95. tt.field.AddTo(enc)
  96. assert.Equal(t, tt.expected, enc.Fields["k"], "%s: unexpected map contents.", tt.desc)
  97. assert.Equal(t, 1, len(enc.Fields), "%s: found extra keys in map: %v", tt.desc, enc.Fields)
  98. }
  99. }