diff --git a/tests/test_arrays.scad b/tests/test_arrays.scad index 8b7e2c5..8e888eb 100644 --- a/tests/test_arrays.scad +++ b/tests/test_arrays.scad @@ -66,8 +66,76 @@ module test_reverse() { test_reverse(); -// TODO: list_remove() -// TODO: list_insert() +module test_deduplicate() { + assert(deduplicate([8,3,4,4,4,8,2,3,3,8,8]) == [8,3,4,8,2,3,8]); + assert(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]) == [8,3,4,8,2,3]); + assert(deduplicate("Hello") == ["H","e","l","o"]); + assert(deduplicate([[3,4],[7,2],[7,1.99],[1,4]],eps=0.1) == [[3,4],[7,2],[1,4]]); +} +test_deduplicate(); + + +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]); +} +test_list_set(); + + +module test_list_remove() { + assert(list_insert([3,6,9,12],1) == [3,9,12]); + assert(list_insert([3,6,9,12],[1,3]) == [3,9]); +} +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_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]); +} +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]); +} +test_bselect(); + + +module test_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]); +} +test_bset(); + + +module test_list_increasing() { + assert(list_increasing([1,2,3,4]) == true); + assert(list_increasing([1,3,2,4]) == false); + assert(list_increasing([4,3,2,1]) == false); +} +test_list_increasing(); + + +module test_list_decreasing() { + assert(list_decreasing([1,2,3,4]) == false); + assert(list_decreasing([4,2,3,1]) == false); + assert(list_decreasing([4,3,2,1]) == true); +} +test_list_decreasing(); module test_list_shortest() { @@ -106,6 +174,16 @@ module test_list_fit() { test_list_fit(); +module test_idx() { + colors = ["red", "green", "blue", "cyan"]; + assert([for (i=idx(colors)) i] == [0,1,2,3]); + assert([for (i=idx(colors,end=-2)) i] == [0,1,2]); + assert([for (i=idx(colors,start=1)) i] == [1,2,3]); + assert([for (i=idx(colors,start=1,end=-2)) i] == [1,2]); +} +test_idx(); + + module test_enumerate() { assert(enumerate(["a","b","c"]) == [[0,"a"], [1,"b"], [2,"c"]]); assert(enumerate([[88,"a"],[76,"b"],[21,"c"]], idx=1) == [[0,"a"], [1,"b"], [2,"c"]]); @@ -114,6 +192,9 @@ module test_enumerate() { test_enumerate(); +// TODO: Add tests for shuffle() + + module test_sort() { assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]); assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"]) == ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]); @@ -174,6 +255,20 @@ module test_pair_wrap() { test_pair_wrap(); +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"]]); +} +test_triplet(); + + +module test_triplet_wrap() { + assert(triplet_wrap([3,4,5,6]) == [[3,4,5], [4,5,6], [5,6,3], [6,3,4]]); + assert(triplet_wrap("ABCD") == [["A","B","C"], ["B","C","D"], ["C","D","A"], ["D","A","B"]]); +} +test_triplet_wrap(); + + module test_zip() { v1 = [1,2,3,4]; v2 = [5,6,7]; diff --git a/tests/test_math.scad b/tests/test_math.scad index 776d048..35b9c45 100644 --- a/tests/test_math.scad +++ b/tests/test_math.scad @@ -13,6 +13,9 @@ module test_quant() { assert(quant(3,3) == 3); assert(quant(4,3) == 3); assert(quant(7,3) == 6); + assert(quant([12,13,13.1,14,14.1,15,16],4) == [12,12,12,16,16,16,16]) + assert(quant([9,10,10.4,10.5,11,12],3) == [9,9,9,12,12,12]) + assert(quant([[9,10,10.4],[10.5,11,12]],3) == [[9,9,9],[12,12,12]]) } test_quant(); @@ -28,6 +31,9 @@ module test_quantdn() { assert(quantdn(3,3) == 3); assert(quantdn(4,3) == 3); assert(quantdn(7,3) == 6); + assert(quantdn([12,13,13.1,14,14.1,15,16],4) == [12,12,12,12,12,12,16]) + assert(quantdn([9,10,10.4,10.5,11,12],3) == [9,9,9,9,9,12]) + assert(quantdn([[9,10,10.4],[10.5,11,12]],3) == [[9,9,9],[9,9,12]]) } test_quantdn(); @@ -43,6 +49,9 @@ module test_quantup() { assert(quantup(3,3) == 3); assert(quantup(4,3) == 6); assert(quantup(7,3) == 9); + assert(quantup([12,13,13.1,14,14.1,15,16],4) == [12,16,16,16,16,16,16]) + assert(quantup([9,10,10.4,10.5,11,12],3) == [9,12,12,12,12,12]) + assert(quantup([[9,10,10.4],[10.5,11,12]],3) == [[9,12,12],[12,12,12]]) } test_quantup(); @@ -120,6 +129,17 @@ module test_posmod() { test_posmod(); +module test_modang() { + assert(modang(-700,360) == 20) + assert(modang(-270,360) == 90) + assert(modang(-120,360) == -120) + assert(modang(120,360) == 120) + assert(modang(270,360) == -90) + assert(modang(700,360) == -20) +} +test_modang(); + + module test_modrange() { assert(modrange(-5,5,3) == [1,2]); assert(modrange(-1,4,3) == [2,0,1]); @@ -140,9 +160,19 @@ module test_sqr() { test_sqr(); +module test_log2() { + assert(log2(0.125) == -3); + assert(log2(16) == 4); + assert(log2(256) == 8); +} +test_log2(); + + +// TODO: Tests for rand_int() // TODO: Tests for gaussian_rand() // TODO: Tests for log_rand() + module test_segs() { assert(segs(50,$fn=8) == 8); assert(segs(50,$fa=2,$fs=2) == 158); @@ -235,6 +265,16 @@ module test_sum() { test_sum(); +module test_cumsum() { + assert(cumsum([1,1,1]) == [1,2,3]); + assert(cumsum([2,2,2]) == [2,4,6]); + assert(cumsum([1,2,3]) == [1,3,6]); + assert(cumsum([-2,-1,0,1,2]) == 0); + assert(cumsum([[1,2,3], [3,4,5], [5,6,7]]) == [[1,2,3],[4,6,8],[9,12,15]); +} +test_cumsum(); + + module test_sum_of_squares() { assert(sum_of_squares([1,2,3]) == 14); assert(sum_of_squares([1,2,4]) == 21); @@ -278,6 +318,28 @@ module test_mean() { test_mean(); +module test_det2() { + assert(det2([[6,-2], [1,8]]) == 50); + assert(det2([[4,7], [3,2]]) == -13); + assert(det2([[4,3], [3,4]]) == 7); +} +test_det2(); + + +module test_det3() { + M = [ [6,4,-2], [1,-2,8], [1,5,7] ]; + assert(det3(M) == -334); +} +test_det3(); + + +module test_determinant() { + M = [ [6,4,-2,9], [1,-2,8,3], [1,5,7,6], [4,2,5,1] ]; + assert(determinant(M) == 2267); +} +test_determinant(); + + // Logic diff --git a/tests/test_vectors.scad b/tests/test_vectors.scad index 1edc7bf..02cf746 100644 --- a/tests/test_vectors.scad +++ b/tests/test_vectors.scad @@ -1,6 +1,25 @@ include +module test_is_vector() { + assert(is_vector([1,2,3]) == true); + assert(is_vector([[1,2,3]]) == false); + assert(is_vector(["foo"]) == false); + assert(is_vector([]) == false); + assert(is_vector(1) == false); + assert(is_vector("foo") == false); + assert(is_vector(true) == false); +} +test_is_vector(); + + +module test_add_scalar() { + assert(add_scalar([1,2,3],3) == [4,5,6]); + assert(add_scalar([[1,2,3],[3,4,5]],3) == [[4,5,6],[6,7,8]]); +} +test_add_scalar() + + module test_vmul() { assert(vmul([3,4,5], [8,7,6]) == [24,28,30]); assert(vmul([1,2,3], [4,5,6]) == [4,10,18]);