-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_test.go
104 lines (82 loc) · 1.94 KB
/
string_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
package uuid_test
import (
"encoding/hex"
"math/rand"
"strings"
"testing"
"github.com/DaanV2/go-uuid"
"github.com/stretchr/testify/require"
)
func Fuzz_String(f *testing.F) {
rnd := rand.New(rand.NewSource(0))
gen_case := func (length int) {
b := make([]byte, length)
_, _ = rnd.Read(b)
f.Add(b)
}
gen_case(0)
gen_case(15)
gen_case(17)
for i := 0; i < 25; i++ {
gen_case(16)
}
for i := 0; i < 25; i++ {
length := (rnd.Int() % 8) + 12
gen_case(length)
}
f.Fuzz(func(t *testing.T, b []byte) {
u, err := uuid.FromBytes(b[:])
if len(b) != 16 {
require.Error(t, err)
return
}
require.NoError(t, err)
actual := u.String()
require.Equal(t, len(u), uuid.TOTAL_BYTES)
require.Equal(t, len(actual), uuid.STRING_LENGTH, "Length of the string should be doubled")
expect := hex.EncodeToString(b)
actual_hex := strings.ReplaceAll(actual, "-", "")
require.Equal(t, expect, actual_hex, "expected %s, got %s", expect, actual)
})
}
func Fuzz_StringHex(f *testing.F) {
rnd := rand.New(rand.NewSource(0))
gen_case := func (length int) {
b := make([]byte, length)
_, _ = rnd.Read(b)
f.Add(b)
}
gen_case(0)
gen_case(15)
gen_case(17)
for i := 0; i < 25; i++ {
gen_case(16)
}
for i := 0; i < 25; i++ {
length := (rnd.Int() % 8) + 12
gen_case(length)
}
f.Fuzz(func(t *testing.T, b []byte) {
u, err := uuid.FromBytes(b[:])
if len(b) != 16 {
require.Error(t, err)
return
}
require.NoError(t, err)
actual := u.StringHex()
require.Equal(t, len(u), uuid.TOTAL_BYTES)
require.Equal(t, len(actual), uuid.TOTAL_BYTES*2, "Length of the string should be doubled")
expect := hex.EncodeToString(b)
require.Equal(t, expect, actual, "expected %s, got %s", expect, actual)
})
}
func Benchmark_StringHex(b *testing.B) {
rnd := rand.New(rand.NewSource(0))
b.ResetTimer()
for i := 0; i < b.N; i++ {
b := make([]byte, 16)
_, _ = rnd.Read(b)
u, _ := uuid.FromBytes(b[:])
_ = u.StringHex()
}
}