forked from aiven/python-snappy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_hadoop_snappy.py
54 lines (44 loc) · 1.69 KB
/
test_hadoop_snappy.py
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#!/usr/bin/env python
import os
import random
import struct
from unittest import TestCase
import snappy.hadoop_snappy
class SnappyStreaming(TestCase):
def test_random(self):
for _ in range(100):
compressor = snappy.hadoop_snappy.StreamCompressor()
decompressor = snappy.hadoop_snappy.StreamDecompressor()
data = b""
compressed = b""
for _ in range(random.randint(0, 3)):
chunk = os.urandom(
random.randint(0, snappy.hadoop_snappy._CHUNK_MAX * 2)
)
data += chunk
compressed += compressor.add_chunk(
chunk
)
upper_bound = random.choice(
[256, snappy.hadoop_snappy._CHUNK_MAX * 2]
)
while compressed:
size = random.randint(0, upper_bound)
chunk, compressed = compressed[:size], compressed[size:]
chunk = decompressor.decompress(chunk)
self.assertEqual(data[:len(chunk)], chunk)
data = data[len(chunk):]
decompressor.flush()
self.assertEqual(len(data), 0)
def test_concatenation(self):
data1 = os.urandom(snappy.hadoop_snappy._CHUNK_MAX * 2)
data2 = os.urandom(4096)
decompressor = snappy.hadoop_snappy.StreamDecompressor()
self.assertEqual(
decompressor.decompress(
snappy.hadoop_snappy.StreamCompressor().compress(data1) +
snappy.hadoop_snappy.StreamCompressor().compress(data2)),
data1 + data2)
if __name__ == "__main__":
import unittest
unittest.main()