-
Notifications
You must be signed in to change notification settings - Fork 0
/
intervalset_test.go
156 lines (129 loc) · 3.96 KB
/
intervalset_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
156
package bandit_test
import (
"fmt"
. "github.com/onsi/ginkgo"
. "github.com/onsi/ginkgo/extensions/table"
. "github.com/onsi/gomega"
. "github.com/iancmcc/bandit"
)
func doIntervalSetOp(op string, a, b *IntervalSet) *IntervalSet {
switch op {
case "&":
return a.Intersection(a, b)
case "|":
return a.Union(a, b)
case "^":
return a.SymmetricDifference(a, b)
case "-":
return a.Difference(a, b)
}
return a
}
var _ = Describe("Set", func() {
var a, b *IntervalSet
BeforeEach(func() {
a = NewIntervalSet(RightOpen(0, 2), RightOpen(4, 6))
b = NewIntervalSet(RightOpen(1, 3), RightOpen(3, 5))
})
DescribeTable("interval set operations",
func(op, expected string) {
Ω(doIntervalSetOp(op, a, b).String()).Should(Equal(expected))
},
Entry("a & b", "&", `[1, 2), [4, 5)`),
Entry("a | b", "|", `[0, 6)`),
Entry("a - b", "-", `[0, 1), [5, 6)`),
Entry("a ^ b", "^", `[0, 1), [2, 4), [5, 6)`),
)
It("should find extents", func() {
a := NewIntervalSet(RightOpen(1, 10), RightOpen(20, 30), RightOpen(40, 50))
Ω(a.Extent().Equals(RightOpen(1, 50))).Should(BeTrue())
a = NewIntervalSet(Below(50))
extent := a.Extent()
Ω(extent.Equals(Below(50))).Should(BeTrue(), "%s != %s", a.Extent(), Below(50))
a = NewIntervalSet(Above(50))
Ω(a.Extent().Equals(Above(50))).Should(BeTrue(), "%s != %s", a.Extent(), Above(50))
a = NewIntervalSet(Empty())
Ω(a.Extent().IsEmpty()).Should(BeTrue())
a = NewIntervalSet(Unbounded())
Ω(a.Extent().Equals(Unbounded())).Should(BeTrue())
})
It("should report equality correctly", func() {
Ω(a.Equals(b)).Should(BeFalse())
a.Union(a, b)
b.Union(b, a)
Ω(a.Equals(b)).Should(BeTrue())
})
It("should find the complement correctly", func() {
Ω(a.Complement(a).String()).Should(Equal(`(-∞, 0), [2, 4), [6, ∞)`))
})
It("should find containing intervals", func() {
ival := a.IntervalContaining(1)
Ω(ival.Equals(RightOpen(0, 2))).Should(BeTrue())
})
It("should return the interval containing a point", func() {
// (-∞, 2) (2, 4] [5, 10] (15, 17), (17, ∞)
ivals := []Interval{
Empty(),
Below(2),
LeftOpen(2, 4),
Closed(5, 10),
Open(15, 17),
Above(17),
}
a = NewIntervalSet(ivals...)
expected := []int{1, 1, 0, 2, 2, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 0, 4, 0, 5, 5, 5}
for i := 0; i < len(expected); i++ {
actual := a.IntervalContaining(uint64(i))
exp := ivals[expected[i]]
Ω(actual.Equals(exp)).Should(BeTrue(), fmt.Sprintf("%s != %s", actual, exp))
}
// Get rid of the unbounded sides to test bounded tries
a.Intersection(a, NewIntervalSet(Closed(1, 19)))
// Set new expectations
expected[0] = 0
expected[20] = 0
ivals[1] = RightOpen(1, 2)
ivals[5] = LeftOpen(17, 19)
for i := 0; i < len(expected); i++ {
actual := a.IntervalContaining(uint64(i))
exp := ivals[expected[i]]
Ω(actual.Equals(exp)).Should(BeTrue(), fmt.Sprintf("%s != %s", actual, exp))
}
})
It("should return the rough number of intervals", func() {
ivals := []Interval{
Empty(),
Below(2),
LeftOpen(2, 4),
Closed(5, 10),
Open(15, 17),
Above(17),
}
for i := 1; i < len(ivals); i++ {
a = NewIntervalSet(ivals[:i]...)
Ω(a.Cardinality()).Should(BeNumerically("==", i-1))
}
})
It("should intersect with (0, inf) correctly", func() {
ivals := []Interval{
MustParseIntervalString("[1581228000, 1581400800)"),
MustParseIntervalString("[1581746400, 1582005600)"),
MustParseIntervalString("[1582351200, 1582610400)"),
MustParseIntervalString("[1582956000, 1583128800)"),
}
a = NewIntervalSet(ivals...)
b = Above(0).AsIntervalSet()
c := (&IntervalSet{}).Intersection(a, b)
/*
aold := temporalset.NewIntervalSet(
interval.RightOpen(1581228000, 1581400800),
interval.RightOpen(1581746400, 1582005600),
interval.RightOpen(1582351200, 1582610400),
interval.RightOpen(1582956000, 1583128800),
)
bold := temporalset.NewIntervalSet(interval.Above(0))
aold.Intersection(bold)
*/
Ω(c.Equals(a)).Should(BeTrue())
})
})