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