-
Notifications
You must be signed in to change notification settings - Fork 10
/
common_test.go
164 lines (142 loc) · 4.01 KB
/
common_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
// ⚡️ Fiber is an Express inspired web framework written in Go with ☕️
// 🤖 Github Repository: https://github.com/gofiber/fiber
// 📌 API Documentation: https://docs.gofiber.io
package utils
import (
"crypto/rand"
"fmt"
"net"
"os"
"testing"
"github.com/stretchr/testify/require"
)
func Test_FunctionName(t *testing.T) {
t.Parallel()
require.Equal(t, "github.com/gofiber/utils/v2.Test_UUID", FunctionName(Test_UUID))
require.Equal(t, "github.com/gofiber/utils/v2.Test_FunctionName.func1", FunctionName(func() {}))
dummyint := 20
require.Equal(t, "int", FunctionName(dummyint))
}
func Test_UUID(t *testing.T) {
t.Parallel()
res := UUID()
require.Len(t, res, 36)
require.NotEqual(t, "00000000-0000-0000-0000-000000000000", res)
}
func Test_UUID_Concurrency(t *testing.T) {
t.Parallel()
iterations := 1000
var res string
ch := make(chan string, iterations)
results := make(map[string]string)
for i := 0; i < iterations; i++ {
go func() {
ch <- UUID()
}()
}
for i := 0; i < iterations; i++ {
res = <-ch
results[res] = res
}
require.Len(t, results, iterations)
}
func Test_UUIDv4(t *testing.T) {
t.Parallel()
res := UUIDv4()
require.Len(t, res, 36)
require.NotEqual(t, "00000000-0000-0000-0000-000000000000", res)
}
func Test_UUIDv4_Concurrency(t *testing.T) {
t.Parallel()
iterations := 1000
var res string
ch := make(chan string, iterations)
results := make(map[string]string)
for i := 0; i < iterations; i++ {
go func() {
ch <- UUIDv4()
}()
}
for i := 0; i < iterations; i++ {
res = <-ch
results[res] = res
}
require.Len(t, results, iterations)
}
func Test_ConvertToBytes(t *testing.T) {
t.Parallel()
require.Equal(t, 0, ConvertToBytes(""))
require.Equal(t, 42, ConvertToBytes("42"))
require.Equal(t, 42, ConvertToBytes("42b"))
require.Equal(t, 42, ConvertToBytes("42B"))
require.Equal(t, 42, ConvertToBytes("42 b"))
require.Equal(t, 42, ConvertToBytes("42 B"))
require.Equal(t, 42*1000, ConvertToBytes("42k"))
require.Equal(t, 42*1000, ConvertToBytes("42K"))
require.Equal(t, 42*1000, ConvertToBytes("42kb"))
require.Equal(t, 42*1000, ConvertToBytes("42KB"))
require.Equal(t, 42*1000, ConvertToBytes("42 kb"))
require.Equal(t, 42*1000, ConvertToBytes("42 KB"))
require.Equal(t, 42*1000000, ConvertToBytes("42M"))
require.Equal(t, int(42.5*1000000), ConvertToBytes("42.5MB"))
require.Equal(t, 42*1000000000, ConvertToBytes("42G"))
require.Equal(t, 0, ConvertToBytes("string"))
require.Equal(t, 0, ConvertToBytes("MB"))
}
func Test_GetArgument(t *testing.T) {
originalArgs := os.Args
// Reset os.Args
defer func() { os.Args = originalArgs }()
testArg := "test-arg"
os.Args = []string{"cmd", testArg}
require.True(t, GetArgument(testArg))
require.False(t, GetArgument("missing-arg"))
}
func Test_IncrementIPRange(t *testing.T) {
t.Parallel()
cases := []struct {
input net.IP
expected net.IP
}{
{net.IP{192, 168, 1, 1}, net.IP{192, 168, 1, 2}},
{net.IP{192, 168, 1, 254}, net.IP{192, 168, 1, 255}},
{net.IP{192, 168, 1, 255}, net.IP{192, 168, 2, 0}},
{net.IP{255, 255, 255, 255}, net.IP{0, 0, 0, 0}},
}
for _, c := range cases {
c := c
t.Run(c.input.String(), func(t *testing.T) {
t.Parallel()
IncrementIPRange(c.input)
require.Equal(t, c.expected, c.input)
})
}
}
// go test -v -run=^$ -bench=Benchmark_ConvertToBytes -benchmem -count=2
func Benchmark_ConvertToBytes(b *testing.B) {
var res int
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = ConvertToBytes("42B")
}
require.Equal(b, 42, res)
})
}
// go test -v -run=^$ -bench=Benchmark_UUID -benchmem -count=2
func Benchmark_UUID(b *testing.B) {
var res string
b.Run("fiber", func(b *testing.B) {
for n := 0; n < b.N; n++ {
res = UUID()
}
require.Len(b, res, 36)
})
b.Run("default", func(b *testing.B) {
rnd := make([]byte, 16)
_, _ = rand.Read(rnd) //nolint: errcheck // No need to check error
for n := 0; n < b.N; n++ {
res = fmt.Sprintf("%x-%x-%x-%x-%x", rnd[0:4], rnd[4:6], rnd[6:8], rnd[8:10], rnd[10:])
}
require.Len(b, res, 36)
})
}