Commit 49f30b15 authored by Juan Batiz-Benet's avatar Juan Batiz-Benet

added random to godeps

parent 7ee0c6dc
......@@ -110,6 +110,10 @@
"Comment": "0.1.0-5-g1976046",
"Rev": "1976046c2b0db0b668791b3e541d76a38b7c1af7"
},
{
"ImportPath": "github.com/jbenet/go-random/random",
"Rev": "623fff67b06299c43b5542d42c427c7c2855a362"
},
{
"ImportPath": "github.com/kr/binarydist",
"Rev": "9955b0ab8708602d411341e55fffd7e0700f86bd"
......
package main
import (
"bytes"
randcrypto "crypto/rand"
"fmt"
"io"
randmath "math/rand"
"os"
"strconv"
)
func main() {
l := len(os.Args)
if l != 2 && l != 3 {
usageError()
}
count, err := strconv.ParseInt(os.Args[1], 10, 64)
if err != nil {
usageError()
}
if l == 2 {
err = writeRandomBytes(count, os.Stdout)
} else {
seed, err2 := strconv.ParseInt(os.Args[2], 10, 64)
if err2 != nil {
usageError()
}
err = writePseudoRandomBytes(count, os.Stdout, seed)
}
if err != nil {
die(err)
}
}
func usageError() {
fmt.Fprintf(os.Stderr, "Usage: %s <count> [<seed>]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "If <seed> is given, output <count> pseudo random bytes made from <seed> (from Go's math/rand)\n")
fmt.Fprintf(os.Stderr, "Otherwise, output <count> random bytes (from Go's crypto/rand)\n")
os.Exit(-1)
}
func die(err error) {
fmt.Fprintf(os.Stderr, "Error: %v", err)
os.Exit(-1)
}
func writeRandomBytes(count int64, w io.Writer) error {
r := &io.LimitedReader{R: randcrypto.Reader, N: count}
_, err := io.Copy(w, r)
return err
}
func writePseudoRandomBytes(count int64, w io.Writer, seed int64) error {
randmath.Seed(seed)
// Configurable buffer size
bufsize := int64(1024 * 1024 * 4)
b := make([]byte, bufsize)
for count > 0 {
if bufsize > count {
bufsize = count
b = b[:bufsize]
}
var n int64
for i := int64(0); i < bufsize; i++ {
n = randmath.Int63()
for j := 0; j < 8 && i < bufsize; j++ {
b[i] = byte(n & 0xff)
n >>= 8
i++
}
}
count = count - bufsize
r := bytes.NewReader(b)
_, err := io.Copy(w, r)
if err != nil {
return err
}
}
return nil
}
package main
import (
"bytes"
"io/ioutil"
"testing"
"time"
)
func TestPseudoRandom(t *testing.T) {
var testCases = []int{
1024,
187654,
1048576,
4932132,
}
for _, size := range testCases {
var buf bytes.Buffer
err := writePseudoRandomBytes(int64(size), &buf, int64(time.Now().UnixNano()))
if err != nil {
t.Fatal(err)
}
if buf.Len() != size {
t.Fatal("buffer not of the right size: %d != %d", buf.Len(), size)
}
}
}
func TestPseudoRandomSeed(t *testing.T) {
var first []byte
var size = int64(1024 * 4)
seed := time.Now().UnixNano()
for i := 0; i < 100; i++ {
var bufs bytes.Buffer
var bufr bytes.Buffer
if err := writePseudoRandomBytes(size, &bufs, seed); err != nil {
t.Fatal(err)
}
if err := writePseudoRandomBytes(size, &bufr, time.Now().UnixNano()); err != nil {
t.Fatal(err)
}
if bufs.Len() != int(size) {
t.Fatal("buffer not of the right size: %d != %d", bufs.Len(), size)
}
if bufr.Len() != int(size) {
t.Fatal("buffer not of the right size: %d != %d", bufr.Len(), size)
}
if first == nil {
first = bufs.Bytes()
} else if !bytes.Equal(first, bufs.Bytes()) {
t.Fatal("seeded constructed different bytes")
}
if bytes.Equal(first, bufr.Bytes()) {
t.Fatal("non-seeded constructed same bytes")
}
}
}
func TestCryptoRandom(t *testing.T) {
var testCases = []int{
1024,
187654,
1048576,
}
for _, size := range testCases {
var buf bytes.Buffer
err := writeRandomBytes(int64(size), &buf)
if err != nil {
t.Fatal(err)
}
if buf.Len() != size {
t.Fatal("buffer not of the right size: %d != %d", buf.Len(), size)
}
}
}
func BenchmarkCryptoRandom(b *testing.B) {
writeRandomBytes(int64(b.N), ioutil.Discard)
}
func BenchmarkPseudoRandom(b *testing.B) {
writePseudoRandomBytes(int64(b.N), ioutil.Discard, time.Now().UnixNano())
}
// package randomdep is here to introduce a dependency in random for godep to
// function properly. this way we can keep go-random vendored and not
// accidentally break our tests when we change it.
package randomdep
import (
random "github.com/jbenet/go-ipfs/Godeps/_workspace/src/github.com/jbenet/go-random/random"
)
var _ = random
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment