github.com/leanovate/gopter@v0.2.9/prop/example_quadratic_test.go (about) 1 package prop_test 2 3 import ( 4 "errors" 5 "math" 6 7 "github.com/leanovate/gopter" 8 "github.com/leanovate/gopter/gen" 9 "github.com/leanovate/gopter/prop" 10 ) 11 12 func solveQuadratic(a, b, c float64) (float64, float64, error) { 13 if a == 0 { 14 return 0, 0, errors.New("No solution") 15 } 16 v := b*b - 4*a*c 17 if v < 0 { 18 return 0, 0, errors.New("No solution") 19 } 20 v = math.Sqrt(v) 21 return (-b + v) / 2 / a, (-b - v) / 2 / a, nil 22 } 23 24 func Example_quadratic() { 25 parameters := gopter.DefaultTestParametersWithSeed(1234) // Example should generate reproducible results, otherwise DefaultTestParameters() will suffice 26 27 properties := gopter.NewProperties(parameters) 28 29 properties.Property("solve quadratic", prop.ForAll( 30 func(a, b, c float64) bool { 31 x1, x2, err := solveQuadratic(a, b, c) 32 if err != nil { 33 return true 34 } 35 return math.Abs(a*x1*x1+b*x1+c) < 1e-5 && math.Abs(a*x2*x2+b*x2+c) < 1e-5 36 }, 37 gen.Float64(), 38 gen.Float64(), 39 gen.Float64(), 40 )) 41 42 properties.Property("solve quadratic with resonable ranges", prop.ForAll( 43 func(a, b, c float64) bool { 44 x1, x2, err := solveQuadratic(a, b, c) 45 if err != nil { 46 return true 47 } 48 return math.Abs(a*x1*x1+b*x1+c) < 1e-5 && math.Abs(a*x2*x2+b*x2+c) < 1e-5 49 }, 50 gen.Float64Range(-1e8, 1e8), 51 gen.Float64Range(-1e8, 1e8), 52 gen.Float64Range(-1e8, 1e8), 53 )) 54 55 // When using testing.T you might just use: properties.TestingRun(t) 56 properties.Run(gopter.ConsoleReporter(false)) 57 // Output: 58 // ! solve quadratic: Falsified after 0 passed tests. 59 // ARG_0: -1.4667384313385178e-05 60 // ARG_0_ORIGINAL (187 shrinks): -1.0960555181801604e+51 61 // ARG_1: 0 62 // ARG_1_ORIGINAL (1 shrinks): -1.1203884793568249e+96 63 // ARG_2: 6.481285637227244e+10 64 // ARG_2_ORIGINAL (905 shrinks): 1.512647219322138e+281 65 // + solve quadratic with resonable ranges: OK, passed 100 tests. 66 }