2015-11-24 20:53:07 +01:00
|
|
|
import unittest
|
|
|
|
|
|
2021-01-31 16:49:12 -05:00
|
|
|
from run_length_encoding import (
|
|
|
|
|
encode,
|
|
|
|
|
decode,
|
|
|
|
|
)
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2023-07-14 15:52:15 -07:00
|
|
|
# These tests are auto-generated with test data from:
|
|
|
|
|
# https://github.com/exercism/problem-specifications/tree/main/exercises/run-length-encoding/canonical-data.json
|
|
|
|
|
# File last updated on 2023-07-14
|
2017-03-24 13:24:32 +01:00
|
|
|
|
2019-10-15 09:52:53 -05:00
|
|
|
|
2018-06-13 09:12:09 -04:00
|
|
|
class RunLengthEncodingTest(unittest.TestCase):
|
2017-03-16 13:39:51 +01:00
|
|
|
def test_encode_empty_string(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(encode(""), "")
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2017-03-16 13:39:51 +01:00
|
|
|
def test_encode_single_characters_only_are_encoded_without_count(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(encode("XYZ"), "XYZ")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_encode_string_with_no_single_characters(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(encode("AABBBCCCC"), "2A3B4C")
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2017-03-16 13:39:51 +01:00
|
|
|
def test_encode_single_characters_mixed_with_repeated_characters(self):
|
2015-11-24 20:53:07 +01:00
|
|
|
self.assertMultiLineEqual(
|
2019-10-15 09:52:53 -05:00
|
|
|
encode("WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB"),
|
|
|
|
|
"12WB12W3B24WB",
|
|
|
|
|
)
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2017-03-16 13:39:51 +01:00
|
|
|
def test_encode_multiple_whitespace_mixed_in_string(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(encode(" hsqq qww "), "2 hs2q q2w2 ")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_encode_lowercase_characters(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(encode("aabbbcccc"), "2a3b4c")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_decode_empty_string(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(decode(""), "")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_decode_single_characters_only(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(decode("XYZ"), "XYZ")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_decode_string_with_no_single_characters(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(decode("2A3B4C"), "AABBBCCCC")
|
2017-03-16 13:39:51 +01:00
|
|
|
|
|
|
|
|
def test_decode_single_characters_with_repeated_characters(self):
|
2015-11-24 20:53:07 +01:00
|
|
|
self.assertMultiLineEqual(
|
2019-10-15 09:52:53 -05:00
|
|
|
decode("12WB12W3B24WB"),
|
|
|
|
|
"WWWWWWWWWWWWBWWWWWWWWWWWWBBBWWWWWWWWWWWWWWWWWWWWWWWWB",
|
|
|
|
|
)
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2017-03-16 13:39:51 +01:00
|
|
|
def test_decode_multiple_whitespace_mixed_in_string(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(decode("2 hs2q q2w2 "), " hsqq qww ")
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2020-10-15 12:49:15 -04:00
|
|
|
def test_decode_lowercase_string(self):
|
2019-10-15 09:52:53 -05:00
|
|
|
self.assertMultiLineEqual(decode("2a3b4c"), "aabbbcccc")
|
2015-11-24 20:53:07 +01:00
|
|
|
|
2019-10-15 09:52:53 -05:00
|
|
|
def test_encode_followed_by_decode_gives_original_string(self):
|
|
|
|
|
self.assertMultiLineEqual(decode(encode("zzz ZZ zZ")), "zzz ZZ zZ")
|