2019-12-08 22:43:49 +01:00
|
|
|
# vim: set fileencoding=utf-8
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from bemani.protocol.lz77 import Lz77, Lz77Decompress
|
2021-05-31 20:09:40 +02:00
|
|
|
from bemani.tests.helpers import get_fixture
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
|
|
|
|
class TestLZ77Decompressor(unittest.TestCase):
|
|
|
|
def test_ringbuffer_fuzz(self) -> None:
|
|
|
|
dec = Lz77Decompress(b'')
|
|
|
|
|
|
|
|
for _ in range(100):
|
|
|
|
amount = random.randint(1, Lz77Decompress.RING_LENGTH)
|
2020-11-28 20:05:27 +01:00
|
|
|
data = os.urandom(amount)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Save our ring position, write a chunk of data
|
|
|
|
readpos = dec.write_pos
|
2021-05-31 20:09:40 +02:00
|
|
|
dec._ring_write(data)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
# Read a chunk of data back from that buffer, see its the same
|
2021-05-31 20:09:40 +02:00
|
|
|
newdata = b''.join(dec._ring_read(readpos, amount))
|
2019-12-08 22:43:49 +01:00
|
|
|
self.assertEqual(data, newdata)
|
|
|
|
|
|
|
|
# Verify integrity of ringbuffer
|
|
|
|
self.assertEqual(len(dec.ring), Lz77Decompress.RING_LENGTH)
|
|
|
|
|
|
|
|
|
|
|
|
class TestLz77RealCompressor(unittest.TestCase):
|
|
|
|
def test_small_data_random(self) -> None:
|
|
|
|
lz77 = Lz77()
|
2020-11-28 20:05:27 +01:00
|
|
|
data = os.urandom(1 * 1024)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
|
|
|
def test_large_data_random(self) -> None:
|
|
|
|
lz77 = Lz77()
|
2020-11-28 20:05:27 +01:00
|
|
|
data = os.urandom(100 * 1024)
|
2019-12-08 22:43:49 +01:00
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
2020-11-12 06:03:33 +01:00
|
|
|
def test_huge_data_random(self) -> None:
|
|
|
|
lz77 = Lz77()
|
2020-11-28 20:05:27 +01:00
|
|
|
data = os.urandom(1 * 1024 * 1024)
|
2020-11-12 06:03:33 +01:00
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
def test_declaration(self) -> None:
|
|
|
|
lz77 = Lz77()
|
|
|
|
data = get_fixture("declaration.txt")
|
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
self.assertTrue(len(compresseddata) < len(data))
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
|
|
|
def test_lorem_ipsum(self) -> None:
|
|
|
|
lz77 = Lz77()
|
|
|
|
data = get_fixture("lorem.txt")
|
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
self.assertTrue(len(compresseddata) < len(data))
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
2020-11-12 06:02:37 +01:00
|
|
|
def test_texture(self) -> None:
|
|
|
|
lz77 = Lz77()
|
|
|
|
data = get_fixture("rawdata")
|
|
|
|
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertNotEqual(data, compresseddata)
|
|
|
|
self.assertTrue(len(compresseddata) < len(data))
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|
|
|
|
|
2019-12-08 22:43:49 +01:00
|
|
|
def test_known_compression(self) -> None:
|
|
|
|
"""
|
|
|
|
Specifically tests for ability to compress an overlap,
|
|
|
|
verifies that we don't regress on known compressions.
|
|
|
|
"""
|
|
|
|
lz77 = Lz77()
|
|
|
|
data = b"abcabcabcabc"
|
|
|
|
compresseddata = lz77.compress(data)
|
|
|
|
self.assertEqual(b"\x07abc\x006\x00\x00", compresseddata)
|
|
|
|
|
|
|
|
decompresseddata = lz77.decompress(compresseddata)
|
|
|
|
self.assertEqual(data, decompresseddata)
|