github.com/xushiwei/go@v0.0.0-20130601165731-2b9d83f45bc9/src/pkg/testing/quick/quick.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 implements utility functions to help with black box testing.
     6  package quick
     7  
     8  import (
     9  	"flag"
    10  	"fmt"
    11  	"math"
    12  	"math/rand"
    13  	"reflect"
    14  	"strings"
    15  )
    16  
    17  var defaultMaxCount *int = flag.Int("quickchecks", 100, "The default number of iterations for each check")
    18  
    19  // A Generator can generate random values of its own type.
    20  type Generator interface {
    21  	// Generate returns a random instance of the type on which it is a
    22  	// method using the size as a size hint.
    23  	Generate(rand *rand.Rand, size int) reflect.Value
    24  }
    25  
    26  // randFloat32 generates a random float taking the full range of a float32.
    27  func randFloat32(rand *rand.Rand) float32 {
    28  	f := rand.Float64() * math.MaxFloat32
    29  	if rand.Int()&1 == 1 {
    30  		f = -f
    31  	}
    32  	return float32(f)
    33  }
    34  
    35  // randFloat64 generates a random float taking the full range of a float64.
    36  func randFloat64(rand *rand.Rand) float64 {
    37  	f := rand.Float64()
    38  	if rand.Int()&1 == 1 {
    39  		f = -f
    40  	}
    41  	return f
    42  }
    43  
    44  // randInt64 returns a random integer taking half the range of an int64.
    45  func randInt64(rand *rand.Rand) int64 { return rand.Int63() - 1<<62 }
    46  
    47  // complexSize is the maximum length of arbitrary values that contain other
    48  // values.
    49  const complexSize = 50
    50  
    51  // Value returns an arbitrary value of the given type.
    52  // If the type implements the Generator interface, that will be used.
    53  // Note: To create arbitrary values for structs, all the fields must be exported.
    54  func Value(t reflect.Type, rand *rand.Rand) (value reflect.Value, ok bool) {
    55  	if m, ok := reflect.Zero(t).Interface().(Generator); ok {
    56  		return m.Generate(rand, complexSize), true
    57  	}
    58  
    59  	switch concrete := t; concrete.Kind() {
    60  	case reflect.Bool:
    61  		return reflect.ValueOf(rand.Int()&1 == 0), true
    62  	case reflect.Float32:
    63  		return reflect.ValueOf(randFloat32(rand)), true
    64  	case reflect.Float64:
    65  		return reflect.ValueOf(randFloat64(rand)), true
    66  	case reflect.Complex64:
    67  		return reflect.ValueOf(complex(randFloat32(rand), randFloat32(rand))), true
    68  	case reflect.Complex128:
    69  		return reflect.ValueOf(complex(randFloat64(rand), randFloat64(rand))), true
    70  	case reflect.Int16:
    71  		return reflect.ValueOf(int16(randInt64(rand))), true
    72  	case reflect.Int32:
    73  		return reflect.ValueOf(int32(randInt64(rand))), true
    74  	case reflect.Int64:
    75  		return reflect.ValueOf(randInt64(rand)), true
    76  	case reflect.Int8:
    77  		return reflect.ValueOf(int8(randInt64(rand))), true
    78  	case reflect.Int:
    79  		return reflect.ValueOf(int(randInt64(rand))), true
    80  	case reflect.Uint16:
    81  		return reflect.ValueOf(uint16(randInt64(rand))), true
    82  	case reflect.Uint32:
    83  		return reflect.ValueOf(uint32(randInt64(rand))), true
    84  	case reflect.Uint64:
    85  		return reflect.ValueOf(uint64(randInt64(rand))), true
    86  	case reflect.Uint8:
    87  		return reflect.ValueOf(uint8(randInt64(rand))), true
    88  	case reflect.Uint:
    89  		return reflect.ValueOf(uint(randInt64(rand))), true
    90  	case reflect.Uintptr:
    91  		return reflect.ValueOf(uintptr(randInt64(rand))), true
    92  	case reflect.Map:
    93  		numElems := rand.Intn(complexSize)
    94  		m := reflect.MakeMap(concrete)
    95  		for i := 0; i < numElems; i++ {
    96  			key, ok1 := Value(concrete.Key(), rand)
    97  			value, ok2 := Value(concrete.Elem(), rand)
    98  			if !ok1 || !ok2 {
    99  				return reflect.Value{}, false
   100  			}
   101  			m.SetMapIndex(key, value)
   102  		}
   103  		return m, true
   104  	case reflect.Ptr:
   105  		v, ok := Value(concrete.Elem(), rand)
   106  		if !ok {
   107  			return reflect.Value{}, false
   108  		}
   109  		p := reflect.New(concrete.Elem())
   110  		p.Elem().Set(v)
   111  		return p, true
   112  	case reflect.Slice:
   113  		numElems := rand.Intn(complexSize)
   114  		s := reflect.MakeSlice(concrete, numElems, numElems)
   115  		for i := 0; i < numElems; i++ {
   116  			v, ok := Value(concrete.Elem(), rand)
   117  			if !ok {
   118  				return reflect.Value{}, false
   119  			}
   120  			s.Index(i).Set(v)
   121  		}
   122  		return s, true
   123  	case reflect.String:
   124  		numChars := rand.Intn(complexSize)
   125  		codePoints := make([]rune, numChars)
   126  		for i := 0; i < numChars; i++ {
   127  			codePoints[i] = rune(rand.Intn(0x10ffff))
   128  		}
   129  		return reflect.ValueOf(string(codePoints)), true
   130  	case reflect.Struct:
   131  		s := reflect.New(t).Elem()
   132  		for i := 0; i < s.NumField(); i++ {
   133  			v, ok := Value(concrete.Field(i).Type, rand)
   134  			if !ok {
   135  				return reflect.Value{}, false
   136  			}
   137  			s.Field(i).Set(v)
   138  		}
   139  		return s, true
   140  	default:
   141  		return reflect.Value{}, false
   142  	}
   143  }
   144  
   145  // A Config structure contains options for running a test.
   146  type Config struct {
   147  	// MaxCount sets the maximum number of iterations. If zero,
   148  	// MaxCountScale is used.
   149  	MaxCount int
   150  	// MaxCountScale is a non-negative scale factor applied to the default
   151  	// maximum. If zero, the default is unchanged.
   152  	MaxCountScale float64
   153  	// If non-nil, rand is a source of random numbers. Otherwise a default
   154  	// pseudo-random source will be used.
   155  	Rand *rand.Rand
   156  	// If non-nil, the Values function generates a slice of arbitrary
   157  	// reflect.Values that are congruent with the arguments to the function
   158  	// being tested. Otherwise, the top-level Values function is used
   159  	// to generate them.
   160  	Values func([]reflect.Value, *rand.Rand)
   161  }
   162  
   163  var defaultConfig Config
   164  
   165  // getRand returns the *rand.Rand to use for a given Config.
   166  func (c *Config) getRand() *rand.Rand {
   167  	if c.Rand == nil {
   168  		return rand.New(rand.NewSource(0))
   169  	}
   170  	return c.Rand
   171  }
   172  
   173  // getMaxCount returns the maximum number of iterations to run for a given
   174  // Config.
   175  func (c *Config) getMaxCount() (maxCount int) {
   176  	maxCount = c.MaxCount
   177  	if maxCount == 0 {
   178  		if c.MaxCountScale != 0 {
   179  			maxCount = int(c.MaxCountScale * float64(*defaultMaxCount))
   180  		} else {
   181  			maxCount = *defaultMaxCount
   182  		}
   183  	}
   184  
   185  	return
   186  }
   187  
   188  // A SetupError is the result of an error in the way that check is being
   189  // used, independent of the functions being tested.
   190  type SetupError string
   191  
   192  func (s SetupError) Error() string { return string(s) }
   193  
   194  // A CheckError is the result of Check finding an error.
   195  type CheckError struct {
   196  	Count int
   197  	In    []interface{}
   198  }
   199  
   200  func (s *CheckError) Error() string {
   201  	return fmt.Sprintf("#%d: failed on input %s", s.Count, toString(s.In))
   202  }
   203  
   204  // A CheckEqualError is the result CheckEqual finding an error.
   205  type CheckEqualError struct {
   206  	CheckError
   207  	Out1 []interface{}
   208  	Out2 []interface{}
   209  }
   210  
   211  func (s *CheckEqualError) Error() string {
   212  	return fmt.Sprintf("#%d: failed on input %s. Output 1: %s. Output 2: %s", s.Count, toString(s.In), toString(s.Out1), toString(s.Out2))
   213  }
   214  
   215  // Check looks for an input to f, any function that returns bool,
   216  // such that f returns false.  It calls f repeatedly, with arbitrary
   217  // values for each argument.  If f returns false on a given input,
   218  // Check returns that input as a *CheckError.
   219  // For example:
   220  //
   221  // 	func TestOddMultipleOfThree(t *testing.T) {
   222  // 		f := func(x int) bool {
   223  // 			y := OddMultipleOfThree(x)
   224  // 			return y%2 == 1 && y%3 == 0
   225  // 		}
   226  // 		if err := quick.Check(f, nil); err != nil {
   227  // 			t.Error(err)
   228  // 		}
   229  // 	}
   230  func Check(function interface{}, config *Config) (err error) {
   231  	if config == nil {
   232  		config = &defaultConfig
   233  	}
   234  
   235  	f, fType, ok := functionAndType(function)
   236  	if !ok {
   237  		err = SetupError("argument is not a function")
   238  		return
   239  	}
   240  
   241  	if fType.NumOut() != 1 {
   242  		err = SetupError("function returns more than one value.")
   243  		return
   244  	}
   245  	if fType.Out(0).Kind() != reflect.Bool {
   246  		err = SetupError("function does not return a bool")
   247  		return
   248  	}
   249  
   250  	arguments := make([]reflect.Value, fType.NumIn())
   251  	rand := config.getRand()
   252  	maxCount := config.getMaxCount()
   253  
   254  	for i := 0; i < maxCount; i++ {
   255  		err = arbitraryValues(arguments, fType, config, rand)
   256  		if err != nil {
   257  			return
   258  		}
   259  
   260  		if !f.Call(arguments)[0].Bool() {
   261  			err = &CheckError{i + 1, toInterfaces(arguments)}
   262  			return
   263  		}
   264  	}
   265  
   266  	return
   267  }
   268  
   269  // CheckEqual looks for an input on which f and g return different results.
   270  // It calls f and g repeatedly with arbitrary values for each argument.
   271  // If f and g return different answers, CheckEqual returns a *CheckEqualError
   272  // describing the input and the outputs.
   273  func CheckEqual(f, g interface{}, config *Config) (err error) {
   274  	if config == nil {
   275  		config = &defaultConfig
   276  	}
   277  
   278  	x, xType, ok := functionAndType(f)
   279  	if !ok {
   280  		err = SetupError("f is not a function")
   281  		return
   282  	}
   283  	y, yType, ok := functionAndType(g)
   284  	if !ok {
   285  		err = SetupError("g is not a function")
   286  		return
   287  	}
   288  
   289  	if xType != yType {
   290  		err = SetupError("functions have different types")
   291  		return
   292  	}
   293  
   294  	arguments := make([]reflect.Value, xType.NumIn())
   295  	rand := config.getRand()
   296  	maxCount := config.getMaxCount()
   297  
   298  	for i := 0; i < maxCount; i++ {
   299  		err = arbitraryValues(arguments, xType, config, rand)
   300  		if err != nil {
   301  			return
   302  		}
   303  
   304  		xOut := toInterfaces(x.Call(arguments))
   305  		yOut := toInterfaces(y.Call(arguments))
   306  
   307  		if !reflect.DeepEqual(xOut, yOut) {
   308  			err = &CheckEqualError{CheckError{i + 1, toInterfaces(arguments)}, xOut, yOut}
   309  			return
   310  		}
   311  	}
   312  
   313  	return
   314  }
   315  
   316  // arbitraryValues writes Values to args such that args contains Values
   317  // suitable for calling f.
   318  func arbitraryValues(args []reflect.Value, f reflect.Type, config *Config, rand *rand.Rand) (err error) {
   319  	if config.Values != nil {
   320  		config.Values(args, rand)
   321  		return
   322  	}
   323  
   324  	for j := 0; j < len(args); j++ {
   325  		var ok bool
   326  		args[j], ok = Value(f.In(j), rand)
   327  		if !ok {
   328  			err = SetupError(fmt.Sprintf("cannot create arbitrary value of type %s for argument %d", f.In(j), j))
   329  			return
   330  		}
   331  	}
   332  
   333  	return
   334  }
   335  
   336  func functionAndType(f interface{}) (v reflect.Value, t reflect.Type, ok bool) {
   337  	v = reflect.ValueOf(f)
   338  	ok = v.Kind() == reflect.Func
   339  	if !ok {
   340  		return
   341  	}
   342  	t = v.Type()
   343  	return
   344  }
   345  
   346  func toInterfaces(values []reflect.Value) []interface{} {
   347  	ret := make([]interface{}, len(values))
   348  	for i, v := range values {
   349  		ret[i] = v.Interface()
   350  	}
   351  	return ret
   352  }
   353  
   354  func toString(interfaces []interface{}) string {
   355  	s := make([]string, len(interfaces))
   356  	for i, v := range interfaces {
   357  		s[i] = fmt.Sprintf("%#v", v)
   358  	}
   359  	return strings.Join(s, ", ")
   360  }