mirror of
https://github.com/BelfrySCAD/BOSL2.git
synced 2025-01-01 09:49:45 +00:00
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:
parent
5269b26703
commit
e0ac4d0c1b
3 changed files with 34 additions and 14 deletions
23
arrays.scad
23
arrays.scad
|
@ -241,7 +241,6 @@ function repeat(val, n, i=0) =
|
||||||
(i>=len(n))? val :
|
(i>=len(n))? val :
|
||||||
[for (j=[1:1:n[i]]) repeat(val, n, i+1)];
|
[for (j=[1:1:n[i]]) repeat(val, n, i+1)];
|
||||||
|
|
||||||
|
|
||||||
// Function: list_range()
|
// Function: list_range()
|
||||||
// Usage:
|
// Usage:
|
||||||
// list_range(n, [s], [e])
|
// list_range(n, [s], [e])
|
||||||
|
@ -447,19 +446,21 @@ function list_set(list=[],indices,values,dflt=0,minlen=0) =
|
||||||
assert(is_list(list))
|
assert(is_list(list))
|
||||||
!is_list(indices)? (
|
!is_list(indices)? (
|
||||||
(is_finite(indices) && indices<len(list))?
|
(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) )
|
: list_set(list,[indices],[values],dflt) )
|
||||||
: assert(is_vector(indices) && is_list(values) && len(values)==len(indices) ,
|
: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")
|
"Index list and value list must have the same length")
|
||||||
let( midx = max(len(list)-1, max(indices)) )
|
let( midx = max(len(list)-1, max(indices)) )
|
||||||
[ for(i=[0:midx] )
|
[ for(i=[0:midx] )
|
||||||
let( j = search(i,indices,0),
|
let( j = search(i,indices,0),
|
||||||
k = j[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] :
|
k!=undef ? values[k] :
|
||||||
i<len(list) ? list[i]:
|
i<len(list) ? list[i]:
|
||||||
dflt ,
|
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:
|
// Example:
|
||||||
// list_insert([3,6,9,12],1,5); // Returns [3,5,6,9,12]
|
// 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]
|
// 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))
|
assert(is_list(list))
|
||||||
! is_list(indices)?
|
!is_list(indices)?
|
||||||
assert( is_finite(indices) && is_finite(values), "Invalid indices/values." )
|
assert( is_finite(indices) && is_finite(values), "Invalid indices/values." )
|
||||||
assert( indices<=len(list), "Indices must be <= len(list) ." )
|
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) ,
|
: assert( is_vector(indices) && is_list(values) && len(values)==len(indices) ,
|
||||||
"Index list and value list must have the same length")
|
"Index list and value list must have the same length")
|
||||||
assert( max(indices)<=len(list), "Indices must be <= len(list) ." )
|
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)] )
|
for(i=[minidx: min(maxidx, len(list)-1)] )
|
||||||
let( j = search(i,indices,0),
|
let( j = search(i,indices,0),
|
||||||
k = j[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] ] ),
|
each ( k != undef ? [ values[k], list[i] ] : [ list[i] ] ),
|
||||||
for(i=[min(maxidx, len(list)-1)+1:1:len(list)-1] ) list[i],
|
for(i=[min(maxidx, len(list)-1)+1:1:len(list)-1] ) list[i],
|
||||||
|
|
|
@ -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) :
|
len(top[0])==2 ? path3d(top,height/2) :
|
||||||
top,
|
top,
|
||||||
bottom = len(bottom[0])==2 ? path3d(bottom,-height/2) : bottom,
|
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]
|
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))
|
assert(is_num(joint_top) || is_vector(joint_top,2))
|
||||||
|
|
|
@ -167,8 +167,17 @@ test_deduplicate_indexed();
|
||||||
|
|
||||||
|
|
||||||
module test_list_set() {
|
module test_list_set() {
|
||||||
assert(list_set([2,3,4,5], 2, 21) == [2,3,21,5]);
|
assert_equal(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], [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();
|
test_list_set();
|
||||||
|
|
||||||
|
@ -176,6 +185,8 @@ test_list_set();
|
||||||
module test_list_remove() {
|
module test_list_remove() {
|
||||||
assert(list_remove([3,6,9,12],1) == [3,9,12]);
|
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,12],[1,3]) == [3,9]);
|
||||||
|
assert(list_remove([3,6,9],[]) == [3,6,9]);
|
||||||
|
assert(list_remove([],[]) == []);
|
||||||
}
|
}
|
||||||
test_list_remove();
|
test_list_remove();
|
||||||
|
|
||||||
|
@ -191,8 +202,12 @@ test_list_remove_values();
|
||||||
|
|
||||||
|
|
||||||
module test_list_insert() {
|
module test_list_insert() {
|
||||||
assert(list_insert([3,6,9,12],1,5) == [3,5,6,9,12]);
|
assert_equal(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,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();
|
test_list_insert();
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue