From a881dbf8967152f7324cdaf4c9c0ea073289e3c9 Mon Sep 17 00:00:00 2001 From: Justin Lin Date: Mon, 5 Jun 2017 15:27:09 +0800 Subject: [PATCH] refactored --- src/line3d.scad | 2 +- src/unittest.scad | 42 ++++++++++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/line3d.scad b/src/line3d.scad index 37546838..a3013a7d 100644 --- a/src/line3d.scad +++ b/src/line3d.scad @@ -47,7 +47,7 @@ module line3d(p1, p2, thickness, p1Style = "CAP_CIRCLE", p2Style = "CAP_CIRCLE") module cap(p, style) { if(style == "CAP_CIRCLE") { cap_leng = r / 1.414; - cap_with(p) + cap_with(p) ` linear_extrude(cap_leng * 2, center = true) circle(r, $fn = frags); diff --git a/src/unittest.scad b/src/unittest.scad index 99f8f735..7e772f27 100644 --- a/src/unittest.scad +++ b/src/unittest.scad @@ -21,14 +21,19 @@ module fail(title, message) { } } -function shift_to_int(point, digits) = - let(pt = point * pow(10, digits)) +function round_n(number, float_digits = 4) = + let(n = pow(10, float_digits)) + round(number * n) / n; + +function mul_round_pt(point, n) = + let(pt = point * n) len(pt) == 2 ? [round(pt[0]), round(pt[1])] : [round(pt[0]), round(pt[1]), round(pt[2])]; -function round_pts(points, float_digits) = - [for(pt = points) shift_to_int(pt, float_digits) / pow(10, float_digits)]; +function round_pts(points, float_digits = 4) = + let(n = pow(10, float_digits)) + [for(pt = points) mul_round_pt(pt, n) / n]; module assertEqualPoint(expected, actual, float_digits = 4) { leng_expected = len(expected); @@ -41,12 +46,14 @@ module assertEqualPoint(expected, actual, float_digits = 4) { ", but: ", leng_actual) ); } else { - shifted_expected = shift_to_int( - expected, float_digits + n = pow(10, float_digits); + + shifted_expected = mul_round_pt( + expected, n ); - shifted_actual = shift_to_int( - actual, float_digits + shifted_actual = mul_round_pt( + actual, n ); if(shifted_expected != shifted_actual) { @@ -76,12 +83,15 @@ module assertEqualPoints(expected, actual, float_digits = 4) { } } -module assertEqual(expected, actual) { - if(expected != actual) { +module assertEqual(expected, actual, float_digits = 4) { + r_expected = round_n(expected, float_digits); + r_actual = round_n(actual, float_digits); + + if(r_expected != r_actual) { fail( "Equality", - str("expected: ", expected, - ", but: ", actual) + str("expected: ", r_expected, + ", but: ", r_actual) ); } } @@ -93,4 +103,12 @@ module assertTrue(truth) { "expected: true, but: false" ); } +} + +module round_echo_pts(points, float_digits = 4) { + echo(round_pts(points, float_digits = 4)); +} + +module round_echo_n(number, float_digits = 4) { + echo(round_n(number, float_digits)); } \ No newline at end of file