| 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 example demonstrates testing a run length encoding scheme. That is, we |
| 12 |
take a sequence and represent it by a shorter sequence where each 'run' of |
| 13 |
consecutive equal elements is represented as a single element plus a count. So |
| 14 |
e.g. |
| 15 |
|
| 16 |
[1, 1, 1, 1, 2, 1] is represented as [[1, 4], [2, 1], [1, 1]] |
| 17 |
|
| 18 |
This demonstrates the useful decode(encode(x)) == x invariant that is often |
| 19 |
a fruitful source of testing with Hypothesis. |
| 20 |
|
| 21 |
It also has an example of testing invariants in response to changes in the |
| 22 |
underlying data. |
| 23 |
""" |
| 24 |
|
| 25 |
from hypothesis import assume, given, strategies as st |
| 26 |
|
| 27 |
|
| 28 |
def run_length_encode(seq): |
| 29 |
"""Encode a sequence as a new run-length encoded sequence.""" |
| 30 |
if not seq: |
| 31 |
return [] |
| 32 |
# By starting off the count at zero we simplify the iteration logic |
| 33 |
# slightly. |
| 34 |
result = [[seq[0], 0]] |
| 35 |
for s in seq: |
| 36 |
if ( |
| 37 |
# If you uncomment this line this branch will be skipped and we'll |
| 38 |
# always append a new run of length 1. Note which tests fail. |
| 39 |
# False and |
| 40 |
s |
| 41 |
== result[-1][0] |
| 42 |
# Try uncommenting this line and see what problems occur: |
| 43 |
# and result[-1][-1] < 2 |
| 44 |
): |
| 45 |
result[-1][1] += 1 |
| 46 |
else: |
| 47 |
result.append([s, 1]) |
| 48 |
return result |
| 49 |
|
| 50 |
|
| 51 |
def run_length_decode(seq): |
| 52 |
"""Take a previously encoded sequence and reconstruct the original from |
| 53 |
it.""" |
| 54 |
result = [] |
| 55 |
for s, i in seq: |
| 56 |
for _ in range(i): |
| 57 |
result.append(s) |
| 58 |
return result |
| 59 |
|
| 60 |
|
| 61 |
# We use lists of a type that should have a relatively high duplication rate, |
| 62 |
# otherwise we'd almost never get any runs. |
| 63 |
Lists = st.lists(st.integers(0, 10)) |
| 64 |
|
| 65 |
|
| 66 |
@given(Lists) |
| 67 |
def test_decodes_to_starting_sequence(ls): |
| 68 |
"""If we encode a sequence and then decode the result, we should get the |
| 69 |
original sequence back. |
| 70 |
|
| 71 |
Otherwise we've done something very wrong. |
| 72 |
""" |
| 73 |
assert run_length_decode(run_length_encode(ls)) == ls |
| 74 |
|
| 75 |
|
| 76 |
@given(Lists, st.data()) |
| 77 |
def test_duplicating_an_element_does_not_increase_length(ls, data): |
| 78 |
"""The previous test could be passed by simply returning the input sequence |
| 79 |
so we need something that tests the compression property of our encoding. |
| 80 |
|
| 81 |
In this test we deliberately introduce or extend a run and assert |
| 82 |
that this does not increase the length of our encoding, because they |
| 83 |
should be part of the same run in the final result. |
| 84 |
""" |
| 85 |
# We use assume to get a valid index into the list. We could also have used |
| 86 |
# e.g. flatmap, but this is relatively straightforward and will tend to |
| 87 |
# perform better. |
| 88 |
assume(ls) |
| 89 |
i = data.draw(st.integers(0, len(ls) - 1)) |
| 90 |
ls2 = list(ls) |
| 91 |
# duplicating the value at i right next to it guarantees they are part of |
| 92 |
# the same run in the resulting compression. |
| 93 |
ls2.insert(i, ls2[i]) |
| 94 |
assert len(run_length_encode(ls2)) == len(run_length_encode(ls)) |