File size: 2,874 Bytes
24c19d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Test the enumerative entropy coding implementation."""

from enumerative_coding import EnumerativeEncoder, ExpGolombCoder


def test_exp_golomb():
    """Test exp-Golomb coding."""
    print("Testing Exp-Golomb coding:")
    
    test_values = [0, 1, 2, 3, 4, 5, 10, 15, 31, 32, 100]
    
    for n in test_values:
        encoded = ExpGolombCoder.encode(n)
        decoded, _ = ExpGolombCoder.decode(encoded, 0)
        print(f"  {n:3d} -> {encoded:>10s} -> {decoded:3d} ({'✓' if n == decoded else '✗'})")


def test_combinatorics():
    """Test combinatorial functions."""
    print("\nTesting combinatorial functions:")
    
    # Test binomial coefficients using the encoder's table
    encoder = EnumerativeEncoder()
    test_cases = [(5, 2), (10, 3), (7, 0), (7, 7), (6, 4)]
    for n, k in test_cases:
        result = encoder.binom_table.get(n, k)
        expected = math.comb(n, k) if hasattr(math, 'comb') else None
        print(f"  C({n},{k}) = {result}" + (f" (expected {expected})" if expected else ""))


def test_simple_sequence():
    """Test encoding/decoding of simple sequences."""
    print("\nTesting simple sequences:")
    
    encoder = EnumerativeEncoder()
    
    test_sequences = [
        [0, 1, 0],
        [0, 1, 1, 2],
        [1, 2, 3, 1, 2, 3],
        [0, 0, 1, 1, 2],
    ]
    
    for seq in test_sequences:
        print(f"\n  Testing sequence: {seq}")
        
        try:
            encoded = encoder.encode(seq)
            decoded = encoder.decode(encoded)
            
            print(f"    Original:  {seq}")
            print(f"    Decoded:   {decoded}")
            print(f"    Correct:   {'✓' if seq == decoded else '✗'}")
            print(f"    Size:      {len(seq)} symbols -> {len(encoded)} bytes")
            
        except Exception as e:
            print(f"    Error: {e}")


def test_paper_example():
    """Test with an example that should match the paper's approach."""
    print("\nTesting paper-style example:")
    
    # Create a sequence with known symbol frequencies
    # This tests the 3-step encoding process
    sequence = [0, 0, 1, 2, 1, 0, 2, 1, 1, 2]  # 3 zeros, 4 ones, 3 twos
    
    print(f"  Sequence: {sequence}")
    print(f"  Symbols:  {sorted(set(sequence))}")
    print(f"  Counts:   {[sequence.count(s) for s in sorted(set(sequence))]}")
    
    encoder = EnumerativeEncoder()
    encoded = encoder.encode(sequence)
    decoded = encoder.decode(encoded)
    
    print(f"  Encoded size: {len(encoded)} bytes")
    print(f"  Correctly decoded: {'✓' if sequence == decoded else '✗'}")
    
    if sequence != decoded:
        print(f"  Expected: {sequence}")
        print(f"  Got:      {decoded}")


if __name__ == "__main__":
    import math
    
    test_exp_golomb()
    test_combinatorics()
    test_simple_sequence()
    test_paper_example()