blob: ca340fe76d6d98dfad071450e6bc5e7af5e90d5b [file] [log] [blame]
Adam Langley878d0e12009-10-28 15:16:20 -07001// 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
5package quick
6
7import (
Rob Pike45e3bcb2011-11-08 15:41:54 -08008 "math/rand"
Robert Griesemer45ca9f72009-12-15 15:41:46 -08009 "reflect"
10 "testing"
Adam Langley878d0e12009-10-28 15:16:20 -070011)
12
Robert Griesemer45ca9f72009-12-15 15:41:46 -080013func fBool(a bool) bool { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070014
Jonathan Hseuc691ae62013-05-14 17:14:59 -070015type TestBoolAlias bool
16
17func fBoolAlias(a TestBoolAlias) TestBoolAlias { return a }
18
Robert Griesemer45ca9f72009-12-15 15:41:46 -080019func fFloat32(a float32) float32 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070020
Jonathan Hseuc691ae62013-05-14 17:14:59 -070021type TestFloat32Alias float32
22
23func fFloat32Alias(a TestFloat32Alias) TestFloat32Alias { return a }
24
Robert Griesemer45ca9f72009-12-15 15:41:46 -080025func fFloat64(a float64) float64 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070026
Jonathan Hseuc691ae62013-05-14 17:14:59 -070027type TestFloat64Alias float64
28
29func fFloat64Alias(a TestFloat64Alias) TestFloat64Alias { return a }
30
Russ Coxf2b5a072011-01-19 23:09:00 -050031func fComplex64(a complex64) complex64 { return a }
32
Jonathan Hseuc691ae62013-05-14 17:14:59 -070033type TestComplex64Alias complex64
34
35func fComplex64Alias(a TestComplex64Alias) TestComplex64Alias { return a }
36
Russ Coxf2b5a072011-01-19 23:09:00 -050037func fComplex128(a complex128) complex128 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070038
Jonathan Hseuc691ae62013-05-14 17:14:59 -070039type TestComplex128Alias complex128
40
41func fComplex128Alias(a TestComplex128Alias) TestComplex128Alias { return a }
42
Robert Griesemer45ca9f72009-12-15 15:41:46 -080043func fInt16(a int16) int16 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070044
Jonathan Hseuc691ae62013-05-14 17:14:59 -070045type TestInt16Alias int16
46
47func fInt16Alias(a TestInt16Alias) TestInt16Alias { return a }
48
Robert Griesemer45ca9f72009-12-15 15:41:46 -080049func fInt32(a int32) int32 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070050
Jonathan Hseuc691ae62013-05-14 17:14:59 -070051type TestInt32Alias int32
52
53func fInt32Alias(a TestInt32Alias) TestInt32Alias { return a }
54
Robert Griesemer45ca9f72009-12-15 15:41:46 -080055func fInt64(a int64) int64 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070056
Jonathan Hseuc691ae62013-05-14 17:14:59 -070057type TestInt64Alias int64
58
59func fInt64Alias(a TestInt64Alias) TestInt64Alias { return a }
60
Robert Griesemer45ca9f72009-12-15 15:41:46 -080061func fInt8(a int8) int8 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070062
Jonathan Hseuc691ae62013-05-14 17:14:59 -070063type TestInt8Alias int8
64
65func fInt8Alias(a TestInt8Alias) TestInt8Alias { return a }
66
Robert Griesemer45ca9f72009-12-15 15:41:46 -080067func fInt(a int) int { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070068
Jonathan Hseuc691ae62013-05-14 17:14:59 -070069type TestIntAlias int
70
71func fIntAlias(a TestIntAlias) TestIntAlias { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070072
Robert Griesemer45ca9f72009-12-15 15:41:46 -080073func fMap(a map[int]int) map[int]int { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070074
Jonathan Hseuc691ae62013-05-14 17:14:59 -070075type TestMapAlias map[int]int
76
77func fMapAlias(a TestMapAlias) TestMapAlias { return a }
78
Robert Griesemer45ca9f72009-12-15 15:41:46 -080079func fSlice(a []byte) []byte { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070080
Jonathan Hseuc691ae62013-05-14 17:14:59 -070081type TestSliceAlias []byte
82
83func fSliceAlias(a TestSliceAlias) TestSliceAlias { return a }
84
Robert Griesemer45ca9f72009-12-15 15:41:46 -080085func fString(a string) string { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070086
Jonathan Hseuc691ae62013-05-14 17:14:59 -070087type TestStringAlias string
88
89func fStringAlias(a TestStringAlias) TestStringAlias { return a }
90
Adam Langley878d0e12009-10-28 15:16:20 -070091type TestStruct struct {
Robert Griesemer45ca9f72009-12-15 15:41:46 -080092 A int
93 B string
Adam Langley878d0e12009-10-28 15:16:20 -070094}
95
Robert Griesemer45ca9f72009-12-15 15:41:46 -080096func fStruct(a TestStruct) TestStruct { return a }
Adam Langley878d0e12009-10-28 15:16:20 -070097
Jonathan Hseuc691ae62013-05-14 17:14:59 -070098type TestStructAlias TestStruct
99
100func fStructAlias(a TestStructAlias) TestStructAlias { return a }
101
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800102func fUint16(a uint16) uint16 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700103
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700104type TestUint16Alias uint16
105
106func fUint16Alias(a TestUint16Alias) TestUint16Alias { return a }
107
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800108func fUint32(a uint32) uint32 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700109
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700110type TestUint32Alias uint32
111
112func fUint32Alias(a TestUint32Alias) TestUint32Alias { return a }
113
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800114func fUint64(a uint64) uint64 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700115
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700116type TestUint64Alias uint64
117
118func fUint64Alias(a TestUint64Alias) TestUint64Alias { return a }
119
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800120func fUint8(a uint8) uint8 { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700121
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700122type TestUint8Alias uint8
123
124func fUint8Alias(a TestUint8Alias) TestUint8Alias { return a }
125
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800126func fUint(a uint) uint { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700127
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700128type TestUintAlias uint
129
130func fUintAlias(a TestUintAlias) TestUintAlias { return a }
131
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800132func fUintptr(a uintptr) uintptr { return a }
Adam Langley878d0e12009-10-28 15:16:20 -0700133
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700134type TestUintptrAlias uintptr
135
136func fUintptrAlias(a TestUintptrAlias) TestUintptrAlias { return a }
137
Adam Langley878d0e12009-10-28 15:16:20 -0700138func fIntptr(a *int) *int {
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800139 b := *a
140 return &b
Adam Langley878d0e12009-10-28 15:16:20 -0700141}
142
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700143type TestIntptrAlias *int
144
145func fIntptrAlias(a TestIntptrAlias) TestIntptrAlias { return a }
146
Chris Kastorff3e9ed272015-02-04 04:43:00 -0800147func fArray(a [4]byte) [4]byte { return a }
148
149type TestArrayAlias [4]byte
150
151func fArrayAlias(a TestArrayAlias) TestArrayAlias { return a }
152
Russ Coxeb692922011-11-01 22:05:34 -0400153func reportError(property string, err error, t *testing.T) {
Adam Langley878d0e12009-10-28 15:16:20 -0700154 if err != nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800155 t.Errorf("%s: %s", property, err)
Adam Langley878d0e12009-10-28 15:16:20 -0700156 }
157}
158
159func TestCheckEqual(t *testing.T) {
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800160 reportError("fBool", CheckEqual(fBool, fBool, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700161 reportError("fBoolAlias", CheckEqual(fBoolAlias, fBoolAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800162 reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700163 reportError("fFloat32Alias", CheckEqual(fFloat32Alias, fFloat32Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800164 reportError("fFloat64", CheckEqual(fFloat64, fFloat64, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700165 reportError("fFloat64Alias", CheckEqual(fFloat64Alias, fFloat64Alias, nil), t)
Russ Coxf2b5a072011-01-19 23:09:00 -0500166 reportError("fComplex64", CheckEqual(fComplex64, fComplex64, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700167 reportError("fComplex64Alias", CheckEqual(fComplex64Alias, fComplex64Alias, nil), t)
Russ Coxf2b5a072011-01-19 23:09:00 -0500168 reportError("fComplex128", CheckEqual(fComplex128, fComplex128, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700169 reportError("fComplex128Alias", CheckEqual(fComplex128Alias, fComplex128Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800170 reportError("fInt16", CheckEqual(fInt16, fInt16, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700171 reportError("fInt16Alias", CheckEqual(fInt16Alias, fInt16Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800172 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700173 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800174 reportError("fInt64", CheckEqual(fInt64, fInt64, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700175 reportError("fInt64Alias", CheckEqual(fInt64Alias, fInt64Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800176 reportError("fInt8", CheckEqual(fInt8, fInt8, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700177 reportError("fInt8Alias", CheckEqual(fInt8Alias, fInt8Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800178 reportError("fInt", CheckEqual(fInt, fInt, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700179 reportError("fIntAlias", CheckEqual(fIntAlias, fIntAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800180 reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700181 reportError("fInt32Alias", CheckEqual(fInt32Alias, fInt32Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800182 reportError("fMap", CheckEqual(fMap, fMap, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700183 reportError("fMapAlias", CheckEqual(fMapAlias, fMapAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800184 reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700185 reportError("fSliceAlias", CheckEqual(fSliceAlias, fSliceAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800186 reportError("fString", CheckEqual(fString, fString, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700187 reportError("fStringAlias", CheckEqual(fStringAlias, fStringAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800188 reportError("fStruct", CheckEqual(fStruct, fStruct, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700189 reportError("fStructAlias", CheckEqual(fStructAlias, fStructAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800190 reportError("fUint16", CheckEqual(fUint16, fUint16, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700191 reportError("fUint16Alias", CheckEqual(fUint16Alias, fUint16Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800192 reportError("fUint32", CheckEqual(fUint32, fUint32, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700193 reportError("fUint32Alias", CheckEqual(fUint32Alias, fUint32Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800194 reportError("fUint64", CheckEqual(fUint64, fUint64, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700195 reportError("fUint64Alias", CheckEqual(fUint64Alias, fUint64Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800196 reportError("fUint8", CheckEqual(fUint8, fUint8, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700197 reportError("fUint8Alias", CheckEqual(fUint8Alias, fUint8Alias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800198 reportError("fUint", CheckEqual(fUint, fUint, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700199 reportError("fUintAlias", CheckEqual(fUintAlias, fUintAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800200 reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700201 reportError("fUintptrAlias", CheckEqual(fUintptrAlias, fUintptrAlias, nil), t)
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800202 reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t)
Jonathan Hseuc691ae62013-05-14 17:14:59 -0700203 reportError("fIntptrAlias", CheckEqual(fIntptrAlias, fIntptrAlias, nil), t)
Chris Kastorff3e9ed272015-02-04 04:43:00 -0800204 reportError("fArray", CheckEqual(fArray, fArray, nil), t)
205 reportError("fArrayAlais", CheckEqual(fArrayAlias, fArrayAlias, nil), t)
Adam Langley878d0e12009-10-28 15:16:20 -0700206}
207
208// This tests that ArbitraryValue is working by checking that all the arbitrary
209// values of type MyStruct have x = 42.
210type myStruct struct {
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800211 x int
Adam Langley878d0e12009-10-28 15:16:20 -0700212}
213
214func (m myStruct) Generate(r *rand.Rand, _ int) reflect.Value {
Russ Cox07abf1c2011-04-25 13:39:36 -0400215 return reflect.ValueOf(myStruct{x: 42})
Adam Langley878d0e12009-10-28 15:16:20 -0700216}
217
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800218func myStructProperty(in myStruct) bool { return in.x == 42 }
Adam Langley878d0e12009-10-28 15:16:20 -0700219
220func TestCheckProperty(t *testing.T) {
Robert Griesemer40621d52009-11-09 12:07:39 -0800221 reportError("myStructProperty", Check(myStructProperty, nil), t)
Adam Langley878d0e12009-10-28 15:16:20 -0700222}
223
224func TestFailure(t *testing.T) {
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800225 f := func(x int) bool { return false }
226 err := Check(f, nil)
Adam Langley878d0e12009-10-28 15:16:20 -0700227 if err == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800228 t.Errorf("Check didn't return an error")
Adam Langley878d0e12009-10-28 15:16:20 -0700229 }
230 if _, ok := err.(*CheckError); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800231 t.Errorf("Error was not a CheckError: %s", err)
Adam Langley878d0e12009-10-28 15:16:20 -0700232 }
233
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800234 err = CheckEqual(fUint, fUint32, nil)
Adam Langley878d0e12009-10-28 15:16:20 -0700235 if err == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800236 t.Errorf("#1 CheckEqual didn't return an error")
Adam Langley878d0e12009-10-28 15:16:20 -0700237 }
238 if _, ok := err.(SetupError); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800239 t.Errorf("#1 Error was not a SetupError: %s", err)
Adam Langley878d0e12009-10-28 15:16:20 -0700240 }
241
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800242 err = CheckEqual(func(x, y int) {}, func(x int) {}, nil)
Adam Langley878d0e12009-10-28 15:16:20 -0700243 if err == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800244 t.Errorf("#2 CheckEqual didn't return an error")
Adam Langley878d0e12009-10-28 15:16:20 -0700245 }
246 if _, ok := err.(SetupError); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800247 t.Errorf("#2 Error was not a SetupError: %s", err)
Adam Langley878d0e12009-10-28 15:16:20 -0700248 }
249
Robert Griesemer45ca9f72009-12-15 15:41:46 -0800250 err = CheckEqual(func(x int) int { return 0 }, func(x int) int32 { return 0 }, nil)
Adam Langley878d0e12009-10-28 15:16:20 -0700251 if err == nil {
Robert Griesemer40621d52009-11-09 12:07:39 -0800252 t.Errorf("#3 CheckEqual didn't return an error")
Adam Langley878d0e12009-10-28 15:16:20 -0700253 }
254 if _, ok := err.(SetupError); !ok {
Robert Griesemer40621d52009-11-09 12:07:39 -0800255 t.Errorf("#3 Error was not a SetupError: %s", err)
Adam Langley878d0e12009-10-28 15:16:20 -0700256 }
257}