11package timeutils
22
33import (
4+ "fmt"
45 "math/rand"
6+ "strings"
57 "testing"
68 "time"
79)
@@ -32,6 +34,17 @@ func TestNewIntervalPanic(t *testing.T) {
3234 _ = NewInterval (start , end )
3335}
3436
37+ func TestIntervalString (t * testing.T ) {
38+ start := time .Now ()
39+ end := start .Add (time .Hour )
40+
41+ i := NewInterval (start , end )
42+
43+ if i .String () != fmt .Sprintf (stringFormat , i .Start .Format (time .RFC3339 ), i .End .Format (time .RFC3339 ), i .Duration ()) {
44+ t .Errorf ("Interval.String() output was not as expected" )
45+ }
46+ }
47+
3548func TestIntervalInclude (t * testing.T ) {
3649 start := time .Now ()
3750 end := start .Add (time .Hour )
@@ -255,6 +268,94 @@ func TestIntervalOverlap(t *testing.T) {
255268 }
256269}
257270
271+ func TestIntervalContiguous (t * testing.T ) {
272+ start := time .Now ()
273+ end := start .Add (time .Hour )
274+
275+ i := NewInterval (start , end )
276+
277+ tests := []struct {
278+ name string
279+ input Interval
280+ expected bool
281+ }{
282+ {
283+ name : `Input equals interval` ,
284+ input : NewInterval (start , end ),
285+ expected : false ,
286+ },
287+ {
288+ name : `Input before interval` ,
289+ input : NewInterval (start .Add (- time .Hour ), start .Add (- time .Second )),
290+ expected : false ,
291+ },
292+ {
293+ name : `Input after interval` ,
294+ input : NewInterval (end .Add (time .Second ), end .Add (time .Hour )),
295+ expected : false ,
296+ },
297+ {
298+ name : `Input's start negatively differs` ,
299+ input : NewInterval (start .Add (- time .Second ), end ),
300+ expected : false ,
301+ },
302+ {
303+ name : `Input's start positively differs` ,
304+ input : NewInterval (start .Add (time .Second ), end ),
305+ expected : false ,
306+ },
307+ {
308+ name : `Input's end negatively differs` ,
309+ input : NewInterval (start , end .Add (- time .Second )),
310+ expected : false ,
311+ },
312+ {
313+ name : `Input's start positively differs` ,
314+ input : NewInterval (start , end .Add (time .Second )),
315+ expected : false ,
316+ },
317+ {
318+ name : `Input's start before interval's start and end before interval's end` ,
319+ input : NewInterval (start .Add (- time .Second ), start .Add (time .Second )),
320+ expected : false ,
321+ },
322+ {
323+ name : `Input's end before interval's end and end after interval's end` ,
324+ input : NewInterval (end .Add (- time .Second ), end .Add (time .Second )),
325+ expected : false ,
326+ },
327+ {
328+ name : `Input is smaller` ,
329+ input : NewInterval (start .Add (time .Second ), end .Add (- time .Second )),
330+ expected : false ,
331+ },
332+ {
333+ name : `Input is bigger` ,
334+ input : NewInterval (start .Add (- time .Second ), end .Add (time .Second )),
335+ expected : false ,
336+ },
337+ {
338+ name : `Input is contiguous to interval's start` ,
339+ input : NewInterval (start .Add (- time .Second ), start ),
340+ expected : true ,
341+ },
342+ {
343+ name : `Input is contiguous to interval's end` ,
344+ input : NewInterval (end , end .Add (time .Second )),
345+ expected : true ,
346+ },
347+ }
348+
349+ for _ , test := range tests {
350+ t .Run (test .name , func (t * testing.T ) {
351+ actual := i .Contiguous (test .input )
352+ if actual != test .expected {
353+ t .Errorf ("%s.Contiguous(%s) was expected to be %v, got %v" , i , test .input , test .expected , actual )
354+ }
355+ })
356+ }
357+ }
358+
258359func TestIntervalSub (t * testing.T ) {
259360 start := time .Now ()
260361 end := start .Add (time .Hour )
@@ -301,6 +402,20 @@ func TestIntervalSub(t *testing.T) {
301402 input : NewInterval (start , end .Add (time .Second )),
302403 expected : Intervals {},
303404 },
405+ {
406+ name : `Input's start before interval's start and end before interval's end` ,
407+ input : NewInterval (start .Add (- time .Second ), start .Add (time .Second )),
408+ expected : Intervals {
409+ NewInterval (start .Add (time .Second ), end ),
410+ },
411+ },
412+ {
413+ name : `Input's start before interval's end and end after interval's end` ,
414+ input : NewInterval (end .Add (- time .Second ), end .Add (time .Second )),
415+ expected : Intervals {
416+ NewInterval (start , end .Add (- time .Second )),
417+ },
418+ },
304419 {
305420 name : `Input is smaller` ,
306421 input : NewInterval (start .Add (time .Second ), end .Add (- time .Second )),
@@ -326,11 +441,39 @@ func TestIntervalSub(t *testing.T) {
326441 }
327442}
328443
444+ func TestIntervalsString (t * testing.T ) {
445+ start := time .Now ()
446+ end := start .Add (time .Hour )
447+
448+ is := Intervals {
449+ NewInterval (start , end ),
450+ NewInterval (start , end ),
451+ NewInterval (start , end .Add (- time .Second )),
452+ NewInterval (start , end .Add (+ time .Second )),
453+ NewInterval (start .Add (- time .Second ), end ),
454+ NewInterval (start .Add (+ time .Second ), end ),
455+ NewInterval (start .Add (- time .Second ), end .Add (- time .Second )),
456+ NewInterval (start .Add (+ time .Second ), end .Add (+ time .Second )),
457+ NewInterval (start .Add (- time .Second ), end .Add (+ time .Second )),
458+ NewInterval (start .Add (+ time .Second ), end .Add (- time .Second )),
459+ }
460+
461+ var expected []string
462+ for _ , i := range is {
463+ expected = append (expected , fmt .Sprintf (stringFormat , i .Start .Format (time .RFC3339 ), i .End .Format (time .RFC3339 ), i .Duration ()))
464+ }
465+
466+ if is .String () != fmt .Sprintf ("[%s]" , strings .Join (expected , ", " )) {
467+ t .Errorf ("Intervals.String() output was not as expected" )
468+ }
469+ }
470+
329471func TestIntervalsEqual (t * testing.T ) {
330472 start := time .Now ()
331473 end := start .Add (time .Hour )
332474
333475 is := Intervals {
476+ NewInterval (start , end ),
334477 NewInterval (start , end ),
335478 NewInterval (start , end .Add (- time .Second )),
336479 NewInterval (start , end .Add (+ time .Second )),
@@ -350,6 +493,7 @@ func TestIntervalsEqual(t *testing.T) {
350493 {
351494 name : `Input equals interval` ,
352495 input : Intervals {
496+ NewInterval (start , end ),
353497 NewInterval (start , end ),
354498 NewInterval (start , end .Add (- time .Second )),
355499 NewInterval (start , end .Add (+ time .Second )),
@@ -365,6 +509,7 @@ func TestIntervalsEqual(t *testing.T) {
365509 {
366510 name : `Randomly shuffled` ,
367511 input : Intervals {
512+ NewInterval (start , end ),
368513 NewInterval (start .Add (+ time .Second ), end .Add (+ time .Second )),
369514 NewInterval (start , end .Add (+ time .Second )),
370515 NewInterval (start , end ),
@@ -378,8 +523,9 @@ func TestIntervalsEqual(t *testing.T) {
378523 expected : true ,
379524 },
380525 {
381- name : `Randomly shuffled` ,
526+ name : `Randomly shuffled #2 ` ,
382527 input : Intervals {
528+ NewInterval (start , end ),
383529 NewInterval (start , end ),
384530 NewInterval (start , end .Add (- time .Minute )),
385531 NewInterval (start , end .Add (+ time .Minute )),
0 commit comments