-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsource64_test.go
62 lines (52 loc) · 1.53 KB
/
source64_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
// +build go1.9
package cryptorand_test
import (
"bytes"
"math"
"math/rand"
"testing"
"github.com/wadey/cryptorand"
)
func TestSource64(t *testing.T) {
s := cryptorand.Source.(rand.Source64)
if s.Uint64() == s.Uint64() {
t.Error("Expected Uint64() to be random")
}
}
func TestNewSource64(t *testing.T) {
b := bytes.NewReader(make([]byte, 8))
s := cryptorand.NewSource(b)
if s.Int63() != 0 {
t.Error("Expected Int63() to be 0 with custom io.Reader")
}
b = bytes.NewReader([]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
s = cryptorand.NewSource(b)
if s.Int63() != 1<<63-1 {
t.Error("Expected Int63() to be max with custom io.Reader")
}
b = bytes.NewReader(make([]byte, 9))
s64 := cryptorand.NewSource(b).(rand.Source64)
v := s64.Uint64()
if v != 0 {
t.Errorf("Expected Uint64() to be 0 with custom io.Reader: %v != 0", v)
}
b = bytes.NewReader([]byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
s64 = cryptorand.NewSource(b).(rand.Source64)
v = s64.Uint64()
if v != math.MaxUint64 {
t.Errorf("Expected Uint64() to be max with custom io.Reader: %v != %v", v, uint64(math.MaxUint64))
}
b = bytes.NewReader([]byte{0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff})
s64 = cryptorand.NewSource(b).(rand.Source64)
v = s64.Uint64()
if v != math.MaxInt64 {
t.Errorf("Unexpected Uint64() with custom io.Reader: %v != %v", v, uint64(math.MaxInt64))
}
}
func BenchmarkRandSource64(b *testing.B) {
s := cryptorand.Source.(rand.Source64)
b.SetBytes(8)
for i := 0; i < b.N; i++ {
s.Uint64()
}
}