-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsweep-line_test.go
87 lines (64 loc) · 1.75 KB
/
sweep-line_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
package polygol
import "testing"
var comparator = func(a, b interface{}) int {
af := a.(float64)
bf := b.(float64)
if af == bf {
return 0
}
if af < bf {
return -1
}
return 1
}
func TestSweepLine(t *testing.T) {
t.Parallel()
// test filling up the tree then emptying it out
sl := newSweepLine(nil, comparator)
k1 := 4.0
k2 := 9.0
k3 := 13.0
k4 := 44.0
n1 := sl.tree.Insert(k1)
n2 := sl.tree.Insert(k2)
n4 := sl.tree.Insert(k4)
n3 := sl.tree.Insert(k3)
expect(t, sl.tree.Find(k1) == n1)
expect(t, sl.tree.Find(k2) == n2)
expect(t, sl.tree.Find(k3) == n3)
expect(t, sl.tree.Find(k4) == n4)
expect(t, sl.tree.Prev(n1) == nil)
expect(t, sl.tree.Next(n1).Item() == k2)
expect(t, sl.tree.Prev(n2).Item() == k1)
expect(t, sl.tree.Next(n2).Item() == k3)
expect(t, sl.tree.Prev(n3).Item() == k2)
expect(t, sl.tree.Next(n3).Item() == k4)
expect(t, sl.tree.Prev(n4).Item() == k3)
expect(t, sl.tree.Next(n4) == nil)
sl.tree.Remove(k2)
expect(t, sl.tree.Find(k2) == nil)
n1 = sl.tree.Find(k1)
n3 = sl.tree.Find(k3)
n4 = sl.tree.Find(k4)
expect(t, sl.tree.Prev(n1) == nil)
expect(t, sl.tree.Next(n1).Item() == k3)
expect(t, sl.tree.Prev(n3).Item() == k1)
expect(t, sl.tree.Next(n3).Item() == k4)
expect(t, sl.tree.Prev(n4).Item() == k3)
expect(t, sl.tree.Next(n4) == nil)
sl.tree.Remove(k4)
expect(t, sl.tree.Find(k4) == nil)
n1 = sl.tree.Find(k1)
n3 = sl.tree.Find(k3)
expect(t, sl.tree.Prev(n1) == nil)
expect(t, sl.tree.Next(n1).Item() == k3)
expect(t, sl.tree.Prev(n3).Item() == k1)
expect(t, sl.tree.Next(n3) == nil)
sl.tree.Remove(k1)
expect(t, sl.tree.Find(k1) == nil)
n3 = sl.tree.Find(k3)
expect(t, sl.tree.Prev(n3) == nil)
expect(t, sl.tree.Next(n3) == nil)
sl.tree.Remove(k3)
expect(t, sl.tree.Find(k3) == nil)
}