Repository navigation
Expand file tree
/
Copy pathheight_interval_test.go
More file actions
113 lines (101 loc) · 2.55 KB
/
Copy pathheight_interval_test.go
File metadata and controls
113 lines (101 loc) · 2.55 KB
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
package height_interval_test
import (
"testing"
hi "github.com/alexes.dev/height_interval"
)
// ── helpers ──────────────────────────────────────────────────────────────────
func iv(e, l int64) hi.Interval { return hi.Interval{Earliest: e, Latest: l} }
func iq(pairs ...int64) hi.IntervalQueue {
q := hi.IntervalQueue{}
for i := 0; i+1 < len(pairs); i += 2 {
q = append(q, iv(pairs[i], pairs[i+1]))
}
return q
}
func eqIQ(a, b hi.IntervalQueue) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}
// ── Interval basics ───────────────────────────────────────────────────────────
func TestSize(t *testing.T) {
if iv(1, 0).Size() != -1 {
t.Fatal("size of [1,0)")
}
if iv(1, 1).Size() != 0 {
t.Fatal("size of [1,1)")
}
if iv(1, 2).Size() != 1 {
t.Fatal("size of [1,2)")
}
}
func TestValid(t *testing.T) {
if iv(1, 0).Valid() {
t.Fatal("[1,0) should be invalid")
}
if iv(1, 1).Valid() {
t.Fatal("[1,1) should be invalid")
}
if !iv(1, 2).Valid() {
t.Fatal("[1,2) should be valid")
}
}
func TestContains(t *testing.T) {
cases := []struct {
a, b hi.Interval
want bool
}{
{iv(1, 2), iv(3, 4), false},
{iv(1, 5), iv(3, 6), false},
{iv(2, 5), iv(1, 5), false},
{iv(3, 5), iv(1, 3), false},
{iv(1, 5), iv(1, 5), true},
}
for _, c := range cases {
if got := c.a.Contains(c.b); got != c.want {
t.Errorf("Contains(%v, %v) = %v, want %v", c.a, c.b, got, c.want)
}
}
}
func TestOverlaps(t *testing.T) {
cases := []struct {
a, b hi.Interval
want bool
}{
{iv(1, 2), iv(3, 4), false},
{iv(1, 4), iv(2, 3), true},
{iv(2, 3), iv(1, 4), true},
{iv(1, 3), iv(2, 4), true},
{iv(2, 4), iv(1, 3), true},
}
for _, c := range cases {
a := c.a
if got := a.Overlaped(c.b); got != c.want {
t.Errorf("Overlaped(%v, %v) = %v, want %v", c.a, c.b, got, c.want)
}
}
}
func TestChunks(t *testing.T) {
a := iv(1, 0)
if chunks := a.Chunks(1); len(chunks) != 0 {
t.Fatal("invalid interval chunks not empty")
}
b := iv(1, 2)
if chunks := b.Chunks(1); !eqIQ(chunks, iq(1, 2)) {
t.Fatalf("got %v", chunks)
}
c := iv(1, 3)
if chunks := c.Chunks(1); !eqIQ(chunks, iq(1, 2, 2, 3)) {
t.Fatalf("got %v", chunks)
}
d := iv(1, 3)
if chunks := d.Chunks(10); !eqIQ(chunks, iq(1, 3)) {
t.Fatalf("got %v", chunks)
}
}