Skip to content

Commit

Permalink
Merge pull request #5 from sidney/handle-multiple-compressed-per-orig…
Browse files Browse the repository at this point in the history
…inal-in-test

Handle multiple compressed versions of files in tests
  • Loading branch information
sidney authored Feb 3, 2021
2 parents 0ce299b + 0c31d4a commit d5c1314
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 3,205 deletions.
7 changes: 5 additions & 2 deletions test/_test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
TESTDATA_PATHS = [os.path.join(TESTDATA_DIR, f) for f in TESTDATA_FILES]

TESTDATA_PATHS_FOR_DECOMPRESSION = glob.glob(
os.path.join(TESTDATA_DIR, '*.compressed'))
os.path.join(TESTDATA_DIR, '*.compressed*'))

TEMP_DIR = tempfile.mkdtemp()

Expand All @@ -45,6 +45,8 @@ def get_temp_compressed_name(filename):
def get_temp_uncompressed_name(filename):
return os.path.join(TEMP_DIR, os.path.basename(filename + '.unbro'))

def get_uncompressed_name(test_data):
return test_data.rsplit('.compressed')[0]

def bind_method_args(method, *args, **kwargs):
return lambda self: method(self, *args, **kwargs)
Expand All @@ -69,7 +71,8 @@ def generate_test_methods(test_case_class,
for method in [m for m in dir(test_case_class) if m.startswith('_test')]:
for testdata in paths:
for (opts_name, opts_dict) in opts:
f = os.path.splitext(os.path.basename(testdata))[0]
f_split = os.path.basename(testdata).rsplit('.compressed')
f = f_split[0]+f_split[1].replace('.', '_')
name = 'test_{method}_{options}_{file}'.format(
method=method, options=opts_name, file=f)
func = bind_method_args(
Expand Down
43 changes: 16 additions & 27 deletions test/decompress_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,18 @@

import unittest
import os
import sys
import timeit
import _test_utils
from brotlidecpy import decompress
from brotli import decompress as brotlidecompress, compress
# This is a brotli python library used for test comparisons that requires Python 3. Skip those tests if running Python 2
if sys.version_info[0] > 2:
from lib.brotlipython import brotlidec


def _get_original_name(test_data):
return test_data.split('.compressed')[0]
from brotli import decompress as brotlidecompress


class TestDecompress(_test_utils.TestCase):

def _test_decompress(self, test_data):
"""This performs the same decompression tests as in the Python bindings of brotli reference implementation"""
temp_uncompressed = _test_utils.get_temp_uncompressed_name(test_data)
original = _get_original_name(test_data)
original = _test_utils.get_uncompressed_name(test_data)
with open(temp_uncompressed, 'wb') as out_file:
with open(test_data, 'rb') as in_file:
out_file.write(decompress(in_file.read()))
Expand All @@ -33,36 +25,33 @@ def _test_brotli_decompress_buffer(self, test_data):
"""This tests that in memory buffer to buffer decompression of test data gets expected results"""
with open(test_data, 'rb') as f:
compressed_buffer = f.read()
with open(os.path.splitext(test_data)[0], 'rb') as f:
with open(_test_utils.get_uncompressed_name(test_data), 'rb') as f:
uncompressed_buffer = f.read()
result_buffer = decompress(compressed_buffer)
self.assertSequenceEqual(uncompressed_buffer, result_buffer, "Failed decompress of %s" %
os.path.splitext(os.path.basename(test_data))[0])
os.path.basename(test_data))

def _test_against_pylib_brotli(self, test_data):
"""This confirms that this package decompresses same as two other brotli packages, the C reference
implementation that is in PyPI and a slow port of the Rust implementation.
It also prints execution times to serve as a performance test, even though unit tests are usually for that"""
with open(os.path.splitext(test_data)[0], 'rb') as f:
"""This confirms that this package decompresses same as the C reference implementation that is in PyPI.
It also prints execution times to serve as a performance test, though unit tests are not usually for that"""
with open(_test_utils.get_uncompressed_name(test_data), 'rb') as f:
original_uncompressed_buffer = f.read()
compressed_buffer = compress(original_uncompressed_buffer)
with open(test_data, 'rb') as f:
compressed_buffer = f.read()
ref_time = timeit.default_timer()
ref_uncompressed_buffer = brotlidecompress(compressed_buffer) # using fast brotli library
ref_time = timeit.default_timer() - ref_time
brotlipy_time = timeit.default_timer()
if sys.version_info[0] > 2: # The brotlipython library only runs in Python 3, don't test against it if in 2
brotlipy_uncompressed_buffer = brotlidec(compressed_buffer, []) # using brotlipython slow Python
brotlipy_time = timeit.default_timer() - brotlipy_time
self.assertSequenceEqual(ref_uncompressed_buffer, brotlipy_uncompressed_buffer)
test_time = timeit.default_timer()
test_uncompressed_buffer = decompress(compressed_buffer) # testing this package, should be intermediate time
test_time = timeit.default_timer() - test_time
self.assertSequenceEqual(ref_uncompressed_buffer, test_uncompressed_buffer)
self.assertSequenceEqual(original_uncompressed_buffer, test_uncompressed_buffer)
print("File '%s' Times msec C ref: %.3f, brotlipy: %.3f, this test: %.3f" %
(os.path.splitext(os.path.basename(test_data))[0],
self.assertSequenceEqual(ref_uncompressed_buffer, original_uncompressed_buffer,
msg="Something wrong with test:"
" Reference decompress does not match uncompressed test data file")
self.assertSequenceEqual(original_uncompressed_buffer, test_uncompressed_buffer,
msg="Test failure in decompress of %s" % os.path.basename(test_data))
print("File '%s' Times msec C ref: %.3f, this test: %.3f" %
(os.path.basename(test_data),
ref_time * 1000,
brotlipy_time * 1000 if (sys.version_info[0] > 2) else float('nan'),
test_time * 1000))


Expand Down
Empty file removed test/lib/__init__.py
Empty file.
Loading

0 comments on commit d5c1314

Please sign in to comment.