forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
func_fnv_test.go
82 lines (76 loc) · 1.75 KB
/
func_fnv_test.go
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
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package ottlfuncs
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/open-telemetry/opentelemetry-collector-contrib/pkg/ottl"
)
func Test_FNV(t *testing.T) {
tests := []struct {
name string
value interface{}
expected interface{}
err bool
}{
{
name: "string",
value: "hello world",
expected: int64(8618312879776256743),
},
{
name: "empty string",
value: "",
expected: int64(-3750763034362895579),
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := FNVHashString[interface{}](&ottl.StandardStringGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return tt.value, nil
},
})
assert.NoError(t, err)
result, err := exprFunc(nil, nil)
if tt.err {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
assert.Equal(t, tt.expected, result)
})
}
}
func Test_FNVError(t *testing.T) {
tests := []struct {
name string
value interface{}
err bool
expectedError string
}{
{
name: "non-string",
value: 10,
expectedError: "expected string but got int",
},
{
name: "nil",
value: nil,
expectedError: "expected string but got nil",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
exprFunc, err := FNVHashString[interface{}](&ottl.StandardStringGetter[interface{}]{
Getter: func(context.Context, interface{}) (interface{}, error) {
return tt.value, nil
},
})
assert.NoError(t, err)
_, err = exprFunc(nil, nil)
assert.ErrorContains(t, err, tt.expectedError)
})
}
}