rsc.io/go@v0.0.0-20150416155037-e040fd465409/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 fBool(a bool) bool { return a } 14 15 type TestBoolAlias bool 16 17 func fBoolAlias(a TestBoolAlias) TestBoolAlias { return a } 18 19 func fFloat32(a float32) float32 { return a } 20 21 type TestFloat32Alias float32 22 23 func fFloat32Alias(a TestFloat32Alias) TestFloat32Alias { return a } 24 25 func fFloat64(a float64) float64 { return a } 26 27 type TestFloat64Alias float64 28 29 func fFloat64Alias(a TestFloat64Alias) TestFloat64Alias { return a } 30 31 func fComplex64(a complex64) complex64 { return a } 32 33 type TestComplex64Alias complex64 34 35 func fComplex64Alias(a TestComplex64Alias) TestComplex64Alias { return a } 36 37 func fComplex128(a complex128) complex128 { return a } 38 39 type TestComplex128Alias complex128 40 41 func fComplex128Alias(a TestComplex128Alias) TestComplex128Alias { return a } 42 43 func fInt16(a int16) int16 { return a } 44 45 type TestInt16Alias int16 46 47 func fInt16Alias(a TestInt16Alias) TestInt16Alias { return a } 48 49 func fInt32(a int32) int32 { return a } 50 51 type TestInt32Alias int32 52 53 func fInt32Alias(a TestInt32Alias) TestInt32Alias { return a } 54 55 func fInt64(a int64) int64 { return a } 56 57 type TestInt64Alias int64 58 59 func fInt64Alias(a TestInt64Alias) TestInt64Alias { return a } 60 61 func fInt8(a int8) int8 { return a } 62 63 type TestInt8Alias int8 64 65 func fInt8Alias(a TestInt8Alias) TestInt8Alias { return a } 66 67 func fInt(a int) int { return a } 68 69 type TestIntAlias int 70 71 func fIntAlias(a TestIntAlias) TestIntAlias { return a } 72 73 func fMap(a map[int]int) map[int]int { return a } 74 75 type TestMapAlias map[int]int 76 77 func fMapAlias(a TestMapAlias) TestMapAlias { return a } 78 79 func fSlice(a []byte) []byte { return a } 80 81 type TestSliceAlias []byte 82 83 func fSliceAlias(a TestSliceAlias) TestSliceAlias { return a } 84 85 func fString(a string) string { return a } 86 87 type TestStringAlias string 88 89 func fStringAlias(a TestStringAlias) TestStringAlias { return a } 90 91 type TestStruct struct { 92 A int 93 B string 94 } 95 96 func fStruct(a TestStruct) TestStruct { return a } 97 98 type TestStructAlias TestStruct 99 100 func fStructAlias(a TestStructAlias) TestStructAlias { return a } 101 102 func fUint16(a uint16) uint16 { return a } 103 104 type TestUint16Alias uint16 105 106 func fUint16Alias(a TestUint16Alias) TestUint16Alias { return a } 107 108 func fUint32(a uint32) uint32 { return a } 109 110 type TestUint32Alias uint32 111 112 func fUint32Alias(a TestUint32Alias) TestUint32Alias { return a } 113 114 func fUint64(a uint64) uint64 { return a } 115 116 type TestUint64Alias uint64 117 118 func fUint64Alias(a TestUint64Alias) TestUint64Alias { return a } 119 120 func fUint8(a uint8) uint8 { return a } 121 122 type TestUint8Alias uint8 123 124 func fUint8Alias(a TestUint8Alias) TestUint8Alias { return a } 125 126 func fUint(a uint) uint { return a } 127 128 type TestUintAlias uint 129 130 func fUintAlias(a TestUintAlias) TestUintAlias { return a } 131 132 func fUintptr(a uintptr) uintptr { return a } 133 134 type TestUintptrAlias uintptr 135 136 func fUintptrAlias(a TestUintptrAlias) TestUintptrAlias { return a } 137 138 func fIntptr(a *int) *int { 139 b := *a 140 return &b 141 } 142 143 type TestIntptrAlias *int 144 145 func fIntptrAlias(a TestIntptrAlias) TestIntptrAlias { return a } 146 147 func fArray(a [4]byte) [4]byte { return a } 148 149 type TestArrayAlias [4]byte 150 151 func fArrayAlias(a TestArrayAlias) TestArrayAlias { return a } 152 153 func reportError(property string, err error, t *testing.T) { 154 if err != nil { 155 t.Errorf("%s: %s", property, err) 156 } 157 } 158 159 func TestCheckEqual(t *testing.T) { 160 reportError("fBool", CheckEqual(fBool, fBool, nil), t) 161 reportError("fBoolAlias", CheckEqual(fBoolAlias, fBoolAlias, nil), t) 162 reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t) 163 reportError("fFloat32Alias", CheckEqual(fFloat32Alias, fFloat32Alias, nil), t) 164 reportError("fFloat64", CheckEqual(fFloat64, fFloat64, nil), t) 165 reportError("fFloat64Alias", CheckEqual(fFloat64Alias, fFloat64Alias, nil), t) 166 reportError("fComplex64", CheckEqual(fComplex64, fComplex64, nil), t) 167 reportError("fComplex64Alias", CheckEqual(fComplex64Alias, fComplex64Alias, nil), t) 168 reportError("fComplex128", CheckEqual(fComplex128, fComplex128, nil), t) 169 reportError("fComplex128Alias", CheckEqual(fComplex128Alias, fComplex128Alias, nil), t) 170 reportError("fInt16", CheckEqual(fInt16, fInt16, nil), t) 171 reportError("fInt16Alias", CheckEqual(fInt16Alias, fInt16Alias, nil), t) 172 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t) 173 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t) 174 reportError("fInt64", CheckEqual(fInt64, fInt64, nil), t) 175 reportError("fInt64Alias", CheckEqual(fInt64Alias, fInt64Alias, nil), t) 176 reportError("fInt8", CheckEqual(fInt8, fInt8, nil), t) 177 reportError("fInt8Alias", CheckEqual(fInt8Alias, fInt8Alias, nil), t) 178 reportError("fInt", CheckEqual(fInt, fInt, nil), t) 179 reportError("fIntAlias", CheckEqual(fIntAlias, fIntAlias, nil), t) 180 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t) 181 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t) 182 reportError("fMap", CheckEqual(fMap, fMap, nil), t) 183 reportError("fMapAlias", CheckEqual(fMapAlias, fMapAlias, nil), t) 184 reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t) 185 reportError("fSliceAlias", CheckEqual(fSliceAlias, fSliceAlias, nil), t) 186 reportError("fString", CheckEqual(fString, fString, nil), t) 187 reportError("fStringAlias", CheckEqual(fStringAlias, fStringAlias, nil), t) 188 reportError("fStruct", CheckEqual(fStruct, fStruct, nil), t) 189 reportError("fStructAlias", CheckEqual(fStructAlias, fStructAlias, nil), t) 190 reportError("fUint16", CheckEqual(fUint16, fUint16, nil), t) 191 reportError("fUint16Alias", CheckEqual(fUint16Alias, fUint16Alias, nil), t) 192 reportError("fUint32", CheckEqual(fUint32, fUint32, nil), t) 193 reportError("fUint32Alias", CheckEqual(fUint32Alias, fUint32Alias, nil), t) 194 reportError("fUint64", CheckEqual(fUint64, fUint64, nil), t) 195 reportError("fUint64Alias", CheckEqual(fUint64Alias, fUint64Alias, nil), t) 196 reportError("fUint8", CheckEqual(fUint8, fUint8, nil), t) 197 reportError("fUint8Alias", CheckEqual(fUint8Alias, fUint8Alias, nil), t) 198 reportError("fUint", CheckEqual(fUint, fUint, nil), t) 199 reportError("fUintAlias", CheckEqual(fUintAlias, fUintAlias, nil), t) 200 reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t) 201 reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t) 202 reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t) 203 reportError("fIntptrAlias", CheckEqual(fIntptrAlias, fIntptrAlias, nil), t) 204 reportError("fArray", CheckEqual(fArray, fArray, nil), t) 205 reportError("fArrayAlais", CheckEqual(fArrayAlias, fArrayAlias, nil), t) 206 } 207 208 // This tests that ArbitraryValue is working by checking that all the arbitrary 209 // values of type MyStruct have x = 42. 210 type myStruct struct { 211 x int 212 } 213 214 func (m myStruct) Generate(r *rand.Rand, _ int) reflect.Value { 215 return reflect.ValueOf(myStruct{x: 42}) 216 } 217 218 func myStructProperty(in myStruct) bool { return in.x == 42 } 219 220 func TestCheckProperty(t *testing.T) { 221 reportError("myStructProperty", Check(myStructProperty, nil), t) 222 } 223 224 func TestFailure(t *testing.T) { 225 f := func(x int) bool { return false } 226 err := Check(f, nil) 227 if err == nil { 228 t.Errorf("Check didn't return an error") 229 } 230 if _, ok := err.(*CheckError); !ok { 231 t.Errorf("Error was not a CheckError: %s", err) 232 } 233 234 err = CheckEqual(fUint, fUint32, nil) 235 if err == nil { 236 t.Errorf("#1 CheckEqual didn't return an error") 237 } 238 if _, ok := err.(SetupError); !ok { 239 t.Errorf("#1 Error was not a SetupError: %s", err) 240 } 241 242 err = CheckEqual(func(x, y int) {}, func(x int) {}, nil) 243 if err == nil { 244 t.Errorf("#2 CheckEqual didn't return an error") 245 } 246 if _, ok := err.(SetupError); !ok { 247 t.Errorf("#2 Error was not a SetupError: %s", err) 248 } 249 250 err = CheckEqual(func(x int) int { return 0 }, func(x int) int32 { return 0 }, nil) 251 if err == nil { 252 t.Errorf("#3 CheckEqual didn't return an error") 253 } 254 if _, ok := err.(SetupError); !ok { 255 t.Errorf("#3 Error was not a SetupError: %s", err) 256 } 257 }