Ejemplo n.º 1
0
def test_to_number_grid():
    assert Parser.from_lines(["123", "456"]).to_number_grid() == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
    assert Parser.from_lines(["1 2 3", "4 5 6"]).to_number_grid(separator=None) == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
    assert Parser.from_lines(["1, 2, 3", "4, 5, 6"]).to_number_grid(separator=",") == \
           Grid.from_values([[1, 2, 3], [4, 5, 6]])
Ejemplo n.º 2
0
def test_data_class_features(test_grid: Grid[int]):
    # Grids should be equal if their data is equal
    assert test_grid == Grid.from_values([[1, 2, 3], [4, 5, 6]])

    # Normal Grid cannot be hashed, since it's mutable
    with pytest.raises(TypeError):
        hash(test_grid)
    # But FrozenGrid can be hashed (still mutable, but at your own risk)
    hash(test_grid.freeze())
Ejemplo n.º 3
0
def test_to_directed_weighted_graph(test_grid: Grid[int]):
    graph_not_diagonal = test_grid.to_directed_weighted_graph(include_diagonal=False)
    assert sorted(graph_not_diagonal.get_nodes()) == [0, 1, 2, 3, 4, 5]
    assert graph_not_diagonal.get_edges() == [
        ((0, 1), 2), ((0, 3), 4), ((1, 0), 1), ((1, 2), 3), ((1, 4), 5),
        ((2, 1), 2), ((2, 5), 6), ((3, 0), 1), ((3, 4), 5),
        ((4, 1), 2), ((4, 3), 4), ((4, 5), 6), ((5, 2), 3), ((5, 4), 5)
    ]

    graph_diagonal = test_grid.to_directed_weighted_graph(include_diagonal=True)
    assert sorted(graph_diagonal.get_nodes()) == [0, 1, 2, 3, 4, 5]
    assert graph_diagonal.get_edges() == [
        ((0, 1), 2), ((0, 3), 4), ((0, 4), 5), ((1, 0), 1), ((1, 2), 3), ((1, 3), 4), ((1, 4), 5), ((1, 5), 6),
        ((2, 1), 2), ((2, 4), 5), ((2, 5), 6), ((3, 0), 1), ((3, 1), 2), ((3, 4), 5),
        ((4, 0), 1), ((4, 1), 2), ((4, 2), 3), ((4, 3), 4), ((4, 5), 6), ((5, 1), 2), ((5, 2), 3), ((5, 4), 5)
    ]

    with pytest.raises(ValueError):
        Grid.from_values([["a", "b"]]).to_directed_weighted_graph(include_diagonal=False)
Ejemplo n.º 4
0
    def to_number_grid(self, separator: Optional[str] = "") -> Grid[int]:
        def split_line(line: str) -> List[str]:
            if separator == "":
                # Empty string means: no separator, take character by character
                return [char for char in line]
            else:
                # None means: default of split, which is any whitespace
                # Any other value means: use this specific separator to split.
                return line.split(separator)

        return Grid.from_values(
            [[int(word.strip()) for word in split_line(line)]
             for line in self.lines])
Ejemplo n.º 5
0
 def to_character_grid(self) -> Grid[str]:
     return Grid.from_values([[character for character in line]
                              for line in self.lines])
Ejemplo n.º 6
0
def test_to_character_grid():
    assert Parser.from_lines(["..#",
                              "#.#"]).to_character_grid() == Grid.from_values(
                                  [[".", ".", "#"], ["#", ".", "#"]])
Ejemplo n.º 7
0
def test_grid() -> Grid[int]:
    return Grid.from_values([[1, 2, 3], [4, 5, 6]])
Ejemplo n.º 8
0
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)]
Ejemplo n.º 9
0
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")]]
Ejemplo n.º 10
0
def test_validate():
    with pytest.raises(ValueError):
        Grid.from_values([])
    with pytest.raises(ValueError):
        Grid.from_values([[1, 2], [3]])
Ejemplo n.º 11
0
def bit_test_grid() -> Grid[int]:
    return Grid.from_values([[1, 1, 1, 1], [0, 1, 0, 0], [1, 1, 0, 0]])
Ejemplo n.º 12
0
def test_count_value(test_grid: Grid[int]):
    grid = Grid.from_values([[1, 1, 0, 1], [1, 0, 1, 0]])
    assert grid.count_value(0) == 3
    assert grid.count_value(1) == 5
    assert grid.count_value(2) == 0