github.com/yanyiwu/go@v0.0.0-20150106053140-03d6637dbb7f/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 reportError(property string, err error, t *testing.T) { 148 if err != nil { 149 t.Errorf("%s: %s", property, err) 150 } 151 } 152 153 func TestCheckEqual(t *testing.T) { 154 reportError("fBool", CheckEqual(fBool, fBool, nil), t) 155 reportError("fBoolAlias", CheckEqual(fBoolAlias, fBoolAlias, nil), t) 156 reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t) 157 reportError("fFloat32Alias", CheckEqual(fFloat32Alias, fFloat32Alias, nil), t) 158 reportError("fFloat64", CheckEqual(fFloat64, fFloat64, nil), t) 159 reportError("fFloat64Alias", CheckEqual(fFloat64Alias, fFloat64Alias, nil), t) 160 reportError("fComplex64", CheckEqual(fComplex64, fComplex64, nil), t) 161 reportError("fComplex64Alias", CheckEqual(fComplex64Alias, fComplex64Alias, nil), t) 162 reportError("fComplex128", CheckEqual(fComplex128, fComplex128, nil), t) 163 reportError("fComplex128Alias", CheckEqual(fComplex128Alias, fComplex128Alias, nil), t) 164 reportError("fInt16", CheckEqual(fInt16, fInt16, nil), t) 165 reportError("fInt16Alias", CheckEqual(fInt16Alias, fInt16Alias, nil), t) 166 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t) 167 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t) 168 reportError("fInt64", CheckEqual(fInt64, fInt64, nil), t) 169 reportError("fInt64Alias", CheckEqual(fInt64Alias, fInt64Alias, nil), t) 170 reportError("fInt8", CheckEqual(fInt8, fInt8, nil), t) 171 reportError("fInt8Alias", CheckEqual(fInt8Alias, fInt8Alias, nil), t) 172 reportError("fInt", CheckEqual(fInt, fInt, nil), t) 173 reportError("fIntAlias", CheckEqual(fIntAlias, fIntAlias, nil), t) 174 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t) 175 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t) 176 reportError("fMap", CheckEqual(fMap, fMap, nil), t) 177 reportError("fMapAlias", CheckEqual(fMapAlias, fMapAlias, nil), t) 178 reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t) 179 reportError("fSliceAlias", CheckEqual(fSliceAlias, fSliceAlias, nil), t) 180 reportError("fString", CheckEqual(fString, fString, nil), t) 181 reportError("fStringAlias", CheckEqual(fStringAlias, fStringAlias, nil), t) 182 reportError("fStruct", CheckEqual(fStruct, fStruct, nil), t) 183 reportError("fStructAlias", CheckEqual(fStructAlias, fStructAlias, nil), t) 184 reportError("fUint16", CheckEqual(fUint16, fUint16, nil), t) 185 reportError("fUint16Alias", CheckEqual(fUint16Alias, fUint16Alias, nil), t) 186 reportError("fUint32", CheckEqual(fUint32, fUint32, nil), t) 187 reportError("fUint32Alias", CheckEqual(fUint32Alias, fUint32Alias, nil), t) 188 reportError("fUint64", CheckEqual(fUint64, fUint64, nil), t) 189 reportError("fUint64Alias", CheckEqual(fUint64Alias, fUint64Alias, nil), t) 190 reportError("fUint8", CheckEqual(fUint8, fUint8, nil), t) 191 reportError("fUint8Alias", CheckEqual(fUint8Alias, fUint8Alias, nil), t) 192 reportError("fUint", CheckEqual(fUint, fUint, nil), t) 193 reportError("fUintAlias", CheckEqual(fUintAlias, fUintAlias, nil), t) 194 reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t) 195 reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t) 196 reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t) 197 reportError("fIntptrAlias", CheckEqual(fIntptrAlias, fIntptrAlias, nil), t) 198 } 199 200 // This tests that ArbitraryValue is working by checking that all the arbitrary 201 // values of type MyStruct have x = 42. 202 type myStruct struct { 203 x int 204 } 205 206 func (m myStruct) Generate(r *rand.Rand, _ int) reflect.Value { 207 return reflect.ValueOf(myStruct{x: 42}) 208 } 209 210 func myStructProperty(in myStruct) bool { return in.x == 42 } 211 212 func TestCheckProperty(t *testing.T) { 213 reportError("myStructProperty", Check(myStructProperty, nil), t) 214 } 215 216 func TestFailure(t *testing.T) { 217 f := func(x int) bool { return false } 218 err := Check(f, nil) 219 if err == nil { 220 t.Errorf("Check didn't return an error") 221 } 222 if _, ok := err.(*CheckError); !ok { 223 t.Errorf("Error was not a CheckError: %s", err) 224 } 225 226 err = CheckEqual(fUint, fUint32, nil) 227 if err == nil { 228 t.Errorf("#1 CheckEqual didn't return an error") 229 } 230 if _, ok := err.(SetupError); !ok { 231 t.Errorf("#1 Error was not a SetupError: %s", err) 232 } 233 234 err = CheckEqual(func(x, y int) {}, func(x int) {}, nil) 235 if err == nil { 236 t.Errorf("#2 CheckEqual didn't return an error") 237 } 238 if _, ok := err.(SetupError); !ok { 239 t.Errorf("#2 Error was not a SetupError: %s", err) 240 } 241 242 err = CheckEqual(func(x int) int { return 0 }, func(x int) int32 { return 0 }, nil) 243 if err == nil { 244 t.Errorf("#3 CheckEqual didn't return an error") 245 } 246 if _, ok := err.(SetupError); !ok { 247 t.Errorf("#3 Error was not a SetupError: %s", err) 248 } 249 }