-
Notifications
You must be signed in to change notification settings - Fork 2
/
sup_test.go
67 lines (60 loc) · 1.38 KB
/
sup_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
package splaytree
import "testing"
import "math/rand"
import "time"
func TestSupremum(t *testing.T) {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
perm := func(upper int) []Item {
nums := rand.Perm(upper)
items := make([]Item, upper)
for i := 0; i < upper; i++ {
items[i] = Int(nums[i])
}
return items
}
tree := NewSplayTree()
if tree.Supremum(Int(-1)) != nil {
t.Errorf("supremum -1")
}
for i := 1; i < 12; i++ {
tree = NewSplayTree()
tree.InsertAll(perm(i))
for k := -1; k <= i; k++ {
sup := tree.Supremum(Int(k))
if sup == nil && i > 0 && k+1 < i {
t.Errorf("supremum %v %v nil", i, k)
}
if sup != nil && sup.(Int) != Int(k+1) {
t.Errorf("supremum %v %v = %v", i, k, sup)
}
}
}
}
func TestInfimum(t *testing.T) {
rand := rand.New(rand.NewSource(time.Now().UnixNano()))
perm := func(upper int) []Item {
nums := rand.Perm(upper)
items := make([]Item, upper)
for i := 0; i < upper; i++ {
items[i] = Int(nums[i])
}
return items
}
tree := NewSplayTree()
if tree.Infimum(Int(-1)) != nil {
t.Errorf("infimum -1")
}
for i := 1; i < 12; i++ {
tree = NewSplayTree()
tree.InsertAll(perm(i))
for k := -1; k <= i; k++ {
inf := tree.Infimum(Int(k))
if inf == nil && k > 0 {
t.Errorf("infimum %v %v nil", i, k)
}
if inf != nil && inf.(Int) != Int(k-1) {
t.Errorf("infimum %v %v = %v", i, k, inf)
}
}
}
}