-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathnumbers_test.go
More file actions
74 lines (59 loc) · 1.9 KB
/
Copy pathnumbers_test.go
File metadata and controls
74 lines (59 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
package cryptorand_test
import (
"testing"
"github.com/stretchr/testify/require"
"github.com/coder/coder/v2/cryptorand"
)
func TestInt63(t *testing.T) {
t.Parallel()
for i := 0; i < 20; i++ {
v, err := cryptorand.Int63()
require.NoError(t, err, "unexpected error from Int63")
t.Logf("value: %v <- random?", v)
require.True(t, v >= 0, "values must be positive")
}
}
func TestInt63n(t *testing.T) {
t.Parallel()
for i := 0; i < 20; i++ {
v, err := cryptorand.Int63n(100)
require.NoError(t, err, "unexpected error from Int63n")
t.Logf("value: %v <- random?", v)
require.GreaterOrEqual(t, v, int64(0), "values must be positive")
require.Less(t, v, int64(100), "values must be less than 100")
}
// Ensure Int63n works for int larger than 32 bits
_, err := cryptorand.Int63n(1 << 35)
require.NoError(t, err, "expected Int63n to work for 64-bit int")
// Expect a panic if max is negative
require.PanicsWithValue(t, "invalid argument to Int63n", func() {
cryptorand.Int63n(0)
})
}
func TestIntn(t *testing.T) {
t.Parallel()
for i := 0; i < 20; i++ {
v, err := cryptorand.Intn(100)
require.NoError(t, err, "unexpected error from Intn")
t.Logf("value: %v <- random?", v)
require.GreaterOrEqual(t, v, 0, "values must be positive")
require.True(t, v < 100, "values must be less than 100")
}
// Ensure Intn works for int larger than 32 bits
_, err := cryptorand.Intn(1 << 35)
require.NoError(t, err, "expected Intn to work for 64-bit int")
// Expect a panic if max is negative
require.PanicsWithValue(t, "invalid argument to Intn", func() {
cryptorand.Intn(0)
})
}
func TestFloat64(t *testing.T) {
t.Parallel()
for i := 0; i < 20; i++ {
v, err := cryptorand.Float64()
require.NoError(t, err, "unexpected error from Float64")
t.Logf("value: %v <- random?", v)
require.True(t, v >= 0.0, "values must be positive")
require.True(t, v < 1.0, "values must be less than 1.0")
}
}