forked from k1LoW/runn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathid_test.go
98 lines (90 loc) · 2.34 KB
/
id_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
package runn
import (
"crypto/sha1" //#nosec G505
"encoding/base32"
"encoding/hex"
"io"
"path/filepath"
"strings"
"testing"
"github.com/samber/lo"
)
func TestGenerateIDsUsingPath(t *testing.T) {
tests := []struct {
paths []string
seedReversePaths []string
}{
{
[]string{"a.yml", "b.yml", "c.yml"},
[]string{"a.yml", "b.yml", "c.yml"},
},
{
[]string{"path/to/a.yml", "path/to/b.yml", "path/to/c.yml"},
[]string{"a.yml", "b.yml", "c.yml"},
},
{
[]string{"path/to/bb/a.yml", "path/to/aa/a.yml"},
[]string{"a.yml/bb", "a.yml/aa"},
},
{
[]string{"path/to/bb/a.yml", "../../path/to/aa/a.yml"},
[]string{"a.yml/bb", "a.yml/aa"},
},
}
for _, tt := range tests {
ops := []*operator{}
for _, p := range tt.paths {
ops = append(ops, &operator{
bookPath: p,
})
}
if err := generateIDsUsingPath(ops); err != nil {
t.Fatal(err)
}
for i, o := range ops {
want, err := generateID(tt.seedReversePaths[i])
if err != nil {
t.Fatal(err)
}
if o.id != want {
t.Errorf("want %s, got %s", want, o.id)
}
}
}
}
func BenchmarkReversePath(b *testing.B) {
for i := 0; i < b.N; i++ {
p := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"
_ = lo.Reverse(strings.Split(filepath.ToSlash(p), "/"))
}
}
func BenchmarkSHA1(b *testing.B) {
for i := 0; i < b.N; i++ {
p := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"
h := sha1.New() //#nosec G401
_, _ = io.WriteString(h, p)
_ = hex.EncodeToString(h.Sum(nil))
}
}
func BenchmarkBase32(b *testing.B) {
for i := 0; i < b.N; i++ {
p := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"
_ = base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString([]byte(p))
}
}
func BenchmarkReverseAndHashBySHA1Path(b *testing.B) {
for i := 0; i < b.N; i++ {
p := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"
r := lo.Reverse(strings.Split(filepath.ToSlash(p), "/"))
h := sha1.New() //#nosec G401
_, _ = io.WriteString(h, strings.Join(r[0:5], "/"))
_ = hex.EncodeToString(h.Sum(nil))
}
}
func BenchmarkReverseAndEncodeByBase32Path(b *testing.B) {
for i := 0; i < b.N; i++ {
p := "/a/b/c/d/e/f/g/h/i/j/k/l/m/n/o/p/q/r/s/t/u/v/w/x/y/z"
r := lo.Reverse(strings.Split(filepath.ToSlash(p), "/"))
_ = base32.StdEncoding.WithPadding(base32.NoPadding).EncodeToString([]byte(strings.Join(r[0:5], "/")))
}
}