| 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 |
"""This file demonstrates testing a binary search. |
| 12 |
|
| 13 |
It's a useful example because the result of the binary search is so clearly |
| 14 |
determined by the invariants it must satisfy, so we can simply test for those |
| 15 |
invariants. |
| 16 |
|
| 17 |
It also demonstrates the useful testing technique of testing how the answer |
| 18 |
should change (or not) in response to movements in the underlying data. |
| 19 |
""" |
| 20 |
|
| 21 |
import itertools |
| 22 |
|
| 23 |
from hypothesis import given, strategies as st |
| 24 |
|
| 25 |
|
| 26 |
def binary_search(ls, v): |
| 27 |
"""Take a list ls and a value v such that ls is sorted and v is comparable |
| 28 |
with the elements of ls. |
| 29 |
|
| 30 |
Return an index i such that 0 <= i <= len(v) with the properties: |
| 31 |
|
| 32 |
1. ls.insert(i, v) is sorted |
| 33 |
2. ls.insert(j, v) is not sorted for j < i |
| 34 |
""" |
| 35 |
# Without this check we will get an index error on the next line when the |
| 36 |
# list is empty. |
| 37 |
if not ls: |
| 38 |
return 0 |
| 39 |
|
| 40 |
# Without this check we will miss the case where the insertion point should |
| 41 |
# be zero: The invariant we maintain in the next section is that lo is |
| 42 |
# always strictly lower than the insertion point. |
| 43 |
if v <= ls[0]: |
| 44 |
return 0 |
| 45 |
|
| 46 |
# Invariant: There is no insertion point i with i <= lo |
| 47 |
lo = 0 |
| 48 |
|
| 49 |
# Invariant: There is an insertion point i with i <= hi |
| 50 |
hi = len(ls) |
| 51 |
while lo + 1 < hi: |
| 52 |
mid = (lo + hi) // 2 |
| 53 |
if v > ls[mid]: |
| 54 |
# Inserting v anywhere below mid would result in an unsorted list |
| 55 |
# because it's > the value at mid. Therefore mid is a valid new lo |
| 56 |
lo = mid |
| 57 |
# Uncommenting the following lines will cause this to return a valid |
| 58 |
# insertion point which is not always minimal. |
| 59 |
# elif v == ls[mid]: |
| 60 |
# return mid |
| 61 |
else: |
| 62 |
# Either v == ls[mid] in which case mid is a valid insertion point |
| 63 |
# or v < ls[mid], in which case all valid insertion points must be |
| 64 |
# < hi. Either way, mid is a valid new hi. |
| 65 |
hi = mid |
| 66 |
assert lo + 1 == hi |
| 67 |
# We now know that there is a valid insertion point <= hi and there is no |
| 68 |
# valid insertion point < hi because hi - 1 is lo. Therefore hi is the |
| 69 |
# answer we were seeking |
| 70 |
return hi |
| 71 |
|
| 72 |
|
| 73 |
def is_sorted(ls): |
| 74 |
"""Is this list sorted?""" |
| 75 |
return all(x <= y for x, y in itertools.pairwise(ls)) |
| 76 |
|
| 77 |
|
| 78 |
Values = st.integers() |
| 79 |
|
| 80 |
# We generate arbitrary lists and turn this into generating sorting lists |
| 81 |
# by just sorting them. |
| 82 |
SortedLists = st.lists(Values).map(sorted) |
| 83 |
|
| 84 |
# We could also do it this way, but that would be a bad idea: |
| 85 |
# SortedLists = st.lists(Values).filter(is_sorted) |
| 86 |
# The problem is that Hypothesis will only generate long sorted lists with very |
| 87 |
# low probability, so we are much better off post-processing values into the |
| 88 |
# form we want than filtering them out. |
| 89 |
|
| 90 |
|
| 91 |
@given(ls=SortedLists, v=Values) |
| 92 |
def test_insert_is_sorted(ls, v): |
| 93 |
"""We test the first invariant: binary_search should return an index such |
| 94 |
that inserting the value provided at that index would result in a sorted |
| 95 |
set.""" |
| 96 |
ls.insert(binary_search(ls, v), v) |
| 97 |
assert is_sorted(ls) |
| 98 |
|
| 99 |
|
| 100 |
@given(ls=SortedLists, v=Values) |
| 101 |
def test_is_minimal(ls, v): |
| 102 |
"""We test the second invariant: binary_search should return an index such |
| 103 |
that no smaller index is a valid insertion point for v.""" |
| 104 |
for i in range(binary_search(ls, v)): |
| 105 |
ls2 = list(ls) |
| 106 |
ls2.insert(i, v) |
| 107 |
assert not is_sorted(ls2) |
| 108 |
|
| 109 |
|
| 110 |
@given(ls=SortedLists, v=Values) |
| 111 |
def test_inserts_into_same_place_twice(ls, v): |
| 112 |
"""In this we test a *consequence* of the second invariant: When we insert |
| 113 |
a value into a list twice, the insertion point should be the same both |
| 114 |
times. This is because we know that v is > the previous element and == the |
| 115 |
next element. |
| 116 |
|
| 117 |
In theory if the former passes, this should always pass. In practice, |
| 118 |
failures are detected by this test with much higher probability because it |
| 119 |
deliberately puts the data into a shape that is likely to trigger a |
| 120 |
failure. |
| 121 |
|
| 122 |
This is an instance of a good general category of test: Testing how the |
| 123 |
function moves in responses to changes in the underlying data. |
| 124 |
""" |
| 125 |
i = binary_search(ls, v) |
| 126 |
ls.insert(i, v) |
| 127 |
assert binary_search(ls, v) == i |