class RGBAdapterTests(unittest.TestCase): def setUp(self): self.rows = 3 self.columns = 3 self.adapter = RGBAdapter(self.columns, self.rows) self.data = SparseMatrix(self.rows, self.columns) for row in range(self.rows): for column in range(self.columns): self.data[row, column] = 1 self.r = SparseMatrix(self.rows, self.columns) self.r.merge(self.data, row_offset = -1, column_offset = -1) self.g = SparseMatrix(self.rows, self.columns) self.g.merge(self.data, row_offset = -1, column_offset = 1) self.b = SparseMatrix(self.rows, self.columns) self.b.merge(self.data, row_offset = 1) self.reference = Image.new('RGB', (self.columns, self.rows)) draw = ImageDraw.Draw(self.reference) draw.point((0, 0), (255, 0, 0)) draw.point((1, 0), (255, 255, 0)) draw.point((2, 0), ( 0, 255, 0)) draw.point((0, 1), (255, 0, 255)) draw.point((1, 1), (255, 255, 255)) draw.point((2, 1), ( 0, 255, 255)) draw.point((0, 2), ( 0, 0, 255)) draw.point((1, 2), ( 0, 0, 255)) draw.point((2, 2), ( 0, 0, 255)) def test_update(self): self.adapter.update(self.r, self.g, self.b) for column in range(self.columns): for row in range(self.rows): with self.subTest(row = row, column = column): self.assertEqual(self.adapter.get_image() .getpixel((row, column)), self.reference.getpixel((row, column))) def test_save(self): filename = '/tmp/adapter.png' self.adapter.update(self.r, self.g, self.b).save(filename) image = Image.open(filename) for column in range(self.columns): for row in range(self.rows): with self.subTest(row = row, column = column): self.assertEqual(image.getpixel((row, column)), self.reference.getpixel((row, column)))
class SparseMatrixOperationsTest(unittest.TestCase): def setUp(self): self.rows = 2 self.columns = 2 self.matrix = SparseMatrix(self.rows, self.columns) self.other = SparseMatrix(self.rows, self.columns) self.other[0, 0] = 1 self.other[0, 1] = 2 self.other[1, 0] = 3 self.other[1, 1] = 4 def test_merge(self): self.matrix.merge(self.other) for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): self.assertEqual(self.matrix[row, column], self.other[row, column]) def test_merge_row_offset(self): self.matrix.merge(self.other, row_offset=1) for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): if row == 0: self.assertEqual(self.matrix[row, column], 0) else: self.assertEqual(self.matrix[row, column], self.other[row - 1, column]) self.assertEqual(len(self.matrix.entries), 2) def test_merge_column_offset(self): self.matrix.merge(self.other, column_offset=1) for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): if column == 0: self.assertEqual(self.matrix[row, column], 0) else: self.assertEqual(self.matrix[row, column], self.other[row, column - 1]) self.assertEqual(len(self.matrix.entries), 2) def test_merge_row_offset_negative(self): self.matrix.merge(self.other, row_offset=-1) for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): if row == 1: self.assertEqual(self.matrix[row, column], 0) else: self.assertEqual(self.matrix[row, column], self.other[row + 1, column]) self.assertEqual(len(self.matrix.entries), 2) def test_merge_column_offset_negative(self): self.matrix.merge(self.other, column_offset=-1) for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): if column == 1: self.assertEqual(self.matrix[row, column], 0) else: self.assertEqual(self.matrix[row, column], self.other[row, column + 1]) self.assertEqual(len(self.matrix.entries), 2) def test_add(self): result = self.matrix + self.other for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): self.assertEqual(result[row, column], self.matrix[row, column] \ + self.other[row, column]) def test_inplace_add(self): original = SparseMatrix(self.rows, self.columns) original.add_entries(self.matrix.entries) self.matrix += self.other for row in range(self.rows): for column in range(self.columns): with self.subTest(row=row, column=column): self.assertEqual(self.matrix[row, column], original[row, column] \ + self.other[row, column])