1include<../std.scad>
  2
  3module test_sort() {
  4    assert(sort([7,3,9,4,3,1,8]) == [1,3,3,4,7,8,9]);
  5    assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [20,[3,1],[3,9],[4],[4,0],[7],[8]]);
  6    assert(sort([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) == [[7],20,[4],[8],[4,0],[3,1],[3,9]]);
  7    assert(sort([[8,6],[3,1],[9,2],[4,3],[3,4],[1,5],[8,0]]) == [[1,5],[3,1],[3,4],[4,3],[8,0],[8,6],[9,2]]);
  8    assert(sort([[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]],idx=1) == [[8,0],[3,1],[9,2],[4,3],[3,4],[1,5],[8,6]]);
  9    assert(sort(["cat", "oat", "sat", "bat", "vat", "rat", "pat", "mat", "fat", "hat", "eat"]) 
 10           == ["bat", "cat", "eat", "fat", "hat", "mat", "oat", "pat", "rat", "sat", "vat"]);
 11    assert(sort([[0,[2,3,4]],[1,[1,2,3]],[2,[2,4,3]]],idx=1)==[[1,[1,2,3]], [0,[2,3,4]], [2,[2,4,3]]]);
 12    assert(sort([0,"1",[1,0],2,"a",[1]])== [0,2,"1","a",[1],[1,0]]);
 13    assert(sort([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])==  [["bat",2],["bat",3],["cat",1],["fat",3],["oat",0]]);
 14}
 15test_sort();
 16
 17
 18module test_sortidx() {
 19    assert(sortidx([3]) == [0]);
 20    assert(sortidx([]) == []);
 21    assert(sortidx([[5,6,7]])==[0]);
 22    assert(sortidx(["abc"]) == [0]);
 23    lst1 = ["da","bax","eaw","cav"];
 24    assert(sortidx(lst1) == [1,3,0,2]);
 25    lst5 = [3,5,1,7];
 26    assert(sortidx(lst5) == [2,0,1,3]);
 27    lst2 = [
 28        ["foo", 88, [0,0,1], false],
 29        ["bar", 90, [0,1,0], true],
 30        ["baz", 89, [1,0,0], false],
 31        ["qux", 23, [1,1,1], true]
 32    ];
 33    assert(sortidx(lst2, idx=1) == [3,0,2,1]);
 34    assert(sortidx(lst2, idx=0) == [1,2,0,3]);
 35    assert(sortidx(lst2, idx=[1,3]) == [3,0,2,1]);
 36    lst3 = [[-4,0,0],[0,0,-4],[0,-4,0],[-4,0,0],[0,-4,0],[0,0,4],
 37            [0,0,-4],[0,4,0],[4,0,0],[0,0,4],[0,4,0],[4,0,0]];
 38    assert(sortidx(lst3)==[0,3,2,4,1,6,5,9,7,10,8,11]);
 39    assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]]) == [3,5,2,4,0,1,6]);
 40    assert(sortidx([[4,0],[7],[3,9],20,[4],[3,1],[8]],idx=1) ==  [1,3,4,6,0,5,2]);
 41    lst4=[0,"1",[1,0],2,"a",[1]];
 42    assert(sortidx(lst4)== [0,3,1,4,5,2]);
 43    assert(sortidx(["cat","oat","sat","bat","vat","rat","pat","mat","fat","hat","eat"]) 
 44             == [3,0,10,8,9,7,1,6,5,2,4]);
 45    assert(sortidx([["oat",0], ["cat",1], ["bat",3], ["bat",2], ["fat",3]])==  [3,2,1,4,0]);
 46    assert(sortidx(["Belfry", "OpenScad", "Library", "Documentation"])==[0,3,2,1]);
 47    assert(sortidx(["x",1,[],0,"abc",true])==[5,3,1,4,0,2]);
 48}
 49test_sortidx();
 50
 51module test_group_sort() {
 52    assert_equal(group_sort([]), [[]]);
 53    assert_equal(group_sort([8]), [[8]]);
 54    assert_equal(group_sort([7,3,9,4,3,1,8]), [[1], [3, 3], [4], [7], [8], [9]]);
 55    assert_equal(group_sort([[5,"a"],[2,"b"], [5,"c"], [3,"d"], [2,"e"] ], idx=0), [[[2, "b"], [2, "e"]], [[3, "d"]], [[5, "a"], [5, "c"]]]);
 56    assert_equal(group_sort([["a",5],["b",6], ["c",1], ["d",2], ["e",6] ], idx=1), [[["c", 1]], [["d", 2]], [["a", 5]], [["b", 6], ["e", 6]]] );
 57}
 58test_group_sort();
 59
 60
 61module test_unique() {
 62    assert_equal(unique([]), []);
 63    assert_equal(unique([8]), [8]);
 64    assert_equal(unique([7,3,9,4,3,1,8]), [1,3,4,7,8,9]);
 65    assert_equal(unique(["A","B","R","A","C","A","D","A","B","R","A"]), ["A", "B", "C", "D", "R"]);
 66}
 67test_unique();
 68
 69
 70module test_unique_count() {
 71    assert_equal(
 72        unique_count([3,1,4,1,5,9,2,6,5,3,5,8,9,7,9,3,2,3,6]),
 73        [[1,2,3,4,5,6,7,8,9],[2,2,4,1,3,2,1,1,3]]
 74    );
 75    assert_equal(
 76        unique_count(["A","B","R","A","C","A","D","A","B","R","A"]),
 77        [["A","B","C","D","R"],[5,2,1,1,2]]
 78    );
 79}
 80test_unique_count();
 81
 82
 83
 84module test_is_increasing() {
 85    assert(is_increasing([1,2,3,4]) == true);
 86    assert(is_increasing([1,2,2,2]) == true);
 87    assert(is_increasing([1,3,2,4]) == false);
 88    assert(is_increasing([4,3,2,1]) == false);
 89    assert(is_increasing([1,2,3,4],strict=true) == true);
 90    assert(is_increasing([1,2,2,2],strict=true) == false);
 91    assert(is_increasing([1,3,2,4],strict=true) == false);
 92    assert(is_increasing([4,3,2,1],strict=true) == false);
 93    assert(is_increasing(["AB","BC","DF"]) == true);
 94    assert(is_increasing(["AB","DC","CF"]) == false);    
 95    assert(is_increasing([[1,2],[1,4],[2,3],[2,2]])==false);
 96    assert(is_increasing([[1,2],[1,4],[2,3],[2,3]])==true);
 97    assert(is_increasing([[1,2],[1,4],[2,3],[2,3]],strict=true)==false);
 98    assert(is_increasing("ABCFZ")==true);
 99    assert(is_increasing("ZYWRA")==false);    
100}
101test_is_increasing();
102
103
104module test_is_decreasing() {
105    assert(is_decreasing([1,2,3,4]) == false);
106    assert(is_decreasing([4,2,3,1]) == false);
107    assert(is_decreasing([4,2,2,1]) == true);
108    assert(is_decreasing([4,3,2,1]) == true);
109    assert(is_decreasing([1,2,3,4],strict=true) == false);
110    assert(is_decreasing([4,2,3,1],strict=true) == false);
111    assert(is_decreasing([4,2,2,1],strict=true) == false);
112    assert(is_decreasing([4,3,2,1],strict=true) == true);
113    assert(is_decreasing(reverse(["AB","BC","DF"])) == true);
114    assert(is_decreasing(reverse(["AB","DC","CF"])) == false);    
115    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,2]]))==false);
116    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]))==true);
117    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]),strict=true)==false);
118    assert(is_decreasing("ABCFZ")==false);
119    assert(is_decreasing("ZYWRA")==true);    
120}
121test_is_decreasing();
122
123
124module test_find_approx() {
125    assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2);
126    assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]);
127    assert(find_approx(1, [2,3,4])==undef);
128    assert(find_approx(1, [2,3,4],all=true)==[]);
129    assert(find_approx(1, [])==undef);
130    assert(find_approx(1, [], all=true)==[]);
131}
132test_find_approx();
133    
134
135
136module test_deduplicate() {
137    assert_equal(deduplicate([8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3,8]);
138    assert_equal(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3]);
139    assert_equal(deduplicate("Hello"), "Helo");
140    assert_equal(deduplicate([[3,4],[7,1.99],[7,2],[1,4]],eps=0.1), [[3,4],[7,2],[1,4]]);
141    assert_equal(deduplicate([], closed=true), []);
142    assert_equal(deduplicate([[1,[1,[undef]]],[1,[1,[undef]]],[1,[2]],[1,[2,[0]]]]), [[1, [1,[undef]]],[1,[2]],[1,[2,[0]]]]);
143}
144test_deduplicate();
145
146
147module test_deduplicate_indexed() {
148    assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1]) == [1,4,1,2,0,1]);
149    assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1], closed=true) == [1,4,1,2,0]);
150}
151test_deduplicate_indexed();
152
153module test_all_zero() {
154    assert(all_zero(0));
155    assert(all_zero([0,0,0]));
156    assert(!all_zero([[0,0,0],[0,0]]));
157    assert(all_zero([EPSILON/2,EPSILON/2,EPSILON/2]));
158    assert(!all_zero(1e-3));
159    assert(!all_zero([0,0,1e-3]));
160    assert(!all_zero([EPSILON*10,0,0]));
161    assert(!all_zero([0,EPSILON*10,0]));
162    assert(!all_zero([0,0,EPSILON*10]));
163    assert(!all_zero(true));
164    assert(!all_zero(false));
165    assert(!all_zero(INF));
166    assert(!all_zero(-INF));
167    assert(!all_zero(NAN));
168    assert(!all_zero("foo"));
169    assert(!all_zero([]));
170    assert(!all_zero([0:1:2]));
171}
172test_all_zero();
173
174
175module test_all_equal() {
176    assert(all_equal([1,1,1,1]));
177    assert(all_equal([[3,4],[3,4],[3,4]]));
178    assert(!all_equal([1,2,1,1]));
179    assert(!all_equal([1,1.001,1,1.001,.999]));
180    assert(all_equal([1,1.001,1,1.001,.999],eps=.01));
181}
182test_all_equal();
183
184
185module test_all_nonzero() {
186    assert(!all_nonzero(0));
187    assert(!all_nonzero([0,0,0]));
188    assert(!all_nonzero([[0,0,0],[0,0]]));
189    assert(!all_nonzero([EPSILON/2,EPSILON/2,EPSILON/2]));
190    assert(all_nonzero(1e-3));
191    assert(!all_nonzero([0,0,1e-3]));
192    assert(!all_nonzero([EPSILON*10,0,0]));
193    assert(!all_nonzero([0,EPSILON*10,0]));
194    assert(!all_nonzero([0,0,EPSILON*10]));
195    assert(all_nonzero([1e-3,1e-3,1e-3]));
196    assert(all_nonzero([EPSILON*10,EPSILON*10,EPSILON*10]));
197    assert(!all_nonzero(true));
198    assert(!all_nonzero(false));
199    assert(!all_nonzero(INF));
200    assert(!all_nonzero(-INF));
201    assert(!all_nonzero(NAN));
202    assert(!all_nonzero("foo"));
203    assert(!all_nonzero([]));
204    assert(!all_nonzero([0:1:2]));
205}
206test_all_nonzero();
207
208
209module test_all_positive() {
210    assert(!all_positive(-2));
211    assert(!all_positive(0));
212    assert(all_positive(2));
213    assert(!all_positive([0,0,0]));
214    assert(!all_positive([0,1,2]));
215    assert(all_positive([3,1,2]));
216    assert(!all_positive([3,-1,2]));
217    assert(!all_positive([]));
218    assert(!all_positive(true));
219    assert(!all_positive(false));
220    assert(!all_positive("foo"));
221    assert(!all_positive([0:1:2]));
222}
223test_all_positive();
224
225
226module test_all_negative() {
227    assert(all_negative(-2));
228    assert(!all_negative(0));
229    assert(!all_negative(2));
230    assert(!all_negative([0,0,0]));
231    assert(!all_negative([0,1,2]));
232    assert(!all_negative([3,1,2]));
233    assert(!all_negative([3,-1,2]));
234    assert(all_negative([-3,-1,-2]));
235    assert(!all_negative([-3,1,-2]));
236    assert(!all_negative([[-5,-7],[-3,-1,-2]]));
237    assert(!all_negative([[-5,-7],[-3,1,-2]]));
238    assert(!all_negative([]));
239    assert(!all_negative(true));
240    assert(!all_negative(false));
241    assert(!all_negative("foo"));
242    assert(!all_negative([0:1:2]));
243}
244test_all_negative();
245
246
247module test_all_nonpositive() {
248    assert(all_nonpositive(-2));
249    assert(all_nonpositive(0));
250    assert(!all_nonpositive(2));
251    assert(all_nonpositive([0,0,0]));
252    assert(!all_nonpositive([0,1,2]));
253    assert(all_nonpositive([0,-1,-2]));
254    assert(!all_nonpositive([3,1,2]));
255    assert(!all_nonpositive([3,-1,2]));
256    assert(!all_nonpositive([]));
257    assert(!all_nonpositive(true));
258    assert(!all_nonpositive(false));
259    assert(!all_nonpositive("foo"));
260    assert(!all_nonpositive([0:1:2]));
261}
262test_all_nonpositive();
263
264
265module test_all_nonnegative() {
266    assert(!all_nonnegative(-2));
267    assert(all_nonnegative(0));
268    assert(all_nonnegative(2));
269    assert(all_nonnegative([0,0,0]));
270    assert(all_nonnegative([0,1,2]));
271    assert(all_nonnegative([3,1,2]));
272    assert(!all_nonnegative([3,-1,2]));
273    assert(!all_nonnegative([-3,-1,-2]));
274    assert(!all_nonnegative([[-5,-7],[-3,-1,-2]]));
275    assert(!all_nonnegative([[-5,-7],[-3,1,-2]]));
276    assert(!all_nonnegative([[5,7],[3,-1,2]]));
277    assert(!all_nonnegative([[5,7],[3,1,2]]));
278    assert(!all_nonnegative([]));
279    assert(!all_nonnegative(true));
280    assert(!all_nonnegative(false));
281    assert(!all_nonnegative("foo"));
282    assert(!all_nonnegative([0:1:2]));
283}
284test_all_nonnegative();
285
286
287module test_approx() {
288    assert_equal(approx(PI, 3.141592653589793236), true);
289    assert_equal(approx(PI, 3.1415926), false);
290    assert_equal(approx(PI, 3.1415926, eps=1e-6), true);
291    assert_equal(approx(-PI, -3.141592653589793236), true);
292    assert_equal(approx(-PI, -3.1415926), false);
293    assert_equal(approx(-PI, -3.1415926, eps=1e-6), true);
294    assert_equal(approx(1/3, 0.3333333333), true);
295    assert_equal(approx(-1/3, -0.3333333333), true);
296    assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true);
297    assert_equal(approx([1,[1,undef]], [1+1e-12,[1,true]]), false);
298    assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true);
299}
300test_approx();
301
302
303
304module test_group_data() {
305    assert_equal(group_data([1,2,0], ["A","B","C"]), [["C"],["A"],["B"]]);
306    assert_equal(group_data([1,3,0], ["A","B","C"]), [["C"],["A"],[],["B"]]);
307    assert_equal(group_data([5,3,1], ["A","B","C"]), [[],["C"],[],["B"],[],["A"]]);
308    assert_equal(group_data([1,3,1], ["A","B","C"]), [[],["A","C"],[],["B"]]);
309}
310test_group_data();
311
312
313module test_compare_vals() {
314    assert(compare_vals(-10,0) < 0);
315    assert(compare_vals(10,0) > 0);
316    assert(compare_vals(10,10) == 0);
317
318    assert(compare_vals("abc","abcd") < 0);
319    assert(compare_vals("abcd","abc") > 0);
320    assert(compare_vals("abcd","abcd") == 0);
321
322    assert(compare_vals(false,false) == 0);
323    assert(compare_vals(true,false) > 0);
324    assert(compare_vals(false,true) < 0);
325    assert(compare_vals(true,true) == 0);
326
327    assert(compare_vals([2,3,4], [2,3,4,5]) < 0);
328    assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0);
329    assert(compare_vals([2,3,4,5], [2,3,4]) > 0);
330    assert(compare_vals([2,3,4,5], [2,3,5,5]) < 0);
331    assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) < 0);
332
333    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
334    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
335    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
336    assert(compare_vals([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
337    assert(compare_vals([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
338    assert(compare_vals([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
339    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
340
341    assert(compare_vals(undef, undef) == 0);
342    assert(compare_vals(undef, true) < 0);
343    assert(compare_vals(undef, 0) < 0);
344    assert(compare_vals(undef, "foo") < 0);
345    assert(compare_vals(undef, [2,3,4]) < 0);
346    assert(compare_vals(undef, [0:3]) < 0);
347
348    assert(compare_vals(true, undef) > 0);
349    assert(compare_vals(true, true) == 0);
350    assert(compare_vals(true, 0) < 0);
351    assert(compare_vals(true, "foo") < 0);
352    assert(compare_vals(true, [2,3,4]) < 0);
353    assert(compare_vals(true, [0:3]) < 0);
354
355    assert(compare_vals(0, undef) > 0);
356    assert(compare_vals(0, true) > 0);
357    assert(compare_vals(0, 0) == 0);
358    assert(compare_vals(0, "foo") < 0);
359    assert(compare_vals(0, [2,3,4]) < 0);
360    assert(compare_vals(0, [0:3]) < 0);
361
362    assert(compare_vals(1, undef) > 0);
363    assert(compare_vals(1, true) > 0);
364    assert(compare_vals(1, 1) == 0);
365    assert(compare_vals(1, "foo") < 0);
366    assert(compare_vals(1, [2,3,4]) < 0);
367    assert(compare_vals(1, [0:3]) < 0);
368
369    assert(compare_vals("foo", undef) > 0);
370    assert(compare_vals("foo", true) > 0);
371    assert(compare_vals("foo", 1) > 0);
372    assert(compare_vals("foo", "foo") == 0);
373    assert(compare_vals("foo", [2,3,4]) < 0);
374    assert(compare_vals("foo", [0:3]) < 0);
375
376    assert(compare_vals([2,3,4], undef) > 0);
377    assert(compare_vals([2,3,4], true) > 0);
378    assert(compare_vals([2,3,4], 1) > 0);
379    assert(compare_vals([2,3,4], "foo") > 0);
380    assert(compare_vals([2,3,4], [2,3,4]) == 0);
381    assert(compare_vals([2,3,4], [0:3]) < 0);
382
383    assert(compare_vals([0:3], undef) > 0);
384    assert(compare_vals([0:3], true) > 0);
385    assert(compare_vals([0:3], 1) > 0);
386    assert(compare_vals([0:3], "foo") > 0);
387    assert(compare_vals([0:3], [2,3,4]) > 0);
388    assert(compare_vals([0:3], [0:3]) == 0);
389}
390test_compare_vals();
391
392
393module test_compare_lists() {
394    assert(compare_lists([2,3,4], [2,3,4,5]) < 0);
395    assert(compare_lists([2,3,4,5], [2,3,4,5]) == 0);
396    assert(compare_lists([2,3,4,5], [2,3,4]) > 0);
397    assert(compare_lists([2,3,4,5], [2,3,5,5]) < 0);
398
399    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
400    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
401    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
402    assert(compare_lists([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
403    assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
404    assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
405    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
406
407    assert(compare_lists("cat", "bat") > 0);
408    assert(compare_lists(["cat"], ["bat"]) > 0);
409}
410test_compare_lists();
411
412
413
414module test_min_index() {
415    assert(min_index([5,3,9,6,2,7,8,2,1])==8);
416    assert(min_index([5,3,9,6,2,7,8,2,7],all=true)==[4,7]);
417}
418test_min_index();
419
420
421module test_max_index() {
422    assert(max_index([5,3,9,6,2,7,8,9,1])==2);
423    assert(max_index([5,3,9,6,2,7,8,9,7],all=true)==[2,7]);
424}
425test_max_index();
426
427