-
Notifications
You must be signed in to change notification settings - Fork 2
/
functions_test.go
155 lines (131 loc) · 4.76 KB
/
functions_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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package evidence
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestK(t *testing.T) {
assert := assert.New(t)
assert.Equal(functionKey("a,b,c"), K("a", "b", "c"))
assert.Equal(functionKey("a,b,c"), K("c", "b", "a"))
assert.Equal(functionKey("a,b,c"), K("a", "c", "a", "b", "c"))
assert.Equal(functionKey(""), K())
assert.Panics(func() {
K("A")
})
assert.Panics(func() {
K("")
})
assert.Panics(func() {
K("_")
})
assert.Panics(func() {
K("a,b,c")
})
}
func TestFocalElements(t *testing.T) {
assert := assert.New(t)
fk := K()
fe := fk.FocalElements()
assert.Len(fe, 0)
fk = K("a")
fe = fk.FocalElements()
assert.Len(fe, 1)
assert.Contains(fe, functionKey("a"))
}
func TestIsSubset(t *testing.T) {
assert := assert.New(t)
assert.True(K("a").IsSubset(K("a", "b", "c")))
assert.False(K("a", "b", "c").IsSubset(K("a")))
assert.True(K("a", "b").IsSubset(K("a", "b", "c")))
assert.True(K("b", "c").IsSubset(K("a", "b", "c")))
assert.True(K("a", "b").IsSubset(K("a", "b")))
assert.False(K("d").IsSubset(K("a", "b")))
assert.False(K("d", "b").IsSubset(K("a", "b")))
assert.True(K().IsSubset(K("a", "b", "c")))
assert.True(K().IsSubset(K()))
assert.False(K("a", "b", "c").IsSubset(K()))
assert.False(K("a").IsSubset(K()))
assert.True(K("yellow", "green").IsSubset(K("red", "yellow", "green")))
assert.False(K("red", "yellow", "green").IsSubset(K("yellow", "green")))
}
func TestIsSuperset(t *testing.T) {
assert := assert.New(t)
assert.False(K("a").IsSuperset(K("a", "b", "c")))
assert.True(K("a", "b", "c").IsSuperset(K("a")))
assert.False(K("a", "b").IsSuperset(K("a", "b", "c")))
assert.False(K("b", "c").IsSuperset(K("a", "b", "c")))
assert.True(K("a", "b").IsSuperset(K("a", "b")))
assert.False(K("d").IsSuperset(K("a", "b")))
assert.False(K("d", "b").IsSuperset(K("a", "b")))
assert.False(K().IsSuperset(K("a", "b", "c")))
assert.True(K().IsSuperset(K()))
assert.True(K("a", "b", "c").IsSuperset(K()))
assert.True(K("a").IsSuperset(K()))
assert.False(K("yellow", "green").IsSuperset(K("red", "yellow", "green")))
assert.True(K("red", "yellow", "green").IsSuperset(K("yellow", "green")))
}
func TestIntersect(t *testing.T) {
assert := assert.New(t)
assert.Equal(K("a"), K("a").Intersect(K("a", "b", "c")))
assert.Equal(K("a"), K("a", "b", "c").Intersect(K("a")))
assert.Equal(K("a", "b"), K("a", "b").Intersect(K("a", "b", "c")))
assert.Equal(K("b", "c"), K("b", "c").Intersect(K("a", "b", "c")))
assert.Equal(K("a", "b"), K("a", "b").Intersect(K("a", "b")))
assert.Equal(K(), K("d").Intersect(K("a", "b")))
assert.Equal(K("b"), K("d", "b").Intersect(K("a", "b")))
assert.Equal(K(), K().Intersect(K("a", "b", "c")))
assert.Equal(K(), K().Intersect(K()))
assert.Equal(K(), K("a", "b", "c").Intersect(K()))
assert.Equal(K(), K("a").Intersect(K()))
assert.Equal(K("yellow", "green"),
K("yellow", "green").Intersect(K("red", "yellow", "green")))
assert.Equal(K("yellow", "green"),
K("red", "yellow", "green").Intersect(K("yellow", "green")))
}
func TestUnion(t *testing.T) {
assert := assert.New(t)
assert.Equal(K("a", "b", "c"), K("a").Union(K("a", "b", "c")))
assert.Equal(K("a", "b", "c"), K("a", "b", "c").Union(K("a")))
assert.Equal(K("a", "b", "c"), K("a", "b").Union(K("a", "b", "c")))
assert.Equal(K("a", "b", "c"), K("b", "c").Union(K("a", "b", "c")))
assert.Equal(K("a", "b"), K("a", "b").Union(K("a", "b")))
assert.Equal(K("a", "b", "d"), K("d").Union(K("a", "b")))
assert.Equal(K("a", "b", "d"), K("d", "b").Union(K("a", "b")))
assert.Equal(K("a", "b", "c"), K().Union(K("a", "b", "c")))
assert.Equal(K(), K().Union(K()))
assert.Equal(K("a", "b", "c"), K("a", "b", "c").Union(K()))
assert.Equal(K("a"), K("a").Union(K()))
assert.Equal(K("red", "yellow", "green"),
K("yellow", "green").Union(K("red", "yellow", "green")))
assert.Equal(K("red", "yellow", "green"),
K("red", "yellow", "green").Union(K("yellow", "green")))
}
func TestPowerset(t *testing.T) {
assert := assert.New(t)
f := &Function{}
f.Set(K("a"), 0.0)
f.Set(K("b"), 0.1)
f.Set(K("c"), 0.3)
f.Set(K("a", "b", "c"), 0.1)
f.Set(K("b", "c"), 0.5)
// [[] [a] [b] [a b] [c] [a c] [b c] [a b c]]
ps := f.Powerset()
assert.Len(ps, 8)
assert.Contains(ps, K())
assert.Contains(ps, K("a"))
assert.Contains(ps, K("b"))
assert.Contains(ps, K("c"))
assert.Contains(ps, K("a", "b"))
assert.Contains(ps, K("a", "c"))
assert.Contains(ps, K("b", "c"))
assert.Contains(ps, K("a", "b", "c"))
// Shouldn't contain things outside the set
assert.NotContains(ps, K("d"))
// Shouldn't contain any invalid keys
assert.NotContains(ps, functionKey(","))
assert.NotContains(ps, functionKey("c,b"))
assert.NotContains(ps, functionKey("c,a"))
assert.NotContains(ps, functionKey("b,a"))
assert.NotContains(ps, functionKey("b,c,a"))
assert.NotContains(ps, functionKey("c,b,a"))
}