-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrampart_test.go
206 lines (191 loc) · 6.55 KB
/
rampart_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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package rampart
import (
"runtime"
"testing"
"time"
)
func TestTimeInterval(t *testing.T) {
now := time.Now()
a := NewIntervalFunc(now.Add(1000), now, func(t1, t2 time.Time) int { return int(t1.Sub(t2)) })
requireEqual(t, now, a.Lesser())
}
func TestNewInterval(t *testing.T) {
t.Run("sorts the tuple", func(t *testing.T) {
a := NewInterval("a", "b")
b := NewInterval("b", "a")
requireEqual(t, a.x, b.x)
requireEqual(t, a.y, b.y)
})
}
func TestLesser(t *testing.T) {
t.Run("returns the lesser element", func(t *testing.T) {
requireEqual(t, "a", NewInterval("a", "b").Lesser())
})
}
func TestGreater(t *testing.T) {
t.Run("returns the greater element", func(t *testing.T) {
requireEqual(t, "b", NewInterval("a", "b").Greater())
})
}
func TestIsEmpty(t *testing.T) {
t.Run("returns true when the interval is empty", func(t *testing.T) {
requireEqual(t, true, NewInterval("a", "a").IsEmpty())
})
t.Run("returns false when the interval is non-empty", func(t *testing.T) {
requireEqual(t, false, NewInterval("a", "b").IsEmpty())
})
}
func TestIsNonEmpty(t *testing.T) {
t.Run("returns false when the interval is empty", func(t *testing.T) {
requireEqual(t, false, NewInterval("a", "a").IsNonEmpty())
})
t.Run("returns true when the interval is non-empty", func(t *testing.T) {
requireEqual(t, true, NewInterval("a", "b").IsNonEmpty())
})
}
func relate(x1, y1 int) func(int, int) Relation {
return func(x2, y2 int) Relation {
return NewInterval(x1, y1).Relate(NewInterval(x2, y2))
}
}
func TestRelate(t *testing.T) {
t.Run("identifies the before relation", func(t *testing.T) {
requireEqual(t, RelationBefore, relate(1, 2)(3, 7))
})
t.Run("identifies the meets relation", func(t *testing.T) {
requireEqual(t, RelationMeets, relate(2, 3)(3, 7))
})
t.Run("identifies the overlaps relation", func(t *testing.T) {
requireEqual(t, RelationOverlaps, relate(2, 4)(3, 7))
})
t.Run("identifies the finished by relation", func(t *testing.T) {
requireEqual(t, RelationFinishedBy, relate(2, 7)(3, 7))
})
t.Run("identifies the contains relation", func(t *testing.T) {
requireEqual(t, RelationContains, relate(2, 8)(3, 7))
})
t.Run("identifies the starts relation", func(t *testing.T) {
requireEqual(t, RelationStarts, relate(3, 4)(3, 7))
})
t.Run("identifies the equal relation", func(t *testing.T) {
requireEqual(t, RelationEqual, relate(3, 7)(3, 7))
})
t.Run("identifies the started by relation", func(t *testing.T) {
requireEqual(t, RelationStartedBy, relate(3, 8)(3, 7))
})
t.Run("identifies the during relation", func(t *testing.T) {
requireEqual(t, RelationDuring, relate(4, 6)(3, 7))
})
t.Run("identifies the finishes relation", func(t *testing.T) {
requireEqual(t, RelationFinishes, relate(6, 7)(3, 7))
})
t.Run("identifies the overlapped by relation", func(t *testing.T) {
requireEqual(t, RelationOverlappedBy, relate(6, 8)(3, 7))
})
t.Run("identifies the met by relation", func(t *testing.T) {
requireEqual(t, RelationMetBy, relate(7, 8)(3, 7))
})
t.Run("identifies the after relation", func(t *testing.T) {
requireEqual(t, RelationAfter, relate(8, 9)(3, 7))
})
t.Run("empty left interval", func(t *testing.T) {
t.Run("before", func(t *testing.T) {
requireEqual(t, RelationBefore, relate(2, 2)(3, 7))
})
t.Run("at lesser", func(t *testing.T) {
requireEqual(t, RelationOverlaps, relate(3, 3)(3, 7))
})
t.Run("during", func(t *testing.T) {
requireEqual(t, RelationDuring, relate(5, 5)(3, 7))
})
t.Run("at greater", func(t *testing.T) {
requireEqual(t, RelationOverlappedBy, relate(7, 7)(3, 7))
})
t.Run("after", func(t *testing.T) {
requireEqual(t, RelationAfter, relate(8, 8)(3, 7))
})
})
t.Run("empty right interval", func(t *testing.T) {
t.Run("before", func(t *testing.T) {
requireEqual(t, RelationAfter, relate(3, 7)(2, 2))
})
t.Run("at lesser", func(t *testing.T) {
requireEqual(t, RelationOverlappedBy, relate(3, 7)(3, 3))
})
t.Run("during", func(t *testing.T) {
requireEqual(t, RelationContains, relate(3, 7)(5, 5))
})
t.Run("at greater", func(t *testing.T) {
requireEqual(t, RelationOverlaps, relate(3, 7)(7, 7))
})
t.Run("after", func(t *testing.T) {
requireEqual(t, RelationBefore, relate(3, 7)(8, 8))
})
})
t.Run("both empty intervals", func(t *testing.T) {
t.Run("before", func(t *testing.T) {
requireEqual(t, RelationBefore, relate(4, 4)(5, 5))
})
t.Run("equal", func(t *testing.T) {
requireEqual(t, RelationEqual, relate(5, 5)(5, 5))
})
t.Run("after", func(t *testing.T) {
requireEqual(t, RelationAfter, relate(6, 6)(5, 5))
})
})
}
func TestInvert(t *testing.T) {
t.Run("inverts the after relation", func(t *testing.T) {
requireEqual(t, RelationBefore, RelationAfter.Invert())
})
t.Run("inverts the before relation", func(t *testing.T) {
requireEqual(t, RelationAfter, RelationBefore.Invert())
})
t.Run("inverts the contains relation", func(t *testing.T) {
requireEqual(t, RelationDuring, RelationContains.Invert())
})
t.Run("inverts the during relation", func(t *testing.T) {
requireEqual(t, RelationContains, RelationDuring.Invert())
})
t.Run("inverts the equal relation", func(t *testing.T) {
requireEqual(t, RelationEqual, RelationEqual.Invert())
})
t.Run("inverts the finished by relation", func(t *testing.T) {
requireEqual(t, RelationFinishes, RelationFinishedBy.Invert())
})
t.Run("inverts the finishes relation", func(t *testing.T) {
requireEqual(t, RelationFinishedBy, RelationFinishes.Invert())
})
t.Run("inverts the meets relation", func(t *testing.T) {
requireEqual(t, RelationMetBy, RelationMeets.Invert())
})
t.Run("inverts the met by relation", func(t *testing.T) {
requireEqual(t, RelationMeets, RelationMetBy.Invert())
})
t.Run("inverts the overlapped by relation", func(t *testing.T) {
requireEqual(t, RelationOverlaps, RelationOverlappedBy.Invert())
})
t.Run("inverts the overlaps relation", func(t *testing.T) {
requireEqual(t, RelationOverlappedBy, RelationOverlaps.Invert())
})
t.Run("inverts the started by relation", func(t *testing.T) {
requireEqual(t, RelationStarts, RelationStartedBy.Invert())
})
t.Run("inverts the starts relation", func(t *testing.T) {
requireEqual(t, RelationStartedBy, RelationStarts.Invert())
})
t.Run("inverts an invalid relation", func(t *testing.T) {
requireEqual(t, RelationUnknown, Relation(42).Invert())
})
}
func requireEqual(t *testing.T, expected, actual any) {
_, file, line, ok := runtime.Caller(1)
if !ok {
return
}
if actual != expected {
t.Errorf("\nTrace:\t%s:%d"+
"\nError:\texpected %v, got %v"+
"\nTest:\t%s", file, line, actual, expected, t.Name())
}
}