From b770833521bb6b142bfd183ee8b0973e90cbe175 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Fri, 26 May 2017 14:24:06 +0800 Subject: [PATCH] added test_polyline2d --- test/test_all.scad | 1 + test/test_polyline2d.scad | 120 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 121 insertions(+) create mode 100644 test/test_polyline2d.scad diff --git a/test/test_all.scad b/test/test_all.scad index 2095ec31..2138d9ce 100644 --- a/test/test_all.scad +++ b/test/test_all.scad @@ -1,6 +1,7 @@ // 2D include ; include ; +include ; // 2D Shape include ; diff --git a/test/test_polyline2d.scad b/test/test_polyline2d.scad new file mode 100644 index 00000000..fc0bc635 --- /dev/null +++ b/test/test_polyline2d.scad @@ -0,0 +1,120 @@ +include ; +include ; + +module test_polyline2d() { + + $fn = 24; + points = [[1, 2], [-5, -4], [-5, 3], [5, 5]]; + line_width = 1; + leng_points = len(points); + + module assertCorrectSegment(index, point1, point2, width) { + assertEqualPoint(points[index - 1], point1); + assertEqualPoint(points[index], point2); + assertEqual(line_width, width); + } + + module assertCorrectCaps(startStyle, endStyle, index, p1Style, p2Style) { + function correctMiddleCaps(p1Style, p2Style) = + p1Style == "CAP_BUTT" && p2Style == "CAP_ROUND"; + + if(index == 1 && (p1Style != startStyle || p2Style != "CAP_ROUND")) { + fail( + "Wrong start caps", + str( + "expected: [", startStyle, ", \"CAP_ROUND\"]", + ", but: ", [p1Style, p2Style] + ) + ); + } + + if((1 < index && index < leng_points - 1) && !correctMiddleCaps(p1Style, p2Style)) { + + fail( + "Wrong middle caps", + str( + "expected: [\"CAP_BUTT\", \"CAP_ROUND\"]", + ", but: ", [p1Style, p2Style] + ) + ); + } + + if(index == leng_points - 1 && (p1Style != "CAP_BUTT" || p2Style != endStyle)) { + fail( + "Wrong end caps", + str( + "expected: [\"CAP_BUTT\, ", endStyle, "]", + ", but: ", [p1Style, p2Style] + ) + ); + } + } + + // testcases + + module test_polyline2d_cap_square() { + echo("==== test_polyline2d_cap_square ===="); + + include ; + + module test_line_segment(index, point1, point2, width, p1Style, p2Style) { + + assertCorrectSegment(index, point1, point2, width); + assertCorrectCaps( + "CAP_SQUARE", "CAP_SQUARE", + index, p1Style, p2Style + ); + } + + polyline2d(points = points, width = line_width); + } + + module test_polyline2d_cap_end_round() { + echo("==== test_polyline2d_cap_end_round ===="); + + include ; + + module test_line_segment(index, point1, point2, width, p1Style, p2Style) { + + assertCorrectSegment(index, point1, point2, width); + assertCorrectCaps( + "CAP_SQUARE", "CAP_ROUND", + index, p1Style, p2Style + ); + } + + polyline2d( + points = points, + width = line_width, + endingStyle = "CAP_ROUND" + ); + } + + module test_polyline2d_cap_round() { + echo("==== test_polyline2d_cap_round ===="); + + include ; + + module test_line_segment(index, point1, point2, width, p1Style, p2Style) { + + assertCorrectSegment(index, point1, point2, width); + assertCorrectCaps( + "CAP_ROUND", "CAP_ROUND", + index, p1Style, p2Style + ); + } + + polyline2d( + points = points, + width = line_width, + startingStyle = "CAP_ROUND", + endingStyle = "CAP_ROUND" + ); + } + + test_polyline2d_cap_square(); + test_polyline2d_cap_end_round(); + test_polyline2d_cap_round(); +} + +test_polyline2d(); \ No newline at end of file