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

     1  package prop_test
     2  
     3  import (
     4  	"math"
     5  	"testing"
     6  
     7  	"github.com/leanovate/gopter"
     8  	"github.com/leanovate/gopter/gen"
     9  	"github.com/leanovate/gopter/prop"
    10  )
    11  
    12  func TestSqrt(t *testing.T) {
    13  	properties := gopter.NewProperties(nil)
    14  
    15  	properties.Property("greater one of all greater one", prop.ForAll(
    16  		func(v float64) bool {
    17  			return math.Sqrt(v) >= 1
    18  		},
    19  		gen.Float64Range(1, math.MaxFloat64),
    20  	))
    21  
    22  	properties.Property("greater one of all greater one alternative", prop.ForAll1(
    23  		gen.Float64Range(1, math.MaxFloat64),
    24  		func(v interface{}) (interface{}, error) {
    25  			return math.Sqrt(v.(float64)) >= 1, nil
    26  		},
    27  	))
    28  
    29  	properties.Property("squared is equal to value", prop.ForAll(
    30  		func(v float64) bool {
    31  			r := math.Sqrt(v)
    32  			return math.Abs(r*r-v) < 1e-10*v
    33  		},
    34  		gen.Float64Range(0, math.MaxFloat64),
    35  	))
    36  
    37  	properties.Property("squared is equal to value alternative", prop.ForAll1(
    38  		gen.Float64Range(0, math.MaxFloat64),
    39  		func(v interface{}) (interface{}, error) {
    40  			s := v.(float64)
    41  			r := math.Sqrt(s)
    42  			return math.Abs(r*r-s) < 1e-10*s, nil
    43  		},
    44  	))
    45  
    46  	properties.TestingRun(t)
    47  
    48  	fail := prop.ForAll(0)
    49  	result := fail(gopter.DefaultGenParameters())
    50  	if result.Status != gopter.PropError {
    51  		t.Errorf("Invalid result: %#v", result)
    52  	}
    53  
    54  	undecided := prop.ForAll(func(a int) bool {
    55  		return true
    56  	}, gen.Int().SuchThat(func(interface{}) bool {
    57  		return false
    58  	}))
    59  	result = undecided(gopter.DefaultGenParameters())
    60  	if result.Status != gopter.PropUndecided {
    61  		t.Errorf("Invalid result: %#v", result)
    62  	}
    63  }