From 6ee49008c0bb6c7618d8c1a52dea069c959ca05b Mon Sep 17 00:00:00 2001 From: Ian Date: Mon, 18 Mar 2019 15:26:24 -0400 Subject: [PATCH] # 32 unit tests for path function closes #32 --- pcbdevice/gcode/path.py | 45 +++++--- pcbdevice/tests/gcode/test_path.py | 174 +++++++++++++++++++++++++++++ 2 files changed, 201 insertions(+), 18 deletions(-) create mode 100644 pcbdevice/tests/gcode/test_path.py diff --git a/pcbdevice/gcode/path.py b/pcbdevice/gcode/path.py index ce8f1c4..53ed7c1 100644 --- a/pcbdevice/gcode/path.py +++ b/pcbdevice/gcode/path.py @@ -11,15 +11,19 @@ def scanHorizontal(image, rTool): for line in range(height): for column in range(width): - if image[line][column] == 1 and image[line][column - 1] != 1: - for px in range(2 * rTool + 1): - if image[line - rTool + px][column - rTool] == 0: - image[line - rTool + px][column - rTool] = 2 + if column > 0: + if image[line][column] == 1 and image[line][column - 1] != 1: + for px in range(2 * rTool + 1): + if column - rTool >= 0 and 0 <= line - rTool + px < height: + if image[line - rTool + px][column - rTool] == 0: + image[line - rTool + px][column - rTool] = 2 - if image[line][column] == 1 and image[line][column + 1] != 1: - for px in range(2 * rTool + 1): - if image[line - rTool + px][column + rTool] == 0: - image[line - rTool + px][column + rTool] = 2 + if column < width - 1: + if image[line][column] == 1 and image[line][column + 1] != 1: + for px in range(2 * rTool + 1): + if column + rTool < width and 0 <= line - rTool + px < height: + if image[line - rTool + px][column + rTool] == 0: + image[line - rTool + px][column + rTool] = 2 return image @@ -37,15 +41,19 @@ def scanVertical(image, rTool): for line in range(height): for column in range(width): - if image[line][column] == 1 and image[line - 1][column] != 1: - for px in range(2 * rTool + 1): - if image[line - rTool][column - rTool + px] == 0: - image[line - rTool][column - rTool + px] = 2 + if line > 0: + if image[line][column] == 1 and image[line - 1][column] != 1: + for px in range(2 * rTool + 1): + if line - rTool >= 0 and 0 <= column - rTool + px < width: + if image[line - rTool][column - rTool + px] == 0: + image[line - rTool][column - rTool + px] = 2 - if image[line][column] == 1 and image[line + 1][column] != 1: - for px in range(2 * rTool + 1): - if image[line + rTool][column - rTool + px] == 0: - image[line + rTool][column - rTool + px] = 2 + if line < height - 1: + if image[line][column] == 1 and image[line + 1][column] != 1: + for px in range(2 * rTool + 1): + if line + rTool < height and 0 <= column - rTool + px < width: + if image[line + rTool][column - rTool + px] == 0: + image[line + rTool][column - rTool + px] = 2 return image @@ -65,8 +73,9 @@ def twoRemoving(image, rTool): if image[line][column] == 1: for pixelx in range(1, 2 * rTool): for pixely in range(1, 2*rTool): - if image[line - rTool + pixelx][column - rTool + pixely] == 2: - image[line - rTool + pixelx][column - rTool + pixely] = 0 + if 0 <= line - rTool + pixelx < height and 0 <= column - rTool + pixely < width: + if image[line - rTool + pixelx][column - rTool + pixely] == 2: + image[line - rTool + pixelx][column - rTool + pixely] = 0 print("image width = " + str(width)) print("image height = " + str(height)) diff --git a/pcbdevice/tests/gcode/test_path.py b/pcbdevice/tests/gcode/test_path.py new file mode 100644 index 0000000..5ff8817 --- /dev/null +++ b/pcbdevice/tests/gcode/test_path.py @@ -0,0 +1,174 @@ +from unittest import TestCase + +from pcbdevice.gcode.path import scanHorizontal, scanVertical, twoRemoving + + +class TestPath(TestCase): + + #inputs + def imageTest(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageTestMulti(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 1, 0, 0, 1, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 1, 0, 0]] + + def imageTestHOut(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageTestVOut(self): + return [[0, 0, 0], + [0, 0, 0], + [0, 0, 0], + [0, 1, 0], + [0, 0, 0], + [0, 0, 0], + [0, 0, 0]] + + def imageTest2(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 2, 2, 1, 2, 2, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageTestMulti2(self): + return [[2, 2, 2, 2, 2, 2, 2], + [2, 2, 2, 2, 2, 2, 2], + [2, 1, 2, 2, 1, 2, 2], + [2, 2, 2, 1, 2, 2, 2], + [2, 2, 2, 2, 2, 2, 2], + [2, 2, 2, 2, 2, 2, 2], + [2, 2, 2, 2, 1, 2, 2]] + + + #horizontal results + def imageResultHr2(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 2, 0, 1, 0, 2, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageResultHr3(self): + return [[2, 0, 0, 0, 0, 0, 2], + [2, 0, 0, 0, 0, 0, 2], + [2, 0, 0, 0, 0, 0, 2], + [2, 0, 0, 1, 0, 0, 2], + [2, 0, 0, 0, 0, 0, 2], + [2, 0, 0, 0, 0, 0, 2], + [2, 0, 0, 0, 0, 0, 2]] + + def imageResultHOut(self): + return [[0, 2, 0, 0, 0, 2, 0], + [0, 2, 0, 1, 0, 2, 0], + [0, 2, 0, 0, 0, 2, 0]] + + def imageResultMultiH(self): + return [[0, 0, 2, 2, 0, 0, 2], + [0, 2, 2, 2, 0, 2, 2], + [0, 1, 2, 2, 1, 2, 2], + [0, 2, 2, 1, 0, 2, 2], + [0, 2, 2, 2, 0, 2, 2], + [0, 2, 2, 0, 0, 2, 2], + [0, 0, 2, 0, 1, 0, 2]] + + + #vertical results + def imageResultVr2(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageResultVr3(self): + return [[2, 2, 2, 2, 2, 2, 2], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [0, 0, 0, 0, 0, 0, 0], + [2, 2, 2, 2, 2, 2, 2]] + + def imageResultVOut(self): + return [[0, 0, 0], + [2, 2, 2], + [0, 0, 0], + [0, 1, 0], + [0, 0, 0], + [2, 2, 2], + [0, 0, 0]] + + def imageResultMultiV(self): + return [[2, 2, 2, 2, 2, 2, 2], + [0, 2, 2, 2, 2, 2, 0], + [0, 1, 0, 0, 1, 0, 0], + [0, 0, 0, 1, 0, 0, 0], + [2, 2, 2, 2, 2, 2, 2], + [0, 2, 2, 2, 2, 2, 0], + [0, 0, 0, 0, 1, 0, 0]] + + #two results + + def imageResult2(self): + return [[0, 0, 0, 0, 0, 0, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 2, 0, 1, 0, 2, 0], + [0, 2, 0, 0, 0, 2, 0], + [0, 2, 2, 2, 2, 2, 0], + [0, 0, 0, 0, 0, 0, 0]] + + def imageResultMulti2(self): + return [[2, 2, 2, 2, 2, 2, 2], + [0, 0, 0, 0, 0, 0, 2], + [0, 1, 0, 0, 1, 0, 2], + [0, 0, 0, 1, 0, 0, 2], + [2, 2, 0, 0, 0, 2, 2], + [2, 2, 2, 0, 0, 0, 2], + [2, 2, 2, 0, 1, 0, 2]] + + + + def test_hori(self): + + self.assertEqual(scanHorizontal(self.imageTest(), 2), self.imageResultHr2()) + self.assertEqual(scanHorizontal(self.imageTest(), 3), self.imageResultHr3()) + self.assertEqual(scanHorizontal(self.imageTestHOut(), 2), self.imageResultHOut()) + self.assertEqual(scanHorizontal(self.imageTest(), 4), self.imageTest()) + self.assertEqual(scanHorizontal(self.imageTestMulti(), 2), self.imageResultMultiH()) + + def test_vert(self): + + self.assertEqual(scanVertical(self.imageTest(), 2), self.imageResultVr2()) + self.assertEqual(scanVertical(self.imageTest(), 3), self.imageResultVr3()) + self.assertEqual(scanVertical(self.imageTestVOut(), 2), self.imageResultVOut()) + self.assertEqual(scanVertical(self.imageTest(), 4), self.imageTest()) + self.assertEqual(scanVertical(self.imageTestMulti(), 2), self.imageResultMultiV()) + + def test_two(self): + + self.assertEqual(twoRemoving(self.imageTest2(), 2), self.imageResult2()) + self.assertEqual(twoRemoving(self.imageTest2(), 3), self.imageTest()) + self.assertEqual(twoRemoving(self.imageTest2(), 7), self.imageTest()) + self.assertEqual(twoRemoving(self.imageTestMulti2(), 2), self.imageResultMulti2())