Skip to content

Commit e515037

Browse files
committed
add tests for hash
1 parent 7dd3691 commit e515037

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

object/object_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
package object_test
2+
3+
import (
4+
"testing"
5+
6+
"github.com/awslabs/operatorpkg/object"
7+
)
8+
9+
func TestHashConsistency(t *testing.T) {
10+
tests := []struct {
11+
name string
12+
input any
13+
expected string
14+
}{
15+
{
16+
name: "string",
17+
input: "test",
18+
expected: "4750482246928359241",
19+
},
20+
{
21+
name: "int",
22+
input: 42,
23+
expected: "6502725087716144517",
24+
},
25+
{
26+
name: "struct",
27+
input: struct{ Name string }{Name: "test"},
28+
expected: "8856711690231820218",
29+
},
30+
{
31+
name: "map",
32+
input: map[string]string{"key": "value"},
33+
expected: "17998630672759287760",
34+
},
35+
{
36+
name: "slice",
37+
input: []string{"a", "b", "c"},
38+
expected: "16898053174053854585",
39+
},
40+
}
41+
42+
for _, tt := range tests {
43+
t.Run(tt.name, func(t *testing.T) {
44+
result := object.Hash(tt.input)
45+
if result != tt.expected {
46+
t.Errorf("Hash() = %v, want %v", result, tt.expected)
47+
}
48+
})
49+
}
50+
51+
// Test consistency
52+
t.Run("consistency", func(t *testing.T) {
53+
input := "consistent"
54+
hash1 := object.Hash(input)
55+
hash2 := object.Hash(input)
56+
if hash1 != hash2 {
57+
t.Errorf("Hash should be consistent: %v != %v", hash1, hash2)
58+
}
59+
})
60+
}
61+
62+
type obj1 struct {
63+
bar map[string]string
64+
}
65+
66+
func TestHashExpectedDifferences(t *testing.T) {
67+
testObjects := []any{
68+
0,
69+
"0",
70+
[]int{},
71+
struct{}{},
72+
struct {
73+
foo []obj1
74+
}{
75+
foo: []obj1{
76+
{
77+
bar: map[string]string{"a": "b", "c": "d"},
78+
},
79+
}},
80+
struct {
81+
foo []obj1
82+
}{
83+
foo: []obj1{
84+
{
85+
bar: map[string]string{"a": "b"},
86+
},
87+
{
88+
bar: map[string]string{"c": "d"},
89+
},
90+
}},
91+
}
92+
testHashes := map[string]struct{}{}
93+
for _, obj := range testObjects {
94+
hash := object.Hash(obj)
95+
if _, exists := testHashes[hash]; exists {
96+
t.Errorf("Hash collision detected: %v", hash)
97+
}
98+
testHashes[hash] = struct{}{}
99+
}
100+
101+
}

0 commit comments

Comments
 (0)