def images(draw): width = draw(Indices) height = draw(Indices) assume(width * height > 0) result = Image(width, height) for i, j, v in draw( st.lists(st.tuples(Indices, Indices, Pixels), average_size=(width * height / 2))): if i < width and j < height: result[i, j] = v return result
def test_flood_fill_partition(): data = [ [0, 0, 0], [1, 1, 1], [0, 0, 0], ] img = Image.from_data(data) flood_fill(img, 0, 0, 5) assert img.to_data() == [ [5, 5, 5], [1, 1, 1], [0, 0, 0], ]
def test_flood_fill_enclosure(): # Image has a ring of colour 1, one pixel in from the border img = Image(10, 10) for i in range(1, 9): img[i, 1] = 1 img[1, i] = 1 img[9 - i, 8] = 1 img[8, 9 - i] = 1 # Flood filling the outside shouldn't touch the inside flood_fill(img, 0, 0, 10) assert img[0, 0] == 10 assert img[9, 9] == 10 assert img[5, 5] == 0 # Flood filling the inside shouldn't touch the outside flood_fill(img, 5, 5, 3) assert img[4, 4] == 3 assert img[0, 0] == 10 assert img[9, 9] == 10
def test_empty_image(): x = Image(0, 0) assert x.size == (0, 0) assert x.to_data() == []
def test_from_empty_image(): assert Image.from_data([]).size == (0, 0)
def test_flood_fill_fills_everything(): img = Image(10, 10) flood_fill(img, 0, 0, 1) assert img.to_data() == [[1] * 10] * 10
def test_indices_must_be_in_bounds(x, y): img = Image(5, 5) with pytest.raises(IndexError): img[x, y]
def test_only_eight_bit_image(): img = Image(10, 10) with pytest.raises(ValueError): img[1, 1] = 257