-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreap_test.go
90 lines (74 loc) · 1.6 KB
/
treap_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
// Copyright 2013 Shayan Pooya. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import (
"sort"
"testing"
)
const (
count = 30000
delNum = 5000
)
func TestBasic1(t *testing.T) {
list := make([]int, 0)
treap := &Treap{}
for i := 0; i < count; i++ {
k := i
addIt := true
for _, j := range list {
if j == k {
addIt = false
break
}
}
if addIt {
treap.Insert(k)
list = append(list, k)
}
}
uniqeElements := len(list)
traverse := treap.Traverse1()
if !sort.IntsAreSorted(traverse) {
t.Error("not sorted", traverse)
}
traverse = treap.Traverse2()
if !sort.IntsAreSorted(traverse) {
t.Error("not sorted", traverse)
}
traverse = treap.Traverse3()
if !sort.IntsAreSorted(traverse) {
t.Error("not sorted", traverse)
}
index := 0
for i := 0; i < delNum; i++ {
key := list[index]
if !sort.IntsAreSorted(treap.Traverse3()) {
t.Error("Traverse3 is not sorted.")
}
removed := treap.Remove(key)
if !removed {
t.Error("Could not remove key: ", key, " with index ", index)
return
}
index += uniqeElements / delNum
if index >= uniqeElements {
break
}
}
}
func TestFind(t *testing.T) {
treap := &Treap{}
for i := 0; i < count; i++ {
treap.Insert(i)
}
if !sort.IntsAreSorted(treap.Traverse2()) {
t.Error("not sorted")
}
for i := 0; i < count; i++ {
foundNode := treap.Find(i)
if foundNode == nil || foundNode.value != i {
t.Error("Could not find ", i)
}
}
}