util: remove broken rand

This commit is contained in:
Juan Batiz-Benet 2014-10-30 04:14:05 -07:00
parent 37ce1863c0
commit cb22b62a9b
4 changed files with 15 additions and 22 deletions

View File

@ -12,7 +12,7 @@ import (
func randNode() (*mdag.Node, util.Key) {
nd := new(mdag.Node)
nd.Data = make([]byte, 32)
util.NewFastRand().Read(nd.Data)
util.NewTimeSeededRand().Read(nd.Data)
k, _ := nd.Key()
return nd, k
}

View File

@ -28,7 +28,7 @@ func getMockDagServ(t *testing.T) mdag.DAGService {
func getNode(t *testing.T, dserv mdag.DAGService, size int64) ([]byte, *mdag.Node) {
dw := NewDagWriter(dserv, &chunk.SizeSplitter{500})
n, err := io.CopyN(dw, u.NewFastRand(), size)
n, err := io.CopyN(dw, u.NewTimeSeededRand(), size)
if err != nil {
t.Fatal(err)
}
@ -58,7 +58,7 @@ func getNode(t *testing.T, dserv mdag.DAGService, size int64) ([]byte, *mdag.Nod
func testModWrite(t *testing.T, beg, size uint64, orig []byte, dm *DagModifier) []byte {
newdata := make([]byte, size)
r := u.NewFastRand()
r := u.NewTimeSeededRand()
r.Read(newdata)
if size+beg > uint64(len(orig)) {
@ -160,7 +160,7 @@ func TestMultiWrite(t *testing.T) {
}
data := make([]byte, 4000)
u.NewFastRand().Read(data)
u.NewTimeSeededRand().Read(data)
for i := 0; i < len(data); i++ {
n, err := dagmod.WriteAt(data[i:i+1], uint64(i))
@ -201,7 +201,7 @@ func TestMultiWriteCoal(t *testing.T) {
}
data := make([]byte, 4000)
u.NewFastRand().Read(data)
u.NewTimeSeededRand().Read(data)
for i := 0; i < len(data); i++ {
n, err := dagmod.WriteAt(data[:i+1], 0)

View File

@ -97,28 +97,21 @@ func (bcr *byteChanReader) Read(b []byte) (int, error) {
}
type randGen struct {
src rand.Source
rand.Rand
}
func NewFastRand() io.Reader {
return &randGen{rand.NewSource(time.Now().UnixNano())}
func NewTimeSeededRand() io.Reader {
src := rand.NewSource(time.Now().UnixNano())
return &randGen{
Rand: *rand.New(src),
}
}
func (r *randGen) Read(p []byte) (n int, err error) {
todo := len(p)
offset := 0
for {
val := int64(r.src.Int63())
for i := 0; i < 8; i++ {
p[offset] = byte(val & 0xff)
todo--
if todo == 0 {
return len(p), nil
}
offset++
val >>= 8
}
for i := 0; i < len(p); i++ {
p[i] = byte(r.Rand.Intn(255))
}
return len(p), nil
}
// GetenvBool is the way to check an env var as a boolean

View File

@ -31,7 +31,7 @@ func TestKey(t *testing.T) {
func TestByteChanReader(t *testing.T) {
data := make([]byte, 1024*1024)
r := NewFastRand()
r := NewTimeSeededRand()
r.Read(data)
dch := make(chan []byte, 8)