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.
This commit is contained in:
Adrian Mariano 2020-12-31 09:32:47 -05:00
parent 5269b26703
commit e0ac4d0c1b
3 changed files with 34 additions and 14 deletions

View file

@ -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<len(list))?
[for (i=idx(list)) i==indices? values : list[i]]
concat([for (i=idx(list)) i==indices? values : list[i]], repeat(dflt, minlen-len(list)))
: list_set(list,[indices],[values],dflt) )
:indices==[] && values==[] ?
concat(list, repeat(dflt, minlen-len(list)))
:assert(is_vector(indices) && is_list(values) && len(values)==len(indices) ,
"Index list and value list must have the same length")
let( midx = max(len(list)-1, max(indices)) )
[ for(i=[0:midx] )
let( j = search(i,indices,0),
k = j[0] )
assert( len(j)<2, "Repeated indices are not acceptable." )
assert( len(j)<2, "Repeated indices are not allowed." )
k!=undef ? values[k] :
i<len(list) ? list[i]:
dflt ,
each repeat(dflt, minlen-max(indices))
each repeat(dflt, minlen-max(len(list),max(indices)))
];
@ -471,12 +472,16 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) =
// Example:
// list_insert([3,6,9,12],1,5); // Returns [3,5,6,9,12]
// list_insert([3,6,9,12],[1,3],[5,11]); // Returns [3,5,6,9,11,12]
function list_insert(list, indices, values, _i=0) =
function list_insert(list, indices, values) =
assert(is_list(list))
!is_list(indices)?
assert( is_finite(indices) && is_finite(values), "Invalid indices/values." )
assert( indices<=len(list), "Indices must be <= len(list) ." )
[for (i=idx(list)) each ( i==indices? [ values, list[i] ] : [ list[i] ] ) ]
[
for (i=idx(list)) each ( i==indices? [ values, list[i] ] : [ list[i] ] ),
if (indices==len(list)) values
]
: indices==[] && values==[] ? list
: assert( is_vector(indices) && is_list(values) && len(values)==len(indices) ,
"Index list and value list must have the same length")
assert( max(indices)<=len(list), "Indices must be <= len(list) ." )
@ -486,7 +491,7 @@ function list_insert(list, indices, values, _i=0) =
for(i=[minidx: min(maxidx, len(list)-1)] )
let( j = search(i,indices,0),
k = j[0],
x = assert( len(j)<2, "Repeated indices are not acceptable." )
x = assert( len(j)<2, "Repeated indices are not allowed." )
)
each ( k != undef ? [ values[k], list[i] ] : [ list[i] ] ),
for(i=[min(maxidx, len(list)-1)+1:1:len(list)-1] ) list[i],

View file

@ -1768,7 +1768,7 @@ function rounded_prism(bottom, top, joint_bot, joint_top, joint_sides, k_bot, k_
len(top[0])==2 ? path3d(top,height/2) :
top,
bottom = len(bottom[0])==2 ? path3d(bottom,-height/2) : bottom,
jssingleok = (is_num(joint_sides) && joint_sides > 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))

View file

@ -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();