-
Notifications
You must be signed in to change notification settings - Fork 10
/
set_internal_test.go
130 lines (106 loc) · 3.62 KB
/
set_internal_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
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
package types
import (
"slices"
"testing"
"github.com/cedar-policy/cedar-go/internal/testutil"
)
type colliderValue struct {
Value Value
HashVal uint64
}
func (c colliderValue) String() string { return "" }
func (c colliderValue) MarshalCedar() []byte { return nil }
func (c colliderValue) Equal(v Value) bool { return v.Equal(c.Value) }
func (c colliderValue) hash() uint64 { return c.HashVal }
func TestSetInternal(t *testing.T) {
t.Parallel()
t.Run("hash", func(t *testing.T) {
t.Parallel()
t.Run("order independent", func(t *testing.T) {
t.Parallel()
s1 := NewSet(Long(42), Long(1337))
s2 := NewSet(Long(1337), Long(42))
testutil.Equals(t, s1.hash(), s2.hash())
})
t.Run("order independent with collisions", func(t *testing.T) {
t.Parallel()
v1 := colliderValue{Value: String("foo"), HashVal: 1337}
v2 := colliderValue{Value: String("bar"), HashVal: 1337}
v3 := colliderValue{Value: String("baz"), HashVal: 1337}
permutations := []Set{
NewSet(v1, v2, v3),
NewSet(v1, v3, v2),
NewSet(v2, v1, v3),
NewSet(v2, v3, v1),
NewSet(v3, v1, v2),
NewSet(v3, v2, v1),
}
expected := permutations[0].hash()
for _, p := range permutations {
testutil.Equals(t, p.hash(), expected)
}
})
t.Run("order independent with interleaving collisions", func(t *testing.T) {
t.Parallel()
v1 := colliderValue{Value: String("foo"), HashVal: 1337}
v2 := colliderValue{Value: String("bar"), HashVal: 1338}
v3 := colliderValue{Value: String("baz"), HashVal: 1337}
permutations := []Set{
NewSet(v1, v2, v3),
NewSet(v1, v3, v2),
NewSet(v2, v1, v3),
NewSet(v2, v3, v1),
NewSet(v3, v1, v2),
NewSet(v3, v2, v1),
}
expected := permutations[0].hash()
for _, p := range permutations {
testutil.Equals(t, p.hash(), expected)
}
})
t.Run("duplicates unimportant", func(t *testing.T) {
t.Parallel()
s1 := NewSet(Long(42), Long(1337))
s2 := NewSet(Long(42), Long(1337), Long(1337))
testutil.Equals(t, s1.hash(), s2.hash())
})
t.Run("empty set", func(t *testing.T) {
t.Parallel()
m1 := Set{}
m2 := NewSet()
testutil.Equals(t, m1.hash(), m2.hash())
})
// These tests don't necessarily hold for all values of Set, but we want to ensure we are considering
// different aspects of the Set, which these particular tests demonstrate.
t.Run("extra element", func(t *testing.T) {
t.Parallel()
s1 := NewSet(Long(42), Long(1337))
s2 := NewSet(Long(42), Long(1337), Long(1))
testutil.FatalIf(t, s1.hash() == s2.hash(), "unexpected hash collision")
})
t.Run("disjoint", func(t *testing.T) {
t.Parallel()
s1 := NewSet(Long(42), Long(1337))
s2 := NewSet(Long(0), String("hi"))
testutil.FatalIf(t, s1.hash() == s2.hash(), "unexpected hash collision")
})
})
t.Run("collisions", func(t *testing.T) {
t.Parallel()
v1 := colliderValue{Value: String("foo"), HashVal: 1337}
v2 := colliderValue{Value: String("bar"), HashVal: 1337}
v3 := colliderValue{Value: String("baz"), HashVal: 1338}
v4 := colliderValue{Value: String("baz"), HashVal: 1337}
set := NewSet(v1, v2, v3, v4)
testutil.Equals(t, set.Len(), 3)
var vals []Value
set.Iterate(func(v Value) bool {
vals = append(vals, v)
return true
})
testutil.Equals(t, slices.ContainsFunc(vals, func(v Value) bool { return v.Equal(v1) }), true)
testutil.Equals(t, slices.ContainsFunc(vals, func(v Value) bool { return v.Equal(v2) }), true)
testutil.Equals(t, slices.ContainsFunc(vals, func(v Value) bool { return v.Equal(v3) }), true)
testutil.Equals(t, slices.ContainsFunc(vals, func(v Value) bool { return v.Equal(v4) }), true)
})
}