# 32 unit tests for path function

closes #32
This commit is contained in:
Ian
2019-03-18 15:26:24 -04:00
parent b4c50e2aea
commit 6ee49008c0
2 changed files with 201 additions and 18 deletions

View File

@@ -11,15 +11,19 @@ def scanHorizontal(image, rTool):
for line in range(height): for line in range(height):
for column in range(width): for column in range(width):
if image[line][column] == 1 and image[line][column - 1] != 1: if column > 0:
for px in range(2 * rTool + 1): if image[line][column] == 1 and image[line][column - 1] != 1:
if image[line - rTool + px][column - rTool] == 0: for px in range(2 * rTool + 1):
image[line - rTool + px][column - rTool] = 2 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: if column < width - 1:
for px in range(2 * rTool + 1): if image[line][column] == 1 and image[line][column + 1] != 1:
if image[line - rTool + px][column + rTool] == 0: for px in range(2 * rTool + 1):
image[line - rTool + px][column + rTool] = 2 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 return image
@@ -37,15 +41,19 @@ def scanVertical(image, rTool):
for line in range(height): for line in range(height):
for column in range(width): for column in range(width):
if image[line][column] == 1 and image[line - 1][column] != 1: if line > 0:
for px in range(2 * rTool + 1): if image[line][column] == 1 and image[line - 1][column] != 1:
if image[line - rTool][column - rTool + px] == 0: for px in range(2 * rTool + 1):
image[line - rTool][column - rTool + px] = 2 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: if line < height - 1:
for px in range(2 * rTool + 1): if image[line][column] == 1 and image[line + 1][column] != 1:
if image[line + rTool][column - rTool + px] == 0: for px in range(2 * rTool + 1):
image[line + rTool][column - rTool + px] = 2 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 return image
@@ -65,8 +73,9 @@ def twoRemoving(image, rTool):
if image[line][column] == 1: if image[line][column] == 1:
for pixelx in range(1, 2 * rTool): for pixelx in range(1, 2 * rTool):
for pixely in range(1, 2*rTool): for pixely in range(1, 2*rTool):
if image[line - rTool + pixelx][column - rTool + pixely] == 2: if 0 <= line - rTool + pixelx < height and 0 <= column - rTool + pixely < width:
image[line - rTool + pixelx][column - rTool + pixely] = 0 if image[line - rTool + pixelx][column - rTool + pixely] == 2:
image[line - rTool + pixelx][column - rTool + pixely] = 0
print("image width = " + str(width)) print("image width = " + str(width))
print("image height = " + str(height)) print("image height = " + str(height))

View File

@@ -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())