github.com/leanovate/gopter@v0.2.9/example_sqrt_test.go (about)

     1  package gopter_test
     2  
     3  import (
     4  	"math"
     5  
     6  	"github.com/leanovate/gopter"
     7  	"github.com/leanovate/gopter/gen"
     8  	"github.com/leanovate/gopter/prop"
     9  )
    10  
    11  func Example_sqrt() {
    12  	parameters := gopter.DefaultTestParameters()
    13  	parameters.Rng.Seed(1234) // Just for this example to generate reproducible results
    14  
    15  	properties := gopter.NewProperties(parameters)
    16  
    17  	properties.Property("greater one of all greater one", prop.ForAll(
    18  		func(v float64) bool {
    19  			return math.Sqrt(v) >= 1
    20  		},
    21  		gen.Float64().SuchThat(func(x float64) bool { return x >= 1.0 }),
    22  	))
    23  
    24  	properties.Property("squared is equal to value", prop.ForAll(
    25  		func(v float64) bool {
    26  			r := math.Sqrt(v)
    27  			return math.Abs(r*r-v) < 1e-10*v
    28  		},
    29  		gen.Float64().SuchThat(func(x float64) bool { return x >= 0.0 }),
    30  	))
    31  
    32  	// When using testing.T you might just use: properties.TestingRun(t)
    33  	properties.Run(gopter.ConsoleReporter(false))
    34  	// Output:
    35  	// + greater one of all greater one: OK, passed 100 tests.
    36  	// + squared is equal to value: OK, passed 100 tests.
    37  }