def test_copy(test_grid: Grid[int]): assert test_grid.copy().rows == [ [Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3)], [Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)] ] assert not test_grid.copy() is test_grid assert not test_grid.copy() is test_grid.copy()
def test_get_local_area_cells(test_grid: Grid[int]): assert test_grid.get_local_area_cells(Coordinate(0, 1), lambda cell1, cell2: cell1.value < cell2.value, include_diagonal=False) == [ Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_local_area_cells(Coordinate(1, 1), lambda cell1, cell2: abs(cell1.value - cell2.value) == 4, include_diagonal=True) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(1, 1), 5) ]
def test_map_values_by_dict(test_grid: Grid[int]): assert test_grid.map_values_by_dict({1: 11, 3: 33, 4: 44}) == Grid([ [Cell(Coordinate(0, 0), 11), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 33)], [Cell(Coordinate(1, 0), 44), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)] ])
def test_map_values_by_function(test_grid: Grid[int]): assert test_grid.map_values_by_function(lambda value: value + 1) == Grid([ [Cell(Coordinate(0, 0), 2), Cell(Coordinate(0, 1), 3), Cell(Coordinate(0, 2), 4)], [Cell(Coordinate(1, 0), 5), Cell(Coordinate(1, 1), 6), Cell(Coordinate(1, 2), 7)] ])
def test_from_values(): assert Grid.from_values([[0, 1]]).rows == [[Cell(Coordinate(0, 0), 0), Cell(Coordinate(0, 1), 1)]] assert Grid.from_values([["a", "b"]]).rows == [[Cell(Coordinate(0, 0), "a"), Cell(Coordinate(0, 1), "b")]]
def test_flip_vertical(test_grid: Grid[int]): assert test_grid.flip_vertical() == Grid([ [Cell(Coordinate(0, 0), 4), Cell(Coordinate(0, 1), 5), Cell(Coordinate(0, 2), 6)], [Cell(Coordinate(1, 0), 1), Cell(Coordinate(1, 1), 2), Cell(Coordinate(1, 2), 3)] ])
def test_get_column_cells(test_grid: Grid[int]): assert test_grid.get_column_cells(0) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(1, 0), 4) ]
def test_get_cells_by_coords(test_grid: Grid[int]): assert test_grid.get_cells_by_coords([Coordinate(0, 1), Coordinate(1, 2)]) == [ Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 2), 6) ]
def test_get_cell_by_index(test_grid: Grid[int]): assert test_grid.get_cell_by_index(1, 2) == Cell(Coordinate(1, 2), 6)
def test_find_cells_by_value(test_grid: Grid[int]): assert test_grid.find_cells_by_value(1) == [Cell(Coordinate(0, 0), 1)] assert test_grid.find_cells_by_value([2, 4]) == [Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4)]
def test_find_cells_by_predicate_on_value(test_grid: Grid[int]): assert test_grid.find_cells_by_predicate_on_value(lambda value: value % 2 == 0) == \ [Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 2), 6)]
def test_find_cells_by_predicate_on_coord(test_grid: Grid[int]): assert test_grid.find_cells_by_predicate_on_coord(lambda coord: coord.row + coord.column == 2) == \ [Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5)]
def test_find_cells_by_predicate_on_cell(test_grid: Grid[int]): assert test_grid.find_cells_by_predicate_on_cell(lambda cell: cell.value >= 3 and cell.coord.column >= 1) == \ [Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]
def test_get_neighbor_cells(test_grid: Grid[int]): assert test_grid.get_neighbor_cells(Coordinate(0, 1), include_diagonal=False) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5) ] assert test_grid.get_neighbor_cells(Coordinate(0, 1), include_diagonal=True) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(0, 1), include_diagonal=True, include_own_cell=True) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(1, 1), include_diagonal=False) == [ Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(1, 1), include_diagonal=True) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(1, 1), include_diagonal=True, include_own_cell=True) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(0, 0), include_diagonal=False) == [ Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 0), 4) ] assert test_grid.get_neighbor_cells(Coordinate(0, 2), include_diagonal=False) == [ Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 2), 6) ] assert test_grid.get_neighbor_cells(Coordinate(1, 0), include_diagonal=False) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(1, 1), 5) ] assert test_grid.get_neighbor_cells(Coordinate(1, 2), include_diagonal=False) == [ Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 1), 5) ]
def test_get_rows_and_columns_cells(test_grid: Grid[int]): assert test_grid.get_rows_and_columns_cells() == [ [Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3)], [Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)], [Cell(Coordinate(0, 0), 1), Cell(Coordinate(1, 0), 4)], [Cell(Coordinate(0, 1), 2), Cell(Coordinate(1, 1), 5)], [Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 2), 6)] ]
def test_map_cells_by_function(test_grid: Grid[int]): assert test_grid.map_cells_by_function(lambda cell: cell.value + cell.coord.row + cell.coord.column) == Grid([ [Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 3), Cell(Coordinate(0, 2), 5)], [Cell(Coordinate(1, 0), 5), Cell(Coordinate(1, 1), 7), Cell(Coordinate(1, 2), 9)] ])
def test_get_item(test_grid: Grid[int]): assert Grid.from_values([[0, 1]])[0] == [Cell(Coordinate(0, 0), 0), Cell(Coordinate(0, 1), 1)]
def test_filter_rows(test_grid: Grid[int]): assert test_grid.filter_rows(lambda row: sum(cell.value for cell in row) > 8) == \ Grid([[Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6)]])
def test_get_cell_by_coord(test_grid: Grid[int]): assert test_grid.get_cell_by_coord(Coordinate(0, 1)) == Cell(Coordinate(0, 1), 2)
def test_rotate_left(test_grid: Grid[int]): assert test_grid.rotate_left(1) == Grid([ [Cell(Coordinate(0, 0), 3), Cell(Coordinate(0, 1), 6)], [Cell(Coordinate(1, 0), 2), Cell(Coordinate(1, 1), 5)], [Cell(Coordinate(2, 0), 1), Cell(Coordinate(2, 1), 4)] ])
def test_get_all_cells(test_grid: Grid[int]): assert test_grid.get_all_cells() == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3), Cell(Coordinate(1, 0), 4), Cell(Coordinate(1, 1), 5), Cell(Coordinate(1, 2), 6) ]
def test_get_row_cells(test_grid: Grid[int]): assert test_grid.get_row_cells(0) == [ Cell(Coordinate(0, 0), 1), Cell(Coordinate(0, 1), 2), Cell(Coordinate(0, 2), 3) ]