From 7e28b012640d3fe2251fb306c6e31eb82c7d24b9 Mon Sep 17 00:00:00 2001 From: Markus Sommer Date: Fri, 5 Apr 2024 20:45:15 +0200 Subject: [PATCH] Property-based tests for majors & strings As a bonus, this gets rid of `math/rand` and the associated `golangci-lint` complaint --- go.mod | 4 ++++ go.sum | 2 ++ major_test.go | 31 ++++++++++++++++++++----------- strings_test.go | 26 +++++++++++--------------- 4 files changed, 37 insertions(+), 26 deletions(-) create mode 100644 go.sum diff --git a/go.mod b/go.mod index 6d53ef0..c5450b1 100644 --- a/go.mod +++ b/go.mod @@ -1 +1,5 @@ module github.com/dtn7/cboring + +go 1.22 + +require pgregory.net/rapid v1.1.0 diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..09120c6 --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +pgregory.net/rapid v1.1.0 h1:CMa0sjHSru3puNx+J0MIAuiiEV4N0qj8/cMWGBBCsjw= +pgregory.net/rapid v1.1.0/go.mod h1:PY5XlDGj0+V1FCq0o192FdRhpKHGTRIWBgqjDBTrq04= diff --git a/major_test.go b/major_test.go index 1669ba3..e2b1e6e 100644 --- a/major_test.go +++ b/major_test.go @@ -3,9 +3,10 @@ package cboring import ( "bufio" "bytes" - "math/rand" "reflect" "testing" + + "pgregory.net/rapid" ) func TestReadMajorsSmall(t *testing.T) { @@ -166,14 +167,17 @@ func TestReadExampleArray(t *testing.T) { } } -func TestReadBigData(t *testing.T) { - var size = 1024 - var payload = make([]byte, size) - rand.Seed(0) - rand.Read(payload) +const ( + dataMinSize = 0 + dataMaxSize = 1024 + dataMinElems = 0 + dataMaxElems = 50000 +) - var elems = []int{100, 500, 1000, 5000, 10000, 50000} - for _, elem := range elems { +func TestReadBigData(t *testing.T) { + rapid.Check(t, func(t *rapid.T) { + payload := rapid.SliceOfN(rapid.Byte(), dataMinSize, dataMaxSize).Draw(t, "Payload") + elem := rapid.IntRange(dataMinElems, dataMaxElems).Draw(t, "Elements") buff := new(bytes.Buffer) bw := bufio.NewWriter(buff) @@ -192,9 +196,14 @@ func TestReadBigData(t *testing.T) { tmp, err := ReadByteString(br) if err != nil { t.Fatalf("Reading no %d errored: %v", i, err) - } else if len(tmp) != size { - t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), size) + } else if len(tmp) != len(payload) { + t.Fatalf("Length no %d mismatches: %d != %d", i, len(tmp), len(payload)) + } + for j := 0; j < len(tmp); j++ { + if tmp[j] != payload[j] { + t.Fatalf("Wrong value at %d: %d != %d", i, j, len(tmp)) + } } } - } + }) } diff --git a/strings_test.go b/strings_test.go index cec09dc..5f93421 100644 --- a/strings_test.go +++ b/strings_test.go @@ -3,9 +3,10 @@ package cboring import ( "bytes" "fmt" - "math/rand" "reflect" "testing" + + "pgregory.net/rapid" ) func TestByteString(t *testing.T) { @@ -44,20 +45,15 @@ func TestByteString(t *testing.T) { } } -func BenchmarkByteString(b *testing.B) { - sizes := []int{ - // Ridiculously small - 0, 1, 128, 256, - // Kibibytes - 1024, 10240, 102400, - // Mebibytes - 1048576, 10485760, 104857600, - } +const ( + stringsMinSize = 0 + stringMaxSize = 104857600 +) - for _, size := range sizes { - rndData := make([]byte, size) - rand.Seed(0) - rand.Read(rndData) +func BenchmarkByteString(b *testing.B) { + rapid.Check(b, func(t *rapid.T) { + rndData := rapid.SliceOfN(rapid.Byte(), stringsMinSize, stringMaxSize).Draw(t, "rndData") + size := len(rndData) b.Run(fmt.Sprintf("%d", size), func(b *testing.B) { for i := 0; i < b.N; i++ { @@ -87,7 +83,7 @@ func BenchmarkByteString(b *testing.B) { } } }) - } + }) } func TestReadTextString(t *testing.T) {