-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathtest_minithesis.py
632 lines (485 loc) · 17.4 KB
/
test_minithesis.py
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
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# This file is part of Minithesis, which may be found at
# https://github.com/DRMacIver/minithesis
#
# This work is copyright (C) 2020 David R. MacIver.
#
# 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/.
from collections import defaultdict
from random import Random
import pytest
from hypothesis import HealthCheck, Phase, given, note, reject, settings
from hypothesis import strategies as st
import minithesis as mt
from minithesis import CachedTestFunction, DirectoryDB, Frozen, Possibility, Status
from minithesis import TestCase as TC
from minithesis import TestingState as State
from minithesis import (
Unsatisfiable,
integers,
just,
lists,
mix_of,
nothing,
run_test,
tuples,
)
@Possibility
def list_of_integers(test_case):
result = []
while test_case.weighted(0.9):
result.append(test_case.choice(10000))
return result
@pytest.mark.parametrize("seed", range(10))
def test_finds_small_list(capsys, seed):
with pytest.raises(AssertionError):
@run_test(database={}, random=Random(seed))
def _(test_case):
ls = test_case.any(lists(integers(0, 10000)))
assert sum(ls) <= 1000
captured = capsys.readouterr()
assert captured.out.strip() == "any(lists(integers(0, 10000))): [1001]"
@pytest.mark.parametrize("seed", range(10))
def test_finds_small_list_even_with_bad_lists(capsys, seed):
"""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.
"""
with pytest.raises(AssertionError):
@Possibility
def bad_list(test_case):
n = test_case.choice(10)
return [test_case.choice(10000) for _ in range(n)]
@run_test(database={}, random=Random(seed))
def _(test_case):
ls = test_case.any(bad_list)
assert sum(ls) <= 1000
captured = capsys.readouterr()
assert captured.out.strip() == "any(bad_list): [1001]"
def test_reduces_additive_pairs(capsys):
with pytest.raises(AssertionError):
@run_test(database={}, max_examples=10000)
def _(test_case):
m = test_case.choice(1000)
n = test_case.choice(1000)
assert m + n <= 1000
captured = capsys.readouterr()
assert [c.strip() for c in captured.out.splitlines()] == [
"choice(1000): 1",
"choice(1000): 1000",
]
def test_reuses_results_from_the_database(tmpdir):
db = DirectoryDB(tmpdir)
count = 0
def run():
with pytest.raises(AssertionError):
@run_test(database=db)
def _(test_case):
nonlocal count
count += 1
assert test_case.choice(10000) < 10
run()
assert len(tmpdir.listdir()) == 1
prev_count = count
run()
assert len(tmpdir.listdir()) == 1
assert count == prev_count + 2
def test_test_cases_satisfy_preconditions():
@run_test()
def _(test_case):
n = test_case.choice(10)
test_case.assume(n != 0)
assert n != 0
def test_error_on_too_strict_precondition():
with pytest.raises(Unsatisfiable):
@run_test()
def _(test_case):
n = test_case.choice(10)
test_case.reject()
def test_error_on_unbounded_test_function(monkeypatch):
monkeypatch.setattr(mt, "BUFFER_SIZE", 10)
with pytest.raises(Unsatisfiable):
@run_test(max_examples=5)
def _(test_case):
while True:
test_case.choice(10)
def test_function_cache():
def tf(tc):
if tc.choice(1000) >= 200:
tc.mark_status(Status.INTERESTING)
if tc.choice(1) == 0:
tc.reject()
state = State(Random(0), tf, 100)
cache = CachedTestFunction(state.test_function)
assert cache([1, 1]) == Status.VALID
assert cache([1]) == Status.OVERRUN
assert cache([1000]) == Status.INTERESTING
assert cache([1000]) == Status.INTERESTING
assert cache([1000, 1]) == Status.INTERESTING
assert state.calls == 2
@pytest.mark.parametrize("max_examples", range(1, 100))
def test_max_examples_is_not_exceeded(max_examples):
"""Targeting has a number of places it checks for
whether we've exceeded the generation limits. This
makes sure we've checked them all.
"""
calls = 0
@run_test(database={}, random=Random(0), max_examples=max_examples)
def _(tc):
nonlocal calls
m = 10000
n = tc.choice(m)
calls += 1
tc.target(n * (m - n))
assert calls == max_examples
@pytest.mark.parametrize("seed", range(100))
def test_finds_a_local_maximum(seed):
"""Targeting has a number of places it checks for
whether we've exceeded the generation limits. This
makes sure we've checked them all.
"""
with pytest.raises(AssertionError):
@run_test(database={}, random=Random(seed), max_examples=200, quiet=True)
def _(tc):
m = tc.choice(1000)
n = tc.choice(1000)
score = -((m - 500) ** 2 + (n - 500) ** 2)
tc.target(score)
assert m != 500 or n != 500
def test_can_target_a_score_upwards_to_interesting(capsys):
with pytest.raises(AssertionError):
@run_test(database={}, max_examples=1000)
def _(test_case):
n = test_case.choice(1000)
m = test_case.choice(1000)
score = n + m
test_case.target(score)
assert score < 2000
captured = capsys.readouterr()
assert [c.strip() for c in captured.out.splitlines()] == [
"choice(1000): 1000",
"choice(1000): 1000",
]
def test_can_target_a_score_upwards_without_failing():
max_score = 0
@run_test(database={}, max_examples=1000)
def _(test_case):
nonlocal max_score
n = test_case.choice(1000)
m = test_case.choice(1000)
score = n + m
test_case.target(score)
max_score = max(score, max_score)
assert max_score == 2000
def test_targeting_when_most_do_not_benefit(capsys):
with pytest.raises(AssertionError):
big = 10000
@run_test(database={}, max_examples=1000)
def _(test_case):
test_case.choice(1000)
test_case.choice(1000)
score = test_case.choice(big)
test_case.target(score)
assert score < big
captured = capsys.readouterr()
assert [c.strip() for c in captured.out.splitlines()] == [
"choice(1000): 0",
"choice(1000): 0",
f"choice({big}): {big}",
]
def test_can_target_a_score_downwards(capsys):
with pytest.raises(AssertionError):
@run_test(database={}, max_examples=1000)
def _(test_case):
n = test_case.choice(1000)
m = test_case.choice(1000)
score = n + m
test_case.target(-score)
assert score > 0
captured = capsys.readouterr()
assert [c.strip() for c in captured.out.splitlines()] == [
"choice(1000): 0",
"choice(1000): 0",
]
def test_prints_a_top_level_weighted(capsys):
with pytest.raises(AssertionError):
@run_test(database={}, max_examples=1000)
def _(test_case):
assert test_case.weighted(0.5)
captured = capsys.readouterr()
assert captured.out.strip() == "weighted(0.5): False"
def test_errors_when_using_frozen():
tc = TC.for_choices([0])
tc.status = Status.VALID
with pytest.raises(Frozen):
tc.mark_status(Status.INTERESTING)
with pytest.raises(Frozen):
tc.choice(10)
with pytest.raises(Frozen):
tc.forced_choice(10)
def test_errors_on_too_large_choice():
tc = TC.for_choices([0])
with pytest.raises(ValueError):
tc.choice(2 ** 64)
def test_can_choose_full_64_bits():
@run_test()
def _(tc):
tc.choice(2 ** 64 - 1)
def test_mapped_possibility():
@run_test()
def _(tc):
n = tc.any(integers(0, 5).map(lambda n: n * 2))
assert n % 2 == 0
def test_selected_possibility():
@run_test()
def _(tc):
n = tc.any(integers(0, 5).satisfying(lambda n: n % 2 == 0))
assert n % 2 == 0
def test_bound_possibility():
@run_test()
def _(tc):
m, n = tc.any(
integers(0, 5).bind(lambda m: tuples(just(m), integers(m, m + 10),))
)
assert m <= n <= m + 10
def test_cannot_witness_nothing():
with pytest.raises(Unsatisfiable):
@run_test()
def _(tc):
tc.any(nothing())
def test_cannot_witness_empty_mix_of():
with pytest.raises(Unsatisfiable):
@run_test()
def _(tc):
tc.any(mix_of())
def test_can_draw_mixture():
@run_test()
def _(tc):
m = tc.any(mix_of(integers(-5, 0), integers(2, 5)))
assert -5 <= m <= 5
assert m != 1
def test_target_and_reduce(capsys):
"""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."""
with pytest.raises(AssertionError):
@run_test(database={})
def _(tc):
m = tc.choice(100000)
tc.target(m)
assert m <= 99900
captured = capsys.readouterr()
assert captured.out.strip() == "choice(100000): 99901"
def test_impossible_weighted():
with pytest.raises(Failure):
@run_test(database={})
def _(tc):
tc.choice(1)
for _ in range(10):
if tc.weighted(0.0):
assert False
if tc.choice(1):
raise Failure()
def test_guaranteed_weighted():
with pytest.raises(Failure):
@run_test(database={})
def _(tc):
if tc.weighted(1.0):
tc.choice(1)
raise Failure()
else:
assert False
def test_size_bounds_on_list():
@run_test(database={})
def _(tc):
ls = tc.any(lists(integers(0, 10), min_size=1, max_size=3))
assert 1 <= len(ls) <= 3
def test_forced_choice_bounds():
with pytest.raises(ValueError):
@run_test(database={})
def _(tc):
tc.forced_choice(2 ** 64)
class Failure(Exception):
pass
@settings(
suppress_health_check=list(HealthCheck),
deadline=None,
report_multiple_bugs=False,
max_examples=50,
)
@given(st.data())
def test_give_minithesis_a_workout(data):
seed = data.draw(st.integers(0, 1000))
rnd = Random(seed)
max_examples = data.draw(st.integers(1, 100))
method_call = st.one_of(
st.tuples(
st.just("mark_status"),
st.sampled_from((Status.INVALID, Status.VALID, Status.INTERESTING)),
),
st.tuples(st.just("target"), st.floats(0.0, 1.0)),
st.tuples(st.just("choice"), st.integers(0, 1000)),
st.tuples(st.just("weighted"), st.floats(0.0, 1.0)),
)
def new_node():
return [None, defaultdict(new_node)]
tree = new_node()
database = {}
failed = False
call_count = 0
valid_count = 0
try:
try:
@run_test(
max_examples=max_examples, random=rnd, database=database, quiet=True,
)
def test_function(test_case):
node = tree
depth = 0
nonlocal call_count, valid_count, failed
call_count += 1
while True:
depth += 1
if node[0] is None:
node[0] = data.draw(method_call)
if node[0] == ("mark_status", Status.INTERESTING):
failed = True
raise Failure()
if node[0] == ("mark_status", Status.VALID):
valid_count += 1
name, *rest = node[0]
result = getattr(test_case, name)(*rest)
node = node[1][result]
except Failure:
failed = True
except Unsatisfiable:
reject()
if not failed:
assert valid_count <= max_examples
assert call_count <= max_examples * 10
except Exception as e:
@note
def tree_as_code():
"""If the test fails, print out a test that will trigger that
failure rather than making me hand-edit it into something useful."""
i = 1
while True:
test_name = f"test_failure_from_hypothesis_{i}"
if test_name not in globals():
break
i += 1
lines = [
f"def {test_name}():",
" with pytest.raises(Failure):",
f" @run_test(max_examples=1000, database={{}}, random=Random({seed}))",
" def _(tc):",
]
varcount = 0
def recur(indent, node):
nonlocal varcount
if node[0] is None:
lines.append(" " * indent + "tc.reject()")
return
method, *args = node[0]
if method == "mark_status":
if args[0] == Status.INTERESTING:
lines.append(" " * indent + "raise Failure()")
elif args[0] == Status.VALID:
lines.append(" " * indent + "return")
elif args[0] == Status.INVALID:
lines.append(" " * indent + "tc.reject()")
else:
lines.append(
" " * indent + f"tc.mark_status(Status.{args[0].name})"
)
elif method == "target":
lines.append(" " * indent + f"tc.target({args[0]})")
recur(indent, *node[1].values())
elif method == "weighted":
cond = f"tc.weighted({args[0]})"
assert len(node[1]) > 0
if len(node[1]) == 2:
lines.append(" " * indent + "if {cond}:")
recur(indent + 4, node[1][True])
lines.append(" " * indent + "else:")
recur(indent + 4, node[1][False])
else:
if True in node[1]:
lines.append(" " * indent + f"if {cond}:")
recur(indent + 4, node[1][True])
else:
assert False in node[1]
lines.append(" " * indent + f"if not {cond}:")
recur(indent + 4, node[1][False])
else:
varcount += 1
varname = f"n{varcount}"
lines.append(
" " * indent
+ f"{varname} = tc.{method}({', '.join(map(repr, args))})"
)
first = True
for k, v in node[1].items():
if v[0] == ("mark_status", Status.INVALID):
continue
lines.append(
" " * indent
+ ("if" if first else "elif")
+ f" {varname} == {k}:"
)
first = False
recur(indent + 4, v)
lines.append(" " * indent + "else:")
lines.append(" " * (indent + 4) + "tc.reject()")
recur(12, tree)
return "\n".join(lines)
raise e
def test_failure_from_hypothesis_1():
with pytest.raises(Failure):
@run_test(max_examples=1000, database={}, random=Random(100))
def _(tc):
n1 = tc.weighted(0.0)
if not n1:
n2 = tc.choice(511)
if n2 == 112:
n3 = tc.choice(511)
if n3 == 124:
raise Failure()
elif n3 == 93:
raise Failure()
else:
tc.mark_status(Status.INVALID)
elif n2 == 93:
raise Failure()
else:
tc.mark_status(Status.INVALID)
def test_failure_from_hypothesis_2():
with pytest.raises(Failure):
@run_test(max_examples=1000, database={}, random=Random(0))
def _(tc):
n1 = tc.choice(6)
if n1 == 6:
n2 = tc.weighted(0.0)
if not n2:
raise Failure()
elif n1 == 4:
n3 = tc.choice(0)
if n3 == 0:
raise Failure()
else:
tc.mark_status(Status.INVALID)
elif n1 == 2:
raise Failure()
else:
tc.mark_status(Status.INVALID)