OSDN Git Service

Update to current version of Go library.
[pf3gnuchains/gcc-fork.git] / libgo / go / testing / quick / quick_test.go
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         "rand"
9         "reflect"
10         "testing"
11         "os"
12 )
13
14 func fBool(a bool) bool { return a }
15
16 func fFloat32(a float32) float32 { return a }
17
18 func fFloat64(a float64) float64 { return a }
19
20 func fComplex64(a complex64) complex64 { return a }
21
22 func fComplex128(a complex128) complex128 { return a }
23
24 func fInt16(a int16) int16 { return a }
25
26 func fInt32(a int32) int32 { return a }
27
28 func fInt64(a int64) int64 { return a }
29
30 func fInt8(a int8) int8 { return a }
31
32 func fInt(a int) int { return a }
33
34 func fUInt8(a uint8) uint8 { return a }
35
36 func fMap(a map[int]int) map[int]int { return a }
37
38 func fSlice(a []byte) []byte { return a }
39
40 func fString(a string) string { return a }
41
42 type TestStruct struct {
43         A int
44         B string
45 }
46
47 func fStruct(a TestStruct) TestStruct { return a }
48
49 func fUint16(a uint16) uint16 { return a }
50
51 func fUint32(a uint32) uint32 { return a }
52
53 func fUint64(a uint64) uint64 { return a }
54
55 func fUint8(a uint8) uint8 { return a }
56
57 func fUint(a uint) uint { return a }
58
59 func fUintptr(a uintptr) uintptr { return a }
60
61 func fIntptr(a *int) *int {
62         b := *a
63         return &b
64 }
65
66 func reportError(property string, err os.Error, t *testing.T) {
67         if err != nil {
68                 t.Errorf("%s: %s", property, err)
69         }
70 }
71
72 func TestCheckEqual(t *testing.T) {
73         reportError("fBool", CheckEqual(fBool, fBool, nil), t)
74         reportError("fFloat32", CheckEqual(fFloat32, fFloat32, nil), t)
75         reportError("fFloat64", CheckEqual(fFloat64, fFloat64, nil), t)
76         reportError("fComplex64", CheckEqual(fComplex64, fComplex64, nil), t)
77         reportError("fComplex128", CheckEqual(fComplex128, fComplex128, nil), t)
78         reportError("fInt16", CheckEqual(fInt16, fInt16, nil), t)
79         reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
80         reportError("fInt64", CheckEqual(fInt64, fInt64, nil), t)
81         reportError("fInt8", CheckEqual(fInt8, fInt8, nil), t)
82         reportError("fInt", CheckEqual(fInt, fInt, nil), t)
83         reportError("fUInt8", CheckEqual(fUInt8, fUInt8, nil), t)
84         reportError("fInt32", CheckEqual(fInt32, fInt32, nil), t)
85         reportError("fMap", CheckEqual(fMap, fMap, nil), t)
86         reportError("fSlice", CheckEqual(fSlice, fSlice, nil), t)
87         reportError("fString", CheckEqual(fString, fString, nil), t)
88         reportError("fStruct", CheckEqual(fStruct, fStruct, nil), t)
89         reportError("fUint16", CheckEqual(fUint16, fUint16, nil), t)
90         reportError("fUint32", CheckEqual(fUint32, fUint32, nil), t)
91         reportError("fUint64", CheckEqual(fUint64, fUint64, nil), t)
92         reportError("fUint8", CheckEqual(fUint8, fUint8, nil), t)
93         reportError("fUint", CheckEqual(fUint, fUint, nil), t)
94         reportError("fUintptr", CheckEqual(fUintptr, fUintptr, nil), t)
95         reportError("fIntptr", CheckEqual(fIntptr, fIntptr, nil), t)
96 }
97
98 // This tests that ArbitraryValue is working by checking that all the arbitrary
99 // values of type MyStruct have x = 42.
100 type myStruct struct {
101         x int
102 }
103
104 func (m myStruct) Generate(r *rand.Rand, _ int) reflect.Value {
105         return reflect.ValueOf(myStruct{x: 42})
106 }
107
108 func myStructProperty(in myStruct) bool { return in.x == 42 }
109
110 func TestCheckProperty(t *testing.T) {
111         reportError("myStructProperty", Check(myStructProperty, nil), t)
112 }
113
114 func TestFailure(t *testing.T) {
115         f := func(x int) bool { return false }
116         err := Check(f, nil)
117         if err == nil {
118                 t.Errorf("Check didn't return an error")
119         }
120         if _, ok := err.(*CheckError); !ok {
121                 t.Errorf("Error was not a CheckError: %s", err)
122         }
123
124         err = CheckEqual(fUint, fUint32, nil)
125         if err == nil {
126                 t.Errorf("#1 CheckEqual didn't return an error")
127         }
128         if _, ok := err.(SetupError); !ok {
129                 t.Errorf("#1 Error was not a SetupError: %s", err)
130         }
131
132         err = CheckEqual(func(x, y int) {}, func(x int) {}, nil)
133         if err == nil {
134                 t.Errorf("#2 CheckEqual didn't return an error")
135         }
136         if _, ok := err.(SetupError); !ok {
137                 t.Errorf("#2 Error was not a SetupError: %s", err)
138         }
139
140         err = CheckEqual(func(x int) int { return 0 }, func(x int) int32 { return 0 }, nil)
141         if err == nil {
142                 t.Errorf("#3 CheckEqual didn't return an error")
143         }
144         if _, ok := err.(SetupError); !ok {
145                 t.Errorf("#3 Error was not a SetupError: %s", err)
146         }
147 }