forked from SaidinWoT/timespan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimespan_test.go
428 lines (389 loc) · 11.1 KB
/
timespan_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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package timespan
import (
"testing"
"time"
)
var times = []time.Time{
time.Date(2014, time.February, 3, 2, 0, 0, 0, time.UTC),
time.Date(2014, time.February, 3, 4, 0, 0, 0, time.UTC),
time.Date(2014, time.February, 3, 6, 0, 0, 0, time.UTC),
time.Date(2014, time.February, 3, 8, 0, 0, 0, time.UTC),
}
var durations = []time.Duration{
time.Duration(2) * time.Hour,
time.Duration(4) * time.Hour,
time.Duration(6) * time.Hour,
time.Duration(-2) * time.Hour,
}
var spans = []Span{
New(times[0], durations[0]), // 2:00 - 4:00
New(times[0], durations[1]), // 2:00 - 6:00
New(times[0], durations[2]), // 2:00 - 8:00
New(times[1], durations[0]), // 4:00 - 6:00
New(times[1], durations[1]), // 4:00 - 8:00
New(times[2], durations[0]), // 6:00 - 8:00
New(times[1], durations[3]), // 2:00 - 4:00
}
func TestNew(t *testing.T) {
if spans[0].Start() != times[0] {
t.Error("Improper timespan start value.")
}
if spans[0].End() != times[1] {
t.Error("Improper timespan end value.")
}
if spans[6].Start() != times[0] {
t.Error("Improper timespan start value for negative duration.")
}
if spans[6].End() != times[1] {
t.Error("Improper timespan end value for negative duration.")
}
}
func TestSerialization(t *testing.T) {
json, err := spans[0].MarshalJSON()
if err != nil {
t.Error("Failed to marshall json:", err)
}
span := Span{}
if err := span.UnmarshalJSON(json); err != nil {
t.Error("Failed to unmarshal json:", err)
}
if span.Start() != times[0] {
t.Error("Deserialization failure: wrong start value:", span.Start())
}
if span.End() != times[1] {
t.Error("Deserialization failure: wrong end value:", span.End())
}
}
func TestAfter(t *testing.T) {
if spans[5].After(times[3]) { // 6:00 - 8:00 >? 8:00
t.Error("Span reported as after its end time.")
}
if spans[5].After(times[2]) { // 6:00 - 8:00 >? 6:00
t.Error("Span reported as after its start time.")
}
if !spans[5].After(times[1]) { // 6:00 - 8:00 >? 4:00
t.Error("Span reported as not after earlier time.")
}
}
func TestBefore(t *testing.T) {
if spans[0].Before(times[0]) { // 2:00 - 4:00 <? 2:00
t.Error("Span reported as before its start time.")
}
if spans[0].Before(times[1]) { // 2:00 - 4:00 <? 4:00
t.Error("Span reported as before its end time.")
}
if !spans[0].Before(times[2]) { // 2:00 - 4:00 <? 6:00
t.Error("Span reported as not before later time.")
}
}
func TestFollows(t *testing.T) {
if !spans[3].Follows(spans[0]) { // 4:00 - 6:00 >? 2:00 - 4:00
t.Error("Span reported as not following an earlier span.")
}
if spans[3].Follows(spans[1]) { // 4:00 - 6:00 >? 2:00 - 6:00
t.Error("Span reported as following a containing span ending at the same time.")
}
if spans[3].Follows(spans[3]) { // 4:00 - 6:00 >? 4:00 - 6:00
t.Error("Span reported as following itself.")
}
if spans[3].Follows(spans[4]) { // 4:00 - 6:00 >? 4:00 - 8:00
t.Error("Span reported as following a containing span ending at a later time.")
}
if spans[3].Follows(spans[5]) { // 4:00 - 6:00 >? 6:00 - 8:00
t.Error("Span reported as following a later span.")
}
if spans[2].Follows(spans[3]) { // 2:00 - 8:00 >? 4:00 - 6:00
t.Error("Span reported as following a fully contained span.")
}
if spans[4].Follows(spans[1]) { // 4:00 - 8:00 >? 2:00 - 6:00
t.Error("Span reported as following an overlapping span ending earlier.")
}
if spans[1].Follows(spans[4]) { // 2:00 - 6:00 >? 4:00 - 8:00
t.Error("Span reported as following an overlapping span ending later.")
}
}
func TestPrecedes(t *testing.T) {
if spans[3].Precedes(spans[0]) { // 4:00 - 6:00 <? 2:00 - 4:00
t.Error("Span reported as preceding an earlier span.")
}
if spans[3].Precedes(spans[1]) { // 4:00 - 6:00 <? 2:00 - 6:00
t.Error("Span reported as preceding a containing span starting earlier.")
}
if spans[3].Precedes(spans[3]) { // 4:00 - 6:00 <? 4:00 - 6:00
t.Error("Span reported as preceding itself.")
}
if spans[3].Precedes(spans[4]) { // 4:00 - 6:00 <? 4:00 - 8:00
t.Error("Span reported as preceding a containing span starting at the same time.")
}
if !spans[3].Precedes(spans[5]) { // 4:00 - 6:00 <? 6:00 - 8:00
t.Error("Span reported as not preceding a later span.")
}
if spans[2].Precedes(spans[3]) { // 2:00 - 8:00 <? 4:00 - 6:00
t.Error("Span reported as preceding a fully contained span.")
}
if spans[4].Precedes(spans[1]) { // 4:00 - 8:00 <? 2:00 - 6:00
t.Error("Span reported as preceding an overlapping span starting earlier.")
}
if spans[1].Precedes(spans[4]) { // 2:00 - 6:00 <? 4:00 - 8:00
t.Error("Span reported as preceding an overlapping span starting later.")
}
}
func TestEqual(t *testing.T) {
if !spans[3].Equal(spans[3]) { // 4:00 - 6:00 =? 4:00 - 6:00
t.Error("Span reported as not equal to itself.")
}
if spans[3].Equal(spans[2]) { // 4:00 - 6:00 =? 2:00 - 8:00
t.Error("Span reported as equal to a fully containing span.")
}
if spans[2].Equal(spans[3]) { // 2:00 - 8:00 =? 4:00 - 6:00
t.Error("Span reported as equal to a fully contained span.")
}
if spans[3].Equal(spans[1]) { // 4:00 - 6:00 <? 2:00 - 6:00
t.Error("Span reported as equal to span with different start time.")
}
if spans[3].Equal(spans[4]) { // 4:00 - 6:00 <? 4:00 - 8:00
t.Error("Span reported as equal to span with different end time.")
}
if spans[1].Equal(spans[4]) { // 2:00 - 6:00 =? 4:00 - 8:00
t.Error("Span reported as equal to overlapping span.")
}
if !spans[0].Equal(spans[6]) { // 2:00 - 4:00 =? 2:00 - 4:00
t.Error("Span reported as not equal.")
}
}
func TestBorders(t *testing.T) {
if spans[0].Borders(spans[0]) {
t.Error("Span borders itself.")
}
if spans[0].Borders(spans[1]) {
t.Error("Span borders an encompassing span.")
}
if spans[0].Borders(spans[5]) {
t.Error("Span borders a non-bordering separate span.")
}
if !spans[0].Borders(spans[3]) {
t.Error("Span does not border a bordering span.")
}
if !spans[3].Borders(spans[0]) {
t.Error("Span does not border a bordering span.")
}
}
func TestContainsTime(t *testing.T) {
if spans[4].ContainsTime(times[0]) {
t.Error("Span contains time preceding its start.")
}
if !spans[4].ContainsTime(times[1]) {
t.Error("Span does not contain start time.")
}
if !spans[4].ContainsTime(times[2]) {
t.Error("Span does not contain time in middle.")
}
if !spans[4].ContainsTime(times[3]) {
t.Error("Span does not contain end time.")
}
}
func TestContains(t *testing.T) {
if spans[4].Contains(spans[0]) {
t.Error("Span contains preceding span.")
}
if spans[4].Contains(spans[1]) {
t.Error("Span contains overlapping span.")
}
if !spans[4].Contains(spans[3]) {
t.Error("Span does not contain fully contained span.")
}
if !spans[4].Contains(spans[4]) {
t.Error("Span does not contain itself.")
}
if spans[1].Contains(spans[5]) {
t.Error("Span contains following span.")
}
}
func TestEncompass(t *testing.T) {
if spans[0].Encompass(spans[0]) != spans[0] {
t.Error("Span encompassing itself is not equal to identity.")
}
if spans[0].Encompass(spans[5]) != spans[2] {
t.Error("Span encompassing separate span does not contain both.")
}
if spans[0].Encompass(spans[1]) != spans[1] {
t.Error("Span encompassing an encompassing span is not equal to the encompassing span.")
}
if spans[2].Encompass(spans[3]) != spans[2] {
t.Error("Span encompassing a contained span is not equal to identity.")
}
}
func TestGap(t *testing.T) {
if !spans[0].Gap(spans[0]).IsZero() {
t.Error("Gap with self is not zero.")
}
if !spans[0].Gap(spans[1]).IsZero() {
t.Error("Gap with encompassing span is not zero.")
}
if spans[0].Gap(spans[5]) != spans[3] {
t.Error("Gap not properly generated.")
}
s := spans[0].Gap(spans[3])
if s.Start() != times[1] || s.End() != times[1] {
t.Error("Gap from bordering spans is not their border.")
}
}
func TestIntersection(t *testing.T) {
if _, b := spans[0].Intersection(spans[5]); b {
t.Error("Intersection of non-intersecting spans is not zero.")
}
if _, b := spans[0].Intersection(spans[3]); b {
t.Error("Intersection of bordering spans is not zero.")
}
if s, _ := spans[0].Intersection(spans[0]); s != spans[0] {
t.Error("Intersection with self is not identity.")
}
if s, _ := spans[0].Intersection(spans[2]); s != spans[0] {
t.Error("Intersection with encompassing span is not identity.")
}
if s, _ := spans[1].Intersection(spans[4]); s != spans[3] {
t.Error("Intersection improperly generated.")
}
}
func TestOffset(t *testing.T) {
if spans[0].Offset(durations[0]) != spans[3] {
t.Error("Offset created improper span.")
}
if spans[5].Offset(-durations[1]) != spans[0] {
t.Error("Negative offset created improper span.")
}
if spans[0].Offset(time.Duration(0)) != spans[0] {
t.Error("Zero offset does not result in identity.")
}
}
func TestOffsetDate(t *testing.T) {
s := New(times[0].AddDate(1, 1, 1), durations[0])
if spans[0].OffsetDate(1, 1, 1) != s {
t.Error("OffsetDate created improper span.")
}
s = New(times[0].AddDate(-1, -1, -1), durations[0])
if spans[0].OffsetDate(-1, -1, -1) != s {
t.Error("Negative OffsetDate created improper span.")
}
if spans[0].OffsetDate(0, 0, 0) != spans[0] {
t.Error("Zero OffsetDate does not result in identity.")
}
}
var (
d time.Duration
r Span
s Span
t time.Time
)
func BenchmarkStart(b *testing.B) {
s = spans[0]
for i := 0; i < b.N; i++ {
t = s.Start()
}
}
func BenchmarkEnd(b *testing.B) {
s = spans[0]
for i := 0; i < b.N; i++ {
t = s.End()
}
}
func BenchmarkDuration(b *testing.B) {
s = spans[0]
for i := 0; i < b.N; i++ {
d = s.Duration()
}
}
func BenchmarkAfter(b *testing.B) {
s = spans[5]
t = times[0]
for i := 0; i < b.N; i++ {
_ = s.After(t)
}
}
func BenchmarkBefore(b *testing.B) {
s = spans[5]
t = times[0]
for i := 0; i < b.N; i++ {
_ = s.Before(t)
}
}
func BenchmarkFollows(b *testing.B) {
s, r = spans[5], spans[0]
for i := 0; i < b.N; i++ {
_ = s.Follows(r)
}
}
func BenchmarkPrecedes(b *testing.B) {
s, r = spans[5], spans[0]
for i := 0; i < b.N; i++ {
_ = s.Precedes(r)
}
}
func BenchmarkContainsTime(b *testing.B) {
s = spans[2]
t = times[1]
for i := 0; i < b.N; i++ {
_ = s.ContainsTime(t)
}
}
func BenchmarkContains(b *testing.B) {
s, r = spans[2], spans[3]
for i := 0; i < b.N; i++ {
_ = s.Contains(r)
}
}
func BenchmarkEncompass(b *testing.B) {
s, r = spans[0], spans[5]
for i := 0; i < b.N; i++ {
_ = s.Encompass(r)
}
}
func BenchmarkGap(b *testing.B) {
s, r = spans[0], spans[5]
for i := 0; i < b.N; i++ {
_ = s.Gap(r)
}
}
func BenchmarkIntersection(b *testing.B) {
s, r = spans[1], spans[4]
for i := 0; i < b.N; i++ {
_, _ = s.Intersection(r)
}
}
func BenchmarkOffset(b *testing.B) {
s = spans[0]
d = durations[1]
for i := 0; i < b.N; i++ {
r = s.Offset(d)
}
}
func BenchmarkOffsetDate(b *testing.B) {
s = spans[0]
for i := 0; i < b.N; i++ {
r = s.OffsetDate(1, 1, 1)
}
}
func BenchmarkOverlaps(b *testing.B) {
s, r = spans[0], spans[1]
for i := 0; i < b.N; i++ {
_ = s.Overlaps(r)
}
}
func BenchmarkIsZero(b *testing.B) {
s = spans[0]
for i := 0; i < b.N; i++ {
_ = s.IsZero()
}
}
func BenchmarkEqual(b *testing.B) {
s, r = spans[0], spans[1]
for i := 0; i < b.N; i++ {
_ = s.Equal(r)
}
}
func BenchmarkBorders(b *testing.B) {
s, r = spans[0], spans[3]
for i := 0; i < b.N; i++ {
_ = s.Borders(r)
}
}