-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_codefree.py
108 lines (96 loc) · 4.13 KB
/
test_codefree.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import pytest
from hypothesis import given, example, strategies as st
from _code.codefree import (
get_cyclic_shifts,
is_periodic,
is_prime_code,
find_prime_codes,
is_valid_commafree,
find_maximal_commafree_codes
)
# Test cyclic shifts
@given(st.text(min_size=1, max_size=10))
def test_cyclic_shifts(word):
shifts = get_cyclic_shifts(word)
# Should return list of same-length strings
assert all(len(s) == len(word) for s in shifts)
# Should return exactly n shifts for length n word
assert len(shifts) == len(word)
# Original word should be in shifts
assert word in shifts
# Test periodicity
@given(st.text(min_size=1, max_size=10))
def test_is_periodic(word):
# Test known periodic words
assert is_periodic(word * 2) # Repeating any word is periodic
# A single character repeated n times is periodic only if n > 1
if len(word) > 1:
assert is_periodic(word[0] * len(word)) # Repeating single char is periodic
@given(st.text(min_size=1, max_size=10))
def test_is_prime_code(word):
shifts = get_cyclic_shifts(word)
if is_prime_code(word):
# If it's a prime code, it should be lexicographically minimal
assert word == min(shifts)
# Test prime code finding
@given(st.integers(min_value=1, max_value=5),
st.lists(st.characters(min_codepoint=48, max_codepoint=57), min_size=1, max_size=3))
def test_find_prime_codes(n, alphabet):
alphabet = list(set(alphabet)) # Remove duplicates
if len(alphabet) > 0:
codes = find_prime_codes(n, alphabet)
# All codes should be of length n
assert all(len(code) == n for code in codes)
# All codes should use only characters from alphabet
assert all(set(code).issubset(set(alphabet)) for code in codes)
# All codes should be prime codes
assert all(is_prime_code(code) for code in codes)
# No code should be periodic
assert all(not is_periodic(code) for code in codes)
# Test comma-free validation
def test_is_valid_commafree():
# Test known valid cases
assert not is_valid_commafree("01", ["10"]) # "01" and "10" do not overlap
assert not is_valid_commafree("00", ["01", "10"]) # "00" does not overlap with "01" or "10"
# Test known invalid cases
assert not is_valid_commafree("01", ["01"]) # Same code
assert not is_valid_commafree("11", []) # Periodic
assert not is_valid_commafree("01", ["001"]) # "01" is a prefix of "001"
# Add more specific test cases
assert is_valid_commafree("001", ["110"]) # Valid case
assert not is_valid_commafree("001", ["100"]) # Invalid case - can overlap
assert not is_valid_commafree("10", ["100"]) # "10" is a prefix of "100"
# Test maximal comma-free codes finding
@given(st.lists(st.text(min_size=2, max_size=2, alphabet="01"), min_size=1, max_size=5))
def test_find_maximal_commafree_codes(prime_codes):
codes = find_maximal_commafree_codes(prime_codes)
if codes:
# All codes should be valid comma-free codes
for i, code in enumerate(codes):
assert is_valid_commafree(code, codes[:i] + codes[i+1:])
# Test specific examples
def test_specific_examples():
# Test with known small examples
alphabet = "01"
n = 2
prime_codes = find_prime_codes(n, alphabet)
maximal_codes = find_maximal_commafree_codes(prime_codes)
assert len(maximal_codes) > 0 # Should find at least one code
# Test caching behavior
def test_caching():
# Test that repeated calls with same parameters return same result
alphabet = "012"
n = 3
result1 = find_prime_codes(n, alphabet)
result2 = find_prime_codes(n, alphabet)
assert result1 == result2
# Test specific case for find_prime_codes
def test_find_prime_codes_specific():
n = 4
alphabet = '012'
expected_result = ['0001', '0002', '0011', '0012', '0021', '0022', '0102', '0111', '0112', '0121', '0122', '0211', '0212', '0221', '0222', '1112', '1122', '1222']
result = find_prime_codes(n, alphabet)
assert len(result) == 18
assert sorted(result) == sorted(expected_result) # Compare sorted lists to ignore order
if __name__ == "__main__":
pytest.main([__file__])