Example #1
0
def test_process_frequencies():
    """The right nodes are in the list."""
    expected = {l: i for i, l in enumerate(("a", "b", "c", "d", "e"), start=1)}
    stream = "".join(itertools.starmap(operator.mul, expected.items()))
    freqs = set(process_frequencies(stream))

    assert freqs == {CharNode(l, f) for l, f in expected.items()}
Example #2
0
def test_stream_counter_data(data_file):
    """The function that processes the frequencies correctly."""
    freq_processed = process_frequencies(data_file)
    mapped_result = {node.value: node.freq for node in freq_processed}
    expected = Counter(data_file)

    assert mapped_result == expected
Example #3
0
def test_preffix_free(data_file):
    """The internal table generates new variable-length prefix-free code."""
    freqs = process_frequencies(data_file)
    t = create_tree_code(freqs)
    table = parse_tree_code(t)

    codes = deque(table.values())
    for code_to_check in codes:
        prefix = {c for c in codes if c.startswith(code_to_check)}
        assert prefix == {code_to_check}
Example #4
0
def test_process_empty_frequencies():
    """For '' ->  []."""
    assert process_frequencies("") == []
Example #5
0
def test_frequencies_add_up(data_file):
    frequencies = process_frequencies(data_file)
    checksum = sum(c.freq for c in frequencies)
    expected = sum(Counter(data_file).values())

    assert checksum == expected