/
sort_test.go
220 lines (185 loc) · 5.49 KB
/
sort_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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
package main
import (
"io/ioutil"
"log"
"os"
"path"
"sort"
"testing"
)
func getDirectoryFiles(dirs []string) []*FileEntry {
var files []*FileEntry
for _, dir := range dirs {
if entries, err := ioutil.ReadDir(dir); err != nil {
log.Println("Could not read dir:", err)
} else {
for _, entry := range entries {
entrypath := path.Join(dir, entry.Name())
if !entry.IsDir() {
if fileinfo, err := os.Lstat(entrypath); err != nil {
log.Println("Could not read file:", err)
} else {
files = append(files, &FileEntry{
dir: dir,
name: fileinfo.Name(),
modtime: fileinfo.ModTime(),
size: fileinfo.Size(),
})
}
}
}
if len(files) < 2 {
log.Println("Not enough files in", dir)
}
}
}
return files
}
func TestSort(t *testing.T) {
files := getDirectoryFiles([]string{"/tmp"})
sort.Stable(SortedByName(files))
if !sort.IsSorted(SortedByName(files)) {
t.Error("Not sorted by name!")
}
sort.Stable(SortedByModTime(files))
if !sort.IsSorted(SortedByModTime(files)) {
t.Error("Not sorted by modtime!")
}
sort.Stable(SortedBySize(files))
if !sort.IsSorted(SortedBySize(files)) {
t.Error("Not sorted by size!")
}
log.Println("TestSort finished")
}
func BenchmarkSortedByName(b *testing.B) {
b.StopTimer()
files := getDirectoryFiles([]string{"/tmp"})
b.StartTimer()
for i := 0; i < b.N; i++ {
sort.Stable(SortedByName(files))
}
}
func BenchmarkSortedByDir(b *testing.B) {
b.StopTimer()
files := getDirectoryFiles([]string{"/tmp"})
b.StartTimer()
for i := 0; i < b.N; i++ {
sort.Stable(SortedByDir(files))
}
}
func BenchmarkSortedByModTime(b *testing.B) {
b.StopTimer()
files := getDirectoryFiles([]string{"/tmp"})
b.StartTimer()
for i := 0; i < b.N; i++ {
sort.Stable(SortedByModTime(files))
}
}
func BenchmarkSortedBySize(b *testing.B) {
b.StopTimer()
files := getDirectoryFiles([]string{"/tmp"})
b.StartTimer()
for i := 0; i < b.N; i++ {
sort.Stable(SortedBySize(files))
}
}
func TestSortMerge(t *testing.T) {
directories := []string{
os.Getenv("HOME") + "/go/src/golocate/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/gtk/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/glib/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/gdk/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/cairo/",
}
var allfiles, byname, bymodtime, bysize []*FileEntry
for _, dir := range directories {
files := getDirectoryFiles([]string{dir})
allfiles = append(allfiles, files...)
bynamefiles := make([]*FileEntry, len(files))
copy(bynamefiles, files)
sort.Stable(SortedByName(bynamefiles))
byname = sortMerge(SORT_BY_NAME, byname, bynamefiles)
if len(byname) < len(allfiles) {
t.Error("Result of sortMerge for SORT_BY_NAME contains less entries then its input", len(byname), len(allfiles))
}
bymodtimefiles := make([]*FileEntry, len(files))
copy(bymodtimefiles, files)
sort.Stable(SortedByModTime(bymodtimefiles))
bymodtime = sortMerge(SORT_BY_MODTIME, bymodtime, bymodtimefiles)
if len(bymodtime) < len(allfiles) {
t.Error("Result of sortMerge for SORT_BY_MODTIME contains less entries then its input", len(bymodtime), len(allfiles))
}
bysizefiles := make([]*FileEntry, len(files))
copy(bysizefiles, files)
sort.Stable(SortedBySize(bysizefiles))
bysize = sortMerge(SORT_BY_SIZE, bysize, bysizefiles)
if len(bysize) < len(allfiles) {
t.Error("Result of sortMerge for SORT_BY_SIZE contains less entries then its input", len(bysize), len(allfiles))
}
}
if !sort.IsSorted(SortedByName(byname)) {
log.Println("---- byname ----")
for i, entry := range byname {
log.Println(i, "\t\t", entry.name)
}
log.Println("---- sort.Sort(SortedByName(byname)) ----")
sort.Sort(SortedByName(allfiles))
for i, entry := range allfiles {
log.Println(i, "\t\t", entry.name)
}
t.Error("Not sorted by name after merging")
}
if !sort.IsSorted(SortedByModTime(bymodtime)) {
t.Error("Not sorted by modtime after merging")
}
if !sort.IsSorted(SortedBySize(bysize)) {
t.Error("Not sorted by size after merging")
}
log.Println("TestSortMerge finished")
}
func BenchmarkSortMerge(b *testing.B) {
directories := []string{
os.Getenv("HOME") + "/go/src/golocate/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/gtk/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/glib/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/gdk/",
os.Getenv("HOME") + "/go/src/golocate/vendor/github.com/gotk3/gotk3/cairo/",
}
benchmarks := []struct {
name string
sorting SortColumn
}{
{"ByName", SORT_BY_NAME},
{"ByDir", SORT_BY_DIR},
{"ByModTime", SORT_BY_MODTIME},
{"BySize", SORT_BY_SIZE},
}
for _, bm := range benchmarks {
var cache [][]*FileEntry
for _, dir := range directories {
files := getDirectoryFiles([]string{dir})
switch bm.sorting {
case SORT_BY_NAME:
sort.Stable(SortedByName(files))
cache = append(cache, files)
case SORT_BY_DIR:
sort.Stable(SortedByDir(files))
cache = append(cache, files)
case SORT_BY_MODTIME:
sort.Stable(SortedByModTime(files))
cache = append(cache, files)
case SORT_BY_SIZE:
sort.Stable(SortedBySize(files))
cache = append(cache, files)
}
}
b.Run(bm.name, func(b *testing.B) {
for i := 0; i < b.N; i++ {
var merged []*FileEntry
for _, files := range cache {
merged = sortMerge(bm.sorting, merged, files)
}
}
})
}
}