diff --git a/arrays.scad b/arrays.scad index 221f4c5..56557f6 100644 --- a/arrays.scad +++ b/arrays.scad @@ -353,7 +353,7 @@ function subindex(v, idx) = [ // Example: // l = ["A","B","C",D"]; // echo([for (p=pair(l)) str(p.y,p.x)]); // Outputs: ["BA", "CB", "DC"] -function pair(v) = [for (i=[0:len(v)-1]) [v[i],v[i%len(v)]]]; +function pair(v) = [for (i=[0:len(v)-2]) [v[i],v[i+1]]]; // Function: pair_wrap() @@ -364,7 +364,7 @@ function pair(v) = [for (i=[0:len(v)-1]) [v[i],v[i%len(v)]]]; // Example: // l = ["A","B","C",D"]; // echo([for (p=pair_wrap(l)) str(p.y,p.x)]); // Outputs: ["BA", "CB", "DC", "AD"] -function pair_wrap(v) = [for (i=[0:len(v)-1]) [v[i],v[i%len(v)]]]; +function pair_wrap(v) = [for (i=[0:len(v)-1]) [v[i],v[(i+1)%len(v)]]]; // Function: zip() diff --git a/tests/test_arrays.scad b/tests/test_arrays.scad index 1eb3713..e386209 100644 --- a/tests/test_arrays.scad +++ b/tests/test_arrays.scad @@ -151,27 +151,41 @@ test_unique(); // Arrays -module test_array_subindex() { +module test_subindex() { v = [[1,2,3,4],[5,6,7,8],[9,10,11,12],[13,14,15,16]]; - assert(array_subindex(v,2) == [3, 7, 11, 15]); - assert(array_subindex(v,[2,1]) == [[3, 2], [7, 6], [11, 10], [15, 14]]); - assert(array_subindex(v,[1:3]) == [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]]); + assert(subindex(v,2) == [3, 7, 11, 15]); + assert(subindex(v,[2,1]) == [[3, 2], [7, 6], [11, 10], [15, 14]]); + assert(subindex(v,[1:3]) == [[2, 3, 4], [6, 7, 8], [10, 11, 12], [14, 15, 16]]); } -test_array_subindex(); +test_subindex(); -module test_array_zip() { +module test_pair() { + assert(pair([3,4,5,6]) == [[3,4], [4,5], [5,6]]); + assert(pair("ABCD") == [["A","B"], ["B","C"], ["C","D"]]); +} +test_pair(); + + +module test_pair_wrap() { + assert(pair_wrap([3,4,5,6]) == [[3,4], [4,5], [5,6], [6,3]]); + assert(pair_wrap("ABCD") == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]); +} +test_pair_wrap(); + + +module test_zip() { v1 = [1,2,3,4]; v2 = [5,6,7]; v3 = [8,9,10,11]; - assert(array_zip(v1,v3) == [[1,8],[2,9],[3,10],[4,11]]); - assert(array_zip([v1,v3]) == [[1,8],[2,9],[3,10],[4,11]]); - assert(array_zip([v1,v2],fit="short") == [[1,5],[2,6],[3,7]]); - assert(array_zip([v1,v2],fit="long") == [[1,5],[2,6],[3,7],[4,undef]]); - assert(array_zip([v1,v2],fit="long", fill=0) == [[1,5],[2,6],[3,7],[4,0]]); - assert(array_zip([v1,v2,v3],fit="long") == [[1,5,8],[2,6,9],[3,7,10],[4,undef,11]]); + assert(zip(v1,v3) == [[1,8],[2,9],[3,10],[4,11]]); + assert(zip([v1,v3]) == [[1,8],[2,9],[3,10],[4,11]]); + assert(zip([v1,v2],fit="short") == [[1,5],[2,6],[3,7]]); + assert(zip([v1,v2],fit="long") == [[1,5],[2,6],[3,7],[4,undef]]); + assert(zip([v1,v2],fit="long", fill=0) == [[1,5],[2,6],[3,7],[4,0]]); + assert(zip([v1,v2,v3],fit="long") == [[1,5,8],[2,6,9],[3,7,10],[4,undef,11]]); } -test_array_zip(); +test_zip(); module test_array_group() {