/go-randdata

A Go package providing a mechanism for unit testing to generate and verify reproducible pseudo-random byte sequences.

Primary LanguageGoMIT LicenseMIT

go-randdata

GitHub GoDoc Go Report Card codecov

go-randdata is a Go package providing a mechanism for unit testing to generate and verify reproducible pseudo-random byte sequences.

Reader is the pseudo-random byte sequence generator. It implements the io.Reader interface and can be Read the generated byte sequence. Verifier is the Reader companion object that implements the io.Writer interface. It verifies that the data written is exactly the same as the byte sequence generated by the Reader.

import (
	"fmt"
	"io"

	"github.com/tunabay/go-randdata"
)

func main() {
	// 5 MB pseudo-random byte sequence, using random seed 123
	r := randdata.New(randdata.Binary, 123, 5000000)

	// paired verifier
	v := r.NewVerifier()
	// v := randdata.NewVerifier(randdata.Binary, 123, 5000000 - 8) // too long
	// v := randdata.NewVerifier(randdata.Binary, 123, 5000000 + 8) // not enough

	// read and veriry data
	buf := make([]byte, 256)
	for {
		n, err := r.Read(buf)
		if 0 < n {
			if _, err := v.Write(buf[:n]); err != nil {
				fmt.Println(err)
				break
			}
		}
		if err != nil {
			if err != io.EOF {
				fmt.Println(err)
			}
			break
		}
	}

	// verify that written data is enough
	if err := v.Close(); err != nil {
		fmt.Println(err)
	}

	fmt.Println("Read:", r.TotalRead())
}

Run in Go Playground

The Reader also generates "jitter" to reading operation. In the above example, calling Read method with the 256 bytes buffer returns randomly shorter written length. While the Read method of the io.Reader interface can return shorter length than passed buffer, program should be able to handle that.

Documentation

See also

License

go-randdata is available under the MIT license. See the LICENSE file for more information.