Completed
Branch master (87ccc1)
by Chris
08:42
created

tests.compression.test_compression_arithmetic   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 97
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 59
dl 0
loc 97
rs 10
c 0
b 0
f 0
wmc 3
1
# -*- coding: utf-8 -*-
2
3
# Copyright 2014-2018 by Christopher C. Little.
4
# This file is part of Abydos.
5
#
6
# Abydos is free software: you can redistribute it and/or modify
7
# it under the terms of the GNU General Public License as published by
8
# the Free Software Foundation, either version 3 of the License, or
9
# (at your option) any later version.
10
#
11
# Abydos is distributed in the hope that it will be useful,
12
# but WITHOUT ANY WARRANTY; without even the implied warranty of
13
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
# GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License
17
# along with Abydos. If not, see <http://www.gnu.org/licenses/>.
18
19
"""abydos.tests.test_compression_arithmetic.
20
21
This module contains unit tests for abydos.compression.arithmetic
22
"""
23
24
from __future__ import unicode_literals
25
26
import unittest
27
from fractions import Fraction
28
29
from abydos.compression.arithmetic import decode, encode, train
30
31
from .. import NIALL
32
33
34
class ArithmeticCoderTestCases(unittest.TestCase):
35
    """Test abydos.compression.arithmetic.train & .arithmetic.encode."""
36
37
    niall_probs = {'a': (Fraction(41, 57), Fraction(91, 114)),
38
                   ' ': (Fraction(25, 114), Fraction(7, 19)),
39
                   'c': (Fraction(97, 114), Fraction(50, 57)),
40
                   'e': (Fraction(29, 57), Fraction(73, 114)),
41
                   "'": (Fraction(56, 57), Fraction(113, 114)),
42
                   'g': (Fraction(47, 57), Fraction(97, 114)),
43
                   '\x00': (Fraction(113, 114), Fraction(1, 1)),
44
                   'i': (Fraction(73, 114), Fraction(41, 57)),
45
                   'M': (Fraction(17, 19), Fraction(52, 57)),
46
                   'K': (Fraction(37, 38), Fraction(56, 57)),
47
                   'j': (Fraction(50, 57), Fraction(17, 19)),
48
                   '\xed': (Fraction(91, 114), Fraction(47, 57)),
49
                   'l': (Fraction(0, 1), Fraction(25, 114)),
50
                   'o': (Fraction(53, 57), Fraction(107, 114)),
51
                   'N': (Fraction(7, 19), Fraction(29, 57)),
52
                   '\xe9': (Fraction(52, 57), Fraction(35, 38)),
53
                   '\xe1': (Fraction(35, 38), Fraction(53, 57)),
54
                   'U': (Fraction(109, 114), Fraction(55, 57)),
55
                   'O': (Fraction(55, 57), Fraction(37, 38)),
56
                   'h': (Fraction(18, 19), Fraction(109, 114)),
57
                   'n': (Fraction(107, 114), Fraction(18, 19))}
58
59
    def test_arithmetic_train(self):
60
        """Test abydos.compression.arithmetic.train."""
61
        self.assertEqual(train(''), {'\x00': (0, 1)})
62
        self.assertEqual(train(' '.join(NIALL)), self.niall_probs)
63
        self.assertEqual(train(' '.join(sorted(NIALL))), self.niall_probs)
64
        self.assertEqual(train(' '.join(NIALL)),
65
                         train(' '.join(sorted(NIALL))))
66
        self.assertEqual(train(' '.join(NIALL)), train('\x00'.join(NIALL)))
67
68
    def test_arithmetic_encode(self):
69
        """Test abydos.compression.arithmetic.encode."""
70
        self.assertEqual(encode('', self.niall_probs), (254, 8))
71
        self.assertEqual(encode('a', self.niall_probs), (3268, 12))
72
        self.assertEqual(encode('Niall', self.niall_probs), (3911665, 23))
73
        self.assertEqual(encode('Ni\x00ll', self.niall_probs), (1932751, 22))
74
        self.assertEqual(encode('Niel', self.niall_probs), (486801, 20))
75
        self.assertEqual(encode('Mean', self.niall_probs), (243067161, 28))
76
        self.assertEqual(encode('Neil Noígíallach', self.niall_probs),
77
                         (2133315320471368785758, 72))
78
        self.assertRaises(KeyError, encode, 'NIALL', self.niall_probs)
79
        self.assertEqual(encode('', {'\x00': (0, 1)}), (1, 1))
80
81
    def test_arithmetic_decode(self):
82
        """Test abydos.compression.arithmetic.decode."""
83
        self.assertEqual(decode(254, 8, self.niall_probs), '')
84
        self.assertEqual(decode(3268, 12, self.niall_probs), 'a')
85
        self.assertEqual(decode(3911665, 23, self.niall_probs), 'Niall')
86
        self.assertEqual(decode(1932751, 22, self.niall_probs), 'Ni ll')
87
        self.assertEqual(decode(486801, 20, self.niall_probs), 'Niel')
88
        self.assertEqual(decode(243067161, 28, self.niall_probs), 'Mean')
89
        self.assertEqual(decode(2133315320471368785758, 72, self.niall_probs),
90
                         'Neil Noígíallach')
91
        self.assertEqual(decode(0, 0, {}), '')
92
        self.assertEqual(decode(1, 1, {'\x00': (0, 1)}), '')
93
94
95
if __name__ == '__main__':
96
    unittest.main()
97