-
Notifications
You must be signed in to change notification settings - Fork 2
/
rsortip_test.go
103 lines (92 loc) · 2.55 KB
/
rsortip_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
package main
import (
"encoding/binary"
"fmt"
"log"
"slices"
"testing"
)
func timemyrsortip(lns lines) {
a := fmt.Sprintf("rsortip %d", len(lns))
defer timer(a)()
rsortip(lns, 0)
}
func Test_rsortip(t *testing.T) {
ls := []int{1 << 5}
ns := []int{1 << 3, 1 << 4, 1 << 5, 1 << 6, 1 << 16, 1 << 20, 1 << 24}
for _, ll := range ls {
for _, nl := range ns {
var lns lines
var l int = ll
var r bool = true
log.Print("testing rsortip of ", nl, " random strings length ", l)
rsl := randomstrings(nl, l, r)
if len(rsl) != int(nl) {
log.Fatal("rsortip test rsl: wanted len ", nl, " got ", len(rsl))
}
// log.Print("strings generated")
for _, s := range rsl {
bln := []byte(s)
lns = append(lns, bln)
}
if len(lns) != int(nl) {
// log.Print(lns)
log.Fatal("rsortip test lns: before rsortip wanted len ", nl, " got ", len(lns))
}
// log.Print("strings converted to bytes")
slns := rsortip(lns, 0)
if len(slns) != int(nl) {
//log.Print(ulns)
log.Fatal("rsortip test ulns: after rsortip wanted len ", nl, " got ", len(slns))
}
// log.Print("byte strings sorted")
var ssl []string
for _, s := range slns {
ssl = append(ssl, string(s))
}
// log.Print("byte strings converted to strings")
if !slices.IsSorted(ssl) {
for i, _ := range ssl {
// log.Println(string(slns[i]))
log.Println(ssl[i])
}
log.Fatal("rsortip not sorted for size ", nl)
} else {
log.Print("rsortip test passed for ", nl)
}
log.Print("string rsortip comparison")
timemyrsortip(lns)
timeslicessort(rsl)
log.Print("testing rsortip of ", nl, " random uints")
lns = randomuintb(nl)
if len(lns) != int(nl) {
log.Fatal("rsortip test rui: wanted len ", nl, " got ", len(lns))
}
// log.Print("uint64 byte strings generated")
slns = rsortip(lns, 0)
if len(slns) != int(nl) {
//log.Print(ulns)
log.Fatal("rsortip test ulns: after rsortip wanted len ", nl, " got ", len(slns))
}
// log.Print("uint64 byte strings sorted")
var ulns []uint64
for _, s := range slns {
ui := binary.BigEndian.Uint64(s)
ulns = append(ulns, ui)
}
// log.Print("uint64 byte strings converted to uint64")
if len(ulns) != int(nl) {
//log.Print(ssl)
log.Fatal("rsortip test ssl: wanted len ", nl, " got ", len(ulns))
}
if !slices.IsSorted(ulns) {
log.Fatal("rsortip failed for size ", nl)
} else {
log.Print("rsortip test passed for ", nl)
}
log.Print("uint64 rsortip comparison")
timemyrsortip(lns)
timeslicessort(ulns)
}
}
}