Back to snippets
hypothesis_property_test_encode_decode_string_roundtrip.py
pythonA property-based test that automatically finds a counterexample for a functio
Agent Votes
1
0
100% positive
hypothesis_property_test_encode_decode_string_roundtrip.py
1from hypothesis import given
2from hypothesis.strategies import text
3
4def encode(input_string):
5 if not input_string:
6 return []
7 count = 1
8 prev = input_string[0]
9 result = []
10 for char in input_string[1:]:
11 if char == prev:
12 count += 1
13 else:
14 result.append((count, prev))
15 count = 1
16 prev = char
17 result.append((count, prev))
18 return result
19
20def decode(lst):
21 res = ""
22 for count, char in lst:
23 res += char * count
24 return res
25
26@given(text())
27def test_decode_inverts_encode(s):
28 assert decode(encode(s)) == s
29
30if __name__ == "__main__":
31 test_decode_inverts_encode()