-
Notifications
You must be signed in to change notification settings - Fork 25
/
combinators_example_test.go
146 lines (127 loc) · 2.23 KB
/
combinators_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Copyright 2020 Gregory Petrosyan <[email protected]>
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package rapid_test
import (
"fmt"
"strconv"
"pgregory.net/rapid"
)
func ExampleCustom() {
type point struct {
x int
y int
}
gen := rapid.Custom(func(t *rapid.T) point {
return point{
x: rapid.IntRange(-100, 100).Draw(t, "x"),
y: rapid.IntRange(-100, 100).Draw(t, "y"),
}
})
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// {-1 23}
// {-3 -50}
// {0 94}
// {-2 -50}
// {11 -57}
}
func recursive() *rapid.Generator[any] {
return rapid.OneOf(
rapid.Bool().AsAny(),
rapid.SliceOfN(rapid.Deferred(recursive), 1, 2).AsAny(),
)
}
func ExampleDeferred() {
gen := recursive()
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// [[[[false] false]]]
// false
// [[true [[[true]]]]]
// true
// true
}
func ExampleMap() {
gen := rapid.Map(rapid.Int(), strconv.Itoa)
for i := 0; i < 5; i++ {
fmt.Printf("%#v\n", gen.Example(i))
}
// Output:
// "-3"
// "-186981"
// "4"
// "-2"
// "43"
}
func ExampleJust() {
gen := rapid.Just(42)
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// 42
// 42
// 42
// 42
// 42
}
func ExampleSampledFrom() {
gen := rapid.SampledFrom([]int{1, 2, 3})
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// 2
// 3
// 2
// 3
// 1
}
func ExamplePermutation() {
gen := rapid.Permutation([]int{1, 2, 3})
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// [2 3 1]
// [3 2 1]
// [2 1 3]
// [3 2 1]
// [1 2 3]
}
func ExampleOneOf() {
gen := rapid.OneOf(rapid.Int32Range(1, 10).AsAny(), rapid.Float32Range(100, 1000).AsAny())
for i := 0; i < 5; i++ {
fmt.Println(gen.Example(i))
}
// Output:
// 997.0737
// 10
// 475.3125
// 2
// 9
}
func ExamplePtr() {
gen := rapid.Ptr(rapid.Int(), true)
for i := 0; i < 5; i++ {
v := gen.Example(i)
if v == nil {
fmt.Println("<nil>")
} else {
fmt.Println("(*int)", *v)
}
}
// Output:
// (*int) 1
// (*int) -3
// <nil>
// (*int) 590
// <nil>
}