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