-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmath_test.go
93 lines (83 loc) · 1.84 KB
/
math_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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package randutil
import (
"regexp"
"testing"
)
func TestMathRandomGenerator(t *testing.T) {
g := NewMathRandomGenerator()
isLetter := regexp.MustCompile(`^[a-zA-Z]+$`).MatchString
for i := 0; i < 10000; i++ {
s := g.GenerateString(10, runesAlpha)
if len(s) != 10 {
t.Error("Generator returned invalid length")
}
if !isLetter(s) {
t.Errorf("Generator returned unexpected character: %s", s)
}
}
}
func TestIntn(t *testing.T) {
g := NewMathRandomGenerator()
localMin := 100
localMax := 0
for i := 0; i < 10000; i++ {
r := g.Intn(100)
if r < 0 || r >= 100 {
t.Fatalf("Out of range of Intn(100): %d", r)
}
if r < localMin {
localMin = r
}
if r > localMax {
localMax = r
}
}
if localMin > 10 {
t.Error("Value around lower boundary was not generated")
}
if localMax < 90 {
t.Error("Value around upper boundary was not generated")
}
}
func TestUint64(t *testing.T) {
g := NewMathRandomGenerator()
localMin := uint64(0xFFFFFFFFFFFFFFFF)
localMax := uint64(0)
for i := 0; i < 10000; i++ {
r := g.Uint64()
if r < localMin {
localMin = r
}
if r > localMax {
localMax = r
}
}
if localMin > 0x1000000000000000 {
t.Error("Value around lower boundary was not generated")
}
if localMax < 0xF000000000000000 {
t.Error("Value around upper boundary was not generated")
}
}
func TestUint32(t *testing.T) {
g := NewMathRandomGenerator()
localMin := uint32(0xFFFFFFFF)
localMax := uint32(0)
for i := 0; i < 10000; i++ {
r := g.Uint32()
if r < localMin {
localMin = r
}
if r > localMax {
localMax = r
}
}
if localMin > 0x10000000 {
t.Error("Value around lower boundary was not generated")
}
if localMax < 0xF0000000 {
t.Error("Value around upper boundary was not generated")
}
}