-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathmath_test.go
79 lines (67 loc) · 1.96 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
// SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
// SPDX-License-Identifier: MIT
package randutil
import (
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
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)
assert.Equal(t, 10, len(s), "Generated string was not the correct length")
assert.True(t, isLetter(s), "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)
assert.GreaterOrEqual(t, r, 0, "Generated value was not greater than 0")
assert.Less(t, r, 100, "Generated value was not less than 100")
if r < localMin {
localMin = r
}
if r > localMax {
localMax = r
}
}
assert.Greater(t, 10, localMin, "Value around lower boundary was not generated")
assert.Less(t, 90, localMax, "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
}
}
assert.Greater(t, uint64(0x1000000000000000), localMin, "Value around lower boundary was not generated")
assert.Less(t, uint64(0xF000000000000000), localMax, "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
}
}
assert.Greater(t, uint32(0x10000000), localMin, "Value around lower boundary was not generated")
assert.Less(t, uint32(0xF0000000), localMax, "Value around upper boundary was not generated")
}