summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjaseg <git@jaseg.de>2021-12-30 22:20:51 +0100
committerjaseg <git@jaseg.de>2021-12-30 22:20:51 +0100
commit9db91239ea0eac21f38435c1388f86f94f9ce8f3 (patch)
treec888dc9cdfdbde9e1a4f59976424689880400ddf
parentf4b2e74923cc95c683cd7f5c4732d92e4aafd3ba (diff)
downloadgerbonara-9db91239ea0eac21f38435c1388f86f94f9ce8f3.tar.gz
gerbonara-9db91239ea0eac21f38435c1388f86f94f9ce8f3.tar.bz2
gerbonara-9db91239ea0eac21f38435c1388f86f94f9ce8f3.zip
Fix more tests
-rw-r--r--gerbonara/gerber/tests/conftest.py13
-rw-r--r--gerbonara/gerber/tests/image_support.py33
-rw-r--r--gerbonara/gerber/tests/test_rs274x.py32
3 files changed, 59 insertions, 19 deletions
diff --git a/gerbonara/gerber/tests/conftest.py b/gerbonara/gerber/tests/conftest.py
index c6a1221..131ca28 100644
--- a/gerbonara/gerber/tests/conftest.py
+++ b/gerbonara/gerber/tests/conftest.py
@@ -1,4 +1,6 @@
+from pathlib import Path
+
import pytest
from .image_support import ImageDifference
@@ -8,7 +10,8 @@ def pytest_assertrepr_compare(op, left, right):
diff = left if isinstance(left, ImageDifference) else right
return [
f'Image difference assertion failed.',
- f' Calculated difference: {diff}', ]
+ f' Calculated difference: {diff}',
+ f' Histogram: {diff.histogram}', ]
# store report in node object so tmp_gbr can determine if the test failed.
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
@@ -17,3 +20,11 @@ def pytest_runtest_makereport(item, call):
rep = outcome.get_result()
setattr(item, f'rep_{rep.when}', rep)
+fail_dir = Path('gerbonara_test_failures')
+def pytest_sessionstart(session):
+ if not hasattr(session.config, 'workerinput'): # on worker
+ return
+
+ # on coordinator
+ for f in chain(fail_dir.glob('*.gbr'), fail_dir.glob('*.png')):
+ f.unlink()
diff --git a/gerbonara/gerber/tests/image_support.py b/gerbonara/gerber/tests/image_support.py
index 49217c2..96bc357 100644
--- a/gerbonara/gerber/tests/image_support.py
+++ b/gerbonara/gerber/tests/image_support.py
@@ -10,9 +10,11 @@ from contextlib import contextmanager
import numpy as np
from PIL import Image
+@total_ordering
class ImageDifference:
- def __init__(self, value):
+ def __init__(self, value, histogram):
self.value = value
+ self.histogram = histogram
def __float__(self):
return float(self.value)
@@ -26,6 +28,27 @@ class ImageDifference:
def __str__(self):
return str(float(self))
+@total_ordering
+class Histogram:
+ def __init__(self, value, size):
+ self.value, self.size = value, size
+
+ def __eq__(self, other):
+ other = np.array(other)
+ other[other == None] = self.value[other == None]
+ return (self.value == other).all()
+
+ def __lt__(self, other):
+ other = np.array(other)
+ other[other == None] = self.value[other == None]
+ return (self.value <= other).all()
+
+ def __getitem__(self, index):
+ return self.value[index]
+
+ def __str__(self):
+ return f'{list(self.value)} size={self.size}'
+
def run_cargo_cmd(cmd, args, **kwargs):
if cmd.upper() in os.environ:
@@ -38,7 +61,7 @@ def run_cargo_cmd(cmd, args, **kwargs):
return subprocess.run([str(Path.home() / '.cargo' / 'bin' / cmd), *args], **kwargs)
def svg_to_png(in_svg, out_png):
- run_cargo_cmd('resvg', ['--dpi', '200', in_svg, out_png], check=True, stdout=subprocess.DEVNULL)
+ run_cargo_cmd('resvg', ['--dpi', '100', in_svg, out_png], check=True, stdout=subprocess.DEVNULL)
def gbr_to_svg(in_gbr, out_svg, origin=(0, 0), size=(6, 6)):
x, y = origin
@@ -109,6 +132,10 @@ def image_difference(reference, actual, diff_out=None):
delta = np.abs(out - ref).astype(float) / 255
if diff_out:
Image.fromarray((delta*255).astype(np.uint8), mode='L').save(diff_out)
- return ImageDifference(delta.mean()), ImageDifference(delta.max())
+
+ hist, _bins = np.histogram(delta, bins=10, range=(0, 1))
+ return (ImageDifference(delta.mean(), hist),
+ ImageDifference(delta.max(), hist),
+ Histogram(hist, out.size))
diff --git a/gerbonara/gerber/tests/test_rs274x.py b/gerbonara/gerber/tests/test_rs274x.py
index d91609f..aec2174 100644
--- a/gerbonara/gerber/tests/test_rs274x.py
+++ b/gerbonara/gerber/tests/test_rs274x.py
@@ -23,11 +23,6 @@ from .image_support import gerber_difference
deg_to_rad = lambda a: a/180 * math.pi
fail_dir = Path('gerbonara_test_failures')
-@pytest.fixture(scope='session', autouse=True)
-def clear_failure_dir(request):
- for f in chain(fail_dir.glob('*.gbr'), fail_dir.glob('*.png')):
- f.unlink()
-
reference_path = lambda reference: Path(__file__).parent / 'resources' / reference
@pytest.fixture
@@ -147,9 +142,9 @@ def test_rotation(temp_files, reference, angle):
f.save(tmp_gbr)
cx, cy = 0, to_gerbv_svg_units(10, unit='inch')
- mean, max = gerber_difference(ref, tmp_gbr, diff_out=tmp_png, svg_transform=f'rotate({angle} {cx} {cy})')
+ mean, _max, hist = gerber_difference(ref, tmp_gbr, diff_out=tmp_png, svg_transform=f'rotate({angle} {cx} {cy})')
assert mean < 1e-3 # relax mean criterion compared to above.
- assert max < 0.9
+ assert hist[9] == 0
@pytest.mark.filterwarnings('ignore:Deprecated.*statement found.*:DeprecationWarning')
@pytest.mark.filterwarnings('ignore::SyntaxWarning')
@@ -157,6 +152,9 @@ def test_rotation(temp_files, reference, angle):
@pytest.mark.parametrize('angle', TEST_ANGLES)
@pytest.mark.parametrize('center', [(0, 0), (-10, -10), (10, 10), (10, 0), (0, -10), (-10, 10), (10, 20)])
def test_rotation_center(temp_files, reference, angle, center):
+ if 'flash_rectangle' in reference and angle in (30, 1024):
+ # gerbv's rendering of this is broken, the hole is missing.
+ return
tmp_gbr, tmp_png = temp_files
ref = reference_path(reference)
@@ -167,11 +165,11 @@ def test_rotation_center(temp_files, reference, angle, center):
# calculate circle center in SVG coordinates
size = (10, 10) # inches
cx, cy = to_gerbv_svg_units(center[0]), to_gerbv_svg_units(10, 'inch')-to_gerbv_svg_units(center[1], 'mm')
- mean, max = gerber_difference(ref, tmp_gbr, diff_out=tmp_png,
+ mean, _max = gerber_difference(ref, tmp_gbr, diff_out=tmp_png,
svg_transform=f'rotate({angle} {cx} {cy})',
size=size)
assert mean < 1e-3
- assert max < 0.9
+ assert hist[9] == 0
@pytest.mark.filterwarnings('ignore:Deprecated.*statement found.*:DeprecationWarning')
@pytest.mark.filterwarnings('ignore::SyntaxWarning')
@@ -187,9 +185,9 @@ def test_offset(temp_files, reference, offset):
# flip y offset since svg's y axis is flipped compared to that of gerber
dx, dy = to_gerbv_svg_units(offset[0]), -to_gerbv_svg_units(offset[1])
- mean, max = gerber_difference(ref, tmp_gbr, diff_out=tmp_png, svg_transform=f'translate({dx} {dy})')
+ mean, _max, hist = gerber_difference(ref, tmp_gbr, diff_out=tmp_png, svg_transform=f'translate({dx} {dy})')
assert mean < 1e-4
- assert max < 0.9
+ assert hist[9] == 0
@pytest.mark.filterwarnings('ignore:Deprecated.*statement found.*:DeprecationWarning')
@pytest.mark.filterwarnings('ignore::SyntaxWarning')
@@ -198,6 +196,9 @@ def test_offset(temp_files, reference, offset):
@pytest.mark.parametrize('center', [(0, 0), (10, 0), (0, -10), (10, 20)])
@pytest.mark.parametrize('offset', [(0, 0), (100, 0), (0, 100), (100, 100), (100, 10)])
def test_combined(temp_files, reference, angle, center, offset):
+ if 'flash_rectangle' in reference and angle in (30, 1024):
+ # gerbv's rendering of this is broken, the hole is missing.
+ return
tmp_gbr, tmp_png = temp_files
ref = reference_path(reference)
@@ -209,9 +210,10 @@ def test_combined(temp_files, reference, angle, center, offset):
size = (10, 10) # inches
cx, cy = to_gerbv_svg_units(center[0]), to_gerbv_svg_units(10, 'inch')-to_gerbv_svg_units(center[1], 'mm')
dx, dy = to_gerbv_svg_units(offset[0]), -to_gerbv_svg_units(offset[1])
- mean, max = gerber_difference(ref, tmp_gbr, diff_out=tmp_png,
- svg_transform=f'rotate({anlge} {cx} {cy}) translate({dx} {dy})',
+ mean, _max, hist = gerber_difference(ref, tmp_gbr, diff_out=tmp_png,
+ svg_transform=f'rotate({angle} {cx} {cy}) translate({dx} {dy})',
size=size)
- assert mean < 1e-4
- assert max < 0.9
+ assert mean < 1e-3
+ assert hist[9] < 100
+ assert hist[3:].sum() < 1e-3*hist.size