|
| 1 | +#!/usr/bin/python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +from __future__ import ( |
| 4 | + absolute_import, division, print_function, unicode_literals |
| 5 | +) |
| 6 | + |
| 7 | +import random |
| 8 | +import sys |
| 9 | + |
| 10 | +from basest.core import best_ratio, decode_raw, encode_raw |
| 11 | + |
| 12 | + |
| 13 | +def test_partial_input_with_larger_input_bases(): |
| 14 | + # input bases in range 3..256 |
| 15 | + for input_base in range(3, 256 + 1): |
| 16 | + # output bases in range 2..256 |
| 17 | + for output_base in range(2, 256 + 1): |
| 18 | + # only continue if output base is not larger than input base |
| 19 | + if not (output_base > input_base): |
| 20 | + # get an encoding ratio to use |
| 21 | + ratio = best_ratio( |
| 22 | + input_base, |
| 23 | + [output_base], |
| 24 | + range(1, 10 + 1) |
| 25 | + )[1] |
| 26 | + # explore the whole input window |
| 27 | + for input_window in range(1, ratio[0] + 1): |
| 28 | + ''' |
| 29 | + generate some random data, as many items as the partial |
| 30 | + window input size that we're exploring |
| 31 | + ''' |
| 32 | + input_data = [ |
| 33 | + random.randint(0, input_base - 1) |
| 34 | + for _ in range(input_window) |
| 35 | + ] |
| 36 | + # encode the data |
| 37 | + encoded_data = encode_raw( |
| 38 | + input_base, output_base, |
| 39 | + ratio[0], ratio[1], |
| 40 | + input_data |
| 41 | + ) |
| 42 | + # decode the data |
| 43 | + decoded_data = decode_raw( |
| 44 | + output_base, input_base, |
| 45 | + ratio[1], ratio[0], |
| 46 | + encoded_data |
| 47 | + ) |
| 48 | + # check what we got back is the same as the original |
| 49 | + assert decoded_data == input_data |
| 50 | + |
| 51 | + |
| 52 | +if __name__ == '__main__': |
| 53 | + for test_function in [ |
| 54 | + test_partial_input_with_larger_input_bases |
| 55 | + ]: |
| 56 | + print("Running '{}'".format(test_function.__name__), end='...') |
| 57 | + sys.stdout.flush() |
| 58 | + test_function() |
| 59 | + print('[DONE]') |
0 commit comments