File: attribute_test.go

package info (click to toggle)
golang-opentelemetry-otel 1.31.0-5
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid
  • size: 11,844 kB
  • sloc: makefile: 237; sh: 51
file content (135 lines) | stat: -rw-r--r-- 3,276 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0

package attribute

import (
	"reflect"
	"testing"
)

var wrapFloat64SliceValue = func(v interface{}) interface{} {
	if vi, ok := v.([]float64); ok {
		return Float64SliceValue(vi)
	}
	return nil
}

var wrapInt64SliceValue = func(v interface{}) interface{} {
	if vi, ok := v.([]int64); ok {
		return Int64SliceValue(vi)
	}
	return nil
}

var wrapBoolSliceValue = func(v interface{}) interface{} {
	if vi, ok := v.([]bool); ok {
		return BoolSliceValue(vi)
	}
	return nil
}

var wrapStringSliceValue = func(v interface{}) interface{} {
	if vi, ok := v.([]string); ok {
		return StringSliceValue(vi)
	}
	return nil
}

var (
	wrapAsBoolSlice    = func(v interface{}) interface{} { return AsBoolSlice(v) }
	wrapAsInt64Slice   = func(v interface{}) interface{} { return AsInt64Slice(v) }
	wrapAsFloat64Slice = func(v interface{}) interface{} { return AsFloat64Slice(v) }
	wrapAsStringSlice  = func(v interface{}) interface{} { return AsStringSlice(v) }
)

func TestSliceValue(t *testing.T) {
	type args struct {
		v interface{}
	}
	tests := []struct {
		name string
		args args
		want interface{}
		fn   func(interface{}) interface{}
	}{
		{
			name: "Float64SliceValue() two items",
			args: args{v: []float64{1, 2.3}}, want: [2]float64{1, 2.3}, fn: wrapFloat64SliceValue,
		},
		{
			name: "Int64SliceValue() two items",
			args: args{[]int64{1, 2}}, want: [2]int64{1, 2}, fn: wrapInt64SliceValue,
		},
		{
			name: "BoolSliceValue() two items",
			args: args{v: []bool{true, false}}, want: [2]bool{true, false}, fn: wrapBoolSliceValue,
		},
		{
			name: "StringSliceValue() two items",
			args: args{[]string{"123", "2"}}, want: [2]string{"123", "2"}, fn: wrapStringSliceValue,
		},
		{
			name: "AsBoolSlice() two items",
			args: args{[2]bool{true, false}}, want: []bool{true, false}, fn: wrapAsBoolSlice,
		},
		{
			name: "AsInt64Slice() two items",
			args: args{[2]int64{1, 3}}, want: []int64{1, 3}, fn: wrapAsInt64Slice,
		},
		{
			name: "AsFloat64Slice() two items",
			args: args{[2]float64{1.2, 3.1}}, want: []float64{1.2, 3.1}, fn: wrapAsFloat64Slice,
		},
		{
			name: "AsStringSlice() two items",
			args: args{[2]string{"1234", "12"}}, want: []string{"1234", "12"}, fn: wrapAsStringSlice,
		},
	}
	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			if got := tt.fn(tt.args.v); !reflect.DeepEqual(got, tt.want) {
				t.Errorf("got %v, want %v", got, tt.want)
			}
		})
	}
}

// sync is a global used to ensure the benchmark are not optimized away.
var sync any

func BenchmarkBoolSliceValue(b *testing.B) {
	b.ReportAllocs()
	s := []bool{true, false, true, false}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		sync = BoolSliceValue(s)
	}
}

func BenchmarkInt64SliceValue(b *testing.B) {
	b.ReportAllocs()
	s := []int64{1, 2, 3, 4}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		sync = Int64SliceValue(s)
	}
}

func BenchmarkFloat64SliceValue(b *testing.B) {
	b.ReportAllocs()
	s := []float64{1.2, 3.4, 5.6, 7.8}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		sync = Float64SliceValue(s)
	}
}

func BenchmarkStringSliceValue(b *testing.B) {
	b.ReportAllocs()
	s := []string{"a", "b", "c", "d"}
	b.ResetTimer()
	for n := 0; n < b.N; n++ {
		sync = StringSliceValue(s)
	}
}