def test_bit_matrix_writer_write_success(num_cols: int, byte_values: List[int]): num_rows = math.floor(len(byte_values) * 8 / num_cols) bit_array = bitarray() bit_arrays = [] with NamedTemporaryFile() as tmp_for_write, open(tmp_for_write.name, "rb") as tmp_for_read: tmp_for_write.write(bytes(byte_values)) tmp_for_write.flush() bmr = BitMatrixReader(tmp_for_read, num_rows, num_cols) for row in bmr: bit_array.extend(row) bit_arrays.append(row) with NamedTemporaryFile() as expected_for_write, open( expected_for_write.name, "rb") as expected_for_read: bit_array.tofile(expected_for_write) expected_for_write.flush() with NamedTemporaryFile() as result_for_write: with BitMatrixWriter(result_for_write, num_rows, num_cols) as bmw: for bit_array in bit_arrays: bmw.write(bit_array) with open(result_for_write.name, "rb") as result_for_read: assert result_for_read.read() == expected_for_read.read()
def test_bit_matrix_reader_creation_success(num_rows: int, num_cols: int): assume(num_rows * num_cols <= 8) with NamedTemporaryFile() as tmp, open(tmp.name, "rb") as infile: tmp.write(bytes(1)) tmp.flush() BitMatrixReader(infile, num_rows, num_cols)
def test_bit_matrix_reader_creation_failure(num_rows: int, num_cols: int): assume(num_rows * num_cols > 8 or num_rows * num_cols == 0) with NamedTemporaryFile() as tmp, open( tmp.name, "rb") as infile, pytest.raises(Exception): tmp.write(bytes(1)) tmp.flush() BitMatrixReader(infile, num_rows, num_cols)
def __enter__(self): self._bit_matrix_files = [] self._bit_matrix_readers = [] for input_path, num_cols in self._input_data: infile = open(input_path, "rb") self._bit_matrix_files.append(infile) self._bit_matrix_readers.append( BitMatrixReader(infile, self._num_rows, num_cols)) return self
def test_bit_matrix_reader_raise_exception_past_iteration( num_cols: int, byte_values: List[int]): num_rows = math.floor(len(byte_values) * 8 / num_cols) with NamedTemporaryFile() as tmp, open(tmp.name, "rb") as infile: tmp.write(bytes(byte_values)) tmp.flush() bmr = BitMatrixReader(infile, num_rows, num_cols) for _ in bmr: pass with pytest.raises(StopIteration): next(bmr)
def test_bit_matrix_writer_write_failure_past_iteration( num_cols: int, byte_values: List[int]): num_rows = math.floor(len(byte_values) * 8 / num_cols) bit_arrays = [] with NamedTemporaryFile() as tmp1, open(tmp1.name, "rb") as infile: tmp1.write(bytes(byte_values)) tmp1.flush() bmr = BitMatrixReader(infile, num_rows, num_cols) for row in bmr: bit_arrays.append(row) with NamedTemporaryFile() as tmp2, BitMatrixWriter(tmp2, num_rows, num_cols) as bmw: for bit_array in bit_arrays: bmw.write(bit_array) with pytest.raises(Exception): bmw.write(bitarray(1))
def test_bit_matrix_reader_iteration_success(num_cols: int, byte_values: List[int]): num_rows = math.floor(len(byte_values) * 8 / num_cols) bit_array = bitarray() bit_array.frombytes(bytes(byte_values)) expected = [] for row_index in range(num_rows): expected.append(bit_array[row_index * num_cols:(row_index + 1) * num_cols]) with NamedTemporaryFile() as tmp, open(tmp.name, "rb") as infile: tmp.write(bytes(byte_values)) tmp.flush() bmr = BitMatrixReader(infile, num_rows, num_cols) result = [row for row in bmr] assert result == expected