pbt 0.8.0 → 1.0.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/CHANGELOG.md +29 -0
- data/README.md +70 -109
- data/lib/pbt/arbitrary/arbitrary.rb +23 -0
- data/lib/pbt/arbitrary/arbitrary_methods.rb +46 -16
- data/lib/pbt/arbitrary/bind_arbitrary.rb +54 -0
- data/lib/pbt/arbitrary/choose_arbitrary.rb +82 -5
- data/lib/pbt/arbitrary/constant.rb +42 -12
- data/lib/pbt/arbitrary/constant_from_arbitrary.rb +3 -3
- data/lib/pbt/arbitrary/float_arbitrary.rb +239 -17
- data/lib/pbt/arbitrary/frequency_arbitrary.rb +1 -1
- data/lib/pbt/arbitrary/integer_arbitrary.rb +31 -14
- data/lib/pbt/arbitrary/map_arbitrary.rb +4 -1
- data/lib/pbt/arbitrary/sample.rb +35 -0
- data/lib/pbt/check/configuration.rb +1 -1
- data/lib/pbt/check/property.rb +5 -0
- data/lib/pbt/check/runner_iterator.rb +38 -3
- data/lib/pbt/check/runner_methods.rb +70 -13
- data/lib/pbt/reporter/run_details_reporter.rb +39 -6
- data/lib/pbt/stateful/command.rb +220 -0
- data/lib/pbt/stateful/errors.rb +58 -0
- data/lib/pbt/stateful/property.rb +276 -280
- data/lib/pbt/stateful/shrinker.rb +318 -0
- data/lib/pbt/stateful/spec.rb +299 -0
- data/lib/pbt/stateful/types.rb +308 -0
- data/lib/pbt/version.rb +1 -1
- data/lib/pbt.rb +34 -19
- data/sig/manifest.yaml +2 -0
- data/sig/pbt/arbitrary.rbs +150 -0
- data/sig/pbt/check.rbs +74 -0
- data/sig/pbt/reporter.rbs +44 -0
- data/sig/pbt/stateful.rbs +244 -0
- data/sig/pbt.rbs +40 -1
- metadata +18 -23
- data/.rspec +0 -3
- data/.standard.yml +0 -3
- data/CLAUDE.md +0 -104
- data/Rakefile +0 -70
- data/benchmark/FIBER.md +0 -97
- data/benchmark/README.md +0 -103
- data/benchmark/example.txt +0 -299
- data/benchmark/failure_simple.rb +0 -29
- data/benchmark/fiber_experiment.rb +0 -39
- data/benchmark/fiber_scheduler/Gemfile +0 -7
- data/benchmark/fiber_scheduler/experiment.rb +0 -131
- data/benchmark/fiber_scheduler/spec/experiment_spec.rb +0 -38
- data/benchmark/success_cpu_bound.rb +0 -35
- data/benchmark/success_io_bound.rb +0 -28
- data/benchmark/success_simple.rb +0 -24
- data/docs/plans/fiber-experiment.md +0 -12
- data/docs/plans/generator-statistics.md +0 -16
- data/docs/plans/stateful-pbt-redesign.md +0 -415
|
@@ -4,13 +4,26 @@ module Pbt
|
|
|
4
4
|
module Arbitrary
|
|
5
5
|
# Generates a random value from a range.
|
|
6
6
|
class ChooseArbitrary < Arbitrary
|
|
7
|
-
|
|
7
|
+
EDGE_CASE_DOMAIN_THRESHOLD = 20
|
|
8
|
+
EDGE_CASE_BIAS_DENOMINATOR = 5
|
|
9
|
+
private_constant :EDGE_CASE_DOMAIN_THRESHOLD, :EDGE_CASE_BIAS_DENOMINATOR
|
|
10
|
+
|
|
11
|
+
# @param range [Range<Integer>, Range<Float>]
|
|
8
12
|
def initialize(range)
|
|
9
13
|
@range = range
|
|
14
|
+
@edge_cases = build_edge_cases
|
|
10
15
|
end
|
|
11
16
|
|
|
12
17
|
# @see Arbitrary#generate
|
|
13
18
|
def generate(rng = Random.new)
|
|
19
|
+
if @edge_cases && rng.rand(EDGE_CASE_BIAS_DENOMINATOR).zero?
|
|
20
|
+
return @edge_cases[rng.rand(@edge_cases.size)]
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
if float_range? && @range.exclude_end?
|
|
24
|
+
return sample_exclusive_float(rng)
|
|
25
|
+
end
|
|
26
|
+
|
|
14
27
|
rng.rand(@range)
|
|
15
28
|
rescue ArgumentError => e
|
|
16
29
|
raise EmptyDomainError, e.message if empty_range?
|
|
@@ -24,7 +37,7 @@ module Pbt
|
|
|
24
37
|
|
|
25
38
|
if integer_range?
|
|
26
39
|
return false unless value.is_a?(Integer)
|
|
27
|
-
|
|
40
|
+
elsif float_range?
|
|
28
41
|
return false unless value.is_a?(Float) && value.finite?
|
|
29
42
|
end
|
|
30
43
|
|
|
@@ -33,9 +46,26 @@ module Pbt
|
|
|
33
46
|
|
|
34
47
|
# @see Arbitrary#shrink
|
|
35
48
|
def shrink(current)
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
49
|
+
return [].each if @range.begin.nil? || @range.end.nil? || empty_range?
|
|
50
|
+
|
|
51
|
+
min = @range.begin
|
|
52
|
+
max = @range.end
|
|
53
|
+
if integer_range?
|
|
54
|
+
effective_max = @range.exclude_end? ? (max - 1) : max
|
|
55
|
+
return [].each if min > effective_max
|
|
56
|
+
|
|
57
|
+
IntegerArbitrary.new(min, effective_max).shrink(current, target: min)
|
|
58
|
+
elsif float_range?
|
|
59
|
+
FloatArbitrary.shrink_float(
|
|
60
|
+
current,
|
|
61
|
+
min: min.to_f,
|
|
62
|
+
max: max.to_f,
|
|
63
|
+
target: min.to_f,
|
|
64
|
+
exclude_end: @range.exclude_end?
|
|
65
|
+
)
|
|
66
|
+
else
|
|
67
|
+
[].each
|
|
68
|
+
end
|
|
39
69
|
end
|
|
40
70
|
|
|
41
71
|
private
|
|
@@ -44,9 +74,56 @@ module Pbt
|
|
|
44
74
|
@range.begin.is_a?(Integer) && @range.end.is_a?(Integer)
|
|
45
75
|
end
|
|
46
76
|
|
|
77
|
+
def float_range?
|
|
78
|
+
@range.begin.is_a?(Float) || @range.end.is_a?(Float)
|
|
79
|
+
end
|
|
80
|
+
|
|
47
81
|
def empty_range?
|
|
48
82
|
!@range.begin.nil? && !@range.end.nil? && !@range.cover?(@range.begin)
|
|
49
83
|
end
|
|
84
|
+
|
|
85
|
+
def build_edge_cases
|
|
86
|
+
return nil if @range.begin.nil? || @range.end.nil? || empty_range?
|
|
87
|
+
|
|
88
|
+
if integer_range?
|
|
89
|
+
min = @range.begin
|
|
90
|
+
effective_max = @range.exclude_end? ? (@range.end - 1) : @range.end
|
|
91
|
+
return nil if min > effective_max || (effective_max - min + 1) <= EDGE_CASE_DOMAIN_THRESHOLD
|
|
92
|
+
|
|
93
|
+
[0, 1, -1, min, effective_max].uniq.select { |v| v.between?(min, effective_max) }.freeze
|
|
94
|
+
elsif float_range?
|
|
95
|
+
lo = @range.begin.to_f
|
|
96
|
+
hi = @range.end.to_f
|
|
97
|
+
return nil unless lo.finite? && hi.finite? && lo < hi
|
|
98
|
+
|
|
99
|
+
candidates = [0.0, 1.0, -1.0, lo, hi]
|
|
100
|
+
candidates << -0.0 if lo < 0.0 && hi >= 0.0
|
|
101
|
+
edges = candidates
|
|
102
|
+
.select { |v| @range.cover?(v) }
|
|
103
|
+
.uniq { |v| v.zero? ? (1.0 / v) : v }
|
|
104
|
+
edges.empty? ? nil : edges.freeze
|
|
105
|
+
end
|
|
106
|
+
end
|
|
107
|
+
|
|
108
|
+
def sample_exclusive_float(rng)
|
|
109
|
+
raise EmptyDomainError, "cannot generate from empty range #{@range.inspect}" if empty_range?
|
|
110
|
+
|
|
111
|
+
lo = @range.begin.to_f
|
|
112
|
+
hi = @range.end.to_f.prev_float
|
|
113
|
+
if !lo.finite? || !hi.finite? || lo > hi
|
|
114
|
+
raise EmptyDomainError, "cannot generate from range #{@range.inspect}"
|
|
115
|
+
end
|
|
116
|
+
|
|
117
|
+
return lo if lo == hi
|
|
118
|
+
|
|
119
|
+
span = hi - lo
|
|
120
|
+
if span.finite?
|
|
121
|
+
rng.rand(lo..hi)
|
|
122
|
+
else
|
|
123
|
+
mid = (lo / 2.0) + (hi / 2.0)
|
|
124
|
+
rng.rand(2).zero? ? rng.rand(lo..mid) : rng.rand(mid..hi)
|
|
125
|
+
end
|
|
126
|
+
end
|
|
50
127
|
end
|
|
51
128
|
end
|
|
52
129
|
end
|
|
@@ -5,25 +5,47 @@ require "date"
|
|
|
5
5
|
module Pbt
|
|
6
6
|
module Arbitrary
|
|
7
7
|
HEXA_CHARS = [*("0".."9"), *("a".."f")].freeze
|
|
8
|
-
CHAR_RANGE = (0..0x10FFFF)
|
|
9
8
|
SYMBOL_SAFE_CHARS = [*("a".."z"), "-"].freeze
|
|
10
9
|
ALPHANUMERIC_CHARS = [*("a".."z"), *("A".."Z"), *("0".."9")].freeze
|
|
11
10
|
PRINTABLE_ASCII_CHARS = [*(" ".."~")].freeze
|
|
12
|
-
ASCII_CHARS = [*PRINTABLE_ASCII_CHARS, "\n", "\r", "\t", "\v", "\b", "\f", "\e", "\
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
11
|
+
ASCII_CHARS = [*PRINTABLE_ASCII_CHARS, "\n", "\r", "\t", "\v", "\b", "\f", "\e", "\x7F", "\a"].freeze
|
|
12
|
+
|
|
13
|
+
# Valid Unicode scalar values exclude UTF-16 surrogates (0xD800..0xDFFF).
|
|
14
|
+
SURROGATE_START = 0xD800
|
|
15
|
+
SURROGATE_COUNT = 0x800
|
|
16
|
+
UNICODE_SCALAR_INDEX_RANGE = (0..(0x10FFFF - SURROGATE_COUNT))
|
|
17
|
+
|
|
18
|
+
# Printable Unicode codepoint segments (excludes C0/C1 controls, DEL, surrogates, and U+FFFE/U+FFFF).
|
|
19
|
+
PRINTABLE_ASCII_CODEPOINT_RANGE = (0x20..0x7E)
|
|
20
|
+
PRINTABLE_BMP_MAIN_CODEPOINT_RANGE = (0xA0..0xD7FF)
|
|
21
|
+
PRINTABLE_BMP_HIGH_CODEPOINT_RANGE = (0xE000..0xFFFD)
|
|
22
|
+
PRINTABLE_SUPPLEMENTARY_CODEPOINT_RANGE = (0x10000..0x10FFFF)
|
|
23
|
+
|
|
24
|
+
CHAR_MAPPER = ->(i) {
|
|
25
|
+
return nil unless i.is_a?(Integer) && UNICODE_SCALAR_INDEX_RANGE.cover?(i)
|
|
26
|
+
|
|
27
|
+
[(i < SURROGATE_START) ? i : (i + SURROGATE_COUNT)].pack("U")
|
|
28
|
+
}
|
|
29
|
+
CHAR_UNMAPPER = ->(v) {
|
|
30
|
+
return -1 unless v.is_a?(String) && v.valid_encoding? && v.length == 1
|
|
31
|
+
|
|
32
|
+
cp = v.ord
|
|
33
|
+
(cp < SURROGATE_START) ? cp : (cp - SURROGATE_COUNT)
|
|
34
|
+
}
|
|
35
|
+
CODEPOINT_MAPPER = ->(cp) {
|
|
36
|
+
return nil unless cp.is_a?(Integer) && cp >= 0 && cp <= 0x10FFFF && !(cp >= SURROGATE_START && cp < SURROGATE_START + SURROGATE_COUNT)
|
|
37
|
+
|
|
38
|
+
[cp].pack("U")
|
|
39
|
+
}
|
|
40
|
+
CODEPOINT_UNMAPPER = ->(v) {
|
|
41
|
+
return -1 unless v.is_a?(String) && v.valid_encoding? && v.length == 1
|
|
42
|
+
|
|
43
|
+
v.ord
|
|
44
|
+
}
|
|
21
45
|
STRING_MAPPER = ->(v) { v.join }
|
|
22
46
|
STRING_UNMAPPER = ->(v) { v.chars }
|
|
23
47
|
SYMBOL_MAPPER = ->(v) { v.join.to_sym }
|
|
24
48
|
SYMBOL_UNMAPPER = ->(v) { v.to_s.chars }
|
|
25
|
-
FLOAT_MAPPER = ->((x, y)) { x.to_f / (y.to_f.abs + 1.0) }
|
|
26
|
-
FLOAT_UNMAPPER = ->(v) { [v, 0] }
|
|
27
49
|
SET_MAPPER = ->(v) { v.to_set }
|
|
28
50
|
SET_UNMAPPER = ->(v) { v.to_a }
|
|
29
51
|
HASH_MAPPER = ->(v) { v.to_h }
|
|
@@ -32,5 +54,13 @@ module Pbt
|
|
|
32
54
|
DATE_UNMAPPER = ->(epoch) { ->(v) { (v - epoch).to_i } }
|
|
33
55
|
TIME_MAPPER = DATE_MAPPER
|
|
34
56
|
TIME_UNMAPPER = DATE_UNMAPPER
|
|
57
|
+
|
|
58
|
+
private_constant :SURROGATE_START, :SURROGATE_COUNT, :UNICODE_SCALAR_INDEX_RANGE,
|
|
59
|
+
:PRINTABLE_ASCII_CODEPOINT_RANGE, :PRINTABLE_BMP_MAIN_CODEPOINT_RANGE,
|
|
60
|
+
:PRINTABLE_BMP_HIGH_CODEPOINT_RANGE, :PRINTABLE_SUPPLEMENTARY_CODEPOINT_RANGE,
|
|
61
|
+
:CHAR_MAPPER, :CHAR_UNMAPPER, :CODEPOINT_MAPPER, :CODEPOINT_UNMAPPER,
|
|
62
|
+
:STRING_MAPPER, :STRING_UNMAPPER, :SYMBOL_MAPPER, :SYMBOL_UNMAPPER,
|
|
63
|
+
:SET_MAPPER, :SET_UNMAPPER, :HASH_MAPPER, :HASH_UNMAPPER,
|
|
64
|
+
:DATE_MAPPER, :DATE_UNMAPPER, :TIME_MAPPER, :TIME_UNMAPPER
|
|
35
65
|
end
|
|
36
66
|
end
|
|
@@ -9,12 +9,11 @@ module Pbt
|
|
|
9
9
|
raise EmptyDomainError, "constant_from requires at least one choice" if choices.empty?
|
|
10
10
|
|
|
11
11
|
@choices = choices.dup
|
|
12
|
-
@idx_arb = IntegerArbitrary.new(0, @choices.size - 1)
|
|
13
12
|
end
|
|
14
13
|
|
|
15
14
|
# @see Arbitrary#generate
|
|
16
15
|
def generate(rng = Random.new)
|
|
17
|
-
@choices[@
|
|
16
|
+
@choices[rng.rand(@choices.size)]
|
|
18
17
|
end
|
|
19
18
|
|
|
20
19
|
# @see Arbitrary#cover?
|
|
@@ -28,8 +27,9 @@ module Pbt
|
|
|
28
27
|
return Enumerator.new {} if index.nil?
|
|
29
28
|
|
|
30
29
|
# Shrinks to values earlier in the list of `choices`.
|
|
30
|
+
idx_arb = IntegerArbitrary.new(0, @choices.size - 1)
|
|
31
31
|
Enumerator.new do |y|
|
|
32
|
-
|
|
32
|
+
idx_arb.shrink(index).each do |idx|
|
|
33
33
|
y << @choices[idx]
|
|
34
34
|
end
|
|
35
35
|
end
|
|
@@ -2,29 +2,251 @@
|
|
|
2
2
|
|
|
3
3
|
module Pbt
|
|
4
4
|
module Arbitrary
|
|
5
|
-
# Generates
|
|
6
|
-
# and
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
5
|
+
# Generates Float values within optional finite `Integer` / `Float` bounds, with opt-in
|
|
6
|
+
# NaN / Infinity support and float-aware binary-search shrinking toward zero (or the nearest bound).
|
|
7
|
+
#
|
|
8
|
+
# Omitted bounds default finite generation to `-1_000_000.0..1_000_000.0` (or a scaled span for
|
|
9
|
+
# large one-sided bounds), while `cover?` only enforces explicitly specified bounds.
|
|
10
|
+
class FloatArbitrary < Arbitrary
|
|
11
|
+
DEFAULT_MIN = -1_000_000.0
|
|
12
|
+
DEFAULT_MAX = 1_000_000.0
|
|
13
|
+
DEFAULT_SPAN = 1_000_000.0
|
|
14
|
+
EDGE_CASE_BIAS_DENOMINATOR = 5
|
|
15
|
+
MAX_SHRINK_STEPS = 16
|
|
16
|
+
private_constant :DEFAULT_MIN, :DEFAULT_MAX, :DEFAULT_SPAN, :EDGE_CASE_BIAS_DENOMINATOR, :MAX_SHRINK_STEPS
|
|
11
17
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
)
|
|
18
|
+
# @param min [Integer, Float, nil] Inclusive finite lower bound.
|
|
19
|
+
# @param max [Integer, Float, nil] Inclusive finite upper bound.
|
|
20
|
+
# @param allow_nan [Boolean] Whether `Float::NAN` may be generated. Default is `false`.
|
|
21
|
+
# @param allow_infinity [Boolean] Whether `+/-Float::INFINITY` may be generated when the corresponding bound is unbounded. Default is `false`.
|
|
22
|
+
def initialize(min: nil, max: nil, allow_nan: false, allow_infinity: false)
|
|
23
|
+
@raw_min = validate_bound!(:min, min)
|
|
24
|
+
@raw_max = validate_bound!(:max, max)
|
|
25
|
+
@min = round_inward_min(@raw_min)
|
|
26
|
+
@max = round_inward_max(@raw_max)
|
|
27
|
+
@allow_nan = !!allow_nan
|
|
28
|
+
@allow_infinity = !!allow_infinity
|
|
29
|
+
@empty = empty_domain?
|
|
30
|
+
@generation_bounds = @empty ? nil : compute_generation_bounds
|
|
31
|
+
@special_values = compute_special_values
|
|
32
|
+
@edge_cases = @empty ? [].freeze : compute_finite_edge_cases
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# @see Arbitrary#generate
|
|
36
|
+
def generate(rng = Random.new)
|
|
37
|
+
if @empty
|
|
38
|
+
raise EmptyDomainError, "no representable Float in range #{@raw_min.inspect}..#{@raw_max.inspect}"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
lo, hi = @generation_bounds
|
|
42
|
+
|
|
43
|
+
if !@special_values.empty? && rng.rand(20).zero?
|
|
44
|
+
return @special_values[rng.rand(@special_values.size)]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
if !@edge_cases.empty? && rng.rand(EDGE_CASE_BIAS_DENOMINATOR).zero?
|
|
48
|
+
return @edge_cases[rng.rand(@edge_cases.size)]
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
if @min.nil? && @max.nil? && rng.rand(2).zero?
|
|
52
|
+
x = rng.rand(-1_000_000..1_000_000)
|
|
53
|
+
y = rng.rand(-1_000_000..1_000_000)
|
|
54
|
+
return x.to_f / (y.to_f.abs + 1.0)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
sample_finite(lo, hi, rng)
|
|
18
58
|
end
|
|
19
59
|
|
|
20
60
|
# @see Arbitrary#cover?
|
|
21
61
|
def cover?(value)
|
|
22
|
-
return false unless value.is_a?(Float)
|
|
23
|
-
return false if
|
|
24
|
-
return
|
|
25
|
-
|
|
62
|
+
return false unless value.is_a?(Float)
|
|
63
|
+
return false if @empty
|
|
64
|
+
return @allow_nan if value.nan?
|
|
65
|
+
|
|
66
|
+
if value.infinite?
|
|
67
|
+
return false unless @allow_infinity
|
|
68
|
+
|
|
69
|
+
return value.positive? ? @max.nil? : @min.nil?
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
return false if @min && value < @min
|
|
73
|
+
return false if @max && value > @max
|
|
74
|
+
|
|
75
|
+
true
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
# @see Arbitrary#shrink
|
|
79
|
+
def shrink(current, target: nil)
|
|
80
|
+
return [].each if @empty
|
|
81
|
+
|
|
82
|
+
lo, hi = shrink_bounds(current)
|
|
83
|
+
self.class.shrink_float(current, min: lo, max: hi, target: target)
|
|
84
|
+
end
|
|
85
|
+
|
|
86
|
+
# Float-aware binary-search shrink helper shared with `ChooseArbitrary`.
|
|
87
|
+
# @api private
|
|
88
|
+
#
|
|
89
|
+
# @param current [Object]
|
|
90
|
+
# @param min [Float]
|
|
91
|
+
# @param max [Float]
|
|
92
|
+
# @param target [Integer, Float, nil]
|
|
93
|
+
# @param exclude_end [Boolean]
|
|
94
|
+
# @return [Enumerator<Float>]
|
|
95
|
+
def self.shrink_float(current, min:, max:, target: nil, exclude_end: false)
|
|
96
|
+
return [].each unless current.is_a?(Float)
|
|
97
|
+
return [].each if min > max || (exclude_end && min >= max)
|
|
98
|
+
|
|
99
|
+
t = (target.nil? ? 0.0 : target.to_f).clamp(min, max)
|
|
100
|
+
t = min if exclude_end && t >= max
|
|
101
|
+
|
|
102
|
+
if current.nan?
|
|
103
|
+
return Enumerator.new { |y| y.yield t }
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
if current.infinite?
|
|
107
|
+
return Enumerator.new do |y|
|
|
108
|
+
y.yield t
|
|
109
|
+
finite_edge = current.positive? ? max : min
|
|
110
|
+
y.yield finite_edge if finite_edge != t && !(exclude_end && finite_edge >= max)
|
|
111
|
+
end
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
return [].each if current == t
|
|
115
|
+
|
|
116
|
+
Enumerator.new do |y|
|
|
117
|
+
seen = {current => true}
|
|
118
|
+
|
|
119
|
+
emit = lambda do |val|
|
|
120
|
+
clamped = val.clamp(min, max)
|
|
121
|
+
return if exclude_end && clamped >= max
|
|
122
|
+
return if seen.key?(clamped)
|
|
123
|
+
|
|
124
|
+
seen[clamped] = true
|
|
125
|
+
y.yield clamped
|
|
126
|
+
end
|
|
127
|
+
|
|
128
|
+
emit.call(t)
|
|
129
|
+
|
|
130
|
+
truncated = current.truncate.to_f
|
|
131
|
+
if truncated != current && truncated != t &&
|
|
132
|
+
((t < current && truncated > t && truncated < current) ||
|
|
133
|
+
(t > current && truncated < t && truncated > current))
|
|
134
|
+
emit.call(truncated)
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
step = ((current / 2.0) - (t / 2.0)).abs
|
|
138
|
+
positive = current > t
|
|
139
|
+
|
|
140
|
+
MAX_SHRINK_STEPS.times do
|
|
141
|
+
break unless step.finite? && step > 0.0
|
|
142
|
+
|
|
143
|
+
candidate = positive ? (current - step) : (current + step)
|
|
144
|
+
break if candidate == current
|
|
145
|
+
|
|
146
|
+
emit.call(candidate)
|
|
147
|
+
step /= 2.0
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
private
|
|
153
|
+
|
|
154
|
+
def validate_bound!(name, value)
|
|
155
|
+
return nil if value.nil?
|
|
156
|
+
|
|
157
|
+
valid_type = value.is_a?(Integer) || value.is_a?(Float)
|
|
158
|
+
unless valid_type && value.to_f.finite?
|
|
159
|
+
raise ArgumentError, "#{name} must be a finite Integer, Float, or nil, got #{value.inspect}"
|
|
160
|
+
end
|
|
161
|
+
|
|
162
|
+
value
|
|
163
|
+
end
|
|
164
|
+
|
|
165
|
+
def round_inward_min(value)
|
|
166
|
+
return nil if value.nil?
|
|
167
|
+
|
|
168
|
+
f = value.to_f
|
|
169
|
+
f = f.next_float if value.is_a?(Integer) && f < value
|
|
170
|
+
f
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
def round_inward_max(value)
|
|
174
|
+
return nil if value.nil?
|
|
175
|
+
|
|
176
|
+
f = value.to_f
|
|
177
|
+
f = f.prev_float if value.is_a?(Integer) && f > value
|
|
178
|
+
f
|
|
179
|
+
end
|
|
180
|
+
|
|
181
|
+
def empty_domain?
|
|
182
|
+
return true if @raw_min && @raw_max && @raw_min > @raw_max
|
|
183
|
+
return true if @min && !@min.finite?
|
|
184
|
+
return true if @max && !@max.finite?
|
|
185
|
+
return true if @min && @max && @min > @max
|
|
186
|
+
|
|
187
|
+
false
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def compute_generation_bounds
|
|
191
|
+
if @min && @max
|
|
192
|
+
[@min, @max].freeze
|
|
193
|
+
elsif @min
|
|
194
|
+
span = [DEFAULT_SPAN, @min.abs].max
|
|
195
|
+
hi = [@min + span, DEFAULT_MAX].max
|
|
196
|
+
[@min, hi.finite? ? hi : Float::MAX].freeze
|
|
197
|
+
elsif @max
|
|
198
|
+
span = [DEFAULT_SPAN, @max.abs].max
|
|
199
|
+
lo = [@max - span, DEFAULT_MIN].min
|
|
200
|
+
[lo.finite? ? lo : -Float::MAX, @max].freeze
|
|
201
|
+
else
|
|
202
|
+
[DEFAULT_MIN, DEFAULT_MAX].freeze
|
|
203
|
+
end
|
|
204
|
+
end
|
|
205
|
+
|
|
206
|
+
def shrink_bounds(current)
|
|
207
|
+
lo, hi = @generation_bounds
|
|
208
|
+
if current.is_a?(Float) && current.finite?
|
|
209
|
+
lo = [lo, current].min if @min.nil?
|
|
210
|
+
hi = [hi, current].max if @max.nil?
|
|
211
|
+
end
|
|
212
|
+
[lo, hi]
|
|
213
|
+
end
|
|
214
|
+
|
|
215
|
+
def compute_special_values
|
|
216
|
+
values = []
|
|
217
|
+
values << Float::NAN if @allow_nan
|
|
218
|
+
if @allow_infinity
|
|
219
|
+
values << Float::INFINITY if @max.nil?
|
|
220
|
+
values << -Float::INFINITY if @min.nil?
|
|
221
|
+
end
|
|
222
|
+
values.freeze
|
|
223
|
+
end
|
|
224
|
+
|
|
225
|
+
def compute_finite_edge_cases
|
|
226
|
+
lo, hi = @generation_bounds
|
|
227
|
+
return [].freeze if lo >= hi
|
|
228
|
+
|
|
229
|
+
candidates = [0.0, 1.0, -1.0]
|
|
230
|
+
candidates << -0.0 if lo < 0.0 && hi >= 0.0
|
|
231
|
+
candidates << @min if @min
|
|
232
|
+
candidates << @max if @max
|
|
233
|
+
candidates
|
|
234
|
+
.select { |v| v.between?(lo, hi) }
|
|
235
|
+
.uniq { |v| v.zero? ? (1.0 / v) : v }
|
|
236
|
+
.freeze
|
|
237
|
+
end
|
|
238
|
+
|
|
239
|
+
def sample_finite(lo, hi, rng)
|
|
240
|
+
return lo if lo == hi
|
|
26
241
|
|
|
27
|
-
|
|
242
|
+
span = hi - lo
|
|
243
|
+
if span.finite?
|
|
244
|
+
rng.rand(lo..hi)
|
|
245
|
+
else
|
|
246
|
+
# Avoid Float overflow when hi - lo exceeds Float::MAX.
|
|
247
|
+
mid = (lo / 2.0) + (hi / 2.0)
|
|
248
|
+
rng.rand(2).zero? ? rng.rand(lo..mid) : rng.rand(mid..hi)
|
|
249
|
+
end
|
|
28
250
|
end
|
|
29
251
|
end
|
|
30
252
|
end
|
|
@@ -13,7 +13,7 @@ module Pbt
|
|
|
13
13
|
end
|
|
14
14
|
@choices = choices.select { |weight, _| weight.positive? }.map(&:dup)
|
|
15
15
|
@total = @choices.sum(&:first)
|
|
16
|
-
raise
|
|
16
|
+
raise EmptyDomainError, "frequency requires at least one positive weight" if @total.zero?
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def generate(rng = Random.new)
|
|
@@ -5,7 +5,9 @@ module Pbt
|
|
|
5
5
|
# Generates random integers between `min` and `max`.
|
|
6
6
|
class IntegerArbitrary < Arbitrary
|
|
7
7
|
DEFAULT_TARGET = 0
|
|
8
|
-
|
|
8
|
+
EDGE_CASE_DOMAIN_THRESHOLD = 20
|
|
9
|
+
EDGE_CASE_BIAS_DENOMINATOR = 5
|
|
10
|
+
private_constant :DEFAULT_TARGET, :EDGE_CASE_DOMAIN_THRESHOLD, :EDGE_CASE_BIAS_DENOMINATOR
|
|
9
11
|
|
|
10
12
|
# @param min [Integer] Minimum value to generate.
|
|
11
13
|
# @param max [Integer] Maximum value to generate.
|
|
@@ -14,10 +16,15 @@ module Pbt
|
|
|
14
16
|
|
|
15
17
|
@min = min
|
|
16
18
|
@max = max
|
|
19
|
+
@edge_cases = build_edge_cases
|
|
17
20
|
end
|
|
18
21
|
|
|
19
22
|
# @see Arbitrary#generate
|
|
20
23
|
def generate(rng = Random.new)
|
|
24
|
+
if @edge_cases && rng.rand(EDGE_CASE_BIAS_DENOMINATOR).zero?
|
|
25
|
+
return @edge_cases[rng.rand(@edge_cases.size)]
|
|
26
|
+
end
|
|
27
|
+
|
|
21
28
|
rng.rand(@min..@max)
|
|
22
29
|
rescue ArgumentError => e
|
|
23
30
|
raise EmptyDomainError, e.message if @min > @max
|
|
@@ -36,10 +43,7 @@ module Pbt
|
|
|
36
43
|
|
|
37
44
|
# @see Arbitrary#shrink
|
|
38
45
|
def shrink(current, target: nil)
|
|
39
|
-
# If no target is specified, use the appropriate bound as target
|
|
40
46
|
target ||= DEFAULT_TARGET.clamp(@min, @max)
|
|
41
|
-
|
|
42
|
-
# Ensure target is within bounds
|
|
43
47
|
target = target.clamp(@min, @max)
|
|
44
48
|
|
|
45
49
|
gap = current - target
|
|
@@ -48,19 +52,32 @@ module Pbt
|
|
|
48
52
|
is_positive_gap = gap > 0
|
|
49
53
|
|
|
50
54
|
Enumerator.new do |y|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
55
|
+
last_yielded = target
|
|
56
|
+
y.yield target
|
|
57
|
+
|
|
58
|
+
step = gap.abs
|
|
59
|
+
while step > 1
|
|
60
|
+
step = step.is_a?(Integer) ? ((step + 1) / 2) : (step / 2.0)
|
|
61
|
+
break if step < 1
|
|
62
|
+
|
|
63
|
+
candidate = (is_positive_gap ? (current - step) : (current + step)).clamp(@min, @max)
|
|
64
|
+
if candidate != current && candidate != last_yielded
|
|
65
|
+
y.yield candidate
|
|
66
|
+
last_yielded = candidate
|
|
67
|
+
end
|
|
61
68
|
end
|
|
62
69
|
end
|
|
63
70
|
end
|
|
71
|
+
|
|
72
|
+
private
|
|
73
|
+
|
|
74
|
+
def build_edge_cases
|
|
75
|
+
return nil unless @min.is_a?(Integer) && @max.is_a?(Integer)
|
|
76
|
+
return nil if @min > @max
|
|
77
|
+
return nil if (@max - @min + 1) <= EDGE_CASE_DOMAIN_THRESHOLD
|
|
78
|
+
|
|
79
|
+
[0, 1, -1, @min, @max].uniq.select { |v| v.between?(@min, @max) }.freeze
|
|
80
|
+
end
|
|
64
81
|
end
|
|
65
82
|
end
|
|
66
83
|
end
|
data/lib/pbt/arbitrary/sample.rb
CHANGED
|
@@ -46,6 +46,41 @@ module Pbt
|
|
|
46
46
|
arbitrary.respond_to?(:contextual?) && arbitrary.contextual?
|
|
47
47
|
end
|
|
48
48
|
|
|
49
|
+
def self.validate_bound_arbitrary!(arb)
|
|
50
|
+
return arb if arb.is_a?(Arbitrary) || (arb.respond_to?(:generate) && arb.respond_to?(:shrink))
|
|
51
|
+
|
|
52
|
+
raise ArgumentError, "bind block must return an arbitrary responding to generate and shrink, got #{arb.class}"
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def self.bind(outer_sample, inner_seed, &binder)
|
|
56
|
+
inner_arb = validate_bound_arbitrary!(binder.call(outer_sample.value))
|
|
57
|
+
inner_sample = capture(inner_arb, Random.new(inner_seed))
|
|
58
|
+
bind_samples(outer_sample, inner_sample, inner_seed, binder)
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def self.bind_samples(outer_sample, inner_sample, inner_seed, binder)
|
|
62
|
+
Sample.new(inner_sample.value) do
|
|
63
|
+
Enumerator.new do |y|
|
|
64
|
+
outer_sample.shrink.each do |shrunk_outer|
|
|
65
|
+
shrunk_inner =
|
|
66
|
+
begin
|
|
67
|
+
shrunk_inner_arb = validate_bound_arbitrary!(binder.call(shrunk_outer.value))
|
|
68
|
+
capture(shrunk_inner_arb, Random.new(inner_seed))
|
|
69
|
+
rescue EmptyDomainError, FilterExhaustedError
|
|
70
|
+
next
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
y << bind_samples(shrunk_outer, shrunk_inner, inner_seed, binder)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
inner_sample.shrink.each do |shrunk_inner|
|
|
77
|
+
y << bind_samples(outer_sample, shrunk_inner, inner_seed, binder)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
private_class_method :bind_samples
|
|
83
|
+
|
|
49
84
|
def self.tuple(samples)
|
|
50
85
|
Sample.new(samples.map(&:value)) do
|
|
51
86
|
Enumerator.new do |y|
|
|
@@ -14,7 +14,7 @@ module Pbt
|
|
|
14
14
|
# @param verbose [Boolean] Whether to print verbose output. Default is `false`.
|
|
15
15
|
# @param worker [Symbol] The concurrency method to use. `:ractor` and `:none` are supported. Default is `:none`.
|
|
16
16
|
# @param num_runs [Integer] The number of runs to perform. Default is `100`.
|
|
17
|
-
# @param seed [Integer] The seed to use for random number generation.
|
|
17
|
+
# @param seed [Integer] The seed to use for random number generation. Initialized once per process by default; override via `Pbt.assert(seed: ...)` / `Pbt.check(seed: ...)` to vary or reproduce a specific failure.
|
|
18
18
|
# @param thread_report_on_exception [Boolean] Whether to report exceptions in threads. It's useful to suppress error logs on Ractor that reports many errors. Default is `false`.
|
|
19
19
|
# @param max_skips [Integer, nil] Maximum number of skipped cases allowed via `Pbt.assume`. Defaults to `num_runs * 10` when `nil`.
|
|
20
20
|
def initialize(
|
data/lib/pbt/check/property.rb
CHANGED
|
@@ -10,6 +10,11 @@ module Pbt
|
|
|
10
10
|
# @param splat_args [Boolean] Whether generated tuple values should be splatted as positional arguments.
|
|
11
11
|
# @param predicate [Proc] Predicate proc to test the generated values. Library users write this.
|
|
12
12
|
def initialize(arb, keyword_args: false, splat_args: false, &predicate)
|
|
13
|
+
unless arb.respond_to?(:generate) && arb.respond_to?(:shrink)
|
|
14
|
+
raise ArgumentError, "Property arbitrary must respond to generate and shrink, got #{arb.class}"
|
|
15
|
+
end
|
|
16
|
+
raise ArgumentError, "Property requires a predicate block" unless predicate
|
|
17
|
+
|
|
13
18
|
@arb = arb
|
|
14
19
|
@keyword_args = keyword_args
|
|
15
20
|
@splat_args = splat_args
|