-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrandom_test.go
More file actions
103 lines (82 loc) 路 1.9 KB
/
Copy pathrandom_test.go
File metadata and controls
103 lines (82 loc) 路 1.9 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
package gouse
import (
"fmt"
"reflect"
"testing"
"time"
)
func TestRandNum(t *testing.T) {
min := 1
max := 10
result := RandNum(min, max)
if result < min || result > max {
t.Errorf("Random() = %d; want >= %d and <= %d", result, min, max)
}
}
func BenchmarkRandNum(b *testing.B) {
min := 1
max := 10
for b.Loop() {
RandNum(min, max)
}
}
func TestRandID(t *testing.T) {
t.Run("NonEmptyString", func(t *testing.T) {
id := RandID()
if len(id) == 0 {
t.Errorf("Expected non-empty string, got an empty string")
}
})
t.Run("StringRepresentationOfNumber", func(t *testing.T) {
id := RandID()
_, err := fmt.Sscanf(id, "%d", new(int64))
if err != nil {
t.Errorf("Expected a valid integer string, got error: %v", err)
}
})
t.Run("UniqueIDs", func(t *testing.T) {
id1 := RandID()
time.Sleep(1 * time.Millisecond)
id2 := RandID()
if id1 == id2 {
t.Errorf("Expected different IDs for consecutive calls, got %s and %s", id1, id2)
}
})
}
func BenchmarkRandID(b *testing.B) {
for b.Loop() {
RandID()
}
}
func TestRandStr(t *testing.T) {
expectedLength := 10
got := RandStr(expectedLength)
if len(got) != expectedLength {
t.Errorf("RandomStr() = %v, want %v", len(got), expectedLength)
}
if reflect.TypeOf(got).Kind() != reflect.String {
t.Errorf("RandomStr() = %v, want %v", reflect.TypeOf(got).Kind(), reflect.String)
}
}
func BenchmarkRandStr(b *testing.B) {
expectedLength := 10
for b.Loop() {
RandStr(expectedLength)
}
}
func TestRandDigit(t *testing.T) {
expectedLength := 6
got := RandDigit(expectedLength)
if len(got) != expectedLength {
t.Errorf("RandomNum() = %v, want %v", len(got), expectedLength)
}
if reflect.TypeOf(got).Kind() != reflect.String {
t.Errorf("RandomNum() = %v, want %v", reflect.TypeOf(got).Kind(), reflect.String)
}
}
func BenchmarkRandDigit(b *testing.B) {
expectedLength := 6
for b.Loop() {
RandDigit(expectedLength)
}
}