BOSL2/tests/test_lists.scad

409 lines
13 KiB
OpenSCAD
Raw Normal View History

2020-07-22 00:05:21 +00:00
include <../std.scad>
2019-05-01 06:45:05 +00:00
// Section: List Query Operations
Sort debugging and optimizing There were bugs in the previous sorting functions. They didn't check the homogeneity of the input list before calling _sort_scalars and _sort_vectors. The bug might result in wrong order and missing list elements in the output. Besides correcting the bug a recode of all sorting functions result in better performance and a enlargement of their scope. With the new functions, list of vectors of any dimension may be sorted, even with idx given, with the native comparison operators. The scope of indexed sorting is also extended. The file test_arrays has been extended to check the new funcionality. New functions: is_homogeneous - checks if a list has elements of the same type (although not distinguing booleans from numbers) up to a given depth _sort_vectors - internal function to sort homgeneous lists of vectors using native comparison operators; extends the scope of the previous _sort_vectors# functions with better performance _lexical_sort - internal function to sort non-homogeneous lists; uses compare_vals _indexed_sort - internal function to perform indexed sorting of non-homogeneous lists; uses compar_vals Changed/reviewed functions: _valid_idx - doesn't requires the input of imin and imax args sort - explores the internal functions to get better performance and an enlarged scope sortidx - explores the internal functions to get better performance and an enlarged scope _sort_general - just for sortings of non-homogeneous lists using compare_vals _array_dim_recurse - changed for bit better performance Functions eliminated: _sort_vectors1 _sort_vectors2 _sort_vectors3 _sort_vectors4
2020-08-30 11:12:36 +00:00
module test_is_homogeneous(){
assert(is_homogeneous([[1,["a"]], [2,["b"]]])==true);
assert(is_homogeneous([[1,["a"]], [2,[true]]])==false);
assert(is_homogeneous([[1,["a"]], [2,[true]]],1)==true);
assert(is_homogeneous([[1,["a"]], [2,[true]]],2)==false);
assert(is_homogeneous([[1,["a"]], [true,["b"]]])==false);
Sort debugging and optimizing There were bugs in the previous sorting functions. They didn't check the homogeneity of the input list before calling _sort_scalars and _sort_vectors. The bug might result in wrong order and missing list elements in the output. Besides correcting the bug a recode of all sorting functions result in better performance and a enlargement of their scope. With the new functions, list of vectors of any dimension may be sorted, even with idx given, with the native comparison operators. The scope of indexed sorting is also extended. The file test_arrays has been extended to check the new funcionality. New functions: is_homogeneous - checks if a list has elements of the same type (although not distinguing booleans from numbers) up to a given depth _sort_vectors - internal function to sort homgeneous lists of vectors using native comparison operators; extends the scope of the previous _sort_vectors# functions with better performance _lexical_sort - internal function to sort non-homogeneous lists; uses compare_vals _indexed_sort - internal function to perform indexed sorting of non-homogeneous lists; uses compar_vals Changed/reviewed functions: _valid_idx - doesn't requires the input of imin and imax args sort - explores the internal functions to get better performance and an enlarged scope sortidx - explores the internal functions to get better performance and an enlarged scope _sort_general - just for sortings of non-homogeneous lists using compare_vals _array_dim_recurse - changed for bit better performance Functions eliminated: _sort_vectors1 _sort_vectors2 _sort_vectors3 _sort_vectors4
2020-08-30 11:12:36 +00:00
}
test_is_homogeneous();
module test_select() {
l = [3,4,5,6,7,8,9];
assert(select(l, 5, 6) == [8,9]);
assert(select(l, 5, 8) == [8,9,3,4]);
assert(select(l, 5, 2) == [8,9,3,4,5]);
assert(select(l, -3, -1) == [7,8,9]);
assert(select(l, 3, 3) == [6]);
assert(select(l, 4) == 7);
assert(select(l, -2) == 8);
assert(select(l, [1:3]) == [4,5,6]);
assert(select(l, [1,3]) == [4,6]);
2019-05-01 06:45:05 +00:00
}
test_select();
module test_slice() {
l = [3,4,5,6,7,8,9];
assert(slice(l, 5, 6) == [8,9]);
assert(slice(l, 5, 8) == [8,9]);
assert(slice(l, 5, 2) == []);
assert(slice(l, -3, -1) == [7,8,9]);
assert(slice(l, 3, 3) == [6]);
assert(slice(l, 4) == [7,8,9]);
assert(slice(l, -2) == [8,9]);
}
test_slice();
module test_last() {
list = [1,2,3,4];
assert(last(list)==4);
assert(last([])==undef);
}
test_last();
module test_list_head() {
list = [1,2,3,4];
assert_equal(list_head(list), [1,2,3]);
assert_equal(list_head([1]), []);
assert_equal(list_head([]), []);
assert_equal(list_head(list,-3), [1,2]);
assert_equal(list_head(list,1), [1,2]);
assert_equal(list_head(list,2), [1,2,3]);
assert_equal(list_head(list,6), [1,2,3,4]);
assert_equal(list_head(list,-6), []);
}
test_list_head();
module test_list_tail() {
list = [1,2,3,4];
assert_equal(list_tail(list), [2,3,4]);
assert_equal(list_tail([1]), []);
assert_equal(list_tail([]), []);
assert_equal(list_tail(list,-3), [2,3,4]);
assert_equal(list_tail(list,2), [3,4]);
assert_equal(list_tail(list,3), [4]);
assert_equal(list_tail(list,6), []);
assert_equal(list_tail(list,-6), [1,2,3,4]);
}
test_list_tail();
2019-05-01 06:45:05 +00:00
module test_in_list() {
assert(in_list("bar", ["foo", "bar", "baz"]));
assert(!in_list("bee", ["foo", "bar", "baz"]));
assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
assert(!in_list("bee", ["foo", "bar", ["bee"]]));
assert(in_list(NAN, [NAN])==false);
2020-07-21 23:55:05 +00:00
assert(!in_list(undef, [3,4,5]));
assert(in_list(undef,[3,4,undef,5]));
assert(!in_list(3,[]));
assert(!in_list(3,[4,5,[3]]));
2019-05-01 06:45:05 +00:00
}
test_in_list();
Sort debugging and optimizing There were bugs in the previous sorting functions. They didn't check the homogeneity of the input list before calling _sort_scalars and _sort_vectors. The bug might result in wrong order and missing list elements in the output. Besides correcting the bug a recode of all sorting functions result in better performance and a enlargement of their scope. With the new functions, list of vectors of any dimension may be sorted, even with idx given, with the native comparison operators. The scope of indexed sorting is also extended. The file test_arrays has been extended to check the new funcionality. New functions: is_homogeneous - checks if a list has elements of the same type (although not distinguing booleans from numbers) up to a given depth _sort_vectors - internal function to sort homgeneous lists of vectors using native comparison operators; extends the scope of the previous _sort_vectors# functions with better performance _lexical_sort - internal function to sort non-homogeneous lists; uses compare_vals _indexed_sort - internal function to perform indexed sorting of non-homogeneous lists; uses compar_vals Changed/reviewed functions: _valid_idx - doesn't requires the input of imin and imax args sort - explores the internal functions to get better performance and an enlarged scope sortidx - explores the internal functions to get better performance and an enlarged scope _sort_general - just for sortings of non-homogeneous lists using compare_vals _array_dim_recurse - changed for bit better performance Functions eliminated: _sort_vectors1 _sort_vectors2 _sort_vectors3 _sort_vectors4
2020-08-30 11:12:36 +00:00
// Section: Basic List Generation
module test_repeat() {
assert(repeat(1, 4) == [1,1,1,1]);
assert(repeat(8, [2,3]) == [[8,8,8], [8,8,8]]);
assert(repeat(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
assert(repeat([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
assert(repeat(4, [2,-1]) == [[], []]);
}
test_repeat();
2019-05-01 06:45:05 +00:00
module test_count() {
assert_equal(count(5), [0,1,2,3,4]);
assert_equal(count(5,3), [3,4,5,6,7]);
assert_equal(count(4,3,2), [3,5,7,9]);
assert_equal(count(5,0,0.25), [0, 0.25, 0.5, 0.75, 1.0]);
}
test_count();
2019-05-01 06:45:05 +00:00
module test_reverse() {
assert(reverse([3,4,5,6]) == [6,5,4,3]);
assert(reverse("abcd") == "dcba");
assert(reverse([]) == []);
2019-05-01 06:45:05 +00:00
}
test_reverse();
module test_list_rotate() {
assert(list_rotate([1,2,3,4,5],-2) == [4,5,1,2,3]);
assert(list_rotate([1,2,3,4,5],-1) == [5,1,2,3,4]);
assert(list_rotate([1,2,3,4,5],0) == [1,2,3,4,5]);
assert(list_rotate([1,2,3,4,5],1) == [2,3,4,5,1]);
assert(list_rotate([1,2,3,4,5],2) == [3,4,5,1,2]);
assert(list_rotate([1,2,3,4,5],3) == [4,5,1,2,3]);
assert(list_rotate([1,2,3,4,5],4) == [5,1,2,3,4]);
assert(list_rotate([1,2,3,4,5],5) == [1,2,3,4,5]);
assert(list_rotate([1,2,3,4,5],6) == [2,3,4,5,1]);
assert(list_rotate([],3) == []);
path = [[1,1],[-1,1],[-1,-1],[1,-1]];
assert(list_rotate(path,1) == [[-1,1],[-1,-1],[1,-1],[1,1]]);
assert(list_rotate(path,2) == [[-1,-1],[1,-1],[1,1],[-1,1]]);
}
test_list_rotate();
2020-03-24 21:51:37 +00:00
2019-10-23 00:10:04 +00:00
module test_list_set() {
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]);
2019-10-23 00:10:04 +00:00
}
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,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([],[]) == []);
assert(list_remove([1,2,3], -1)==[1,2,3]);
assert(list_remove([1,2,3], 3)==[1,2,3]);
assert(list_remove([1,2,3], [-1,3])==[1,2,3]);
assert(list_remove([1,2,3], [-1,1,3])==[1,3]);
2019-10-23 00:10:04 +00:00
}
test_list_remove();
module test_list_remove_values() {
animals = ["bat", "cat", "rat", "dog", "bat", "rat"];
assert(list_remove_values(animals, "rat") == ["bat","cat","dog","bat","rat"]);
assert(list_remove_values(animals, "bat", all=true) == ["cat","rat","dog","rat"]);
assert(list_remove_values(animals, ["bat","rat"]) == ["cat","dog","bat","rat"]);
assert(list_remove_values(animals, ["bat","rat"], all=true) == ["cat","dog"]);
assert(list_remove_values(animals, ["tucan","rat"], all=true) == ["bat","cat","dog","bat"]);
test = [3,4,[5,6],7,5,[5,6],4,[6,5],7,[4,4]];
assert_equal(list_remove_values(test,4), [3, [5, 6], 7, 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
assert_equal(list_remove_values(test,[4,4]), [3, [5, 6], 7, 5, [5, 6], [6, 5], 7, [4, 4]]);
assert_equal(list_remove_values(test,[4,7]), [3, [5, 6], 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
assert_equal(list_remove_values(test,[5,6]), [3, 4, [5, 6], 7, [5, 6], 4, [6, 5], 7, [4, 4]]);
assert_equal(list_remove_values(test,[[5,6]]), [3,4,7,5,[5,6],4,[6,5],7,[4,4]]);
assert_equal(list_remove_values(test,[[5,6]],all=true), [3,4,7,5,4,[6,5],7,[4,4]]);
assert_equal(list_remove_values(test,4,all=true), [3, [5, 6], 7, 5, [5, 6], [6, 5],7, [4, 4]]);
assert_equal(list_remove_values(test,[4,7],all=true), [3, [5, 6], 5, [5, 6], [6, 5], [4, 4]]);
assert_equal(list_remove_values(test,[]),test);
assert_equal(list_remove_values(test,[],all=true),test);
assert_equal(list_remove_values(test,99), test);
assert_equal(list_remove_values(test,99,all=true), test);
assert_equal(list_remove_values(test,[99,100],all=true), test);
assert_equal(list_remove_values(test,[99,100]), test);
2019-10-23 00:10:04 +00:00
}
test_list_remove_values();
module test_list_insert() {
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]);
2019-10-23 00:10:04 +00:00
}
test_list_insert();
module test_bselect() {
assert(bselect([3,4,5,6,7], [false,false,false,false,false]) == []);
assert(bselect([3,4,5,6,7], [false,true,true,false,true]) == [4,5,7]);
assert(bselect([3,4,5,6,7], [true,true,true,true,true]) == [3,4,5,6,7]);
2019-10-23 00:10:04 +00:00
}
test_bselect();
2019-11-07 07:40:05 +00:00
module test_list_bset() {
assert(list_bset([false,true,false,true,false], [3,4]) == [0,3,0,4,0]);
assert(list_bset([false,true,false,true,false], [3,4], dflt=1) == [1,3,1,4,1]);
2019-10-23 00:10:04 +00:00
}
2019-11-07 07:40:05 +00:00
test_list_bset();
2019-10-23 00:10:04 +00:00
module test_min_length() {
assert(min_length(["foobar", "bazquxx", "abcd"]) == 4);
2019-05-01 06:45:05 +00:00
}
test_min_length();
2019-05-01 06:45:05 +00:00
module test_max_length() {
assert(max_length(["foobar", "bazquxx", "abcd"]) == 7);
2019-05-01 06:45:05 +00:00
}
test_max_length();
2019-05-01 06:45:05 +00:00
module test_list_pad() {
assert(list_pad([4,5,6], 5, 8) == [4,5,6,8,8]);
assert(list_pad([4,5,6,7,8], 5, 8) == [4,5,6,7,8]);
assert(list_pad([4,5,6,7,8,9], 5, 8) == [4,5,6,7,8,9]);
2019-05-01 06:45:05 +00:00
}
test_list_pad();
2019-10-23 00:10:04 +00:00
module test_idx() {
colors = ["red", "green", "blue", "cyan"];
assert([for (i=idx(colors)) i] == [0,1,2,3]);
assert([for (i=idx(colors,e=-2)) i] == [0,1,2]);
assert([for (i=idx(colors,s=1)) i] == [1,2,3]);
assert([for (i=idx(colors,s=1,e=-2)) i] == [1,2]);
2019-10-23 00:10:04 +00:00
}
test_idx();
2019-11-07 07:40:05 +00:00
module test_shuffle() {
nums1 = count(100);
nums2 = shuffle(nums1,33);
nums3 = shuffle(nums2,99);
assert(sort(nums2)==nums1);
assert(sort(nums3)==nums1);
assert(nums1!=nums2);
assert(nums2!=nums3);
assert(nums1!=nums3);
str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
shufstr = shuffle(str,12);
assert(shufstr != str && sort(shufstr)==str);
2019-11-07 07:40:05 +00:00
}
test_shuffle();
2019-10-23 00:10:04 +00:00
// Sets
module test_set_union() {
assert_equal(
set_union([2,3,5,7,11], [1,2,3,5,8]),
[2,3,5,7,11,1,8]
);
assert_equal(
set_union([2,3,5,7,11], [1,2,3,5,8], get_indices=true),
[[5,0,1,2,6],[2,3,5,7,11,1,8]]
);
}
test_set_union();
module test_set_difference() {
assert_equal(
set_difference([2,3,5,7,11], [1,2,3,5,8]),
[7,11]
);
}
test_set_difference();
module test_set_intersection() {
assert_equal(
set_intersection([2,3,5,7,11], [1,2,3,5,8]),
[2,3,5]
);
}
test_set_intersection();
2019-05-01 06:45:05 +00:00
// Arrays
module test_force_list() {
assert_equal(force_list([3,4,5]), [3,4,5]);
assert_equal(force_list(5), [5]);
assert_equal(force_list(7, n=3), [7,7,7]);
assert_equal(force_list(4, n=3, fill=1), [4,1,1]);
}
test_force_list();
2019-05-10 09:59:12 +00:00
module test_pair() {
assert(pair([3,4,5,6]) == [[3,4], [4,5], [5,6]]);
assert(pair("ABCD") == [["A","B"], ["B","C"], ["C","D"]]);
assert(pair([3,4,5,6],true) == [[3,4], [4,5], [5,6], [6,3]]);
assert(pair("ABCD",true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
assert(pair([3,4,5,6],wrap=true) == [[3,4], [4,5], [5,6], [6,3]]);
assert(pair("ABCD",wrap=true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
assert_equal(pair([],wrap=true),[]);
assert_equal(pair([],wrap=false),[]);
assert_equal(pair([1],wrap=true),[]);
assert_equal(pair([1],wrap=false),[]);
assert_equal(pair([1,2],wrap=false),[[1,2]]);
assert_equal(pair([1,2],wrap=true),[[1,2],[2,1]]);
2019-05-10 09:59:12 +00:00
}
test_pair();
2019-10-23 00:10:04 +00:00
module test_triplet() {
assert(triplet([3,4,5,6,7]) == [[3,4,5], [4,5,6], [5,6,7]]);
assert(triplet("ABCDE") == [["A","B","C"], ["B","C","D"], ["C","D","E"]]);
2021-11-01 22:14:31 +00:00
assert(triplet([3,4,5,6],true) == [[6,3,4],[3,4,5], [4,5,6], [5,6,3]]);
assert(triplet("ABCD",true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
assert(triplet("ABCD",wrap=true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
assert_equal(triplet([],wrap=true),[]);
assert_equal(triplet([],wrap=false),[]);
assert_equal(triplet([1],wrap=true),[]);
assert_equal(triplet([1],wrap=false),[]);
assert_equal(triplet([1,2],wrap=true),[]);
assert_equal(triplet([1,2],wrap=false),[]);
assert_equal(triplet([1,2,3],wrap=true),[[3,1,2],[1,2,3],[2,3,1]]);
assert_equal(triplet([1,2,3],wrap=false),[[1,2,3]]);
2019-10-23 00:10:04 +00:00
}
test_triplet();
module test_combinations() {
assert(combinations([3,4,5,6]) == [[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]);
assert(combinations([3,4,5,6],n=3) == [[3,4,5],[3,4,6],[3,5,6],[4,5,6]]);
}
test_combinations();
module test_repeat_entries() {
list = [0,1,2,3];
assert(repeat_entries(list, 6) == [0,0,1,2,2,3]);
assert(repeat_entries(list, 6, exact=false) == [0,0,1,1,2,2,3,3]);
assert(repeat_entries(list, [1,1,2,1], exact=false) == [0,1,2,2,3]);
}
test_repeat_entries();
module test_list_to_matrix() {
v = [1,2,3,4,5,6];
assert(list_to_matrix(v,2) == [[1,2], [3,4], [5,6]]);
assert(list_to_matrix(v,3) == [[1,2,3], [4,5,6]]);
assert(list_to_matrix(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
2019-05-01 06:45:05 +00:00
}
test_list_to_matrix();
2019-05-01 06:45:05 +00:00
module test_flatten() {
assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
2020-07-28 18:02:35 +00:00
assert(flatten([]) == []);
2019-05-01 06:45:05 +00:00
}
test_flatten();
2020-07-28 18:02:35 +00:00
module test_full_flatten() {
assert(full_flatten([[1,2,3], [4,5,[6,[7],8]]]) == [1,2,3,4,5,6,7,8]);
assert(full_flatten([]) == []);
}
test_full_flatten();
module test_list_shape() {
assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);
assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 2) == 3);
assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9]]]) == [2,undef,3]);
assert(list_shape([1,2,3,4,5,6,7,8,9]) == [9]);
assert(list_shape([[1],[2],[3],[4],[5],[6],[7],[8],[9]]) == [9,1]);
assert(list_shape([]) == [0]);
assert(list_shape([[]]) == [1,0]);
assert(list_shape([[],[]]) == [2,0]);
assert(list_shape([[],[1]]) == [2,undef]);
2019-05-01 06:45:05 +00:00
}
test_list_shape();
2019-05-01 06:45:05 +00:00
// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap