-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrandom_test.go
72 lines (60 loc) · 1.44 KB
/
random_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
package cryptanalysis
import (
"testing"
)
type byteint struct {
bytearray []byte
result uint64
}
type intrange struct {
start uint64
end uint64
}
func TestRandomBytes(t *testing.T) {
var tests = []int{1, 5, 10}
for _, test := range tests {
r, _ := RandomBytes(test)
if len(r) != test {
t.Error("Expected", test, "got", len(r))
}
}
}
func TestBytesToInt(t *testing.T) {
var tests = []byteint{
{[]byte{128, 0, 0, 0, 0, 0, 0, 0}, 9223372036854775808},
{[]byte{0, 0, 0, 0, 128, 0, 0, 0}, 2147483648},
{[]byte{0, 0, 0, 0, 0, 0, 0, 1}, 1},
{[]byte{0, 0, 0, 0, 0, 3}, 3},
{[]byte{0, 0, 0, 5}, 5},
}
_, err := BytesToInt([]byte{})
if err == nil {
t.Error("Empty byte array should produce an error.")
}
_, err = BytesToInt([]byte{0, 0, 0, 0, 0, 0, 0, 0, 0})
if err == nil {
t.Error("Long byte array should produce an error.")
}
for _, test := range tests {
i, _ := BytesToInt(test.bytearray)
if i != test.result {
t.Error("Expected", test.result, "got", i)
}
}
}
func TestRandomIntRange(t *testing.T) {
var tests = []intrange{
{0, 10},
{9223372036854775800, 9223372036854775808},
}
_, err := RandomIntRange(10, 0)
if err == nil {
t.Error("Start greater than end should produce an error.")
}
for _, test := range tests {
i, _ := RandomIntRange(test.start, test.end)
if i < test.start || i > test.end {
t.Error("Expected integer within range of", test.start, "and", test.end, "got", i)
}
}
}