summaryrefslogtreecommitdiff
path: root/tests/letter_compress_test.py
blob: a466277704c252377cda349489e86d60a5c22752 (plain)
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
#!/usr/bin/env python3

import random
import math
import unittest

import bootstrap
import letter_compress
import unittest_utils as uu


class TestLetterCompress(unittest.TestCase):

    def test_with_random_strings(self):
        alphabet = 'abcdefghijklmnopqrstuvwxyz .,"-'
        for n in range(20):
            message = ""
            for letter in random.choices(alphabet, k=random.randrange(10, 5000)):
                message += letter
            mlen = len(message)
            compressed = letter_compress.compress(message)
            clen = len(compressed)
            self.assertEqual(math.ceil(mlen * 5.0 / 8.0), clen)
            decompressed = letter_compress.decompress(compressed)
            self.assertEqual(decompressed, message, f'The bad message string was "{message}"')


if __name__ == '__main__':
    bootstrap.initialize(unittest.main)()