Debian Codegraph @

tests/test_ppmd7.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 data = b"This file is located in a folder.This file is located in the root."
11 encoded = b"\x00T\x16C\x98\xbdi\x9b\n\xf1B^N\xac\xc8}:\xbak&\xc1\x7f\x01p\xc51C\xb0b\x1b@\x9a\xb6h\x9a-0\x98\xc0\\'"
12 READ_BLOCKSIZE = 16384
13
14
15 def test_ppmd7_encoder():
16 encoder = pyppmd.Ppmd7Encoder(6, 16 << 20)
17 result = encoder.encode(data)
18 result += encoder.flush()
19 assert len(result) == 41
20 assert result == encoded
21
22
23 def test_ppmd7_encoder2():
24 encoder = pyppmd.Ppmd7Encoder(6, 16 << 20)
25 result = encoder.encode(data[:33])
26 result += encoder.encode(data[33:])
27 result += encoder.flush(endmark=False)
28 assert len(result) == 41
29 assert result == encoded
30
31
32 def test_ppmd7_decoder():
33 decoder = pyppmd.Ppmd7Decoder(6, 16 << 20)
34 result = decoder.decode(encoded, 66)
35 assert result == data
36 assert decoder.eof
37 assert not decoder.needs_input
38
39
40 def test_ppmd7_decoder2():
41 decoder = pyppmd.Ppmd7Decoder(6, 16 << 20)
42 result = decoder.decode(encoded[:33], 33)
43 result += decoder.decode(encoded[33:], 28)
44 assert not decoder.eof
45 while len(result) < 66:
46 if decoder.needs_input:
47 result += decoder.decode(b"\0", 66 - len(result))
48 break
49 else:
50 result += decoder.decode(b"", 66 - len(result))
51 assert result == data
52 assert not decoder.needs_input
53 assert decoder.eof
54
55
56 # test mem_size less than original file size as well
57 @pytest.mark.parametrize("mem_size", [(16 << 20), (1 << 20)])
58 def test_ppmd7_encode_decode(tmp_path, mem_size):
59 length = 0
60 m = hashlib.sha256()
61 with testdata_path.joinpath("10000SalesRecords.csv").open("rb") as f:
62 with tmp_path.joinpath("target.ppmd").open("wb") as target:
63 enc = pyppmd.Ppmd7Encoder(6, mem_size)
64 data = f.read(READ_BLOCKSIZE)
65 while len(data) > 0:
66 m.update(data)
67 length += len(data)
68 target.write(enc.encode(data))
69 data = f.read(READ_BLOCKSIZE)
70 target.write(enc.flush())
71 shash = m.digest()
72 m2 = hashlib.sha256()
73 assert length == 1237262
74 remaining = length
75 with tmp_path.joinpath("target.ppmd").open("rb") as target:
76 with tmp_path.joinpath("target.csv").open("wb") as out:
77 dec = pyppmd.Ppmd7Decoder(6, mem_size)
78 while remaining > 0:
79 data = target.read(READ_BLOCKSIZE)
80 res = dec.decode(data, min(remaining, READ_BLOCKSIZE))
81 if len(res) == 0:
82 if dec.needs_input:
83 res += dec.decode(b"\0", remaining)
84 else:
85 res += dec.decode(b"", remaining)
86 break
87 remaining -= len(res)
88 m2.update(res)
89 out.write(res)
90 assert remaining == 0
91 thash = m2.digest()
92 assert thash == shash
93
94
95 chunk_sizes = [
96 189703,
97 198694,
98 189694,
99 189742,
100 189776,
101 189823,
102 189690,
103 189723,
104 189724,
105 189766,
106 189751,
107 189778,
108 189825,
109 189835,
110 189805,
111 189820,
112 189810,
113 189776,
114 189779,
115 189776,
116 189833,
117 189880,
118 189857,
119 189823,
120 189846,
121 189848,
122 189887,
123 189847,
124 189870,
125 189857,
126 189888,
127 189943,
128 189900,
129 189915,
130 189940,
131 189932,
132 189912,
133 189970,
134 189943,
135 189967,
136 189999,
137 189990,
138 189947,
139 189944,
140 189982,
141 189987,
142 189962,
143 189956,
144 189953,
145 189960,
146 ]
147
148
149 def test_ppmd7_decode_chunks():
150 with testdata_path.joinpath("testdata2.ppmd").open("rb") as f:
151 dec = pyppmd.Ppmd7Decoder(6, 16 << 20)
152 for i in range(30):
153 remaining = chunk_sizes[i]
154 result = b""
155 while remaining > 0:
156 data = f.read(READ_BLOCKSIZE)
157 out = dec.decode(data, remaining)
158 if len(out) == 0:
159 if dec.needs_input:
160 out += dec.decode(b"\0", remaining)
161 else:
162 out += dec.decode(b"", remaining)
163 break
164 remaining -= len(out)
165 result += out
166 assert len(result) == chunk_sizes[i]