pbt 0.7.0 → 0.8.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.standard.yml +1 -1
- data/CHANGELOG.md +15 -0
- data/CLAUDE.md +2 -2
- data/README.md +94 -17
- data/benchmark/FIBER.md +97 -0
- data/benchmark/fiber_experiment.rb +39 -0
- data/benchmark/fiber_scheduler/Gemfile +7 -0
- data/benchmark/fiber_scheduler/experiment.rb +131 -0
- data/benchmark/fiber_scheduler/spec/experiment_spec.rb +38 -0
- data/docs/plans/fiber-experiment.md +12 -0
- data/docs/plans/generator-statistics.md +16 -0
- data/docs/plans/stateful-pbt-redesign.md +415 -0
- data/lib/pbt/arbitrary/arbitrary.rb +63 -10
- data/lib/pbt/arbitrary/arbitrary_methods.rb +51 -24
- data/lib/pbt/arbitrary/array_arbitrary.rb +45 -11
- data/lib/pbt/arbitrary/choose_arbitrary.rb +27 -0
- data/lib/pbt/arbitrary/constant.rb +0 -1
- data/lib/pbt/arbitrary/constant_arbitrary.rb +5 -0
- data/lib/pbt/arbitrary/constant_from_arbitrary.rb +39 -0
- data/lib/pbt/arbitrary/filter_arbitrary.rb +40 -2
- data/lib/pbt/arbitrary/fixed_hash_arbitrary.rb +16 -0
- data/lib/pbt/arbitrary/float_arbitrary.rb +31 -0
- data/lib/pbt/arbitrary/frequency_arbitrary.rb +61 -0
- data/lib/pbt/arbitrary/integer_arbitrary.rb +11 -0
- data/lib/pbt/arbitrary/map_arbitrary.rb +40 -2
- data/lib/pbt/arbitrary/one_of_arbitrary.rb +46 -13
- data/lib/pbt/arbitrary/sample.rb +97 -0
- data/lib/pbt/arbitrary/tuple_arbitrary.rb +22 -0
- data/lib/pbt/check/case.rb +1 -1
- data/lib/pbt/check/configuration.rb +5 -3
- data/lib/pbt/check/property.rb +23 -4
- data/lib/pbt/check/runner_iterator.rb +84 -10
- data/lib/pbt/check/runner_methods.rb +47 -17
- data/lib/pbt/check/tosser.rb +1 -1
- data/lib/pbt/reporter/run_details.rb +2 -2
- data/lib/pbt/reporter/run_details_reporter.rb +13 -0
- data/lib/pbt/reporter/run_execution.rb +47 -4
- data/lib/pbt/stateful/property.rb +5 -1
- data/lib/pbt/statistics.rb +28 -0
- data/lib/pbt/version.rb +1 -1
- data/lib/pbt.rb +45 -9
- metadata +16 -3
|
@@ -3,13 +3,16 @@
|
|
|
3
3
|
require "pbt/arbitrary/arbitrary"
|
|
4
4
|
require "pbt/arbitrary/constant"
|
|
5
5
|
require "pbt/arbitrary/constant_arbitrary"
|
|
6
|
+
require "pbt/arbitrary/constant_from_arbitrary"
|
|
6
7
|
require "pbt/arbitrary/array_arbitrary"
|
|
7
8
|
require "pbt/arbitrary/integer_arbitrary"
|
|
8
9
|
require "pbt/arbitrary/tuple_arbitrary"
|
|
9
10
|
require "pbt/arbitrary/fixed_hash_arbitrary"
|
|
10
11
|
require "pbt/arbitrary/choose_arbitrary"
|
|
11
12
|
require "pbt/arbitrary/one_of_arbitrary"
|
|
13
|
+
require "pbt/arbitrary/frequency_arbitrary"
|
|
12
14
|
require "pbt/arbitrary/map_arbitrary"
|
|
15
|
+
require "pbt/arbitrary/float_arbitrary"
|
|
13
16
|
require "pbt/arbitrary/filter_arbitrary"
|
|
14
17
|
|
|
15
18
|
module Pbt
|
|
@@ -28,7 +31,7 @@ module Pbt
|
|
|
28
31
|
#
|
|
29
32
|
# @param max [Integer] Upper limit for the generated integers (included).
|
|
30
33
|
# @return [Arbitrary<Integer>]
|
|
31
|
-
def nat(max:
|
|
34
|
+
def nat(max: 1_000_000)
|
|
32
35
|
integer(min: 0, max: max)
|
|
33
36
|
end
|
|
34
37
|
|
|
@@ -68,28 +71,52 @@ module Pbt
|
|
|
68
71
|
|
|
69
72
|
# Picks a random integer in the given range.
|
|
70
73
|
#
|
|
71
|
-
# @see Pbt.
|
|
74
|
+
# @see Pbt.constant_from
|
|
72
75
|
# @param range [Range<Integer>] Range of integers to choose from.
|
|
73
76
|
# @return [Arbitrary<Integer>]
|
|
74
77
|
def choose(range)
|
|
75
78
|
ChooseArbitrary.new(range)
|
|
76
79
|
end
|
|
77
80
|
|
|
78
|
-
# Picks a random
|
|
81
|
+
# Picks a random constant value from the given choices.
|
|
79
82
|
# The choices can be of any type.
|
|
80
83
|
#
|
|
81
|
-
# @
|
|
82
|
-
#
|
|
83
|
-
#
|
|
84
|
-
|
|
85
|
-
|
|
84
|
+
# @example
|
|
85
|
+
# Pbt.constant_from(:a, 1, 0.1).generate # => :a or 1 or 0.1
|
|
86
|
+
#
|
|
87
|
+
# @param choices [Array<Object>] Non-empty list of constant choices.
|
|
88
|
+
# @return [ConstantFromArbitrary]
|
|
89
|
+
def constant_from(*choices)
|
|
90
|
+
ConstantFromArbitrary.new(choices)
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# Uniformly selects one of the given arbitraries and generates a value from it.
|
|
94
|
+
# Shrinks within the selected arbitrary when used with `Pbt.property` or `sample`.
|
|
95
|
+
#
|
|
96
|
+
# @example
|
|
97
|
+
# Pbt.one_of(Pbt.integer, Pbt.constant(nil))
|
|
98
|
+
#
|
|
99
|
+
# @param arbitraries [Array<Arbitrary>] Non-empty list of arbitraries.
|
|
100
|
+
# @return [OneOfArbitrary]
|
|
101
|
+
def one_of(*arbitraries)
|
|
102
|
+
OneOfArbitrary.new(arbitraries)
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
# Select a generator by relative integer weight. Zero-weight choices are ignored.
|
|
106
|
+
# Shrinks within the selected generator when used with Pbt.property or sample.
|
|
107
|
+
# @example
|
|
108
|
+
# Pbt.frequency([3, Pbt.integer], [1, Pbt.constant(nil)])
|
|
109
|
+
# @param choices [Array<Array(Integer, Arbitrary)>]
|
|
110
|
+
# @return [FrequencyArbitrary]
|
|
111
|
+
def frequency(*choices)
|
|
112
|
+
FrequencyArbitrary.new(choices)
|
|
86
113
|
end
|
|
87
114
|
|
|
88
115
|
# For a lowercase hexadecimal character.
|
|
89
116
|
#
|
|
90
117
|
# @return [Arbitrary<String>]
|
|
91
118
|
def hexa
|
|
92
|
-
|
|
119
|
+
constant_from(*HEXA_CHARS)
|
|
93
120
|
end
|
|
94
121
|
|
|
95
122
|
# For lowercase hexadecimal strings.
|
|
@@ -98,21 +125,21 @@ module Pbt
|
|
|
98
125
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
99
126
|
# @return [Arbitrary<String>]
|
|
100
127
|
def hexa_string(**kwargs)
|
|
101
|
-
array(hexa, **kwargs)
|
|
128
|
+
MapArbitrary.new(array(hexa, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
102
129
|
end
|
|
103
130
|
|
|
104
131
|
# For a single unicode character (including printable and non-printable).
|
|
105
132
|
#
|
|
106
133
|
# @return [Arbitrary<String>]
|
|
107
134
|
def char
|
|
108
|
-
choose(CHAR_RANGE)
|
|
135
|
+
MapArbitrary.new(choose(CHAR_RANGE), CHAR_MAPPER, CHAR_UNMAPPER, bijective: true)
|
|
109
136
|
end
|
|
110
137
|
|
|
111
138
|
# For an alphanumeric character.
|
|
112
139
|
#
|
|
113
140
|
# @return [Arbitrary<String>]
|
|
114
141
|
def alphanumeric_char
|
|
115
|
-
|
|
142
|
+
constant_from(*ALPHANUMERIC_CHARS)
|
|
116
143
|
end
|
|
117
144
|
|
|
118
145
|
# For alphanumeric strings.
|
|
@@ -121,14 +148,14 @@ module Pbt
|
|
|
121
148
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
122
149
|
# @return [Arbitrary<String>]
|
|
123
150
|
def alphanumeric_string(**kwargs)
|
|
124
|
-
array(alphanumeric_char, **kwargs)
|
|
151
|
+
MapArbitrary.new(array(alphanumeric_char, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
125
152
|
end
|
|
126
153
|
|
|
127
154
|
# For an ascii character.
|
|
128
155
|
#
|
|
129
156
|
# @return [Arbitrary<String>]
|
|
130
157
|
def ascii_char
|
|
131
|
-
|
|
158
|
+
constant_from(*ASCII_CHARS)
|
|
132
159
|
end
|
|
133
160
|
|
|
134
161
|
# For ascii strings.
|
|
@@ -137,14 +164,14 @@ module Pbt
|
|
|
137
164
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
138
165
|
# @return [Arbitrary<String>]
|
|
139
166
|
def ascii_string(**kwargs)
|
|
140
|
-
array(ascii_char, **kwargs)
|
|
167
|
+
MapArbitrary.new(array(ascii_char, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
141
168
|
end
|
|
142
169
|
|
|
143
170
|
# For a printable ascii character.
|
|
144
171
|
#
|
|
145
172
|
# @return [Arbitrary<String>]
|
|
146
173
|
def printable_ascii_char
|
|
147
|
-
|
|
174
|
+
constant_from(*PRINTABLE_ASCII_CHARS)
|
|
148
175
|
end
|
|
149
176
|
|
|
150
177
|
# For printable ascii strings.
|
|
@@ -153,14 +180,14 @@ module Pbt
|
|
|
153
180
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
154
181
|
# @return [Arbitrary<String>]
|
|
155
182
|
def printable_ascii_string(**kwargs)
|
|
156
|
-
array(printable_ascii_char, **kwargs)
|
|
183
|
+
MapArbitrary.new(array(printable_ascii_char, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
157
184
|
end
|
|
158
185
|
|
|
159
186
|
# For a printable character.
|
|
160
187
|
#
|
|
161
188
|
# @return [Arbitrary<String>]
|
|
162
189
|
def printable_char
|
|
163
|
-
|
|
190
|
+
constant_from(*PRINTABLE_CHARS)
|
|
164
191
|
end
|
|
165
192
|
|
|
166
193
|
# For printable strings.
|
|
@@ -169,7 +196,7 @@ module Pbt
|
|
|
169
196
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
170
197
|
# @return [Arbitrary<String>]
|
|
171
198
|
def printable_string(**kwargs)
|
|
172
|
-
array(printable_char, **kwargs)
|
|
199
|
+
MapArbitrary.new(array(printable_char, **kwargs), STRING_MAPPER, STRING_UNMAPPER, bijective: true)
|
|
173
200
|
end
|
|
174
201
|
|
|
175
202
|
# For symbols.
|
|
@@ -178,14 +205,14 @@ module Pbt
|
|
|
178
205
|
# @param kwargs [Hash] Options for ArrayArbitrary. See `.array` for more information.
|
|
179
206
|
# @return [Arbitrary<Symbol>]
|
|
180
207
|
def symbol(**kwargs)
|
|
181
|
-
array(
|
|
208
|
+
MapArbitrary.new(array(constant_from(*SYMBOL_SAFE_CHARS), empty: false, **kwargs), SYMBOL_MAPPER, SYMBOL_UNMAPPER, bijective: true)
|
|
182
209
|
end
|
|
183
210
|
|
|
184
211
|
# For floats.
|
|
185
212
|
#
|
|
186
213
|
# @return [Arbitrary<Float>]
|
|
187
214
|
def float
|
|
188
|
-
|
|
215
|
+
FloatArbitrary.new
|
|
189
216
|
end
|
|
190
217
|
|
|
191
218
|
# For symbols.
|
|
@@ -223,7 +250,7 @@ module Pbt
|
|
|
223
250
|
#
|
|
224
251
|
# @return [Arbitrary<Boolean>]
|
|
225
252
|
def boolean
|
|
226
|
-
|
|
253
|
+
constant_from(true, false)
|
|
227
254
|
end
|
|
228
255
|
|
|
229
256
|
# For any constant values.
|
|
@@ -253,7 +280,7 @@ module Pbt
|
|
|
253
280
|
# @return [Arbitrary<Date>]
|
|
254
281
|
def date(base_date: Date.today, past_offset_days: -18250, future_offset_days: 18250)
|
|
255
282
|
offset_arb = integer(min: past_offset_days, max: future_offset_days)
|
|
256
|
-
|
|
283
|
+
MapArbitrary.new(offset_arb, DATE_MAPPER.call(base_date), DATE_UNMAPPER.call(base_date), bijective: true)
|
|
257
284
|
end
|
|
258
285
|
|
|
259
286
|
# For past dates between `base_date - past_offset_days` and `base_date`.
|
|
@@ -282,7 +309,7 @@ module Pbt
|
|
|
282
309
|
# @return [Arbitrary<Time>]
|
|
283
310
|
def time(base_time: Time.now, past_offset_seconds: -1576800000, future_offset_seconds: 1576800000)
|
|
284
311
|
offset_arb = integer(min: past_offset_seconds, max: future_offset_seconds)
|
|
285
|
-
|
|
312
|
+
MapArbitrary.new(offset_arb, TIME_MAPPER.call(base_time), TIME_UNMAPPER.call(base_time), bijective: true)
|
|
286
313
|
end
|
|
287
314
|
|
|
288
315
|
# For past times between `base_time + past_offset_seconds` and `base_time`
|
|
@@ -25,24 +25,58 @@ module Pbt
|
|
|
25
25
|
length.times.map { @value_arb.generate(rng) }
|
|
26
26
|
end
|
|
27
27
|
|
|
28
|
+
def contextual?
|
|
29
|
+
Sample.contextual?(@value_arb)
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
def sample(rng = Random.new)
|
|
33
|
+
samples = Array.new(@length_arb.generate(rng)) { Sample.capture(@value_arb, rng) }
|
|
34
|
+
Sample.array(samples, @length_arb, @min_length)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# @see Arbitrary#cover?
|
|
38
|
+
def cover?(value)
|
|
39
|
+
return false unless value.is_a?(Array)
|
|
40
|
+
return false if value.size < @min_length
|
|
41
|
+
return false if !@max_length.nil? && value.size > @max_length
|
|
42
|
+
|
|
43
|
+
unknown = false
|
|
44
|
+
value.each do |item|
|
|
45
|
+
covered = Arbitrary.cover_result(@value_arb, item)
|
|
46
|
+
return false if covered == false
|
|
47
|
+
|
|
48
|
+
unknown = true if covered.nil?
|
|
49
|
+
end
|
|
50
|
+
unknown ? nil : true
|
|
51
|
+
end
|
|
52
|
+
|
|
28
53
|
# @see Arbitrary#shrink
|
|
29
54
|
def shrink(current)
|
|
30
|
-
return Enumerator.new { |_| } if current.size == @min_length
|
|
31
|
-
|
|
32
55
|
Enumerator.new do |y|
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
56
|
+
if current.size > @min_length
|
|
57
|
+
# First, shrink the length of the array via contiguous slices.
|
|
58
|
+
emitted_n_minus_1 = false
|
|
59
|
+
@length_arb.shrink(current.size).each do |length|
|
|
60
|
+
emitted_n_minus_1 = true if length == current.size - 1
|
|
61
|
+
if length == 0
|
|
62
|
+
y.yield []
|
|
63
|
+
next
|
|
64
|
+
end
|
|
65
|
+
current.each_cons(length) do |con|
|
|
66
|
+
y.yield con
|
|
67
|
+
end
|
|
39
68
|
end
|
|
40
|
-
|
|
41
|
-
|
|
69
|
+
|
|
70
|
+
# Second, try deleting individual elements (including non-contiguous interior elements).
|
|
71
|
+
if current.size >= 3
|
|
72
|
+
indices = emitted_n_minus_1 ? (1...(current.size - 1)) : (0...current.size)
|
|
73
|
+
indices.each do |i|
|
|
74
|
+
y.yield [*current[...i], *current[i + 1..]]
|
|
75
|
+
end
|
|
42
76
|
end
|
|
43
77
|
end
|
|
44
78
|
|
|
45
|
-
#
|
|
79
|
+
# Third, shrink each element of the array (even when already at min_length).
|
|
46
80
|
current.each_with_index do |item, i|
|
|
47
81
|
@value_arb.shrink(item).each do |val|
|
|
48
82
|
y.yield [*current[...i], val, *current[i + 1..]]
|
|
@@ -12,6 +12,23 @@ module Pbt
|
|
|
12
12
|
# @see Arbitrary#generate
|
|
13
13
|
def generate(rng = Random.new)
|
|
14
14
|
rng.rand(@range)
|
|
15
|
+
rescue ArgumentError => e
|
|
16
|
+
raise EmptyDomainError, e.message if empty_range?
|
|
17
|
+
|
|
18
|
+
raise
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# @see Arbitrary#cover?
|
|
22
|
+
def cover?(value)
|
|
23
|
+
return false if @range.begin.nil? || @range.end.nil?
|
|
24
|
+
|
|
25
|
+
if integer_range?
|
|
26
|
+
return false unless value.is_a?(Integer)
|
|
27
|
+
else
|
|
28
|
+
return false unless value.is_a?(Float) && value.finite?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
@range.cover?(value)
|
|
15
32
|
end
|
|
16
33
|
|
|
17
34
|
# @see Arbitrary#shrink
|
|
@@ -20,6 +37,16 @@ module Pbt
|
|
|
20
37
|
min, max = [@range.begin, @range.end].sort
|
|
21
38
|
IntegerArbitrary.new(min, max).shrink(current, target: min)
|
|
22
39
|
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def integer_range?
|
|
44
|
+
@range.begin.is_a?(Integer) && @range.end.is_a?(Integer)
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def empty_range?
|
|
48
|
+
!@range.begin.nil? && !@range.end.nil? && !@range.cover?(@range.begin)
|
|
49
|
+
end
|
|
23
50
|
end
|
|
24
51
|
end
|
|
25
52
|
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Generates one of the given constant values.
|
|
6
|
+
class ConstantFromArbitrary < Arbitrary
|
|
7
|
+
# @param choices [Array<Object>] Non-empty list of constant choices.
|
|
8
|
+
def initialize(choices)
|
|
9
|
+
raise EmptyDomainError, "constant_from requires at least one choice" if choices.empty?
|
|
10
|
+
|
|
11
|
+
@choices = choices.dup
|
|
12
|
+
@idx_arb = IntegerArbitrary.new(0, @choices.size - 1)
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
# @see Arbitrary#generate
|
|
16
|
+
def generate(rng = Random.new)
|
|
17
|
+
@choices[@idx_arb.generate(rng)]
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @see Arbitrary#cover?
|
|
21
|
+
def cover?(value)
|
|
22
|
+
@choices.any? { |choice| choice.equal?(value) || choice.eql?(value) }
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
# @see Arbitrary#shrink
|
|
26
|
+
def shrink(current)
|
|
27
|
+
index = @choices.index { |choice| choice.equal?(current) || choice.eql?(current) }
|
|
28
|
+
return Enumerator.new {} if index.nil?
|
|
29
|
+
|
|
30
|
+
# Shrinks to values earlier in the list of `choices`.
|
|
31
|
+
Enumerator.new do |y|
|
|
32
|
+
@idx_arb.shrink(index).each do |idx|
|
|
33
|
+
y << @choices[idx]
|
|
34
|
+
end
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -4,19 +4,57 @@ module Pbt
|
|
|
4
4
|
module Arbitrary
|
|
5
5
|
# Generates values from another arbitrary, but only if they pass a predicate.
|
|
6
6
|
class FilterArbitrary < Arbitrary
|
|
7
|
+
DEFAULT_MAX_ATTEMPTS = 1_000
|
|
8
|
+
|
|
7
9
|
# @param arb [Arbitrary] Arbitrary to generate values to be filtered.
|
|
10
|
+
# @param max_attempts [Integer] Maximum generation attempts before raising `FilterExhaustedError`.
|
|
8
11
|
# @param refinement [Proc] Predicate proc to test each produced element. Return true to keep the element, false otherwise.
|
|
9
|
-
def initialize(arb, &refinement)
|
|
12
|
+
def initialize(arb, max_attempts: DEFAULT_MAX_ATTEMPTS, &refinement)
|
|
13
|
+
raise ArgumentError, "filter requires a block" unless refinement
|
|
14
|
+
unless max_attempts.is_a?(Integer) && max_attempts.positive?
|
|
15
|
+
raise ArgumentError, "max_attempts must be a positive Integer"
|
|
16
|
+
end
|
|
17
|
+
|
|
10
18
|
@arb = arb
|
|
19
|
+
@max_attempts = max_attempts
|
|
11
20
|
@refinement = refinement
|
|
12
21
|
end
|
|
13
22
|
|
|
14
23
|
# @see Arbitrary#generate
|
|
15
24
|
def generate(rng = Random.new)
|
|
16
|
-
|
|
25
|
+
@max_attempts.times do
|
|
17
26
|
val = @arb.generate(rng)
|
|
18
27
|
return val if @refinement.call(val)
|
|
19
28
|
end
|
|
29
|
+
|
|
30
|
+
raise FilterExhaustedError, "FilterArbitrary failed to generate a matching value after #{@max_attempts} attempts"
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def contextual?
|
|
34
|
+
Sample.contextual?(@arb)
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
def sample(rng = Random.new)
|
|
38
|
+
@max_attempts.times do
|
|
39
|
+
sample = Sample.capture(@arb, rng)
|
|
40
|
+
return sample.filter(&@refinement) if @refinement.call(sample.value)
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
raise FilterExhaustedError, "FilterArbitrary failed to generate a matching value after #{@max_attempts} attempts"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# @see Arbitrary#cover?
|
|
47
|
+
def cover?(value)
|
|
48
|
+
base = Arbitrary.cover_result(@arb, value)
|
|
49
|
+
return false if base == false
|
|
50
|
+
|
|
51
|
+
passed = begin
|
|
52
|
+
!!@refinement.call(value)
|
|
53
|
+
rescue
|
|
54
|
+
return nil
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
passed ? base : false
|
|
20
58
|
end
|
|
21
59
|
|
|
22
60
|
# @see Arbitrary#shrink
|
|
@@ -16,6 +16,22 @@ module Pbt
|
|
|
16
16
|
@keys.zip(values).to_h
|
|
17
17
|
end
|
|
18
18
|
|
|
19
|
+
def contextual?
|
|
20
|
+
Sample.contextual?(@arb)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
def sample(rng = Random.new)
|
|
24
|
+
@arb.sample(rng).map { |values| @keys.zip(values).to_h }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# @see Arbitrary#cover?
|
|
28
|
+
def cover?(value)
|
|
29
|
+
return false unless value.is_a?(Hash) && value.size == @keys.size
|
|
30
|
+
return false unless @keys.all? { |k| value.key?(k) }
|
|
31
|
+
|
|
32
|
+
@arb.cover?(@keys.map { |k| value[k] })
|
|
33
|
+
end
|
|
34
|
+
|
|
19
35
|
# @see Arbitrary#shrink
|
|
20
36
|
def shrink(current)
|
|
21
37
|
# This is not the most comprehensive but allows a reasonable number of entries in the shrink
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Generates finite Float values via `x.to_f / (y.to_f.abs + 1.0)` over default integer bounds
|
|
6
|
+
# and shrinks them toward zero.
|
|
7
|
+
class FloatArbitrary < MapArbitrary
|
|
8
|
+
MAX_ABS = 1_000_000.0
|
|
9
|
+
MIN_NONZERO_ABS = 1.0 / 1_000_001.0
|
|
10
|
+
private_constant :MAX_ABS, :MIN_NONZERO_ABS
|
|
11
|
+
|
|
12
|
+
def initialize
|
|
13
|
+
super(
|
|
14
|
+
TupleArbitrary.new(IntegerArbitrary.new(-1_000_000, 1_000_000), IntegerArbitrary.new(-1_000_000, 1_000_000)),
|
|
15
|
+
FLOAT_MAPPER,
|
|
16
|
+
FLOAT_UNMAPPER
|
|
17
|
+
)
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
# @see Arbitrary#cover?
|
|
21
|
+
def cover?(value)
|
|
22
|
+
return false unless value.is_a?(Float) && value.finite?
|
|
23
|
+
return false if value.abs > MAX_ABS
|
|
24
|
+
return true if value == 0.0
|
|
25
|
+
return false if value.abs < MIN_NONZERO_ABS
|
|
26
|
+
|
|
27
|
+
nil
|
|
28
|
+
end
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
end
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Pbt
|
|
4
|
+
module Arbitrary
|
|
5
|
+
# Selects a generator using nonnegative integer weights.
|
|
6
|
+
class FrequencyArbitrary < Arbitrary
|
|
7
|
+
def initialize(choices)
|
|
8
|
+
unless choices.all? { |pair|
|
|
9
|
+
pair.is_a?(Array) && pair.size == 2 && pair[0].is_a?(Integer) && pair[0] >= 0 &&
|
|
10
|
+
pair[1].respond_to?(:generate) && pair[1].respond_to?(:shrink)
|
|
11
|
+
}
|
|
12
|
+
raise ArgumentError, "frequency expects [nonnegative Integer weight, arbitrary] pairs"
|
|
13
|
+
end
|
|
14
|
+
@choices = choices.select { |weight, _| weight.positive? }.map(&:dup)
|
|
15
|
+
@total = @choices.sum(&:first)
|
|
16
|
+
raise ArgumentError, "frequency requires at least one positive weight" if @total.zero?
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def generate(rng = Random.new)
|
|
20
|
+
select(rng).generate(rng)
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
# Preserve the selected branch for shrinking, even after subsequent generation.
|
|
24
|
+
def sample(rng = Random.new)
|
|
25
|
+
Sample.capture(select(rng), rng)
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
def contextual?
|
|
29
|
+
true
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# @see Arbitrary#cover?
|
|
33
|
+
def cover?(value)
|
|
34
|
+
unknown = false
|
|
35
|
+
@choices.each do |_weight, arb|
|
|
36
|
+
covered = Arbitrary.cover_result(arb, value)
|
|
37
|
+
return true if covered == true
|
|
38
|
+
|
|
39
|
+
unknown = true if covered.nil?
|
|
40
|
+
end
|
|
41
|
+
unknown ? nil : false
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
# A bare value cannot identify its branch. Use sample or Pbt.property to shrink.
|
|
45
|
+
def shrink(current)
|
|
46
|
+
[].each
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
private
|
|
50
|
+
|
|
51
|
+
def select(rng)
|
|
52
|
+
ticket = rng.rand(@total)
|
|
53
|
+
@choices.each do |weight, arbitrary|
|
|
54
|
+
return arbitrary if ticket < weight
|
|
55
|
+
|
|
56
|
+
ticket -= weight
|
|
57
|
+
end
|
|
58
|
+
end
|
|
59
|
+
end
|
|
60
|
+
end
|
|
61
|
+
end
|
|
@@ -10,6 +10,8 @@ module Pbt
|
|
|
10
10
|
# @param min [Integer] Minimum value to generate.
|
|
11
11
|
# @param max [Integer] Maximum value to generate.
|
|
12
12
|
def initialize(min, max)
|
|
13
|
+
raise ArgumentError, "min and max cannot be nil" if min.nil? || max.nil?
|
|
14
|
+
|
|
13
15
|
@min = min
|
|
14
16
|
@max = max
|
|
15
17
|
end
|
|
@@ -23,6 +25,15 @@ module Pbt
|
|
|
23
25
|
raise
|
|
24
26
|
end
|
|
25
27
|
|
|
28
|
+
# @see Arbitrary#cover?
|
|
29
|
+
def cover?(value)
|
|
30
|
+
return false unless value.is_a?(Integer)
|
|
31
|
+
return false if !@min.nil? && value < @min
|
|
32
|
+
return false if !@max.nil? && value > @max
|
|
33
|
+
|
|
34
|
+
true
|
|
35
|
+
end
|
|
36
|
+
|
|
26
37
|
# @see Arbitrary#shrink
|
|
27
38
|
def shrink(current, target: nil)
|
|
28
39
|
# If no target is specified, use the appropriate bound as target
|
|
@@ -6,11 +6,16 @@ module Pbt
|
|
|
6
6
|
class MapArbitrary < Arbitrary
|
|
7
7
|
# @param arb [Arbitrary] Arbitrary to generate values to be mapped.
|
|
8
8
|
# @param mapper [Proc] Proc to map generated values. Mainly used for generation.
|
|
9
|
-
# @param unmapper [Proc]
|
|
10
|
-
|
|
9
|
+
# @param unmapper [Proc, nil] Optional proc to unmap generated values for bare `#shrink`.
|
|
10
|
+
# @param bijective [Boolean] Internal flag indicating `mapper`/`unmapper` form a bijection on the codomain.
|
|
11
|
+
def initialize(arb, mapper, unmapper = nil, bijective: false)
|
|
12
|
+
raise ArgumentError, "mapper must be callable" unless mapper.respond_to?(:call)
|
|
13
|
+
raise ArgumentError, "unmapper must be callable or nil" if !unmapper.nil? && !unmapper.respond_to?(:call)
|
|
14
|
+
|
|
11
15
|
@arb = arb
|
|
12
16
|
@mapper = mapper
|
|
13
17
|
@unmapper = unmapper
|
|
18
|
+
@bijective = bijective
|
|
14
19
|
end
|
|
15
20
|
|
|
16
21
|
# @see Arbitrary#generate
|
|
@@ -18,8 +23,41 @@ module Pbt
|
|
|
18
23
|
@mapper.call(@arb.generate(rng))
|
|
19
24
|
end
|
|
20
25
|
|
|
26
|
+
# @private
|
|
27
|
+
def contextual?
|
|
28
|
+
@unmapper.nil? || Sample.contextual?(@arb)
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
# @see Arbitrary#sample
|
|
32
|
+
def sample(rng = Random.new)
|
|
33
|
+
Sample.capture(@arb, rng).map { |value| @mapper.call(value) }
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
# @see Arbitrary#cover?
|
|
37
|
+
def cover?(value)
|
|
38
|
+
return nil if @unmapper.nil?
|
|
39
|
+
|
|
40
|
+
unmapped, mapped = begin
|
|
41
|
+
u = @unmapper.call(value)
|
|
42
|
+
[u, @mapper.call(u)]
|
|
43
|
+
rescue
|
|
44
|
+
return nil
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
round_trip = mapped.equal?(value) || mapped.eql?(value)
|
|
48
|
+
return @bijective ? false : nil unless round_trip
|
|
49
|
+
|
|
50
|
+
inner = Arbitrary.cover_result(@arb, unmapped)
|
|
51
|
+
return true if inner == true
|
|
52
|
+
return false if @bijective && inner == false
|
|
53
|
+
|
|
54
|
+
nil
|
|
55
|
+
end
|
|
56
|
+
|
|
21
57
|
# @see Arbitrary#shrink
|
|
22
58
|
def shrink(current)
|
|
59
|
+
return [].each if @unmapper.nil?
|
|
60
|
+
|
|
23
61
|
Enumerator.new do |y|
|
|
24
62
|
@arb.shrink(@unmapper.call(current)).each do |v|
|
|
25
63
|
y.yield @mapper.call(v)
|