-
Notifications
You must be signed in to change notification settings - Fork 0
/
mt64_test.go
77 lines (71 loc) · 1.47 KB
/
mt64_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
package crazy
import (
"bytes"
"crypto/rand"
"testing"
)
func TestMT64Seed(t *testing.T) {
mt := NewMT64()
// MT64.SeedIV() uses .Seed(), so testing IV tests both.
mt.SeedIV(nil)
mt.SeedIV([]byte{mt64N: 0})
mt.SeedIV([]byte{8 * mt64N: 0})
mt.SeedIV([]byte{9 * mt64N: 0})
}
func TestMT64SeedConsistency(t *testing.T) {
iv := make([]byte, 128)
mt := NewMT64()
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*mt64N; i++ {
rand.Read(iv)
mt.SeedIV(iv)
mt.Read(x)
mt.SeedIV(iv)
mt.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
}
}
func TestMT64Save(t *testing.T) {
b := bytes.Buffer{}
mt := CryptoSeeded(NewMT64(), mt64N).(*MT64)
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*mt64N; i++ {
mt.Save(&b)
mt.Read(x)
mt.Restore(&b)
mt.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
b.Reset()
}
}
func TestMT64Copy(t *testing.T) {
mt := CryptoSeeded(NewMT64(), mt64N).(*MT64)
x, y := make([]byte, 8000), make([]byte, 8000)
for i := 0; i < 2*mt64N; i++ {
cp := mt.Copy()
mt.Read(x)
cp.Read(y)
if !bytes.Equal(x, y) {
t.Fail()
}
}
}
func BenchmarkMT64(b *testing.B) {
mt := CryptoSeeded(NewMT64(), mt64N).(*MT64)
f := func(p []byte) func(b *testing.B) {
return func(b *testing.B) {
b.SetBytes(int64(len(p)))
for n := 0; n < b.N; n++ {
mt.Read(p)
}
}
}
b.Run("8", f(make([]byte, 8)))
b.Run("K", f(make([]byte, 1<<10)))
b.Run("M", f(make([]byte, 1<<25)))
b.Run("G", f(make([]byte, 1<<30)))
}