From d45f289a9526de09db18ba248dab9b9e9f45b4d0 Mon Sep 17 00:00:00 2001 From: RonaldoCMP Date: Tue, 28 Jul 2020 22:55:21 +0100 Subject: [PATCH] update --- math.scad | 2 +- shapes2d.scad | 1 + tests/test_all.scad | 29 +++++++++++++++++++ tests/test_math.scad | 69 ++++++++++++++++++++++++++++++++++++++------ 4 files changed, 91 insertions(+), 10 deletions(-) create mode 100644 tests/test_all.scad diff --git a/math.scad b/math.scad index a5b12b6..23a8174 100644 --- a/math.scad +++ b/math.scad @@ -1244,7 +1244,7 @@ function C_div(z1,z2) = // Section: Polynomials -// Function: polynomial() +// Function: polynomial() // Usage: // polynomial(p, z) // Description: diff --git a/shapes2d.scad b/shapes2d.scad index 9622ad4..f328952 100644 --- a/shapes2d.scad +++ b/shapes2d.scad @@ -787,6 +787,7 @@ function rect(size=1, center, rounding=0, chamfer=0, anchor, spin=0) = size = is_num(size)? [size,size] : point2d(size), anchor = get_anchor(anchor, center, FRONT+LEFT, FRONT+LEFT), complex = rounding!=0 || chamfer!=0 + , xxx = echo(anchor=anchor)echo(size=size) ) (rounding==0 && chamfer==0)? let( path = [ diff --git a/tests/test_all.scad b/tests/test_all.scad new file mode 100644 index 0000000..c9c89ac --- /dev/null +++ b/tests/test_all.scad @@ -0,0 +1,29 @@ +//include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include +include + + diff --git a/tests/test_math.scad b/tests/test_math.scad index 3233e6f..eefb18d 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -1,5 +1,17 @@ include <../std.scad> +module test_all_numeric() { + assert( all_numeric(1)); + assert( all_numeric([1,2,[3,4]])); + assert( all_numeric([])); + assert(!all_numeric([1,2,[3,"a"]])); + assert(!all_numeric([1,2,[3,true]])); + assert(!all_numeric([1,2,[3,NAN]])); + assert(!all_numeric([1,2,[3,INF]])); + assert( all_numeric([1,2,[3,[]]])); +} +test_all_numeric(); + // Simple Calculations module test_quant() { @@ -110,6 +122,8 @@ module test_approx() { assert_equal(approx(1/3, 0.3333333333), true); assert_equal(approx(-1/3, -0.3333333333), true); assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true); + assert_equal(approx([1,[1,undef]], [1+1e-12,[1,true]]), false); + assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true); } test_approx(); @@ -342,7 +356,9 @@ module test_cumsum() { assert_equal(cumsum([2,2,2]), [2,4,6]); assert_equal(cumsum([1,2,3]), [1,3,6]); assert_equal(cumsum([-2,-1,0,1,2]), [-2,-3,-3,-2,0]); + assert_equal(cumsum([-2,-1,0,1,2],3), [1,0,0,1,3]); assert_equal(cumsum([[1,2,3], [3,4,5], [5,6,7]]), [[1,2,3],[4,6,8],[9,12,15]]); + assert_equal(cumsum([[1,2,3], [3,4,5], [5,6,7]], [1,0,0]), [[2,2,3],[5,6,8],[10,12,15]]); } test_cumsum(); @@ -389,7 +405,6 @@ module test_mean() { } test_mean(); - module test_median() { assert_equal(median([2,3,7]), 4.5); assert_equal(median([[1,2,3], [3,4,5], [8,9,10]]), [4.5,5.5,6.5]); @@ -397,6 +412,16 @@ module test_median() { test_median(); +module test_convolve() { + assert_equal(convolve([],[1,2,1]), []); + assert_equal(convolve([1,1],[]), []); + assert_equal(convolve([1,1],[1,2,1]), [1,3,3,1]); + assert_equal(convolve([1,2,3],[1,2,1]), [1,4,8,8,3]); +} +test_convolve(); + + + module test_matrix_inverse() { assert_approx(matrix_inverse(rot([20,30,40])), [[0.663413948169,0.556670399226,-0.5,0],[-0.47302145844,0.829769465589,0.296198132726,0],[0.579769465589,0.0400087565481,0.813797681349,0],[0,0,0,1]]); } @@ -583,6 +608,24 @@ module test_factorial() { } test_factorial(); +module test_binomial() { + assert_equal(binomial(1), [1,1]); + assert_equal(binomial(2), [1,2,1]); + assert_equal(binomial(3), [1,3,3,1]); + assert_equal(binomial(5), [1,5,10,10,5,1]); +} +test_binomial(); + +module test_binomial_coefficient() { + assert_equal(binomial_coefficient(2,1), 2); + assert_equal(binomial_coefficient(3,2), 3); + assert_equal(binomial_coefficient(4,2), 6); + assert_equal(binomial_coefficient(10,7), 120); + assert_equal(binomial_coefficient(10,7), binomial(10)[7]); + assert_equal(binomial_coefficient(15,4), binomial(15)[4]); +} +test_binomial_coefficient(); + module test_gcd() { assert_equal(gcd(15,25), 5); @@ -682,6 +725,7 @@ test_linear_solve(); module test_outer_product(){ assert_equal(outer_product([1,2,3],[4,5,6]), [[4,5,6],[8,10,12],[12,15,18]]); + assert_equal(outer_product([1,2],[4,5,6]), [[4,5,6],[8,10,12]]); assert_equal(outer_product([9],[7]), [[63]]); } test_outer_product(); @@ -782,8 +826,10 @@ test_deriv3(); module test_polynomial(){ - assert_equal(polynomial([],12),0); - assert_equal(polynomial([],[12,4]),[0,0]); + assert_equal(polynomial([0],12),0); + assert_equal(polynomial([0],[12,4]),[0,0]); +// assert_equal(polynomial([],12),0); +// assert_equal(polynomial([],[12,4]),[0,0]); assert_equal(polynomial([1,2,3,4],3),58); assert_equal(polynomial([1,2,3,4],[3,-1]),[47,-41]); assert_equal(polynomial([0,0,2],4),2); @@ -879,16 +925,20 @@ test_qr_factor(); module test_poly_mult(){ assert_equal(poly_mult([3,2,1],[4,5,6,7]),[12,23,32,38,20,7]); - assert_equal(poly_mult([3,2,1],[]),[]); + assert_equal(poly_mult([3,2,1],[0]),[0]); +// assert_equal(poly_mult([3,2,1],[]),[]); assert_equal(poly_mult([[1,2],[3,4],[5,6]]), [15,68,100,48]); - assert_equal(poly_mult([[1,2],[],[5,6]]), []); - assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]); + assert_equal(poly_mult([[1,2],[0],[5,6]]), [0]); +// assert_equal(poly_mult([[1,2],[],[5,6]]), []); + assert_equal(poly_mult([[3,4,5],[0,0,0]]),[0]); +// assert_equal(poly_mult([[3,4,5],[0,0,0]]),[]); } test_poly_mult(); - + module test_poly_div(){ - assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]); + assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[0]]); +// assert_equal(poly_div(poly_mult([4,3,3,2],[2,1,3]), [2,1,3]),[[4,3,3,2],[]]); assert_equal(poly_div([1,2,3,4],[1,2,3,4,5]), [[], [1,2,3,4]]); assert_equal(poly_div(poly_add(poly_mult([1,2,3,4],[2,0,2]), [1,1,2]), [1,2,3,4]), [[2,0,2],[1,1,2]]); assert_equal(poly_div([1,2,3,4], [1,-3]), [[1,5,18],[58]]); @@ -899,7 +949,8 @@ test_poly_div(); module test_poly_add(){ assert_equal(poly_add([2,3,4],[3,4,5,6]),[3,6,8,10]); assert_equal(poly_add([1,2,3,4],[-1,-2,3,4]), [6,8]); - assert_equal(poly_add([1,2,3],-[1,2,3]),[]); + assert_equal(poly_add([1,2,3],-[1,2,3]),[0]); +// assert_equal(poly_add([1,2,3],-[1,2,3]),[]); } test_poly_add();