| 1 |
# This file is part of Hypothesis, which may be found at |
| 2 |
# https://github.com/HypothesisWorks/hypothesis/ |
| 3 |
# |
| 4 |
# Copyright the Hypothesis Authors. |
| 5 |
# Individual contributors are listed in AUTHORS.rst and the git log. |
| 6 |
# |
| 7 |
# This Source Code Form is subject to the terms of the Mozilla Public License, |
| 8 |
# v. 2.0. If a copy of the MPL was not distributed with this file, You can |
| 9 |
# obtain one at https://mozilla.org/MPL/2.0/. |
| 10 |
|
| 11 |
from hypothesis import example, given, strategies as st |
| 12 |
from hypothesis.internal.conjecture.shrinking import Ordering |
| 13 |
|
| 14 |
|
| 15 |
@example([0, 1, 1, 1, 1, 1, 1, 0]) |
| 16 |
@example([0, 0]) |
| 17 |
@example([0, 1, -1]) |
| 18 |
@given(st.lists(st.integers())) |
| 19 |
def test_shrinks_down_to_sorted_the_slow_way(ls): |
| 20 |
# We normally would short-circuit and find that we can sort this |
| 21 |
# automatically, but here we test that a single run_step could put the |
| 22 |
# list in sorted order anyway if it had to, and that that is just an |
| 23 |
# optimisation. |
| 24 |
shrinker = Ordering(ls, lambda ls: True, full=False) |
| 25 |
shrinker.run_step() |
| 26 |
assert list(shrinker.current) == sorted(ls) |
| 27 |
|
| 28 |
|
| 29 |
def test_can_partially_sort_a_list(): |
| 30 |
finish = Ordering.shrink([5, 4, 3, 2, 1, 0], lambda x: x[0] > x[-1]) |
| 31 |
assert finish == (1, 2, 3, 4, 5, 0) |
| 32 |
|
| 33 |
|
| 34 |
def test_can_partially_sort_a_list_2(): |
| 35 |
finish = Ordering.shrink([5, 4, 3, 2, 1, 0], lambda x: x[0] > x[2], full=True) |
| 36 |
assert finish <= (1, 2, 0, 3, 4, 5) |
| 37 |
|
| 38 |
|
| 39 |
def test_adaptively_shrinks_around_hole(): |
| 40 |
initial = list(range(1000, 0, -1)) |
| 41 |
initial[500] = 2000 |
| 42 |
|
| 43 |
intended_result = sorted(initial) |
| 44 |
intended_result.insert(500, intended_result.pop()) |
| 45 |
|
| 46 |
shrinker = Ordering(initial, lambda ls: ls[500] == 2000, full=True) |
| 47 |
shrinker.run() |
| 48 |
|
| 49 |
assert shrinker.current[500] == 2000 |
| 50 |
|
| 51 |
assert list(shrinker.current) == intended_result |
| 52 |
assert shrinker.calls <= 60 |