flyingmutant / rapid

Rapid is a modern Go property-based testing library
https://pkg.go.dev/pgregory.net/rapid
Mozilla Public License 2.0
576 stars 25 forks source link

`Make` cannot handle recursive types #67

Closed varungandhi-src closed 2 months ago

varungandhi-src commented 2 months ago

Thanks for creating this library. I noticed what seems to be a bug in the implementation of Make

import (
    "testing"
    "github.com/stretchr/testify/require"
    "pgregory.net/rapid"
)

type Tree struct {
    value    int
    children []Tree
}

func TestPanics(t *testing.T) {
    require.NotPanics(t, func() {
        rapid.Check(t, func(t *rapid.T) {
            _ = rapid.SliceOfN(rapid.Make[Tree](), 10, 10).Draw(t, "tree")
        })
    })
}

This causes a stack overflow with Go 1.22.1

It would be valuable to fix the implementation of Make or add a warning to the docs indicating that it shouldn't be used with recursive types directly.

flyingmutant commented 2 months ago

Thanks for the report. This is more of a missing feature than a bug: to properly terminate all recursive generators, rapid has to track the recursion depth and lower the probability of continuing the recursion the deeper we are, and it does not do it yet. I've added the warning to the documentation about that.

varungandhi-src commented 2 months ago

Thanks!