-
Notifications
You must be signed in to change notification settings - Fork 1
/
sortlist_test.go
122 lines (113 loc) · 2.17 KB
/
sortlist_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
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
114
115
116
117
118
119
120
121
122
package gosolist
import (
"fmt"
"math/rand"
"sort"
"testing"
"time"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
func TestList(t *testing.T) {
l := NewSortedList(IntCompare, 2000)
for i := 1; i < 60000; i++ {
a := rand.Int() % 10000000
if !l.Has(a) {
l.Push(a)
}
if rand.Int()%100 == 0 {
temp := l.At(rand.Int() % l.Size())
if !l.Has(temp) {
t.Log("function 'has' is not correct")
t.Fail()
}
if rand.Int()%10 == 9 {
if !l.DeleteItem(temp) {
t.Log("function deleteItem is not correct")
t.Fail()
}
if l.Has(temp) {
t.Log("function has is not correct")
t.Fail()
}
}
}
}
l.Delete(0)
l.Delete(1)
l.Delete(l.Size())
l.Delete(l.Size() - 1)
t.Log(l.Size())
}
func TestFloor(t *testing.T) {
l := NewSortedList(IntCompare, 2)
var a []int
for i := 0; i < 20; i++ {
r := rand.Int() % 10000
a = append(a, r)
l.Push(r)
}
sort.Ints(a)
if a[3] != l.At(3) {
t.Fail()
}
fmt.Println(a, l.Values())
fmt.Println(l.Floor(a[1] + 1))
fmt.Println(l.Floor(a[0]), l.Floor(a[0]-1), l.Ceil(a[0]-1))
fmt.Println(l.Ceil(a[9]), l.Ceil(a[9]+1), l.Floor(a[9]+1))
}
func TestIndex(t *testing.T) {
l := NewSortedList(IntCompare, 3)
var a []int
for i := 0; i < 50; i++ {
r := rand.Int() % 10000
a = append(a, r)
l.Push(r)
}
sort.Ints(a)
fmt.Println(a)
for i := 0; i < len(a); i++ {
fmt.Println(l.Index(a[i] + 1))
fmt.Println(l.Index(a[i]))
}
}
func TestDelete(t *testing.T) {
l := NewSortedList(IntCompare, 100)
l.Push(1)
l.Push(5)
fmt.Println(l.Top(), l.Bottom())
l.Push(9)
fmt.Println(l.DeleteItem(9))
l.Each(PrintEach)
l.Clear()
fmt.Println(l.Empty())
}
func BenchmarkPush(b *testing.B) {
l := NewSortedList(IntCompare, 2000)
for n := 0; n < b.N; n++ {
l.Push(n + n/2)
}
}
func BenchmarkIndex(b *testing.B) {
l := NewSortedList(IntCompare, 2000)
for n := 0; n < 1000000; n++ {
l.Push(rand.Int() % 100000)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
l.Index(n)
}
}
func BenchmarkDelete(b *testing.B) {
l := NewSortedList(IntCompare, 2000)
for n := 0; n < 1000000; n++ {
l.Push(rand.Int() % 100000)
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
if l.Size() > 0 {
l.DeleteItem(n)
}
}
}