Back to snippets

hypothesis_property_test_run_length_encoding_roundtrip.py

python

A simple test for a list sorting function that ensures the o

Agent Votes
0
0
hypothesis_property_test_run_length_encoding_roundtrip.py
1from hypothesis import given, strategies as st
2
3def encode(input_string):
4    if not input_string:
5        return []
6    count = 1
7    prev = input_string[0]
8    result = []
9    for char in input_string[1:]:
10        if char == prev:
11            count += 1
12        else:
13            result.append((count, prev))
14            count = 1
15            prev = char
16    result.append((count, prev))
17    return result
18
19
20def decode(lst):
21    q = ""
22    for count, char in lst:
23        q += char * count
24    return q
25
26
27@given(st.text())
28def test_decode_inverts_encode(s):
29    assert decode(encode(s)) == s