-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_test.go
129 lines (106 loc) · 3.09 KB
/
random_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
// v0.8.0
// Author: DIEHL E.
// © Nov 2024
package test
import (
"math/rand/v2"
"os"
"path/filepath"
"strings"
"testing"
)
func Test_test_RandomString(t *testing.T) {
_, assert := Describe(t)
for i := 1; i < 50; i++ {
s := RandomString(i)
assert.Equal(i, len(s), "failed to generate a string of %d characters", i)
}
s := RandomString(0)
assert.NotEqual(0, len(s), "failed to generate with 0 size: %d", len(s))
}
func TestGenerateAlphaRandomString(t *testing.T) {
_, assert := Describe(t)
s := RandomAlphaString(254, All)
assert.Equal(254, len(s))
s = RandomAlphaString(254, AlphaNum)
assert.NotContains(s, "@.!$&_+-:;*?#/\\,()[]{}<>%")
s = RandomAlphaString(254, AlphaNumNoSpace)
assert.NotContains(s, " @.!$&_+-:;*?#/\\,()[]{}<>%")
s = RandomAlphaString(254, Alpha)
assert.NotContains(s, "01234567890 @.!$&_+-:;*?#/\\,()[]{}<>%")
s = RandomAlphaString(254, Caps)
assert.NotContains(s, "abcdefghijklmnopqrstuvwxyz01234567890 @.!$&_+-:;*?#/\\,()[]{}<>%")
s = RandomAlphaString(254, Small)
assert.NotContains(s, "ABCDEFGHIJKLMNOPQRSTUVWXYZ01234567890 @.!$&_+-:;*?#/\\,()[]{}<>%")
s = RandomAlphaString(254, 1000)
assert.Equal("", s)
}
func Test_test_GenerateName(t *testing.T) {
_, assert := Describe(t)
for i := 1; i < 50; i++ {
s := RandomName(i)
assert.Equal(i, len(s), "failed to generate a string of %d characters", i)
}
s := RandomName(0)
assert.NotEqual(0, len(s), "failed to generate with 0 size: %d", len(s))
}
func Test_test_RandomSlice(t *testing.T) {
_, assert := Describe(t)
n := rand.IntN(255) + 1
d := RandomSlice(n)
assert.Equal(n, len(d))
d1 := RandomSlice(0)
assert.NotZero(len(d1))
}
func Test_test_RandomID(t *testing.T) {
_, assert := Describe(t)
id := RandomID()
assert.Equal(16, len(id))
assert.NotContains(id, " @.!$&_+-:;*?#/\\,()[]{}<>%")
}
func Test_RandomCSVFile(t *testing.T) {
require, assert := Describe(t)
name := "testdata/" + RandomID() + ".csv"
err := RandomCSVFile(name, rand.IntN(10)+1, rand.IntN(10)+1, ';')
require.NoError(err)
assert.FileExists(name)
_ = os.Remove(name)
require.Error(RandomCSVFile("bad/"+name, 10, 10, ';'))
}
func Test_RandomFileWithDir(t *testing.T) {
require, assert := Describe(t)
n, err := RandomFileWithDir(3, "trial", "")
require.NoError(err)
assert.FileExists(n)
_ = os.Remove(n)
isPanic(os.MkdirAll("testdata/trial", 0777))
n, err = RandomFileWithDir(3, "trial", "testdata/trial")
require.NoError(err)
assert.FileExists(filepath.Join("testdata/trial", n))
_ = os.Remove(filepath.Join("testdata/trial", n))
_, err = RandomFileWithDir(3, "trial", "testdata/bad")
assert.Error(err)
}
func Benchmark_RandomSlice(b *testing.B) {
for i := 0; i < b.N; i++ {
RandomSlice(1024)
}
}
func Test_SwapCase(t *testing.T) {
_, assert := Describe(t)
s := RandomString(100)
s1 := SwapCase(s)
assert.NotEqual(s, s1)
assert.Equal(strings.ToLower(s), strings.ToLower(s1))
}
func Benchmark_RandomFile(b *testing.B) {
for i := 0; i < b.N; i++ {
n, _ := RandomFileWithDir(10, "tst", "testdata/trial")
_ = os.Remove(filepath.Join("testdata/trial", n))
}
}
func isPanic(err error) {
if err != nil {
panic(err)
}
}