github.com/AndrienkoAleksandr/go@v0.0.19/src/testing/quick/quick_test.go (about)

     1  // Copyright 2009 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package quick
     6  
     7  import (
     8  	"math/rand"
     9  	"reflect"
    10  	"testing"
    11  )
    12  
    13  func fArray(a [4]byte) [4]byte { return a }
    14  
    15  type TestArrayAlias [4]byte
    16  
    17  func fArrayAlias(a TestArrayAlias) TestArrayAlias { return a }
    18  
    19  func fBool(a bool) bool { return a }
    20  
    21  type TestBoolAlias bool
    22  
    23  func fBoolAlias(a TestBoolAlias) TestBoolAlias { return a }
    24  
    25  func fFloat32(a float32) float32 { return a }
    26  
    27  type TestFloat32Alias float32
    28  
    29  func fFloat32Alias(a TestFloat32Alias) TestFloat32Alias { return a }
    30  
    31  func fFloat64(a float64) float64 { return a }
    32  
    33  type TestFloat64Alias float64
    34  
    35  func fFloat64Alias(a TestFloat64Alias) TestFloat64Alias { return a }
    36  
    37  func fComplex64(a complex64) complex64 { return a }
    38  
    39  type TestComplex64Alias complex64
    40  
    41  func fComplex64Alias(a TestComplex64Alias) TestComplex64Alias { return a }
    42  
    43  func fComplex128(a complex128) complex128 { return a }
    44  
    45  type TestComplex128Alias complex128
    46  
    47  func fComplex128Alias(a TestComplex128Alias) TestComplex128Alias { return a }
    48  
    49  func fInt16(a int16) int16 { return a }
    50  
    51  type TestInt16Alias int16
    52  
    53  func fInt16Alias(a TestInt16Alias) TestInt16Alias { return a }
    54  
    55  func fInt32(a int32) int32 { return a }
    56  
    57  type TestInt32Alias int32
    58  
    59  func fInt32Alias(a TestInt32Alias) TestInt32Alias { return a }
    60  
    61  func fInt64(a int64) int64 { return a }
    62  
    63  type TestInt64Alias int64
    64  
    65  func fInt64Alias(a TestInt64Alias) TestInt64Alias { return a }
    66  
    67  func fInt8(a int8) int8 { return a }
    68  
    69  type TestInt8Alias int8
    70  
    71  func fInt8Alias(a TestInt8Alias) TestInt8Alias { return a }
    72  
    73  func fInt(a int) int { return a }
    74  
    75  type TestIntAlias int
    76  
    77  func fIntAlias(a TestIntAlias) TestIntAlias { return a }
    78  
    79  func fMap(a map[int]int) map[int]int { return a }
    80  
    81  type TestMapAlias map[int]int
    82  
    83  func fMapAlias(a TestMapAlias) TestMapAlias { return a }
    84  
    85  func fPtr(a *int) *int {
    86  	if a == nil {
    87  		return nil
    88  	}
    89  	b := *a
    90  	return &b
    91  }
    92  
    93  type TestPtrAlias *int
    94  
    95  func fPtrAlias(a TestPtrAlias) TestPtrAlias { return a }
    96  
    97  func fSlice(a []byte) []byte { return a }
    98  
    99  type TestSliceAlias []byte
   100  
   101  func fSliceAlias(a TestSliceAlias) TestSliceAlias { return a }
   102  
   103  func fString(a string) string { return a }
   104  
   105  type TestStringAlias string
   106  
   107  func fStringAlias(a TestStringAlias) TestStringAlias { return a }
   108  
   109  type TestStruct struct {
   110  	A int
   111  	B string
   112  }
   113  
   114  func fStruct(a TestStruct) TestStruct { return a }
   115  
   116  type TestStructAlias TestStruct
   117  
   118  func fStructAlias(a TestStructAlias) TestStructAlias { return a }
   119  
   120  func fUint16(a uint16) uint16 { return a }
   121  
   122  type TestUint16Alias uint16
   123  
   124  func fUint16Alias(a TestUint16Alias) TestUint16Alias { return a }
   125  
   126  func fUint32(a uint32) uint32 { return a }
   127  
   128  type TestUint32Alias uint32
   129  
   130  func fUint32Alias(a TestUint32Alias) TestUint32Alias { return a }
   131  
   132  func fUint64(a uint64) uint64 { return a }
   133  
   134  type TestUint64Alias uint64
   135  
   136  func fUint64Alias(a TestUint64Alias) TestUint64Alias { return a }
   137  
   138  func fUint8(a uint8) uint8 { return a }
   139  
   140  type TestUint8Alias uint8
   141  
   142  func fUint8Alias(a TestUint8Alias) TestUint8Alias { return a }
   143  
   144  func fUint(a uint) uint { return a }
   145  
   146  type TestUintAlias uint
   147  
   148  func fUintAlias(a TestUintAlias) TestUintAlias { return a }
   149  
   150  func fUintptr(a uintptr) uintptr { return a }
   151  
   152  type TestUintptrAlias uintptr
   153  
   154  func fUintptrAlias(a TestUintptrAlias) TestUintptrAlias { return a }
   155  
   156  func reportError(property string, err error, t *testing.T) {
   157  	if err != nil {
   158  		t.Errorf("%s: %s", property, err)
   159  	}
   160  }
   161  
   162  func TestCheckEqual(t *testing.T) {
   163  	reportError("fArray", CheckEqual(fArray, fArray, nil), t)
   164  	reportError("fArrayAlias", CheckEqual(fArrayAlias, fArrayAlias, nil), t)
   165  	reportError("fBool", CheckEqual(fBool, fBool, nil), t)
   166  	reportError("fBoolAlias", CheckEqual(fBoolAlias, fBoolAlias, nil), t)
   167  	reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t)
   168  	reportError("fFloat32Alias", CheckEqual(fFloat32Alias, fFloat32Alias, nil), t)
   169  	reportError("fFloat64", CheckEqual(fFloat64, fFloat64, nil), t)
   170  	reportError("fFloat64Alias", CheckEqual(fFloat64Alias, fFloat64Alias, nil), t)
   171  	reportError("fComplex64", CheckEqual(fComplex64, fComplex64, nil), t)
   172  	reportError("fComplex64Alias", CheckEqual(fComplex64Alias, fComplex64Alias, nil), t)
   173  	reportError("fComplex128", CheckEqual(fComplex128, fComplex128, nil), t)
   174  	reportError("fComplex128Alias", CheckEqual(fComplex128Alias, fComplex128Alias, nil), t)
   175  	reportError("fInt16", CheckEqual(fInt16, fInt16, nil), t)
   176  	reportError("fInt16Alias", CheckEqual(fInt16Alias, fInt16Alias, nil), t)
   177  	reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
   178  	reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t)
   179  	reportError("fInt64", CheckEqual(fInt64, fInt64, nil), t)
   180  	reportError("fInt64Alias", CheckEqual(fInt64Alias, fInt64Alias, nil), t)
   181  	reportError("fInt8", CheckEqual(fInt8, fInt8, nil), t)
   182  	reportError("fInt8Alias", CheckEqual(fInt8Alias, fInt8Alias, nil), t)
   183  	reportError("fInt", CheckEqual(fInt, fInt, nil), t)
   184  	reportError("fIntAlias", CheckEqual(fIntAlias, fIntAlias, nil), t)
   185  	reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
   186  	reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t)
   187  	reportError("fMap", CheckEqual(fMap, fMap, nil), t)
   188  	reportError("fMapAlias", CheckEqual(fMapAlias, fMapAlias, nil), t)
   189  	reportError("fPtr", CheckEqual(fPtr, fPtr, nil), t)
   190  	reportError("fPtrAlias", CheckEqual(fPtrAlias, fPtrAlias, nil), t)
   191  	reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t)
   192  	reportError("fSliceAlias", CheckEqual(fSliceAlias, fSliceAlias, nil), t)
   193  	reportError("fString", CheckEqual(fString, fString, nil), t)
   194  	reportError("fStringAlias", CheckEqual(fStringAlias, fStringAlias, nil), t)
   195  	reportError("fStruct", CheckEqual(fStruct, fStruct, nil), t)
   196  	reportError("fStructAlias", CheckEqual(fStructAlias, fStructAlias, nil), t)
   197  	reportError("fUint16", CheckEqual(fUint16, fUint16, nil), t)
   198  	reportError("fUint16Alias", CheckEqual(fUint16Alias, fUint16Alias, nil), t)
   199  	reportError("fUint32", CheckEqual(fUint32, fUint32, nil), t)
   200  	reportError("fUint32Alias", CheckEqual(fUint32Alias, fUint32Alias, nil), t)
   201  	reportError("fUint64", CheckEqual(fUint64, fUint64, nil), t)
   202  	reportError("fUint64Alias", CheckEqual(fUint64Alias, fUint64Alias, nil), t)
   203  	reportError("fUint8", CheckEqual(fUint8, fUint8, nil), t)
   204  	reportError("fUint8Alias", CheckEqual(fUint8Alias, fUint8Alias, nil), t)
   205  	reportError("fUint", CheckEqual(fUint, fUint, nil), t)
   206  	reportError("fUintAlias", CheckEqual(fUintAlias, fUintAlias, nil), t)
   207  	reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t)
   208  	reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t)
   209  }
   210  
   211  // This tests that ArbitraryValue is working by checking that all the arbitrary
   212  // values of type MyStruct have x = 42.
   213  type myStruct struct {
   214  	x int
   215  }
   216  
   217  func (m myStruct) Generate(r *rand.Rand, _ int) reflect.Value {
   218  	return reflect.ValueOf(myStruct{x: 42})
   219  }
   220  
   221  func myStructProperty(in myStruct) bool { return in.x == 42 }
   222  
   223  func TestCheckProperty(t *testing.T) {
   224  	reportError("myStructProperty", Check(myStructProperty, nil), t)
   225  }
   226  
   227  func TestFailure(t *testing.T) {
   228  	f := func(x int) bool { return false }
   229  	err := Check(f, nil)
   230  	if err == nil {
   231  		t.Errorf("Check didn't return an error")
   232  	}
   233  	if _, ok := err.(*CheckError); !ok {
   234  		t.Errorf("Error was not a CheckError: %s", err)
   235  	}
   236  
   237  	err = CheckEqual(fUint, fUint32, nil)
   238  	if err == nil {
   239  		t.Errorf("#1 CheckEqual didn't return an error")
   240  	}
   241  	if _, ok := err.(SetupError); !ok {
   242  		t.Errorf("#1 Error was not a SetupError: %s", err)
   243  	}
   244  
   245  	err = CheckEqual(func(x, y int) {}, func(x int) {}, nil)
   246  	if err == nil {
   247  		t.Errorf("#2 CheckEqual didn't return an error")
   248  	}
   249  	if _, ok := err.(SetupError); !ok {
   250  		t.Errorf("#2 Error was not a SetupError: %s", err)
   251  	}
   252  
   253  	err = CheckEqual(func(x int) int { return 0 }, func(x int) int32 { return 0 }, nil)
   254  	if err == nil {
   255  		t.Errorf("#3 CheckEqual didn't return an error")
   256  	}
   257  	if _, ok := err.(SetupError); !ok {
   258  		t.Errorf("#3 Error was not a SetupError: %s", err)
   259  	}
   260  }
   261  
   262  // Recursive data structures didn't terminate.
   263  // Issues 8818 and 11148.
   264  func TestRecursive(t *testing.T) {
   265  	type R struct {
   266  		Ptr      *R
   267  		SliceP   []*R
   268  		Slice    []R
   269  		Map      map[int]R
   270  		MapP     map[int]*R
   271  		MapR     map[*R]*R
   272  		SliceMap []map[int]R
   273  	}
   274  
   275  	f := func(r R) bool { return true }
   276  	Check(f, nil)
   277  }
   278  
   279  func TestEmptyStruct(t *testing.T) {
   280  	f := func(struct{}) bool { return true }
   281  	Check(f, nil)
   282  }
   283  
   284  type (
   285  	A struct{ B *B }
   286  	B struct{ A *A }
   287  )
   288  
   289  func TestMutuallyRecursive(t *testing.T) {
   290  	f := func(a A) bool { return true }
   291  	Check(f, nil)
   292  }
   293  
   294  // Some serialization formats (e.g. encoding/pem) cannot distinguish
   295  // between a nil and an empty map or slice, so avoid generating the
   296  // zero value for these.
   297  func TestNonZeroSliceAndMap(t *testing.T) {
   298  	type Q struct {
   299  		M map[int]int
   300  		S []int
   301  	}
   302  	f := func(q Q) bool {
   303  		return q.M != nil && q.S != nil
   304  	}
   305  	err := Check(f, nil)
   306  	if err != nil {
   307  		t.Fatal(err)
   308  	}
   309  }
   310  
   311  func TestInt64(t *testing.T) {
   312  	var lo, hi int64
   313  	f := func(x int64) bool {
   314  		if x < lo {
   315  			lo = x
   316  		}
   317  		if x > hi {
   318  			hi = x
   319  		}
   320  		return true
   321  	}
   322  	cfg := &Config{MaxCount: 10000}
   323  	Check(f, cfg)
   324  	if uint64(lo)>>62 == 0 || uint64(hi)>>62 == 0 {
   325  		t.Errorf("int64 returned range %#016x,%#016x; does not look like full range", lo, hi)
   326  	}
   327  }