From d7e30321ae6ae4c82a8ab7455f6ce33afd719c67 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/__init__.py | 0 aoc/tests/test_datastructures.py | 112 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 112 insertions(+) create mode 100644 aoc/tests/__init__.py create mode 100644 aoc/tests/test_datastructures.py (limited to 'aoc/tests') diff --git a/aoc/tests/__init__.py b/aoc/tests/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/aoc/tests/test_datastructures.py b/aoc/tests/test_datastructures.py new file mode 100644 index 0000000..1c291cc --- /dev/null +++ b/aoc/tests/test_datastructures.py @@ -0,0 +1,112 @@ +# -*- 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