package grand_test

import (
	"bytes"
	"fmt"
	"testing"

	"xdx.jelly/xgcl/grand"
)

func TestRandZero(t *testing.T) {
	randReader := grand.ReaderZero
	buf := make([]byte, 100)
	n, err := randReader.Read(buf)
	if err != nil || n != 100 {
		t.Log(n, err)
		t.Fail()
	}
}

func TestRandFromBytes(t *testing.T) {
	buf := make([]byte, 100)
	_, _ = grand.ReaderOne.Read(buf)
	fmt.Println(buf)
	randReader := bytes.NewReader(buf)

	buf2 := make([]byte, 51)
	n, err := randReader.Read(buf2)
	fmt.Println(n, err, buf2)
	if err != nil || n != len(buf2) {
		t.Log(n, err)
		t.Fail()
	}
	for _, b := range buf2 {
		if b != 1 {
			t.Fail()
		}
	}

	n, err = randReader.Read(buf2)
	fmt.Println(n, err, buf2)
	if err != nil || n != len(buf2) {
		t.Log(n, err)
		t.Fail()
	}
	for _, b := range buf2 {
		if b != 1 {
			t.Fail()
		}
	}

}

func TestInt(t *testing.T) {
	for bits := 0; bits < 10000; bits++ {
		n := grand.Int(bits)
		if n.BitLen() != bits {
			t.Fatalf("bits = %d, n = %x", bits, n)
		}
	}
}
