Documentation ¶
Overview ¶
Package generator provides generators for all basic go types and can be used to generate random data for provided data type. arbitrary.Generators define constraints that control how data is generated and for generated data they usually provide corresponding shrinker. Goal of generators is to be a powerful tool for generating any type of data.
Index ¶
- func Any() arbitrary.Generator
- func Array(element arbitrary.Generator) arbitrary.Generator
- func ArrayFrom(elements ...arbitrary.Generator) arbitrary.Generator
- func Bool() arbitrary.Generator
- func Chan(limits ...constraints.Length) arbitrary.Generator
- func ChanRecv(element arbitrary.Generator, limits ...constraints.Length) arbitrary.Generator
- func Complex128(limits ...constraints.Complex128) arbitrary.Generator
- func Complex64(limits ...constraints.Complex64) arbitrary.Generator
- func Constant(constant interface{}) arbitrary.Generator
- func ConstantFrom(constant interface{}, constants ...interface{}) arbitrary.Generator
- func Float32(limits ...constraints.Float32) arbitrary.Generator
- func Float64(limits ...constraints.Float64) arbitrary.Generator
- func Func(outputs ...arbitrary.Generator) arbitrary.Generator
- func Int(limits ...constraints.Int) arbitrary.Generator
- func Int16(limits ...constraints.Int16) arbitrary.Generator
- func Int32(limits ...constraints.Int32) arbitrary.Generator
- func Int64(limits ...constraints.Int64) arbitrary.Generator
- func Int8(limits ...constraints.Int8) arbitrary.Generator
- func Invalid(err error) arbitrary.Generator
- func Map(keyGenerator, ValueGenerator arbitrary.Generator, limits ...constraints.Length) arbitrary.Generator
- func Nil() arbitrary.Generator
- func OneFrom(generator arbitrary.Generator, generators ...arbitrary.Generator) arbitrary.Generator
- func Ptr(element arbitrary.Generator, limits ...constraints.Ptr) arbitrary.Generator
- func Recursive(genFunc func(Recurse) arbitrary.Generator, depth uint) arbitrary.Generator
- func Rune(limits ...constraints.Rune) arbitrary.Generator
- func Slice(elementGenerator arbitrary.Generator, limits ...constraints.Length) arbitrary.Generator
- func Stream(seed, count uint64, streamer streamer) error
- func Streamer(target interface{}, generators ...arbitrary.Generator) streamer
- func String(limits ...constraints.String) arbitrary.Generator
- func Struct(fields ...map[string]arbitrary.Generator) arbitrary.Generator
- func Uint(limits ...constraints.Uint) arbitrary.Generator
- func Uint16(limits ...constraints.Uint16) arbitrary.Generator
- func Uint32(limits ...constraints.Uint32) arbitrary.Generator
- func Uint64(limits ...constraints.Uint64) arbitrary.Generator
- func Uint8(limits ...constraints.Uint8) arbitrary.Generator
- func Weighted(weights []uint64, generators ...arbitrary.Generator) arbitrary.Generator
- type Recurse
Examples ¶
- Any
- Array
- ArrayFrom
- Bool
- Chan
- Chan (Constraints)
- ChanRecv
- ChanRecv (Constraints)
- Complex128
- Complex128 (Constraints)
- Complex64
- Complex64 (Constraints)
- Constant
- ConstantFrom
- Float32
- Float32 (Constraints)
- Float64
- Float64 (Constraints)
- Func
- Int
- Int (Constraints)
- Int16
- Int16 (Constraints)
- Int32
- Int32 (Constraints)
- Int64
- Int64 (Constraints)
- Int8
- Int8 (Constraints)
- Map
- Map (Constraints)
- Nil
- OneFrom
- Ptr
- Ptr (NoNil)
- Recursive (BinaryTree)
- Recursive (RecursiveFunction)
- Rune
- Rune (Constraints)
- Slice
- Slice (Constraints)
- String
- String (Constraints)
- Struct
- Uint
- Uint (Constraints)
- Uint16
- Uint16 (Constraints)
- Uint32
- Uint32 (Constraints)
- Uint64
- Uint64 (Constraints)
- Uint8
- Uint8 (Constraints)
- Weighted
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Any ¶
Any returns generator with default constraints for a type specified by generator's target. Unsupported target: interface{}
Example ¶
This example demonstrates usage of Any() generator for generation of values for 3 types (int, uint and Point). Any() works for all go types except interfaces.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { type Point struct { X int16 Y int16 Z int8 } streamer := generator.Streamer( func(i int, u uint, p Point) { fmt.Printf("%d, %d, %#v\n", i, u, p) }, generator.Any(), generator.Any(), generator.Any(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -4518808235179270133, 7861855757602232086, generator_test.Point{X:5150, Y:-11651, Z:-40} -2122761628320059770, 16172318933975836041, generator_test.Point{X:31913, Y:-20516, Z:-56} -6485228379443441869, 9950499355085743487, generator_test.Point{X:31469, Y:16403, Z:-120} 5972778420317109720, 18356510534673821416, generator_test.Point{X:-30288, Y:-3364, Z:-95} 3277659493131553792, 16536373503259581585, generator_test.Point{X:-20183, Y:22302, Z:-117} 2889002371220678169, 9050008079631751930, generator_test.Point{X:-2582, Y:-12674, Z:-7} 1861954357100430827, 9301751449745155624, generator_test.Point{X:-16049, Y:12306, Z:74} -1080827893950765636, 4332503251610791914, generator_test.Point{X:24459, Y:2740, Z:109} -4610581452772180400, 11452572414278503431, generator_test.Point{X:3632, Y:-17110, Z:83} 260101872073892018, 17022257214824873781, generator_test.Point{X:-5456, Y:-13741, Z:-12}
func Array ¶
Array returns generator for array types. Array element's generator is specified by "element" parameter. Error is returned if generator's target is not array type or element's generator returns an error.
Example ¶
This example demonstrates usage of Array(Int()) generator for generation of array of integers with 5 elements. Array() requires a generator for it's elements to be passed to it. Int() generator is used as an element generator.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(arr [5]int) { fmt.Printf("%#v\n", arr) }, generator.Array(generator.Int()), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: [5]int{-4518808235179270133, -5036824528102830934, 8071137008395949086, -2122761628320059770, 6925466992496964832} [5]int{5606570076237929230, -6485228379443441869, -1089963290385541773, -315038161257240872, -8800248522230157011} [5]int{5309191430948421708, 8901768139528370850, -1284006505070580203, 3029607914297333936, -7505562437545936694} [5]int{9177598355735269079, 2353700405777826677, 2889002371220678169, 2430368660537815426, -5013668637975760780} [5]int{1861954357100430827, 3930565546038334149, 4265511144525599390, 9001397090059497490, -7811778195032818482} [5]int{3434633470139019496, 4491726675100777446, 7551152765542507822, 6992623394542015136, -4610581452772180400} [5]int{2983335422402563632, -3236400634926555689, -8318806587974000740, 7656290481659077236, -1073273973791335576} [5]int{1135614103155420740, -7031127273457604653, 2574016966663937194, 5790223617635911539, 8164215293865182704} [5]int{-6776706801480370880, -2849222499850869647, -8212568795191619498, -1767412366155247756, -6821913011751915842} [5]int{7534629738184250638, 1276965762818039701, -1600752837639152423, -6612685186773303684, -4142208901106344656}
func ArrayFrom ¶
ArrayFrom returns generator of array types. Unlike Array where one generator is used for all elements of array, ArrayFrom accepts a generator for each individual element which is specified with element parameter. This behavior allows imposing different constraints for each array element. Error is returned if generator's target is not array type, number of element generators doesn't match the size of the array, or any of the element generators return an error.
Example ¶
This example demonstrates usage of ArrayFrom() generator and Int() generator for generation of array of integers with 5 elements. ArrayFrom() requires generator for each element of the array to be passed to it. Five Int() generator are used for array elements.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(arr [5]int) { fmt.Printf("%#v\n", arr) }, generator.ArrayFrom( generator.Int(constraints.Int{Min: 0, Max: 9}), generator.Int(constraints.Int{Min: 10, Max: 19}), generator.Int(constraints.Int{Min: 20, Max: 29}), generator.Int(constraints.Int{Min: 30, Max: 39}), generator.Int(constraints.Int{Min: 40, Max: 49}), ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: [5]int{5, 12, 26, 30, 40} [5]int{6, 11, 25, 36, 44} [5]int{3, 10, 23, 31, 48} [5]int{8, 15, 27, 32, 49} [5]int{2, 18, 25, 33, 49} [5]int{9, 14, 29, 39, 40} [5]int{1, 13, 24, 37, 44} [5]int{9, 18, 20, 36, 49} [5]int{7, 14, 23, 32, 41} [5]int{6, 14, 24, 38, 48}
func Bool ¶
Bool returns generator of bool types. Error is returned if generator's target is not bool type.
Example ¶
This example demonstrates usage of Bool() generator for generation of bool values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(b bool) { fmt.Printf("%v\n", b) }, generator.Bool(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: true false false true true false false true true false
func Chan ¶
func Chan(limits ...constraints.Length) arbitrary.Generator
Chan returns generator of chan types. Channel's buffer size range is defined with "limits" parameter. arbitrary.Arbitraryd channel is empty and can be used for all 3 channel types (chan, <-chan and chan <-). Error is returned if generator's target is not chan type or limit's lower bound i higher than limit's upper bound.
Example ¶
This example demonstrates usage of Chan() generator for generation of chan int values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ch chan int) { fmt.Printf("chan size: %d\n", cap(ch)) }, generator.Chan(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: chan size: 31 chan size: 16 chan size: 80 chan size: 86 chan size: 69 chan size: 22 chan size: 84 chan size: 3 chan size: 64 chan size: 30
Example (Constraints) ¶
This example demonstrates usage of Chan() generator with constraints for generation of chan int values. Constraints define capacity of generated channel and in this example generated channel will have capacity in range [0, 10]
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ch chan int) { fmt.Printf("chan size: %d\n", cap(ch)) }, generator.Chan(constraints.Length{ Min: 0, Max: 10, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: chan size: 5 chan size: 2 chan size: 6 chan size: 0 chan size: 0 chan size: 6 chan size: 1 chan size: 5 chan size: 6 chan size: 4
func ChanRecv ¶ added in v0.5.1
ChanRecv is a generator of read-only channels. The generated channel is closed, buffered, and has a capacity that falls within the range specified by constraints.Length. It is filled with values of the channel's element type. The 'gen' parameter arbitrary.Generator is used to fill the channel with values and must match the type of the channel's elements. Although the 'limits' parameter is variadic, only the first value passed is used. If the 'limits' parameter is omitted, constraints.LengthDefault is used instead. The following example demonstrates how to use ChanRecv to generate a <-chan int that can have between 10 and 20 int elements within it:
generator.ChanRecv(generator.Int(), constraints.Length{Min: 10, Max: 20})
ChanRecv will return an error:
- If the generator's target is not <-chan T
- If the 'limits' parameter is invalid: constraints.Length.Min > constraints.Length.Max, constraints.Length.Max > math.MaxInt64
Example ¶
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ch <-chan int) { ns := []int{} for n := range ch { ns = append(ns, n) } fmt.Println(ns) }, generator.ChanRecv(generator.Int(constraints.Int{ Min: 0, Max: 100, })), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: [16 80 86 69 22 84 3 64 30 91 3 45 81 31 40 62 92 21 71 50 57 76 79 95 40 69 19 94 73 9 9] [96 81 67 36 23 59 44 52 57 56 14 30 64 77 23 29 15 52 93 51 34 1 13 44 86 68 84 40 61 19 12 57 25 83 11 0 76 87 45 73 72] [89 12 88 96 34 10 80 74 64 27 0 36 42 7 48 4 4 95 54 46 14 43 0 17 9 17 16 13 87 19 61 30 36 38 25 47 25 56 67 88 51 29 42 62 81] [66 22 96 2 46 21 89 12 0 7 24 10 100 38 11 18 95 28 36 100 19 69 40 94 49 89 94 30 72 48 18 6 50 28 70 79 22 74 16 68 64 70 33 42 84 11 7 3 99 52 36 46 33 64 50 32 36 48 51 81 37 36 12 7 84 48 86 64 71 86 41 41 42 47 25 17] [37 17 94 50 16 20 53 100 80 89 93 65 9 78 16 45 75 90 54 12 24 94 60 90 37 90 80 68 98 8 91 66 45 16 49 61 52 42 62 81 67 86 21 73 61 16 3 22 27 27 11 62 24 9 64 14 84 36 35 56 62 96 15 14 38 25 10 84 42 43 93 38 12 44 43 31 12 88 66 43 47 41 40] [70 79 28 51 70 67 16 28 97 14 87 1 91 99 24 21 23 94 39 63 59] [68 28 45 75 4 68 42 54 80 67 53 35 84 60 2 92 28] [45 58 33 93 14 40 80 83 67 27 21 87 25 73 50 90 75 72 62 0 32 26 56 96 6 73 39 18 84 2 95 12 85 1 43 35 77 13 6 45 4 12 34 83 59 29 1 47 69 48 100 35 42 13 19 36 67 88 24 12 1 24 80 29] [53 30 45 39 26 98] [26 71 84 83 33 60 15 68 98 5 97 10 27 88 86 20 54 84 5 59 19 77 14 1 97 67 0 99 12 44 99 84 95 11 13 96 67 69 55 88 68 25 47 66 98 78 57 73 14 3 51 77 39 25 90 53 74 84 22 51 79 81 93 79 43 7 48 83 29 61 29 24 68 60 64 56 10 33 97 61 70 68 93 77 1 19 92 21 44 86]
Example (Constraints) ¶
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ch <-chan int) { ns := []int{} for n := range ch { ns = append(ns, n) } fmt.Println(ns) }, generator.ChanRecv( generator.Int(constraints.Int{Min: 0, Max: 100}), constraints.Length{Min: 0, Max: 10}, ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: [31 16 80 86 69] [84 3 64 30 91 3] [31] [62 92 21 71 50 57 76 79] [95 40] [19 94 73 9 9] [96 81 67 36 23 59 44 52 57] [14 30 64 77 23 29 15 52] [34 1 13] [68 84 40 61 19 12]
func Complex128 ¶
func Complex128(limits ...constraints.Complex128) arbitrary.Generator
Complex128 is generator for complex128 types. Range of complex128 values that can be generated is defined by "limits" parameter. If no constraints are provided default range is used [-math.MaxFloat64, math.MaxFloat64] for both real and imaginary part of complex128. Error is returned if generator's target is not complex128 type or constraints for real or imaginary part of complex128 are invalid.
Example ¶
This example demonstrates usage of Complex128() generator for generation of complex128 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(c complex128) { fmt.Printf("%g\n", c) }, generator.Complex128(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: (-1.3131993411626801e-06+2.6499394281062435e-102i) (-5.544832151478103e+28-1.6925393061358905e+61i) (-1.3148118989728537e-70-4.579610072238924e+81i) (-3.1950849453106405e+135+1.6727313548317884e-205i) (2.78249623188919e+86+5.04589615041297e+124i) (9.142337453567358e-167-1.374619546296366e+49i) (-4.1757521047374676e+224+4.010186513222081e+152i) (6.556580829938433e+219+9.452945816540518e+154i) (4.151349376347527e-207+4.786664746486726e+126i) (-2.872024231170062e+70+1.8272776570354314e-65i)
Example (Constraints) ¶
This example demonstrates usage of Complex128() generator with constraints for generation of complex128 values. Constraints define range of generatable float64 values for real and imaginary parts of complex128 number.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(c complex128) { fmt.Printf("%g\n", c) }, generator.Complex128(constraints.Complex128{ Real: constraints.Float64{ Min: 10, Max: 20, }, Imaginary: constraints.Float64{ Min: -200, Max: -100, }, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: (14.738934753084298-115.39130813223936i) (13.197699354402783-196.80036731935937i) (12.532697960464143-175.5617077932535i) (17.038394305448502-153.8634523801138i) (11.219426600620917-113.24664941496796i) (13.31424363947598-140.03814918293662i) (12.825849578744581-158.8143128221763i) (11.81996096177609-107.61529798063697i) (11.084114411123993-115.55906892458002i) (11.405528609648313-101.38558928949668i)
func Complex64 ¶
func Complex64(limits ...constraints.Complex64) arbitrary.Generator
Complex64 is generator for complex64 types. Range of complex64 values that can be generated is defined by limits parameter. If no constraints are provided default range is used [-math.MaxFloat32, math.MaxFloat32] for both real and imaginary part of complex64. Error is returned if generator's target is not complex64 type or constraints for real or imaginary part of complex64 are invalid.
Example ¶
This example demonstrates usage of Complex64() generator for generation of complex64 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(c complex64) { fmt.Printf("%g\n", c) }, generator.Complex64(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: (-2.4597852e+30-2.84718e-09i) (3.9155332e-22+5.336489e+16i) (5.3546978e+23-6.6470676e-23i) (2.0516037e-32+6.878372e+15i) (-2.5820768e+34-7.02896e-34i) (0.42347318-3.3288446e+30i) (0.0015746137-1.0883707e+20i) (-7812.692+5.0595798e+33i) (-1.0232254e+22-1.7621445e-22i) (-6.7646325e-35-1.2282892e-32i)
Example (Constraints) ¶
This example demonstrates usage of Complex64() generator with constraints for generation of complex64 values. Constraints define range of generatable float32 values for real and imaginary parts of complex64 number.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(c complex64) { fmt.Printf("%g\n", c) }, generator.Complex64(constraints.Complex64{ Real: constraints.Float32{ Min: -3, Max: -1, }, Imaginary: constraints.Float32{ Min: 3, Max: 5, }, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: (-1.2046497+4.1142726i) (-2.701786+3.1750083i) (-1.6229705+3.9623466i) (-2.4881487+3.222718i) (-2.5111911+3.9246528i) (-1.8536431+4.56498i) (-2.5411756+3.1288548i) (-1.6691408+3.4864495i) (-1.8248223+3.6883054i) (-1.5765601+3.0912352i)
func Constant ¶
Constant returns generator that always generates the value passed to it via "constant" parameter. Error is returned if value passed to generator doesn't match generator's target.
Example ¶
This example demonstrates usage of Constant() generator for generating string values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(s string) { fmt.Printf("%s\n", s) }, generator.Constant("I won't write test cases"), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases I won't write test cases
func ConstantFrom ¶
Constant returns generator that returns one of the constants. Error is returned if number of constants is 0 or chosen constant doesn't match generator's target
Example ¶
This example demonstrates usage of ConstantFrom() generator for generating string values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(in interface{}) { fmt.Printf("%v\n", in) }, generator.ConstantFrom("red", "green", "blue", "yellow", "black"), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: blue red red green black yellow red yellow yellow green
func Float32 ¶
func Float32(limits ...constraints.Float32) arbitrary.Generator
Float32 returns generator for float32 types. Range of float64 values that can be generated is defined by "limits" paramter. If no constraints are provided default float32 range [-math.MaxFloat32, math.MaxFloat32] is used instead. Error is returned if generator's target is not float32 type, "limits" paramter has invalid values (-Inf, NaN, +Inf), or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Float32() generator for generation of float32 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(f float32) { fmt.Printf("%g\n", f) }, generator.Float32(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -2.4597852e+30 -2.84718e-09 3.9155332e-22 5.336489e+16 5.3546978e+23 -6.6470676e-23 2.0516037e-32 6.878372e+15 -2.5820768e+34 -7.02896e-34
Example (Constraints) ¶
This example demonstrates usage of Float32() generator with constraints for generation of float32 values. Constraints defines range of generatable float32 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(f float32) { fmt.Printf("%g\n", f) }, generator.Float32(constraints.Float32{ Min: -2, Max: -1, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -1.2046497 -1.5285681 -1.6229705 -1.9818655 -1.4811733 -1.8536431 -1.6691408 -1.8248223 -1.5765601 -1.0456176
func Float64 ¶
func Float64(limits ...constraints.Float64) arbitrary.Generator
Float64 returns generator for float64 types. Range of float64 values that can be generated is defined by "limits" parameter. If no limits are provided default float64 range [-math.MaxFloat64, math.MaxFloat64] is used instead. Error is returned if generator's target is not float64 type, "limits" paramter has invalid values (-Inf, NaN, +Inf), or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Float64() generator for generation of float64 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(f float64) { fmt.Printf("%#v\n", f) }, generator.Float64(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -1.3131993411626801e-06 2.6499394281062435e-102 -5.544832151478103e+28 -1.6925393061358905e+61 -1.3148118989728537e-70 -4.579610072238924e+81 -3.1950849453106405e+135 1.6727313548317884e-205 2.78249623188919e+86 5.04589615041297e+124
Example (Constraints) ¶
This example demonstrates usage of Float64() generator with constraints for generation of float64 values. Constraints defines range of generatable float64 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(f float64) { fmt.Printf("%#v\n", f) }, generator.Float64(constraints.Float64{ Min: 1, Max: 5, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 3.4237086631530884 1.24048918956624 2.967593950595308 2.138899399920753 3.365311348740657 1.809075842134793 1.1524283250776146 4.827915588435498 2.5447450908036644 2.197932525132764
func Func ¶
Func returns generator for pure functions. arbitrary.Arbitraryd function is defined by it's output values, and generator for each output value needs to be provided through "outputs" parameter. Error is returned if generator's target is not a function, len(outputs) doesn't match number of function output values, or generator for any of output values returns an error.
Example ¶
This example demonstrates usage of Func() generator for generation of pure functions. Func() generator requires generators for it's output values to be passed to it. In this example generated function will return []int, thus Slice(Int()) generator is used.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(f func(x, y int) []int) { fmt.Printf("%#v\n", f(1, 2)) }, generator.Func(generator.Slice( generator.Int(constraints.Int{ Min: 0, Max: 10, }), constraints.Length{ Min: 2, Max: 5, }, )), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: []int{4, 2, 10, 10, 3} []int{4, 6, 1, 4, 9} []int{7, 6, 5, 8} []int{5, 8, 4, 10} []int{4, 9, 0, 4} []int{5, 4, 5, 3} []int{10, 2, 8, 3} []int{1, 9, 4, 3} []int{4, 3} []int{5, 5, 8, 10}
func Int ¶
func Int(limits ...constraints.Int) arbitrary.Generator
Int returns generator for int types. Range of int values that can be generated is defined by "limits" parameter. If no limits are provided default int range is used instead. Error is returned if generator's target is not int type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Int() generator for generation of int values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int) { fmt.Printf("%d\n", n) }, generator.Int(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -4518808235179270133 -5036824528102830934 8071137008395949086 -2122761628320059770 6925466992496964832 5606570076237929230 -6485228379443441869 -1089963290385541773 -315038161257240872 -8800248522230157011
Example (Constraints) ¶
This example demonstrates usage of Int() generator with constraints for generation of int values. Constraints defines range of generatable int values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int) { fmt.Printf("%d\n", n) }, generator.Int(constraints.Int{ Min: 0, Max: 10, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 5 2 6 0 0 6 1 5 6 4
func Int16 ¶
func Int16(limits ...constraints.Int16) arbitrary.Generator
Int16 returns generator for int16 types. Range of int16 values that can be generated is defined by "limits" parameter. If no limits are provided default int16 range [math.MinInt16, math.MaxInt16] is used instead. Error is returned if generator's target is not int16 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Int16() generator for generation of int16 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int16) { fmt.Printf("%d\n", n) }, generator.Int16(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 12790 -13142 -5150 -3323 3378 -4175 5523 -5629 31913 -20516
Example (Constraints) ¶
This example demonstrates usage of Int16() generator with constraints for generation of int16 values. Constraints defines range of generatable int16 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int16) { fmt.Printf("%d\n", n) }, generator.Int16(constraints.Int16{ Min: -200, Max: -100, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -131 -116 -180 -186 -169 -122 -184 -103 -164 -130
func Int32 ¶
func Int32(limits ...constraints.Int32) arbitrary.Generator
Int32 returns generator for int32 types. Range of int32 values that can be generated is defined by "limits" parameter. If no limits are provided default int64 range [math.MinInt32, math.MaxInt32] is used instead. Error is returned if generator's target is not int32 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Int32() generator for generation of int32 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int32) { fmt.Printf("%d\n", n) }, generator.Int32(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 826517535 898515203 649801091 -2023694845 -141136839 1633988338 2006390345 -16579444 -2004448480 -1121568663
Example (Constraints) ¶
This example demonstrates usage of Int32() generator with constraints for generation of int32 values. Constraints defines range of generatable int32 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int32) { fmt.Printf("%d\n", n) }, generator.Int32(constraints.Int32{ Min: -5, Max: 5, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 2 0 5 -4 -3 0 4 1 2 3
func Int64 ¶
func Int64(limits ...constraints.Int64) arbitrary.Generator
Int64 returns generator for int64 types. Range of int64 values that can be generated is defined by "limits" parameter. If no limits are provided default int64 range [math.MinInt64, math.MaxInt64] is used instead. Error is returned if generator's target is not int64 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Int64() generator for generation of int64 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int64) { fmt.Printf("%d\n", n) }, generator.Int64(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -4518808235179270133 -5036824528102830934 8071137008395949086 -2122761628320059770 6925466992496964832 5606570076237929230 -6485228379443441869 -1089963290385541773 -315038161257240872 -8800248522230157011
Example (Constraints) ¶
This example demonstrates usage of Int64() generator with constraints for generation of int64 values. Constraints defines range of generatable int64 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int64) { fmt.Printf("%d\n", n) }, generator.Int64(constraints.Int64{ Min: -1000, Max: 1000, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -502 854 453 259 859 81 808 -616 967 -185
func Int8 ¶
func Int8(limits ...constraints.Int8) arbitrary.Generator
Int8 returns generator for int8 types. Range of int8 values that can be generated is defined by "limits" parameter. If no limits are provided default int8 range [math.MinInt8, math.MaxInt8] is used instead. Error is returned if generator's target is not int8 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Int8() generator for generation of int8 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int8) { fmt.Printf("%d\n", n) }, generator.Int8(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: -31 -91 -40 50 79 -40 -116 -44 102 -23
Example (Constraints) ¶
This example demonstrates usage of Int8() generator with constraints for generation of int8 values. Constraints defines range of generatable int8 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int8) { fmt.Printf("%d\n", n) }, generator.Int8(constraints.Int8{ Min: 100, Max: 127, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 121 118 122 116 116 122 117 105 122 120
func Invalid ¶
Invalid returns arbitrary.Generator that always returns an error. "err" parameter specifies error returned by generator
func Map ¶
func Map(keyGenerator, ValueGenerator arbitrary.Generator, limits ...constraints.Length) arbitrary.Generator
Map returns generator for map types. arbitrary.Generators for map's key and value are specified by "key" and "value" parameters, respectively. Range of map size values is defined by "limits" parameter. If "limits" parameter is not specified default [0, 100] range is used instead. Error is returned if generator's target is not a map type, key generator returns an error, value generator returns an error or limits.Min > limits.Max
Note: arbitrary.Generator will always try to create a map within size limits. This means that during key generation it will take into account collision with existing map key's. Pool of values from which keys are generated must have number of unique values equal to map's maximum length value defined by limits parameter, otherwise map generation will be stuck in endless loop.
Example ¶
This example demonstrates usage of Map(Int8(), Bool()), generator for generation of map[int8]bool values. Map() generator requires generators for generating map's key-value pairs. In this example Int8() is used as a key generator and bool is used as value generator.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(m map[int8]bool) { fmt.Printf("%#v\n", m) }, generator.Map( generator.Int8(), generator.Bool(), ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: map[int8]bool{-127:false, -120:true, -118:true, -116:false, -102:true, -81:false, -76:false, -69:true, -68:true, -57:false, -50:false, -40:false, -30:true, -25:true, -16:true, -10:false, -4:true, 0:false, 7:false, 11:false, 19:true, 21:false, 33:true, 36:true, 40:false, 48:false, 72:false, 79:false, 99:false, 101:false, 126:false} map[int8]bool{-117:true, -94:false, -73:false, -42:false, -21:false, -12:false, 14:true, 27:false, 28:false, 35:false, 38:false, 39:true, 43:false, 45:false, 52:true, 75:false, 116:true} map[int8]bool{-127:true, -126:true, -125:false, -123:false, -120:true, -119:false, -115:true, -112:false, -111:false, -109:true, -106:true, -103:true, -101:true, -95:false, -90:true, -88:true, -85:false, -83:false, -81:false, -79:false, -78:false, -76:true, -72:false, -70:true, -69:false, -68:false, -63:false, -58:true, -55:false, -54:true, -51:true, -47:true, -45:true, -43:false, -42:false, -40:false, -36:false, -33:true, -29:false, -28:false, -25:true, -23:false, -19:false, -13:true, -12:true, -10:true, -3:true, -1:true, 1:false, 3:true, 8:true, 12:true, 14:true, 15:false, 20:false, 26:false, 28:false, 30:false, 31:false, 32:false, 34:true, 39:false, 40:true, 41:true, 43:false, 46:true, 47:true, 50:false, 56:true, 62:true, 69:true, 71:true, 76:false, 77:true, 78:false, 79:false, 84:false, 92:true, 95:true, 98:true, 99:true, 100:true, 106:false, 108:false, 109:true, 111:true, 113:true, 114:true, 118:true, 119:true, 122:false, 123:true, 127:false} map[int8]bool{-108:false, -97:false, -92:false, -90:false, -85:false, -82:true, -68:false, -61:true, -56:true, -40:true, -27:false, -11:true, 12:false, 20:true, 30:true, 49:false, 56:true, 61:false, 70:true, 78:false, 80:true, 82:false, 103:false, 104:false, 111:false, 119:true} map[int8]bool{-128:true, -126:false, -124:false, -122:true, -120:false, -119:false, -116:false, -113:true, -112:true, -109:false, -107:true, -104:true, -100:true, -98:false, -91:false, -88:false, -82:true, -81:false, -80:false, -79:false, -76:false, -68:false, -67:false, -63:false, -61:false, -57:true, -53:true, -52:false, -49:true, -44:false, -42:true, -41:false, -29:false, -24:false, -21:false, -18:true, -13:false, -12:true, -10:false, -9:true, -8:true, -6:true, -3:false, -2:true, 0:true, 1:true, 3:true, 8:true, 12:true, 16:false, 17:false, 18:false, 21:true, 24:false, 28:false, 34:true, 36:false, 38:true, 42:true, 46:false, 51:false, 52:true, 59:true, 68:false, 69:false, 71:false, 72:false, 76:false, 77:true, 82:true, 85:true, 88:false, 89:true, 90:true, 91:false, 94:true, 95:false, 96:true, 100:false, 101:false, 106:true, 108:true, 111:false, 115:false, 120:false, 122:false, 123:false, 125:false} map[int8]bool{-128:true, -104:false, -96:true, -85:false, -69:true, -68:false, -54:true, -42:false, -27:true, 0:true, 20:true, 29:true, 44:true, 93:false, 114:true, 126:true} map[int8]bool{-128:true, -57:false, -40:true, -6:true, 13:false, 38:false} map[int8]bool{-110:true, -102:false, -90:true, -75:false, -58:true, -57:false, -55:true, -52:false, -49:false, -1:false, 2:true, 17:true, 33:false, 35:true, 45:true, 47:true, 51:false, 56:true, 66:false, 67:true, 68:true, 70:true, 72:false, 86:true, 91:true, 105:false, 115:false, 118:true, 120:false, 123:false} map[int8]bool{-124:false, -123:true, -121:true, -120:true, -115:true, -111:true, -106:true, -103:false, -99:true, -98:false, -94:false, -91:true, -77:false, -74:false, -71:false, -69:false, -68:true, -66:false, -61:false, -59:false, -58:true, -56:false, -48:false, -41:true, -36:false, -35:true, -34:true, -30:true, -24:false, -21:true, -19:false, -16:false, -14:false, -12:false, -9:false, -6:true, 0:false, 4:false, 8:true, 11:false, 12:false, 17:true, 20:false, 21:true, 22:false, 35:true, 37:true, 40:true, 43:false, 44:false, 54:false, 56:true, 58:true, 60:false, 61:false, 62:false, 72:true, 83:false, 86:false, 91:false, 92:false, 96:true, 111:true, 112:true, 115:true, 117:true, 120:true, 123:false} map[int8]bool{-97:true, -75:false, -69:false, -46:true, -44:true, -8:true, 19:false, 27:true, 35:false, 36:true, 61:true, 87:true, 106:false, 107:true, 115:false, 123:false, 124:true}
Example (Constraints) ¶
This example demonstrates usage of Map(Int8(), Uint8())generator with constraints for generation of map[int8]uint8 values. Map() generator requires generators for generating map's key-value pairs. Int8() and Uint8() are used as map's key and value generators (respectively). Constraints define range of generatable values for map's size.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(m map[int8]uint8) { fmt.Printf("%#v\n", m) }, generator.Map( generator.Int8(), generator.Uint8(), constraints.Length{ Min: 0, Max: 5, }, ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: map[int8]uint8{-40:0xa9, -31:0x2d, 21:0xcc, 40:0x68, 79:0xdf} map[int8]uint8{} map[int8]uint8{-67:0xef} map[int8]uint8{-127:0xed, -102:0xa2, -57:0x40, 19:0xed} map[int8]uint8{-120:0xad, -10:0x1b, 72:0xe0} map[int8]uint8{} map[int8]uint8{-54:0xab, -19:0x26, 4:0x73, 126:0xd7} map[int8]uint8{-25:0xaa} map[int8]uint8{-76:0x2e} map[int8]uint8{-127:0x69, -10:0x71, 7:0x98, 18:0x9c, 101:0x28}
func Nil ¶
Nil returns generator for types that can have nil values. Supported types are: chan, slice, map, func, interface and pointers. Error is returned if target is not one of the supported types.
Example ¶
This example demonstrates how to use Nil() generator for generating nil values for channels, pointers, slices, maps and interfaces.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(p1 chan int, p2 *int, p3 []int, p4 map[string]int, p5 interface{}) { fmt.Printf("%#v, %#v, %#v, %#v, %#v\n", p1, p2, p3, p4, p5) }, generator.Nil(), generator.Nil(), generator.Nil(), generator.Nil(), generator.Nil(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil> (chan int)(nil), (*int)(nil), []int(nil), map[string]int(nil), <nil>
func OneFrom ¶
OneFrom returns one of the provided generators. Error is returned if number of generators is 0, or chosen generator returns an error.
Example ¶
This example demonstrates how to use OneFrom() combinator and Int() generator to generate int values. OneFrom() will select one of the generators passed to it to generate new int value. In this example there are 4 Int() generators with different constraints passed to OneFrom() generator, and every time a int value needs to be generated one of them will be selected randomly.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n int) { fmt.Printf("%d\n", n) }, generator.OneFrom( generator.Int(constraints.Int{Min: 500, Max: 1000}), generator.Int(constraints.Int{Min: 5, Max: 10}), generator.Int(constraints.Int{Min: -10, Max: -5}), generator.Int(constraints.Int{Min: -1000, Max: -500}), ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 5 883 8 -5 -7 5 893 -907 544 -736
func Ptr ¶
Ptr returns an arbitrary.Generator that creates pointer types. The element parameter defines the type to which the pointer points. The limits parameter, even though it is variadic, evaluates only the first instance of constraints.Ptr. If limits are omitted, constraints.PtrDefault is used instead. constraints.Ptr.NilFrequency influences the occurrence of nil pointers:
- NilFrequency of 0 ensures no nil pointers will be generated
- NilFrequency > 0 uses formula 1/NilFrequency.
An error is returned if the generator's target parameter is not a pointer or if the element generator returns an error.
Example ¶
This example demonstrates how to use the Ptr and Int generators to generate *int values. The Ptr generator requires a generator for the type that the pointer points to, so the Int generator is used in this case.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n *int) { if n != nil { fmt.Printf("%v\n", *n) } else { fmt.Printf("%v\n", n) } }, generator.Ptr(generator.Int()), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 3087144572626463248 -322463145728654719 6634247955539956817 -2122761628320059770 6925466992496964832 <nil> 5606570076237929230 5259823212710600989 -1089963290385541773 <nil>
Example (NoNil) ¶
This example demonstrates how to use the Ptr and Uint generators in conjunction with constraints.Ptr to generate non-nil *uint values. By setting NilFrequency to 0, it ensures that nil pointers will never be generated. The Ptr generator requires a generator of the type to which the pointer points, so the Uint generator is used in this case.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n *uint) { fmt.Printf("%v\n", *n) }, generator.Ptr(generator.Uint(), constraints.Ptr{NilFrequency: 0}), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 4518808235179270133 3087144572626463248 7861855757602232086 12784885724210938115 2119085704421221023 1543285579645681342 15398783846516204029 9472434474353809100 3877601997538530707 16172318933975836041
func Recursive ¶ added in v0.3.1
Recursive can be used to define recursive types (structures and functions). The 'genFunc' parameter is a function that provides a Recurse function, which can be used to specify a recursive call of the generator returned by 'genFunc'. The 'depth' parameter controls how deep the recursion goes (a value of n will cause n recursions).
Example (BinaryTree) ¶
type Node struct { Value int Left *Node Right *Node } var nodeString func(*Node) string nodeString = func(n *Node) string { if n == nil { return "nil" } return fmt.Sprintf("{Value: %d, Left: %s Right %s}", n.Value, nodeString(n.Left), nodeString(n.Right)) } err := Stream(0, 5, Streamer( func(tree *Node) { fmt.Println(nodeString(tree)) }, Recursive(func(r Recurse) arbitrary.Generator { return Ptr(Struct(map[string]arbitrary.Generator{ "Value": Int(constraints.Int{Min: 0, Max: 10}), "Left": r(), "Right": r(), }), constraints.Ptr{NilFrequency: 3}) }, 3), )) if err != nil { panic(fmt.Errorf("Unexpected error: '%s'", err)) }
Output: {Value: 2, Left: nil Right {Value: 0, Left: nil Right {Value: 5, Left: nil Right {Value: 3, Left: nil Right nil}}}} {Value: 3, Left: {Value: 1, Left: {Value: 8, Left: {Value: 5, Left: nil Right nil} Right nil} Right {Value: 10, Left: {Value: 2, Left: nil Right nil} Right {Value: 5, Left: nil Right nil}}} Right nil} {Value: 9, Left: {Value: 4, Left: {Value: 9, Left: {Value: 1, Left: nil Right nil} Right {Value: 7, Left: nil Right nil}} Right {Value: 4, Left: {Value: 8, Left: nil Right nil} Right nil}} Right nil} {Value: 6, Left: {Value: 9, Left: {Value: 7, Left: {Value: 4, Left: nil Right nil} Right {Value: 3, Left: nil Right nil}} Right nil} Right {Value: 6, Left: {Value: 4, Left: {Value: 8, Left: nil Right nil} Right {Value: 9, Left: nil Right nil}} Right {Value: 3, Left: {Value: 0, Left: nil Right nil} Right {Value: 7, Left: nil Right nil}}}} {Value: 9, Left: {Value: 9, Left: {Value: 8, Left: {Value: 2, Left: nil Right nil} Right {Value: 10, Left: nil Right nil}} Right {Value: 10, Left: {Value: 0, Left: nil Right nil} Right {Value: 10, Left: nil Right nil}}} Right {Value: 4, Left: {Value: 3, Left: nil Right nil} Right nil}}
Example (RecursiveFunction) ¶
type recursive func() (int, recursive) err := Stream(0, 10, Streamer( func(fn recursive) { ns := []int{} for fn != nil { var n int n, fn = fn() ns = append(ns, n) } fmt.Println(ns) }, Recursive(func(r Recurse) arbitrary.Generator { return Weighted( []uint64{4, 1}, Func(Int(constraints.Int{Min: 0, Max: 10}), r()), Nil(), ) }, 10), )) if err != nil { panic(fmt.Errorf("Unexpected error: '%s'", err)) }
Output: [4 2 7 5 9 8 8] [0] [10] [10 6] [1 8 9] [] [10 8 8 3 9 6 10 4 3 9 2] [1 7] [] []
func Rune ¶
func Rune(limits ...constraints.Rune) arbitrary.Generator
Rune returns generator for rune types. Range of rune values that can be generated is defined by "limits" parameter. If no limits are provided default [0, 0x10ffff] code point range is used which includes all Unicode16 characters. Error is returned if minimal code point is greater than maximal code point, minimal code point is lower than 0 or maximal code point is greater than 0x10ffff
Example ¶
This example demonstrates how to use Rune() generator for generation of rune values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(r rune) { fmt.Printf("%c\n", r) }, generator.Rune(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 뿐 𬟀 띛
Example (Constraints) ¶
This example demonstrates how to use Rune() generator with constraints for generation of rune values. Constraints define range of generatble rune values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(r rune) { fmt.Printf("%c\n", r) }, generator.Rune(constraints.Rune{ MinCodePoint: 'a', MaxCodePoint: 'z', }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: v s w q q w r f w u
func Slice ¶
Slice returns generator for slice types. Slice elements are generated with generator specified by "element" parameter. Range of slice size values is defined by "limits" parameter. If "limits" parameter is not specified default [0, 100] range is used instead. Error is returned if generator's target is not a slice type, element generator returns an error, or limits.Min > limits.Max
Example ¶
This example demonstrates how to use Slice(Int()) generator for generation of []int values. Slice requires generator for it's elements to be passed to it, thus Int() generator is used.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ints []int) { fmt.Printf("%#v\n", ints) }, generator.Slice( generator.Int(constraints.Int{ Min: 0, Max: 100, }), ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: []int{16, 80, 86, 69, 22, 84, 3, 64, 30, 91, 3, 45, 81, 31, 40, 62, 92, 21, 71, 50, 57, 76, 79, 95, 40, 69, 19, 94, 73, 9, 9} []int{96, 81, 67, 36, 23, 59, 44, 52, 57, 56, 14, 30, 64, 77, 23, 29, 15, 52, 93, 51, 34, 1, 13, 44, 86, 68, 84, 40, 61, 19, 12, 57, 25, 83, 11, 0, 76, 87, 45, 73, 72} []int{89, 12, 88, 96, 34, 10, 80, 74, 64, 27, 0, 36, 42, 7, 48, 4, 4, 95, 54, 46, 14, 43, 0, 17, 9, 17, 16, 13, 87, 19, 61, 30, 36, 38, 25, 47, 25, 56, 67, 88, 51, 29, 42, 62, 81} []int{66, 22, 96, 2, 46, 21, 89, 12, 0, 7, 24, 10, 100, 38, 11, 18, 95, 28, 36, 100, 19, 69, 40, 94, 49, 89, 94, 30, 72, 48, 18, 6, 50, 28, 70, 79, 22, 74, 16, 68, 64, 70, 33, 42, 84, 11, 7, 3, 99, 52, 36, 46, 33, 64, 50, 32, 36, 48, 51, 81, 37, 36, 12, 7, 84, 48, 86, 64, 71, 86, 41, 41, 42, 47, 25, 17} []int{37, 17, 94, 50, 16, 20, 53, 100, 80, 89, 93, 65, 9, 78, 16, 45, 75, 90, 54, 12, 24, 94, 60, 90, 37, 90, 80, 68, 98, 8, 91, 66, 45, 16, 49, 61, 52, 42, 62, 81, 67, 86, 21, 73, 61, 16, 3, 22, 27, 27, 11, 62, 24, 9, 64, 14, 84, 36, 35, 56, 62, 96, 15, 14, 38, 25, 10, 84, 42, 43, 93, 38, 12, 44, 43, 31, 12, 88, 66, 43, 47, 41, 40} []int{70, 79, 28, 51, 70, 67, 16, 28, 97, 14, 87, 1, 91, 99, 24, 21, 23, 94, 39, 63, 59} []int{68, 28, 45, 75, 4, 68, 42, 54, 80, 67, 53, 35, 84, 60, 2, 92, 28} []int{45, 58, 33, 93, 14, 40, 80, 83, 67, 27, 21, 87, 25, 73, 50, 90, 75, 72, 62, 0, 32, 26, 56, 96, 6, 73, 39, 18, 84, 2, 95, 12, 85, 1, 43, 35, 77, 13, 6, 45, 4, 12, 34, 83, 59, 29, 1, 47, 69, 48, 100, 35, 42, 13, 19, 36, 67, 88, 24, 12, 1, 24, 80, 29} []int{53, 30, 45, 39, 26, 98} []int{26, 71, 84, 83, 33, 60, 15, 68, 98, 5, 97, 10, 27, 88, 86, 20, 54, 84, 5, 59, 19, 77, 14, 1, 97, 67, 0, 99, 12, 44, 99, 84, 95, 11, 13, 96, 67, 69, 55, 88, 68, 25, 47, 66, 98, 78, 57, 73, 14, 3, 51, 77, 39, 25, 90, 53, 74, 84, 22, 51, 79, 81, 93, 79, 43, 7, 48, 83, 29, 61, 29, 24, 68, 60, 64, 56, 10, 33, 97, 61, 70, 68, 93, 77, 1, 19, 92, 21, 44, 86}
Example (Constraints) ¶
This example demonstrates how to use Slice(Int()) generator with constraints for generation of []int values. Constraints define range of generatable values for slice's size.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(ints []int) { fmt.Printf("%#v\n", ints) }, generator.Slice( generator.Int(constraints.Int{ Min: 0, Max: 100, }), constraints.Length{Min: 0, Max: 10}, ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: []int{31, 16, 80, 86, 69} []int{84, 3, 64, 30, 91, 3} []int{31} []int{62, 92, 21, 71, 50, 57, 76, 79} []int{95, 40} []int{19, 94, 73, 9, 9} []int{96, 81, 67, 36, 23, 59, 44, 52, 57} []int{14, 30, 64, 77, 23, 29, 15, 52} []int{34, 1, 13} []int{68, 84, 40, 61, 19, 12}
func Stream ¶ added in v0.1.7
Stream streams data using a streamer. Seed for random number generation is specified by seed paramere, Number of values that will be generated is specified by "count" parameter Error is returned if streamer returns an error.
func Streamer ¶ added in v0.1.7
Streamer returns new streamer that generates data using "generators" parameter and streams it to it's target. Target must be a function that has no output values. Number of input parameters can be arbitrary. Error is returned if "target" is not a function, number of generators doesn't match target's number of input parameters, target doesn't have 0 output values or if any of generators returns an error.
func String ¶
func String(limits ...constraints.String) arbitrary.Generator
String returns generator for string types. Range of slice size is defined by "limits" parameter. If "limits" parameter is not specified default [0, 100] range is used instead. Error is returned if generator's target is not a string type, or limits.Min > limits.Max
Example ¶
This example demontrates how to use String() generator for generation of string values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(s string) { fmt.Printf("%s\n", s) }, generator.String(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 뿐𬟀띛𪊟𑖓粩𐀞 𣃍𫜬𥮙磸𢙑矠鰮 𑋅𩯏 𬡔𔒤ꃹ𱶥𥔌𠰇𭣇⦪닝𒒘𭻐깄堭🥲𦃻 ꦎ𮧞𫏐𨶛蕭 뾤먵𰥢𑒊끔𡏡🍃 𡫍𦹯𗒇싓ﴊ𪻖𣛖鱨 馹𡪱ࡄ𧽿𧳓𱧮𨔴 ක𘁦𧘁 𪐳𠅅᛭ꢵ闽ᯀ熝𝡘𲅛𫨮𬢺�𗉘綤ꆓ𬤅
Example (Constraints) ¶
This example demontrates how to use String() generator with constraints for generation of string values. Constraints define range of generatables values for string's runes and string's size.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(s string) { fmt.Printf("%s\n", s) }, generator.String( constraints.String{ Rune: constraints.Rune{ MinCodePoint: 'a', MaxCodePoint: 'z', }, Length: constraints.Length{ Min: 2, Max: 10, }, }, ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: swqqwrf udadnrii hszmpsi tjjujja dex zyoagn putcbnmwe inytmz ylamx nzmyacimkq
func Struct ¶
Struct returns generator for struct types. arbitrary.Generators for struct fields can be passed through "fields" parameter. If generator for a field is not provided, Any() generator is used for that field. Error is returned if generator's target is not struct, generator for a field that struct doesn't contain is specified or any of the field generators returns an error.
Example ¶
This example demonstrates how to use Struct() generator for generation of struct values. Struct() generator requires map[string]arbitrary.Generator, where map's key-value pairs represent struct's field generators. Provided field generator or Any() generator (if field generator is not provided) will be used to generate data for struct's field. In this example generators for fields X and Y are provided while for Z is ommited.
package main import ( "fmt" "github.com/steffnova/go-check/arbitrary" "github.com/steffnova/go-check/generator" ) func main() { // Point struct will be used as struct example type Point struct { X int16 Y int16 Z int8 } streamer := generator.Streamer( func(p Point) { fmt.Printf("%#v\n", p) }, generator.Struct(map[string]arbitrary.Generator{ "X": generator.Int16(), "Y": generator.Int16(), }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: generator_test.Point{X:12790, Y:-13142, Z:-30} generator_test.Point{X:3323, Y:-16168, Z:50} generator_test.Point{X:-4175, Y:5523, Z:116} generator_test.Point{X:-29920, Y:-6926, Z:-23} generator_test.Point{X:20130, Y:6440, Z:19} generator_test.Point{X:20179, Y:-28489, Z:-10} generator_test.Point{X:16896, Y:-743, Z:-95} generator_test.Point{X:3072, Y:-7313, Z:-30} generator_test.Point{X:20655, Y:29016, Z:81} generator_test.Point{X:-7244, Y:12674, Z:7}
func Uint ¶
func Uint(limits ...constraints.Uint) arbitrary.Generator
UInt returns generator for uint types. Range of uint values that can be generated is defined by "limits" parameter. If no limits are provided default uint range is used instead. Error is returned if generator's target is not uint type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Unt() generator for generation of uint values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint) { fmt.Printf("%d\n", n) }, generator.Uint(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 4518808235179270133 3087144572626463248 7861855757602232086 12784885724210938115 2119085704421221023 1543285579645681342 15398783846516204029 9472434474353809100 3877601997538530707 16172318933975836041
Example (Constraints) ¶
This example demonstrates usage of Int() generator with constraints for generation of int values. Constraints defines range of generatable int values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint) { fmt.Printf("%d\n", n) }, generator.Uint(constraints.Uint{ Min: 0, Max: 10, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 5 2 6 0 0 6 1 5 6 4
func Uint16 ¶
func Uint16(limits ...constraints.Uint16) arbitrary.Generator
Uint16 returns generator for uint16 types. Range of int16 values that can be generated is defined by "limits" parameter. If no limits are provided default uint16 range [0, math.MaxUint16] is used instead. Error is returned if generator's target is not uint16 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Unt16() generator for generation of uint16 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint16) { fmt.Printf("%d\n", n) }, generator.Uint16(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 24565 49138 12790 59920 49104 44529 16643 51136 46939 5201
Example (Constraints) ¶
This example demonstrates usage of Uint16() generator with constraints for generation of int16 values. Constraints defines range of generatable int values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint16) { fmt.Printf("%d\n", n) }, generator.Uint16(constraints.Uint16{ Min: 100, Max: 500, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 131 116 442 483 378 359 130 447 487 145
func Uint32 ¶
func Uint32(limits ...constraints.Uint32) arbitrary.Generator
Uint32 returns generator for uint32 types. Range of int32 values that can be generated is defined by "limits" parameter. If no limits are provided default uint32 range [0, math.MaxUint32] is used instead. Error is returned if generator's target is not uint32 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Unt32() generator for generation of uint32 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint32) { fmt.Printf("%d\n", n) }, generator.Uint32(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 3649778518 2615979505 1530763030 898515203 2935165982 649801091 2329218299 2225250975 3653467838 2023694845
Example (Constraints) ¶
This example demonstrates usage of Uint32() generator with constraints for generation of uint32 values. Constraints defines range of generatable int32 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint32) { fmt.Printf("%d\n", n) }, generator.Uint32(constraints.Uint32{ Min: 10000, Max: 20000, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 18181 15910 10259 11984 15150 11069 15201 13323 18863 19832
func Uint64 ¶
func Uint64(limits ...constraints.Uint64) arbitrary.Generator
Uint64 returns generator for uint64 types. Range of int64 values that can be generated is defined by "limits" parameter. If no limits are provided default uint64 range [0, math.MaxUint64] is used instead. Error is returned if generator's target is not uint64 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Unt64() generator for generation of uint64 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint64) { fmt.Printf("%d\n", n) }, generator.Uint64(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 4518808235179270133 3087144572626463248 7861855757602232086 12784885724210938115 2119085704421221023 1543285579645681342 15398783846516204029 9472434474353809100 3877601997538530707 16172318933975836041
Example (Constraints) ¶
This example demonstrates usage of Uint64() generator with constraints for generation of uint64 values. Constraints defines range of generatable int64 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint64) { fmt.Printf("%d\n", n) }, generator.Uint64(constraints.Uint64{ Min: 0, Max: 100, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 31 16 80 86 69 22 84 3 64 30
func Uint8 ¶
func Uint8(limits ...constraints.Uint8) arbitrary.Generator
Uint8 returns generator for uint8 types. Range of int8 values that can be generated is defined by "limits" parameter. If no limits are provided default uint8 range [0, math.MaxUint8] is used instead. Error is returned if generator's target is not uint8 type or limits.Min is greater than limits.Max.
Example ¶
This example demonstrates usage of Unt8() generator for generation of uint8 values.
package main import ( "fmt" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint8) { fmt.Printf("%d\n", n) }, generator.Uint8(), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 31 16 30 45 81 251 159 190 104 92
Example (Constraints) ¶
This example demonstrates usage of Uint8() generator with constraints for generation of uint8 values. Constraints defines range of generatable uint8 values.
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n uint8) { fmt.Printf("%d\n", n) }, generator.Uint8(constraints.Uint8{ Min: 20, Max: 50, }), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 41 38 42 36 36 42 37 25 42 40
func Weighted ¶
Weighted returns one of the generators based on their weight. Weights and generators are specified by "weights" and "generators" parameters respectively. Number of weights and generators must be the same and greater than 0. Total sum of all weights can't exceed math.Uint64. Error is returned if number of "weights" and "generators" is invalid, sum of all weights exceed math.Uint64, weight value is lower than 1, or weighted generators returns an error.
Example ¶
This example demonstrates usage of Weighted() combinator and Nil() and PtrTo(Uint64()) generators for generation of *uint64 values. Weighted() will use one of the generators passed to it based on generator's weight. Weights define how often a generator will be selected by Weighted(). Selection chance is calculated as generator's weight devided by summ of all weights and multiplied by 100. In this example Nil() generator will have 10% selection chance (1/10 * 100) and PtrTo(Uint64()) will have 90% selection chance (9/10 * 100)
package main import ( "fmt" "github.com/steffnova/go-check/constraints" "github.com/steffnova/go-check/generator" ) func main() { streamer := generator.Streamer( func(n *uint64) { if n == nil { fmt.Printf("%v\n", n) } else { fmt.Printf("%d\n", *n) } }, generator.Weighted( []uint64{1, 9}, generator.Nil(), generator.Ptr(generator.Uint64(), constraints.Ptr{NilFrequency: 0}), ), ) if err := generator.Stream(0, 10, streamer); err != nil { panic(err) } }
Output: 4518808235179270133 <nil> 7861855757602232086 5254077479683016640 11116474692239114024 15398783846516204029 14677457169740829639 9472434474353809100 2396012503939351775 3877601997538530707