-
Notifications
You must be signed in to change notification settings - Fork 13
/
timsortint_test.go
104 lines (82 loc) · 1.5 KB
/
timsortint_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
package timsort
import (
"fmt"
"math/rand"
"testing"
)
func makeTestArrayI(size int) []int {
a := make([]int, size)
for i := 0; i < size; i++ {
a[i] = i & 0xeeeeee
}
return a
}
func IsSortedI(a []int, lessThan IntLessThan) bool {
len := len(a)
if len < 2 {
return true
}
prev := a[0]
for i := 1; i < len; i++ {
if lessThan(a[i], prev) {
fmt.Println("false")
return false
}
}
return true
}
// use this comparator for sorting
func intLessThan(a, b int) bool {
return a < b
}
func TestSmokeI(t *testing.T) {
a := []int{3, 1, 2}
err := Ints(a, intLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSortedI(a, intLessThan) {
t.Error("not sorted")
}
}
func Test1KI(t *testing.T) {
a := makeTestArrayI(1024)
err := Ints(a, intLessThan)
if err != nil {
fmt.Printf("sort: %v\n", err)
t.Fatalf("sort: %v", err)
}
if !IsSortedI(a, intLessThan) {
t.Error("not sorted")
}
}
func Test1MI(t *testing.T) {
a := makeTestArrayI(1024 * 1024)
err := Ints(a, intLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSortedI(a, intLessThan) {
t.Error("not sorted")
}
}
func makeRandomArrayI(size int) []int {
a := make([]int, size)
for i := 0; i < size; i++ {
a[i] = rand.Intn(100)
}
return a
}
func TestRandom1MI(t *testing.T) {
size := 1024 * 1024
a := makeRandomArrayI(size)
b := make([]int, size)
copy(b, a)
err := Ints(a, intLessThan)
if err != nil {
t.Fatalf("sort: %v", err)
}
if !IsSortedI(a, intLessThan) {
t.Error("not sorted")
}
}