Panics on struct arbitraries
hasufell opened this issue · 1 comments
hasufell commented
The adjusted example from the doc:
package main
import (
"errors"
"math/cmplx"
"github.com/leanovate/gopter"
"github.com/leanovate/gopter/arbitrary"
"github.com/leanovate/gopter/gen"
)
type QudraticEquation struct {
A, B, C complex128
}
func (q QudraticEquation) Eval(x complex128) complex128 {
return q.A*x*x + q.B*x + q.C
}
func (q QudraticEquation) Solve() (complex128, complex128, error) {
if q.A == 0 {
return 0, 0, errors.New("No solution")
}
v := q.B*q.B - 4*q.A*q.C
v = cmplx.Sqrt(v)
return (-q.B + v) / 2 / q.A, (-q.B - v) / 2 / q.A, nil
}
func main() {
parameters := gopter.DefaultTestParameters()
parameters.Rng.Seed(1234) // Just for this example to generate reproducable results
arbitraries := arbitrary.DefaultArbitraries()
arbitraries.RegisterGen(gen.Complex128Box(-1e8-1e8i, 1e8+1e8i)) // Only use complex values within a range
properties := gopter.NewProperties(parameters)
properties.Property("Quadratic equations can be solved", arbitraries.ForAll(
func(quadratic QudraticEquation) bool {
x1, x2, err := quadratic.Solve()
if err != nil {
return true
}
return cmplx.Abs(quadratic.Eval(x1)) < 1e-5 && cmplx.Abs(quadratic.Eval(x2)) < 1e-5
}))
properties.Property("Quadratic equations can be solved alternative", arbitraries.ForAll(
func(a, b, c complex128) bool {
quadratic := &QudraticEquation{
A: a,
B: b,
C: c,
}
x1, x2, err := quadratic.Solve()
if err != nil {
return true
}
return cmplx.Abs(quadratic.Eval(x1)) < 1e-5 && cmplx.Abs(quadratic.Eval(x2)) < 1e-5
}))
// When using testing.T you might just use: properties.TestingRun(t)
properties.Run(gopter.ConsoleReporter(false))
}
Output:
! Quadratic equations can be solved: Error on property evaluation after 0
passed tests: Check paniced: runtime error: invalid memory address or nil
pointer dereference goroutine 1 [running]:
runtime/debug.Stack(0xc42004ba90, 0x4ffa40, 0x5d0ca0)
/usr/x86_64-pc-linux-gnu/lib/go/src/runtime/debug/stack.go:24 +0xa7
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.Save
Prop.func1.1(0xc42004bd20)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/prop.go:19 +0x6e
panic(0x4ffa40, 0x5d0ca0)
/usr/x86_64-pc-linux-gnu/lib/go/src/runtime/panic.go:505 +0x229
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter/prop
.ForAll.func1(0xc420098460, 0x52aaf8)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/prop/forall.go:31 +0x13b
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.Save
Prop.func1(0xc420098460, 0x0)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/prop.go:24 +0x6c
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.Prop
.Check.func1(0x0, 0xc42007e3d0, 0x5d7f60)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/prop.go:52 +0x15c
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.(*ru
nner).runWorkers(0xc420082510, 0x0)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/runner.go:45 +0x2d9
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.Prop
.Check(0xc42007e3a0, 0xc4200a05c0, 0x52741e)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/prop.go:110 +0x1af
github.com/hasufell/gopter-examples/vendor/github.com/leanovate/gopter.(*Pr
operties).Run(0xc420082480, 0x53a700, 0xc420098420, 0x2d)
/home/julian/go/src/github.com/hasufell/gopter-examples/vendor/github.com/
leanovate/gopter/properties.go:37 +0xc9
main.main()
/home/julian/go/src/github.com/hasufell/gopter-examples/gopterexamples.go:
64 +0x2af
+ Quadratic equations can be solved alternative: OK, passed 100 tests.
untoldwind commented
Extended the example with both variants (i.e. struct and pointer to struct)