-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil_test.go
More file actions
177 lines (147 loc) · 4.25 KB
/
Copy pathutil_test.go
File metadata and controls
177 lines (147 loc) · 4.25 KB
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
//
package main
import (
"bufio"
"encoding/json"
"fmt"
"log"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
"golang.org/x/exp/rand"
)
func TestUtil(t *testing.T) {
a, b := movePerfsToArtist("a", "b [c]")
assert.Equal(t, a, "a c")
assert.Equal(t, b, "b")
ints := []int{1, 2, 3, 4, 5}
assert.Equal(
t,
*remove(&ints, 3),
[]int{1, 2, 5, 4},
)
assert.Equal(
t,
*remove(&[]string{"1", "2", "5", "4"}, "3"),
[]string{"1", "2", "5", "4"},
)
// assert.Equal(
// t,
// strings.Join([]string{"1", "2", "5", "4"}, "\n")+"\n",
// "1\n2\n5\n4\n",
// )
albums := []string{"a (1990)", "b (1989)", "c (1988)"}
sortByYear(albums)
assert.Equal(t, albums, []string{"c (1988)", "b (1989)", "a (1990)"})
// assert.Equal(
// t,
// &ints,
// &removed,
// )
strData := map[string]any{"1": "1"}
intData := map[string]any{"1": 1}
strBytes, _ := json.Marshal(strData)
intBytes, _ := json.Marshal(intData)
assert.NotEqual(t, strBytes, intBytes)
assert.False(t, anyValue(map[int]bool{1: false, 2: false}))
assert.False(t, anyValue(map[int]bool{1: false}))
assert.False(t, anyValue(map[int]bool{}))
assert.True(t, anyValue(map[int]bool{1: true, 2: false}))
assert.True(t, anyValue(map[int]bool{1: true}))
}
func BenchmarkReadfile(b *testing.B) {
// https://stackoverflow.com/a/16615559
by, err := os.ReadFile(config.Library.Queue)
if err != nil {
log.Fatalln(err)
}
strings.Split(string(by), "\n")
}
func BenchmarkNewScanner(b *testing.B) {
// https://stackoverflow.com/a/16615559
var relpaths []string
file, err := os.Open(config.Library.Queue)
if err != nil {
log.Fatalln(err)
}
defer file.Close()
sc := bufio.NewScanner(file)
for sc.Scan() {
relpaths = append(relpaths, sc.Text())
}
if sc.Err() != nil {
log.Fatalln(err)
}
_ = relpaths
}
var (
randStrings = make([]string, 10000)
sameStrings = make([]string, 10000)
artists []string
)
func init() {
x := rand.New(rand.NewSource(1))
_ = x
// tl;dr: bigrams are exceptional at random inputs, but still great at
// real world inputs
// BenchmarkSubstringSearchRKLower-12 1000000000 0.0008277 ns/op
// BenchmarkSubstringSearchBigram-12 1000000000 0.0000006 ns/op
// for i := range len(randStrings) {
// var s []rune
// for range 10 {
// s = append(s, rune(65+x.Intn(57)))
// }
// randStrings[i] = string(s)
// }
// Bigrams = makeBigrams(randStrings)
// BenchmarkSubstringSearchRKLower-12 1000000000 0.002818 ns/op
// BenchmarkSubstringSearchBigram-12 1000000000 0.0000051 ns/op
var s []rune
for range 100 {
s = append(s, rune(65+x.Intn(57)))
}
for i := range len(sameStrings) {
sameStrings[i] = string(s)
}
Bigrams = makeBigrams(sameStrings)
// BenchmarkSubstringSearchRKLower-12 1000000000 0.002496 ns/op
// BenchmarkSubstringSearchBigram-12 1000000000 0.0001683 ns/op
// artists, _ = descend(config.Library.Root)
// Bigrams = makeBigrams(artists)
}
func benchmarkWrapper(f func([]string, string) []int) {
// f(randStrings, "ab")
f(sameStrings, "jp")
// f(artists, "johann")
}
func BenchmarkSubstringSearchRKLower(b *testing.B) { benchmarkWrapper(searchSubstring) }
func BenchmarkSubstringSearchBigram(b *testing.B) { benchmarkWrapper(searchSubstringBigram) }
func TestSubstring(t *testing.T) {
fmt.Println(sameStrings[0])
for _, x := range []struct {
needle string
count int
countB int
}{
{needle: "jp", count: 10000, countB: 10000},
{needle: "xoba", count: 0, countB: 10000}, // fuzzy!
} {
assert.Len(t, searchSubstring(sameStrings, x.needle), x.count, x.needle)
assert.Len(t, searchSubstringBigram(sameStrings, x.needle), x.countB, x.needle)
}
// assert.Len(t, searchSubstringBigram([]string{"qua", "qub", "aqu"}, "qu"), 3)
}
func TestSubstringDot(t *testing.T) {
items := []string{"aac", "abc", "acc"}
assert.Len(t, searchSubstringBigram(items, "a.c"), len(items))
}
func TestSubstringQu(t *testing.T) {
// WARN: need to overwrite global var (probably code smell)
Bigrams = makeBigrams([]string{"qua", "qub", "aqu"})
assert.Len(t, searchSubstringBigram([]string{"qua", "qub", "aqu"}, "qu"), 3)
}
// func BenchmarkWalk(b *testing.B) { generateAlloc(100) }
// // 5.6 s cold, 2.5 s warm
// // TODO: mark slow test
// func TestWalkAlloc(t *testing.T) { generateQueue(100) }