Debian Codegraph @

tests/test_ppmd8.py

raw symbols SCIP
1 import hashlib
2 import os
3 import pathlib
4
5 import pytest
6
7 import pyppmd
8
9 testdata_path = pathlib.Path(os.path.dirname(__file__)).joinpath("data")
10 source = b"This file is located in a folder.This file is located in the root.\n"
11 encoded = (
12 b"\x54\x16\x43\x6d\x5c\xd8\xd7\x3a\xb3\x58\x31\xac\x1d\x09\x23\xfd\x11\xd5\x72\x62\x73"
13 b"\x13\xb6\xce\xb2\xe7\x6a\xb9\xf6\xe8\x66\xf5\x08\xc3\x0a\x09\x36\x12\xeb\xda\xda\xba"
14 )
15
16 READ_BLOCKSIZE = 16384
17
18
19 def test_ppmd8_encoder1():
20 encoder = pyppmd.Ppmd8Encoder(6, 8 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART)
21 result = encoder.encode(source)
22 result += encoder.flush()
23 assert result == encoded
24
25
26 def test_ppmd8_encoder2():
27 encoder = pyppmd.Ppmd8Encoder(6, 8 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART)
28 result = encoder.encode(source[:33])
29 result += encoder.encode(source[33:])
30 result += encoder.flush()
31 assert result == encoded
32
33
34 def test_ppmd8_decoder1():
35 decoder = pyppmd.Ppmd8Decoder(6, 8 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART)
36 result = decoder.decode(encoded, -1)
37 result += decoder.decode(b"", -1)
38 assert result == source
39 # assert decoder.eof and not decoder.needs_input
40
41
42 def test_ppmd8_decoder2():
43 decoder = pyppmd.Ppmd8Decoder(6, 8 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART)
44 result = decoder.decode(encoded[:20])
45 result += decoder.decode(encoded[20:])
46 result += decoder.decode(b"", -1)
47 assert result == source
48 # assert decoder.eof and not decoder.needs_input
49
50
51 # test mem_size less than original file size as well
52 @pytest.mark.parametrize(
53 "mem_size, restore_method",
54 [
55 (8 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART),
56 (8 << 20, pyppmd.PPMD8_RESTORE_METHOD_CUT_OFF),
57 (1 << 20, pyppmd.PPMD8_RESTORE_METHOD_RESTART),
58 (1 << 20, pyppmd.PPMD8_RESTORE_METHOD_CUT_OFF),
59 ],
60 )
61 @pytest.mark.timeout(20)
62 def test_ppmd8_encode_decode(tmp_path, mem_size, restore_method):
63 length = 0
64 m = hashlib.sha256()
65 with testdata_path.joinpath("10000SalesRecords.csv").open("rb") as f:
66 with tmp_path.joinpath("target.ppmd").open("wb") as target:
67 enc = pyppmd.Ppmd8Encoder(6, mem_size, restore_method=restore_method)
68 data = f.read(READ_BLOCKSIZE)
69 while len(data) > 0:
70 m.update(data)
71 length += len(data)
72 target.write(enc.encode(data))
73 data = f.read(READ_BLOCKSIZE)
74 target.write(enc.flush(endmark=True))
75 shash = m.digest()
76 m2 = hashlib.sha256()
77 assert length == 1237262
78 length = 0
79 with tmp_path.joinpath("target.ppmd").open("rb") as target:
80 with tmp_path.joinpath("target.csv").open("wb") as out:
81 dec = pyppmd.Ppmd8Decoder(6, mem_size, restore_method=restore_method)
82 data = target.read(READ_BLOCKSIZE)
83 while not dec.eof:
84 res = dec.decode(data)
85 m2.update(res)
86 out.write(res)
87 length += len(res)
88 if len(data) == 0:
89 break
90 data = target.read(READ_BLOCKSIZE)
91 assert length == 1237262
92 thash = m2.digest()
93 assert thash == shash
94
95
96 def test_ppmd8_encode_decode_shortage():
97 txt = "\U0001127f\U00069f6a\U00069f6a"
98 obj = txt.encode("UTF-8")
99 enc = pyppmd.Ppmd8Encoder(3, 2048)
100 data = enc.encode(obj)
101 data += enc.flush()
102 length = len(obj)
103 dec = pyppmd.Ppmd8Decoder(3, 2048)
104 res = dec.decode(data, length)
105 if len(res) < length:
106 res += dec.decode(b"\0", length - len(res))
107 assert obj == res
108
109
110 def test_ppmdcompress():
111 compressor = pyppmd.PpmdCompressor(6, 8 << 20, restore_method=pyppmd.PPMD8_RESTORE_METHOD_RESTART, variant="I")
112 result = compressor.compress(source)
113 result += compressor.flush()
114 assert result == encoded
115
116
117 def test_ppmddecompress():
118 decomp = pyppmd.PpmdDecompressor(6, 8 << 20, restore_method=pyppmd.PPMD8_RESTORE_METHOD_RESTART, variant="I")
119 result = decomp.decompress(encoded)
120 assert result == source