From 4147da1317c19fa61d6aa265e8370e63231f9207 Mon Sep 17 00:00:00 2001 From: Tom van der Lee Date: Sun, 19 Nov 2023 16:55:03 +0100 Subject: Initial commit --- aoc/tests/test_datastructures.py | 79 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 aoc/tests/test_datastructures.py (limited to 'aoc/tests/test_datastructures.py') diff --git a/aoc/tests/test_datastructures.py b/aoc/tests/test_datastructures.py new file mode 100644 index 0000000..42cc44d --- /dev/null +++ b/aoc/tests/test_datastructures.py @@ -0,0 +1,79 @@ +# -*- coding: utf-8 -*- +from aoc.datastructures import Coordinate, Coordinate3 + + +class TestCoordinate: + def test_addition(self): + c1 = Coordinate(1, 1) + c2 = Coordinate(2, 2) + assert c1 + c2 == Coordinate(3, 3) + + def test_subtraction(self): + c1 = Coordinate(2, 2) + c2 = Coordinate(1, 1) + assert c1 - c2 == Coordinate(1, 1) + + def test_manhattan_distance(self): + c1 = Coordinate(1, 1) + c2 = Coordinate(3, 3) + assert c1.manhattan_distance(c2) == 4 + + def test_polarity(self): + c1 = Coordinate(2, -3) + assert c1.polarity == Coordinate(1, -1) + + def test_neighbours(self): + c = Coordinate(0, 0) + neighbours = {Coordinate(-1, 0), Coordinate(1, 0), Coordinate(0, -1), Coordinate(0, 1), + Coordinate(-1, -1), Coordinate(-1, 1), Coordinate(1, -1), Coordinate(1, 1)} + assert set(c.neighbours()) == neighbours + + def test_neighbours_no_diagonal(self): + c = Coordinate(0, 0) + neighbours_no_diagonal = {Coordinate(-1, 0), Coordinate(1, 0), Coordinate(0, -1), Coordinate(0, 1)} + assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal + + +class TestCoordinate3: + def test_addition(self): + c1 = Coordinate3(1, 1, 1) + c2 = Coordinate3(2, 2, 2) + assert c1 + c2 == Coordinate3(3, 3, 3) + + def test_subtraction(self): + c1 = Coordinate3(2, 2, 2) + c2 = Coordinate3(1, 1, 1) + assert c1 - c2 == Coordinate3(1, 1, 1) + + def test_manhattan_distance(self): + c1 = Coordinate3(1, 1, 1) + c2 = Coordinate3(3, 3, 3) + assert c1.manhattan_distance(c2) == 6 + + def test_polarity(self): + c1 = Coordinate3(2, -3, 0) + assert c1.polarity == Coordinate3(1, -1, 0) + + def test_neighbours(self): + c = Coordinate3(0, 0, 0) + neighbours = { + Coordinate3(-1, -1, -1), Coordinate3(-1, -1, 0), Coordinate3(-1, -1, 1), + Coordinate3(-1, 0, -1), Coordinate3(-1, 0, 0), Coordinate3(-1, 0, 1), + Coordinate3(-1, 1, -1), Coordinate3(-1, 1, 0), Coordinate3(-1, 1, 1), + Coordinate3(0, -1, -1), Coordinate3(0, -1, 0), Coordinate3(0, -1, 1), + Coordinate3(0, 0, -1), Coordinate3(0, 0, 1), + Coordinate3(0, 1, -1), Coordinate3(0, 1, 0), Coordinate3(0, 1, 1), + Coordinate3(1, -1, -1), Coordinate3(1, -1, 0), Coordinate3(1, -1, 1), + Coordinate3(1, 0, -1), Coordinate3(1, 0, 0), Coordinate3(1, 0, 1), + Coordinate3(1, 1, -1), Coordinate3(1, 1, 0), Coordinate3(1, 1, 1) + } + assert set(c.neighbours()) == neighbours + + def test_neighbours_no_diagonal(self): + c = Coordinate3(0, 0, 0) + neighbours_no_diagonal = { + Coordinate3(-1, 0, 0), Coordinate3(1, 0, 0), + Coordinate3(0, -1, 0), Coordinate3(0, 1, 0), + Coordinate3(0, 0, -1), Coordinate3(0, 0, 1) + } + assert set(c.neighbours(no_diagonal=True)) == neighbours_no_diagonal -- cgit v1.2.3