From e0ac4d0c1b5620f9bb05c90679da12c857b6c412 Mon Sep 17 00:00:00 2001 From: Adrian Mariano Date: Thu, 31 Dec 2020 09:32:47 -0500 Subject: [PATCH] Fixed bugs in list_set, list_remove, list_insert that were revealed by adding degenerate case tests. List_set was broken for minlen nonzero (no tests). Added seed to shuffle. Fixed bounds check error in rounded_prism. --- arrays.scad | 23 ++++++++++++++--------- rounding.scad | 2 +- tests/test_arrays.scad | 23 +++++++++++++++++++---- 3 files changed, 34 insertions(+), 14 deletions(-) diff --git a/arrays.scad b/arrays.scad index b0a9a74..ab134c4 100644 --- a/arrays.scad +++ b/arrays.scad @@ -241,7 +241,6 @@ function repeat(val, n, i=0) = (i>=len(n))? val : [for (j=[1:1:n[i]]) repeat(val, n, i+1)]; - // Function: list_range() // Usage: // list_range(n, [s], [e]) @@ -447,19 +446,21 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) = assert(is_list(list)) !is_list(indices)? ( (is_finite(indices) && indices 0) || (is_vector(joint_sides,2) && joint_sides[0]>=0 && joint_sides[1]>=0), + jssingleok = (is_num(joint_sides) && joint_sides >= 0) || (is_vector(joint_sides,2) && joint_sides[0]>=0 && joint_sides[1]>=0), jsvecok = is_list(joint_sides) && len(joint_sides)==N && []==[for(entry=joint_sides) if (!(is_num(entry) || is_vector(entry,2))) entry] ) assert(is_num(joint_top) || is_vector(joint_top,2)) diff --git a/tests/test_arrays.scad b/tests/test_arrays.scad index 4483104..aac41d4 100644 --- a/tests/test_arrays.scad +++ b/tests/test_arrays.scad @@ -167,8 +167,17 @@ test_deduplicate_indexed(); module test_list_set() { - assert(list_set([2,3,4,5], 2, 21) == [2,3,21,5]); - assert(list_set([2,3,4,5], [1,3], [81,47]) == [2,81,4,47]); + assert_equal(list_set([2,3,4,5], 2, 21), [2,3,21,5]); + assert_equal(list_set([2,3,4,5], [1,3], [81,47]), [2,81,4,47]); + assert_equal(list_set([2,3,4,5], [2], [21]), [2,3,21,5]); + assert_equal(list_set([1,2,3], [], []), [1,2,3]); + assert_equal(list_set([1,2,3], [1,5], [4,4]), [1,4,3,0,0,4]); + assert_equal(list_set([1,2,3], [1,5], [4,4],dflt=12), [1,4,3,12,12,4]); + assert_equal(list_set([1,2,3], [1,2], [4,4],dflt=12, minlen=5), [1,4,4,12,12]); + assert_equal(list_set([1,2,3], 1, 4, dflt=12, minlen=5), [1,4,3,12,12]); + assert_equal(list_set([1,2,3], [],[],dflt=12, minlen=5), [1,2,3,12,12]); + assert_equal(list_set([1,2,3], 5,9), [1,2,3,0,0,9]); + assert_equal(list_set([1,2,3], 5,9,dflt=12), [1,2,3,12,12,9]); } test_list_set(); @@ -176,6 +185,8 @@ test_list_set(); module test_list_remove() { assert(list_remove([3,6,9,12],1) == [3,9,12]); assert(list_remove([3,6,9,12],[1,3]) == [3,9]); + assert(list_remove([3,6,9],[]) == [3,6,9]); + assert(list_remove([],[]) == []); } test_list_remove(); @@ -191,8 +202,12 @@ test_list_remove_values(); module test_list_insert() { - assert(list_insert([3,6,9,12],1,5) == [3,5,6,9,12]); - assert(list_insert([3,6,9,12],[1,3],[5,11]) == [3,5,6,9,11,12]); + assert_equal(list_insert([3,6,9,12],1,5),[3,5,6,9,12]); + assert_equal(list_insert([3,6,9,12],[1,3],[5,11]),[3,5,6,9,11,12]); + assert_equal(list_insert([3],1,4), [3,4]); + assert_equal(list_insert([3],[0,1], [1,2]), [1,3,2]); + assert_equal(list_insert([1,2,3],[],[]),[1,2,3]); + assert_equal(list_insert([], 0, 4),[4]); } test_list_insert();