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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
require "test_helper"

module Minitest::Thesis
  class ThesisTest < Minitest::Thesis::Test
    class Failure < StandardError; end

    def test_finds_small_list
      (0...10).each do |seed|
        out, _ = capture_io do
          assert_raises(Minitest::Assertion) do
            run_test("finds_small_list", database: {}, random: Random.new(seed)) do |test_case|
              ls = test_case.any(lists(integers(0, 10_000)))
              assert ls.sum <= 1_000
            end
          end
        end

        assert_equal <<~OUT, out
          any(lists(integers(0, 10000))): [1001]
        OUT
      end
    end

    # Minithesis can't really handle shrinking arbitrary monadic bind, but length
    # parameters are a common case of monadic bind that it has a little bit of
    # special casing for. This test ensures that that special casing works.
    #
    # The problem is that if you generate a list by drawing a length and then
    # drawing that many elements, you can end up with something like ``[1001, 0,
    # 0]`` then deleting those zeroes in the middle is a pain. minithesis will
    # solve this by first sorting those elements, so that we have ``[0, 0,
    # 1001]``, and then lowering the length by two, turning it into ``[1001]`` as
    # desired.
    def test_finds_small_list_even_with_bad_lists
      bad_list = Possibility.new("bad_list") {|tc|
        n = tc.choice(10)
        Array.new(n) { tc.choice(10_000) }
      }

      (0...10).each do |seed|
        out, _ = capture_io do
          assert_raises(Minitest::Assertion) do
            run_test("finds_small_list_even_with_bad_lists", database: {}, random: Random.new(seed)) do |test_case|
              ls = test_case.any(bad_list)
              assert ls.sum <= 1_000
            end
          end
        end

        assert_equal <<~OUT, out
          any(bad_list): [1001]
        OUT
      end
    end

    def test_reduces_additive_pairs
      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("reduces_additive_pairs", database: {}, max_examples: 10_000) do |test_case|
            m = test_case.choice(1000)
            n = test_case.choice(1000)
            assert m + n <= 1000
          end
        end
      end

      assert_equal <<~OUT, out
        choice(1000): 1
        choice(1000): 1000
      OUT
    end

    def test_reuses_results_from_the_database
      Dir.mktmpdir do |tmpdir|
        db = DirectoryDb.new(tmpdir)
        count = 0

        run = -> {
          assert_raises(Minitest::Assertion) do
            run_test("reuses_results_from_the_database", database: db, quiet: true) do |test_case|
              count += 1
              assert test_case.choice(10_000) < 10
            end
          end
        }

        run.()

        assert_equal 1, Dir.children(tmpdir).length
        prev_count = count

        run.()

        assert_equal 1, Dir.children(tmpdir).length
        assert_equal prev_count + 2, count
      end
    end

    def test_test_cases_satisfy_preconditions
      run_test("test_cases_satisfy_preconditions", database: {}) do |test_case|
        n = test_case.choice(10)
        test_case.assume(n != 0)
        refute_equal 0, n
      end
    end

    def test_error_on_too_strict_precondition
      assert_raises(Unsatisfiable) do
        run_test("error_on_too_strict_precondition", database: {}) do |test_case|
          test_case.choice(10)
          test_case.reject
        end
      end
    end

    def test_error_on_unbounded_test_function
      orig_buffer_size = BUFFER_SIZE
      suppress_warnings do
        Minitest::Thesis.const_set(:BUFFER_SIZE, 10)
      end

      assert_raises(Unsatisfiable) do
        run_test("error_on_unbounded_test_function", database: {}, max_examples: 5) do |test_case|
          loop do
            test_case.choice(10)
          end
        end
      end
    ensure
      suppress_warnings do
        Minitest::Thesis.const_set(:BUFFER_SIZE, orig_buffer_size)
      end
    end

    def test_function_cache
      tf = ->(tc) do
        tc.mark_status(Status::INTERESTING) if tc.choice(1_000) >= 200
        tc.reject if tc.choice(1).zero?
      end

      state = TestingState.new(random: Random.new(0), test_function: tf, max_examples: 100)
      cache = CachedTestFunction.new {|tc| state.test_function(tc) }

      assert_equal Status::VALID, cache.([1, 1])
      assert_equal Status::OVERRUN, cache.([1])
      assert_equal Status::INTERESTING, cache.([1_000])
      assert_equal Status::INTERESTING, cache.([1_000])
      assert_equal Status::INTERESTING, cache.([1_000, 1])

      assert_equal 2, state.calls
    end

    # Targeting has a number of places it checks for whether we've exceeded the
    # generation limits. This makes sure we've checked them all.
    def test_max_examples_is_not_exceeded
      (1...100).each do |max_examples|
        calls = 0

        run_test(
          "max_examples_is_not_exceeded",
          database: {},
          random: Random.new(0),
          max_examples:,
        ) do |tc|
          m = 10000
          n = tc.choice(m)
          calls += 1
          tc.target(n * (m - n))
        end

        assert_equal max_examples, calls
      end
    end


    # Targeting has a number of places it checks for whether we've exceeded the
    # generation limits. This makes sure we've checked them all.
    def test_finds_a_local_maximum
      (0...100).each do |seed|
        assert_raises(Minitest::Assertion) do
          run_test(
            "finds_a_local_maximum",
            database: {},
            random: Random.new(seed),
            max_examples: 200,
            quiet: true
          ) do |tc|
            m = tc.choice(1000)
            n = tc.choice(1000)
            score = -((m - 500) ** 2 + (n - 500) ** 2)
            tc.target(score)
            assert m != 500 || n != 500
          end
        end
      end
    end

    def test_can_target_a_score_upwards_to_interesting
      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("can_target_a_score_upwards_to_interesting", database: {}, max_examples: 1000) do |test_case|
            n = test_case.choice(1000)
            m = test_case.choice(1000)
            score = n + m
            test_case.target(score)
            assert score < 2000
          end
        end
      end

      assert_equal <<~OUT, out
        choice(1000): 1000
        choice(1000): 1000
      OUT
    end

    def test_can_target_a_score_upwards_without_failing
      max_score = 0

      run_test("can_target_a_score_upwards_without_failing", database: {}, max_examples: 1000) do |test_case|
        n = test_case.choice(1000)
        m = test_case.choice(1000)
        score = n + m
        test_case.target(score)
        max_score = [score, max_score].max
      end

      assert_equal 2000, max_score
    end

    def test_targeting_when_most_do_not_benefit
      big = 10_000

      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("targeting_when_most_do_not_benefit", database: {}, max_examples: 1000) do |test_case|
            test_case.choice(1000)
            test_case.choice(1000)
            score = test_case.choice(big)
            test_case.target(score)
            assert score < big
          end
        end
      end

      assert_equal <<~OUT, out
        choice(1000): 0
        choice(1000): 0
        choice(#{big}): #{big}
      OUT
    end

    def test_can_target_a_score_downwards
      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("can_target_a_score_downwards", database: {}, max_examples: 1000) do |test_case|
            n = test_case.choice(1000)
            m = test_case.choice(1000)
            score = n + m
            test_case.target(-score)
            assert score.positive?
          end
        end
      end

      assert_equal <<~OUT, out
        choice(1000): 0
        choice(1000): 0
      OUT
    end

    def test_prints_a_top_level_weighted
      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("prints_a_top_level_weighted", database: {}, max_examples: 1000) do |test_case|
            assert test_case.weighted(0.5).nonzero?
          end
        end
      end

      assert_equal <<~OUT, out
        weighted(0.5): 0
      OUT
    end

    def test_errors_when_using_frozen
      tc = TestCase.for_choices([0])
      tc.status = Status::VALID

      assert_raises(Frozen) do
        tc.mark_status(Status::INTERESTING)
      end

      assert_raises(Frozen) do
        tc.choice(10)
      end

      assert_raises(Frozen) do
        tc.forced_choice(10)
      end
    end

    def test_errors_on_too_large_choice
      tc = TestCase.for_choices([0])
      assert_raises(RangeError) do
        tc.choice(2 ** 64)
      end
    end

    def test_can_choose_full_64_bits
      run_test("can_choose_full_64_bits", database: {}) do |tc|
        tc.choice(2 ** 64 - 1)
      end
    end

    def test_mapped_possibility
      run_test("mapped_possibility", database: {}) do |tc|
        n = tc.any(integers(0, 5).map {|n| n * 2 })
        assert n.even?
      end
    end

    def test_selected_possibility
      run_test("selected_possibility", database: {}) do |tc|
        n = tc.any(
          integers(0, 5)
            .satisfying(&:even?)
        )
        assert n.even?
      end
    end

    def test_bound_possibility
      run_test("bound_possibility", database: {}) do |tc|
        m, n = tc.any(
          integers(0, 5).bind {|m| tuples(just(m), integers(m, m + 10)) }
        )
        assert (m..m+10).cover?(n)
      end
    end

    def test_cannot_witness_nothing
      assert_raises(Unsatisfiable) do
        run_test("cannot_witness_nothing", database: {}) do |tc|
          tc.any(nothing)
        end
      end
    end

    def test_cannot_witness_empty_mix_of
      assert_raises(Unsatisfiable) do
        run_test("cannot_witness_empty_mix_of", database: {}) do |tc|
          tc.any(mix_of)
        end
      end
    end

    def test_can_draw_mixture
      run_test("can_draw_mixture", database: {}) do |tc|
        m = tc.any(mix_of(integers(-5, 0), integers(2, 5)))
        assert (-5..5).cover?(m)
        refute_equal 1, m
      end
    end

    # This test is very hard to trigger without targeting, and targeting will
    # tend to overshoot the score, so we will see multiple interesting test cases
    # before shrinking.
    def test_target_and_reduce
      out, _ = capture_io do
        assert_raises(Minitest::Assertion) do
          run_test("target_and_reduce", database: {}) do |tc|
            m = tc.choice(100_000)
            tc.target(m)
            assert m <= 99_900
          end
        end
      end

      assert_equal <<~OUT, out
        choice(100000): 99901
      OUT
    end

    def test_impossible_weighted
      assert_raises(Failure) do
        run_test("impossible_weighted", database: {}, quiet: true) do |tc|
          tc.choice(1)
          10.times do
            assert false unless tc.weighted(0.0).zero?
          end
          raise Failure if tc.choice(1).zero?
        end
      end
    end

    def test_guaranteed_weighted
      assert_raises(Failure) do
        run_test("guaranteed_weighted", database: {}, quiet: true) do |tc|
          if tc.weighted(1.0).nonzero?
            tc.choice(1)
            raise Failure
          else
            assert false
          end
        end
      end
    end

    def test_size_bounds_on_list
      run_test("size_bounds_on_list", database: {}) do |tc|
        ls = tc.any(lists(integers(0, 10), min_size: 1, max_size: 3))
        assert (1..3).cover?(ls.length)
      end
    end

    def test_forced_choice_bounds
      assert_raises(RangeError) do
        run_test("forced_choice_bounds", database: {}) do |tc|
          tc.forced_choice(2 ** 64)
        end
      end
    end

    def test_failure_from_hypothesis_1
      assert_raises(Failure) do
        run_test("failure_from_hypothesis_1", database: {}, random: Random.new(100), max_examples: 1000, quiet: true) do |tc|
          n1 = tc.weighted(0.0)
          if n1.zero?
            n2 = tc.choice(511)
            if n2 == 112
              n3 = tc.choice(511)
              if n3 == 124
                raise Failure
              elsif n3 == 93
                raise Failure
              else
                tc.mark_status(Status::INVALID)
              end
            elsif n2 == 93
              raise Failure
            else
              tc.mark_status(Status::INVALID)
            end
          end
        end
      end
    end

    def test_failure_from_hypothesis_2
      assert_raises(Failure) do
        run_test("failure_from_hypothesis_2", database: {}, random: Random.new(0), max_examples: 1000, quiet: true) do |tc|
          n1 = tc.choice(6)
          if n1 == 6
            n2 = tc.weighted(0.0)
            if n2.zero?
              raise Failure
            end
          elsif n1 == 4
            n3 = tc.choice(0)
            if n3 == 0
              raise Failure
            else
              tc.mark_status(Status::INVALID)
            end
          elsif n1 == 2
            raise Failure
          else
            tc.mark_status(Status::INVALID)
          end
        end
      end
    end

    def test_refactoring_cache
      old = ->(node, choices, status) {
        choices.each.with_index do |c, i|
          if i + 1 < choices.length || status == Status::OVERRUN
            node = if node.has_key?(c)
                     node[c]
                   else
                     node[c] = {}
                   end
          else
            node[c] = status
          end
        end
      }

      new = ->(node, choices, status) {
        *rest, last = choices
        rest.each do |c|
          node = if node.has_key?(c)
                   node[c]
                 else
                   node[c] = {}
                 end
        end
        unless last.nil?
          node[last] = status == Status::OVERRUN ? {} : status
        end
      }

      run_test("refactoring_cache", database: {}) do |tc|
        old_tree = {}
        new_tree = {}

        choices = tc.any(lists(integers(0, 10)))
        status = Status.new(tc.choice(4))

        old.(old_tree, choices, status)
        new.(new_tree, choices, status)

        assert_equal old_tree, new_tree
      end
    end

    private

    def suppress_warnings
      original_verbosity = $VERBOSE
      $VERBOSE = nil
      yield
      $VERBOSE = original_verbosity
    end
  end
end