Debian Codegraph @

attribute/internal/attribute_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package attribute
5
6 import (
7 "reflect"
8 "testing"
9 )
10
11 var wrapFloat64SliceValue = func(v any) any {
12 if vi, ok := v.([]float64); ok {
13 return SliceValue(vi)
14 }
15 return nil
16 }
17
18 var wrapInt64SliceValue = func(v any) any {
19 if vi, ok := v.([]int64); ok {
20 return SliceValue(vi)
21 }
22 return nil
23 }
24
25 var wrapBoolSliceValue = func(v any) any {
26 if vi, ok := v.([]bool); ok {
27 return SliceValue(vi)
28 }
29 return nil
30 }
31
32 var wrapStringSliceValue = func(v any) any {
33 if vi, ok := v.([]string); ok {
34 return SliceValue(vi)
35 }
36 return nil
37 }
38
39 var (
40 wrapAsBoolSlice = func(v any) any { return AsSlice[bool](v) }
41 wrapAsInt64Slice = func(v any) any { return AsSlice[int64](v) }
42 wrapAsFloat64Slice = func(v any) any { return AsSlice[float64](v) }
43 wrapAsStringSlice = func(v any) any { return AsSlice[string](v) }
44 )
45
46 func TestSliceValue(t *testing.T) {
47 type args struct {
48 v any
49 }
50 tests := []struct {
51 name string
52 args args
53 want any
54 fn func(any) any
55 }{
56 {
57 name: "Float64SliceValue() two items",
58 args: args{v: []float64{1, 2.3}}, want: [2]float64{1, 2.3}, fn: wrapFloat64SliceValue,
59 },
60 {
61 name: "Int64SliceValue() two items",
62 args: args{[]int64{1, 2}}, want: [2]int64{1, 2}, fn: wrapInt64SliceValue,
63 },
64 {
65 name: "BoolSliceValue() two items",
66 args: args{v: []bool{true, false}}, want: [2]bool{true, false}, fn: wrapBoolSliceValue,
67 },
68 {
69 name: "StringSliceValue() two items",
70 args: args{[]string{"123", "2"}}, want: [2]string{"123", "2"}, fn: wrapStringSliceValue,
71 },
72 {
73 name: "AsBoolSlice() two items",
74 args: args{[2]bool{true, false}}, want: []bool{true, false}, fn: wrapAsBoolSlice,
75 },
76 {
77 name: "AsInt64Slice() two items",
78 args: args{[2]int64{1, 3}}, want: []int64{1, 3}, fn: wrapAsInt64Slice,
79 },
80 {
81 name: "AsFloat64Slice() two items",
82 args: args{[2]float64{1.2, 3.1}}, want: []float64{1.2, 3.1}, fn: wrapAsFloat64Slice,
83 },
84 {
85 name: "AsStringSlice() two items",
86 args: args{[2]string{"1234", "12"}}, want: []string{"1234", "12"}, fn: wrapAsStringSlice,
87 },
88 }
89 for _, tt := range tests {
90 t.Run(tt.name, func(t *testing.T) {
91 if got := tt.fn(tt.args.v); !reflect.DeepEqual(got, tt.want) {
92 t.Errorf("got %v, want %v", got, tt.want)
93 }
94 })
95 }
96 }
97
98 func TestAsSliceMismatchedType(t *testing.T) {
99 tests := []struct {
100 name string
101 fn func() any
102 }{
103 {name: "bool from int64 array", fn: func() any { return AsSlice[bool]([2]int64{1, 2}) }},
104 {name: "int64 from float64 array", fn: func() any { return AsSlice[int64]([2]float64{1, 2}) }},
105 {name: "float64 from string array", fn: func() any { return AsSlice[float64]([2]string{"1", "2"}) }},
106 {name: "string from bool array", fn: func() any { return AsSlice[string]([2]bool{true, false}) }},
107 }
108
109 for _, tt := range tests {
110 t.Run(tt.name, func(t *testing.T) {
111 got := tt.fn()
112 rv := reflect.ValueOf(got)
113 if !rv.IsNil() {
114 t.Fatalf("got %v, want nil", got)
115 }
116 })
117 }
118 }
119
120 // sync is a global used to ensure the benchmark are not optimized away.
121 var sync any
122
123 func BenchmarkBoolSliceValue(b *testing.B) {
124 for _, bench := range []struct {
125 name string
126 s []bool
127 }{
128 {name: "Len2", s: []bool{true, false}},
129 {name: "Len8", s: []bool{true, false, true, false, true, false, true, false}},
130 } {
131 b.Run(bench.name, func(b *testing.B) {
132 b.ReportAllocs()
133 for b.Loop() {
134 sync = SliceValue(bench.s)
135 }
136 })
137 }
138 }
139
140 func BenchmarkInt64SliceValue(b *testing.B) {
141 for _, bench := range []struct {
142 name string
143 s []int64
144 }{
145 {name: "Len2", s: []int64{1, 2}},
146 {name: "Len8", s: []int64{1, 2, 3, 4, 5, 6, 7, 8}},
147 } {
148 b.Run(bench.name, func(b *testing.B) {
149 b.ReportAllocs()
150 for b.Loop() {
151 sync = SliceValue(bench.s)
152 }
153 })
154 }
155 }
156
157 func BenchmarkFloat64SliceValue(b *testing.B) {
158 for _, bench := range []struct {
159 name string
160 s []float64
161 }{
162 {name: "Len2", s: []float64{1.2, 3.4}},
163 {name: "Len8", s: []float64{1.2, 3.4, 5.6, 7.8, 9.1, 2.3, 4.5, 6.7}},
164 } {
165 b.Run(bench.name, func(b *testing.B) {
166 b.ReportAllocs()
167 for b.Loop() {
168 sync = SliceValue(bench.s)
169 }
170 })
171 }
172 }
173
174 func BenchmarkStringSliceValue(b *testing.B) {
175 for _, bench := range []struct {
176 name string
177 s []string
178 }{
179 {name: "Len2", s: []string{"a", "b"}},
180 {name: "Len8", s: []string{"a", "b", "c", "d", "e", "f", "g", "h"}},
181 } {
182 b.Run(bench.name, func(b *testing.B) {
183 b.ReportAllocs()
184 for b.Loop() {
185 sync = SliceValue(bench.s)
186 }
187 })
188 }
189 }
190
191 func BenchmarkAsFloat64Slice(b *testing.B) {
192 for _, bench := range []struct {
193 name string
194 in any
195 }{
196 {name: "Len2", in: [2]float64{1, 2.3}},
197 {name: "Len8", in: [8]float64{1, 2.3, 3.4, 4.5, 5.6, 6.7, 7.8, 8.9}},
198 } {
199 b.Run(bench.name, func(b *testing.B) {
200 b.ReportAllocs()
201 for b.Loop() {
202 sync = AsSlice[float64](bench.in)
203 }
204 })
205 }
206 }