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
 84
 85module test_list_wrap() {
 86    assert(list_wrap([[1,2,3],[4,5,6],[1,8,9]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]);
 87    assert(list_wrap([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]) == [[1,2,3],[4,5,6],[1,8,9],[1,2,3]]);
 88    assert(list_wrap([])==[]);
 89    assert(list_wrap([3])==[3]);
 90}
 91test_list_wrap();
 92
 93
 94module test_list_unwrap() {
 95    assert(list_unwrap([[1,2,3],[4,5,6],[1,8,9]]) == [[1,2,3],[4,5,6],[1,8,9]]);
 96    assert(list_unwrap([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]) == [[1,2,3],[4,5,6],[1,8,9]]);
 97    assert(list_unwrap([])==[]);
 98    assert(list_unwrap([3])==[3]);
 99}
100test_list_unwrap();
101
102
103
104module test_is_increasing() {
105    assert(is_increasing([1,2,3,4]) == true);
106    assert(is_increasing([1,2,2,2]) == true);
107    assert(is_increasing([1,3,2,4]) == false);
108    assert(is_increasing([4,3,2,1]) == false);
109    assert(is_increasing([1,2,3,4],strict=true) == true);
110    assert(is_increasing([1,2,2,2],strict=true) == false);
111    assert(is_increasing([1,3,2,4],strict=true) == false);
112    assert(is_increasing([4,3,2,1],strict=true) == false);
113    assert(is_increasing(["AB","BC","DF"]) == true);
114    assert(is_increasing(["AB","DC","CF"]) == false);    
115    assert(is_increasing([[1,2],[1,4],[2,3],[2,2]])==false);
116    assert(is_increasing([[1,2],[1,4],[2,3],[2,3]])==true);
117    assert(is_increasing([[1,2],[1,4],[2,3],[2,3]],strict=true)==false);
118    assert(is_increasing("ABCFZ")==true);
119    assert(is_increasing("ZYWRA")==false);    
120}
121test_is_increasing();
122
123
124module test_is_decreasing() {
125    assert(is_decreasing([1,2,3,4]) == false);
126    assert(is_decreasing([4,2,3,1]) == false);
127    assert(is_decreasing([4,2,2,1]) == true);
128    assert(is_decreasing([4,3,2,1]) == true);
129    assert(is_decreasing([1,2,3,4],strict=true) == false);
130    assert(is_decreasing([4,2,3,1],strict=true) == false);
131    assert(is_decreasing([4,2,2,1],strict=true) == false);
132    assert(is_decreasing([4,3,2,1],strict=true) == true);
133    assert(is_decreasing(reverse(["AB","BC","DF"])) == true);
134    assert(is_decreasing(reverse(["AB","DC","CF"])) == false);    
135    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,2]]))==false);
136    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]))==true);
137    assert(is_decreasing(reverse([[1,2],[1,4],[2,3],[2,3]]),strict=true)==false);
138    assert(is_decreasing("ABCFZ")==false);
139    assert(is_decreasing("ZYWRA")==true);    
140}
141test_is_decreasing();
142
143
144
145module test_are_ends_equal() {
146    assert(!are_ends_equal([[1,2,3],[4,5,6],[1,8,9]]));
147    assert(are_ends_equal([[1,2,3],[4,5,6],[1,8,9],[1,2,3]]));
148    assert(are_ends_equal([1,2,3,1.00004],eps=1e-2));
149    assert(are_ends_equal([3]));
150}
151test_are_ends_equal();
152
153
154
155
156module test_find_approx() {
157    assert(find_approx(1, [2,3,1.05,4,1,2,.99], eps=.1)==2);
158    assert(find_approx(1, [2,3,1.05,4,1,2,.99], all=true, eps=.1)==[2,4,6]);
159    assert(find_approx(1, [2,3,4])==undef);
160    assert(find_approx(1, [2,3,4],all=true)==[]);
161    assert(find_approx(1, [])==undef);
162    assert(find_approx(1, [], all=true)==[]);
163}
164test_find_approx();
165    
166
167
168module test_deduplicate() {
169    assert_equal(deduplicate([8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3,8]);
170    assert_equal(deduplicate(closed=true, [8,3,4,4,4,8,2,3,3,8,8]), [8,3,4,8,2,3]);
171    assert_equal(deduplicate("Hello"), "Helo");
172    assert_equal(deduplicate([[3,4],[7,1.99],[7,2],[1,4]],eps=0.1), [[3,4],[7,2],[1,4]]);
173    assert_equal(deduplicate([], closed=true), []);
174    assert_equal(deduplicate([[1,[1,[undef]]],[1,[1,[undef]]],[1,[2]],[1,[2,[0]]]]), [[1, [1,[undef]]],[1,[2]],[1,[2,[0]]]]);
175}
176test_deduplicate();
177
178
179module test_deduplicate_indexed() {
180    assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1]) == [1,4,1,2,0,1]);
181    assert(deduplicate_indexed([8,6,4,6,3], [1,4,3,1,2,2,0,1], closed=true) == [1,4,1,2,0]);
182}
183test_deduplicate_indexed();
184
185module test_all_zero() {
186    assert(all_zero(0));
187    assert(all_zero([0,0,0]));
188    assert(!all_zero([[0,0,0],[0,0]]));
189    assert(all_zero([EPSILON/2,EPSILON/2,EPSILON/2]));
190    assert(!all_zero(1e-3));
191    assert(!all_zero([0,0,1e-3]));
192    assert(!all_zero([EPSILON*10,0,0]));
193    assert(!all_zero([0,EPSILON*10,0]));
194    assert(!all_zero([0,0,EPSILON*10]));
195    assert(!all_zero(true));
196    assert(!all_zero(false));
197    assert(!all_zero(INF));
198    assert(!all_zero(-INF));
199    assert(!all_zero(NAN));
200    assert(!all_zero("foo"));
201    assert(!all_zero([]));
202    assert(!all_zero([0:1:2]));
203}
204test_all_zero();
205
206
207module test_all_equal() {
208    assert(all_equal([1,1,1,1]));
209    assert(all_equal([[3,4],[3,4],[3,4]]));
210    assert(!all_equal([1,2,1,1]));
211    assert(!all_equal([1,1.001,1,1.001,.999]));
212    assert(all_equal([1,1.001,1,1.001,.999],eps=.01));
213}
214test_all_equal();
215
216
217module test_all_nonzero() {
218    assert(!all_nonzero(0));
219    assert(!all_nonzero([0,0,0]));
220    assert(!all_nonzero([[0,0,0],[0,0]]));
221    assert(!all_nonzero([EPSILON/2,EPSILON/2,EPSILON/2]));
222    assert(all_nonzero(1e-3));
223    assert(!all_nonzero([0,0,1e-3]));
224    assert(!all_nonzero([EPSILON*10,0,0]));
225    assert(!all_nonzero([0,EPSILON*10,0]));
226    assert(!all_nonzero([0,0,EPSILON*10]));
227    assert(all_nonzero([1e-3,1e-3,1e-3]));
228    assert(all_nonzero([EPSILON*10,EPSILON*10,EPSILON*10]));
229    assert(!all_nonzero(true));
230    assert(!all_nonzero(false));
231    assert(!all_nonzero(INF));
232    assert(!all_nonzero(-INF));
233    assert(!all_nonzero(NAN));
234    assert(!all_nonzero("foo"));
235    assert(!all_nonzero([]));
236    assert(!all_nonzero([0:1:2]));
237}
238test_all_nonzero();
239
240
241module test_all_positive() {
242    assert(!all_positive(-2));
243    assert(!all_positive(0));
244    assert(all_positive(2));
245    assert(!all_positive([0,0,0]));
246    assert(!all_positive([0,1,2]));
247    assert(all_positive([3,1,2]));
248    assert(!all_positive([3,-1,2]));
249    assert(!all_positive([]));
250    assert(!all_positive(true));
251    assert(!all_positive(false));
252    assert(!all_positive("foo"));
253    assert(!all_positive([0:1:2]));
254}
255test_all_positive();
256
257
258module test_all_negative() {
259    assert(all_negative(-2));
260    assert(!all_negative(0));
261    assert(!all_negative(2));
262    assert(!all_negative([0,0,0]));
263    assert(!all_negative([0,1,2]));
264    assert(!all_negative([3,1,2]));
265    assert(!all_negative([3,-1,2]));
266    assert(all_negative([-3,-1,-2]));
267    assert(!all_negative([-3,1,-2]));
268    assert(!all_negative([[-5,-7],[-3,-1,-2]]));
269    assert(!all_negative([[-5,-7],[-3,1,-2]]));
270    assert(!all_negative([]));
271    assert(!all_negative(true));
272    assert(!all_negative(false));
273    assert(!all_negative("foo"));
274    assert(!all_negative([0:1:2]));
275}
276test_all_negative();
277
278
279module test_all_nonpositive() {
280    assert(all_nonpositive(-2));
281    assert(all_nonpositive(0));
282    assert(!all_nonpositive(2));
283    assert(all_nonpositive([0,0,0]));
284    assert(!all_nonpositive([0,1,2]));
285    assert(all_nonpositive([0,-1,-2]));
286    assert(!all_nonpositive([3,1,2]));
287    assert(!all_nonpositive([3,-1,2]));
288    assert(!all_nonpositive([]));
289    assert(!all_nonpositive(true));
290    assert(!all_nonpositive(false));
291    assert(!all_nonpositive("foo"));
292    assert(!all_nonpositive([0:1:2]));
293}
294test_all_nonpositive();
295
296
297module test_all_nonnegative() {
298    assert(!all_nonnegative(-2));
299    assert(all_nonnegative(0));
300    assert(all_nonnegative(2));
301    assert(all_nonnegative([0,0,0]));
302    assert(all_nonnegative([0,1,2]));
303    assert(all_nonnegative([3,1,2]));
304    assert(!all_nonnegative([3,-1,2]));
305    assert(!all_nonnegative([-3,-1,-2]));
306    assert(!all_nonnegative([[-5,-7],[-3,-1,-2]]));
307    assert(!all_nonnegative([[-5,-7],[-3,1,-2]]));
308    assert(!all_nonnegative([[5,7],[3,-1,2]]));
309    assert(!all_nonnegative([[5,7],[3,1,2]]));
310    assert(!all_nonnegative([]));
311    assert(!all_nonnegative(true));
312    assert(!all_nonnegative(false));
313    assert(!all_nonnegative("foo"));
314    assert(!all_nonnegative([0:1:2]));
315}
316test_all_nonnegative();
317
318
319module test_approx() {
320    assert_equal(approx(PI, 3.141592653589793236), true);
321    assert_equal(approx(PI, 3.1415926), false);
322    assert_equal(approx(PI, 3.1415926, eps=1e-6), true);
323    assert_equal(approx(-PI, -3.141592653589793236), true);
324    assert_equal(approx(-PI, -3.1415926), false);
325    assert_equal(approx(-PI, -3.1415926, eps=1e-6), true);
326    assert_equal(approx(1/3, 0.3333333333), true);
327    assert_equal(approx(-1/3, -0.3333333333), true);
328    assert_equal(approx(10*[cos(30),sin(30)], 10*[sqrt(3)/2, 1/2]), true);
329    assert_equal(approx([1,[1,undef]], [1+1e-12,[1,true]]), false);
330    assert_equal(approx([1,[1,undef]], [1+1e-12,[1,undef]]), true);
331}
332test_approx();
333
334
335
336module test_group_data() {
337    assert_equal(group_data([1,2,0], ["A","B","C"]), [["C"],["A"],["B"]]);
338    assert_equal(group_data([1,3,0], ["A","B","C"]), [["C"],["A"],[],["B"]]);
339    assert_equal(group_data([5,3,1], ["A","B","C"]), [[],["C"],[],["B"],[],["A"]]);
340    assert_equal(group_data([1,3,1], ["A","B","C"]), [[],["A","C"],[],["B"]]);
341}
342test_group_data();
343
344
345module test_compare_vals() {
346    assert(compare_vals(-10,0) < 0);
347    assert(compare_vals(10,0) > 0);
348    assert(compare_vals(10,10) == 0);
349
350    assert(compare_vals("abc","abcd") < 0);
351    assert(compare_vals("abcd","abc") > 0);
352    assert(compare_vals("abcd","abcd") == 0);
353
354    assert(compare_vals(false,false) == 0);
355    assert(compare_vals(true,false) > 0);
356    assert(compare_vals(false,true) < 0);
357    assert(compare_vals(true,true) == 0);
358
359    assert(compare_vals([2,3,4], [2,3,4,5]) < 0);
360    assert(compare_vals([2,3,4,5], [2,3,4,5]) == 0);
361    assert(compare_vals([2,3,4,5], [2,3,4]) > 0);
362    assert(compare_vals([2,3,4,5], [2,3,5,5]) < 0);
363    assert(compare_vals([[2,3,4,5]], [[2,3,5,5]]) < 0);
364
365    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
366    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
367    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
368    assert(compare_vals([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
369    assert(compare_vals([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
370    assert(compare_vals([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
371    assert(compare_vals([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
372
373    assert(compare_vals(undef, undef) == 0);
374    assert(compare_vals(undef, true) < 0);
375    assert(compare_vals(undef, 0) < 0);
376    assert(compare_vals(undef, "foo") < 0);
377    assert(compare_vals(undef, [2,3,4]) < 0);
378    assert(compare_vals(undef, [0:3]) < 0);
379
380    assert(compare_vals(true, undef) > 0);
381    assert(compare_vals(true, true) == 0);
382    assert(compare_vals(true, 0) < 0);
383    assert(compare_vals(true, "foo") < 0);
384    assert(compare_vals(true, [2,3,4]) < 0);
385    assert(compare_vals(true, [0:3]) < 0);
386
387    assert(compare_vals(0, undef) > 0);
388    assert(compare_vals(0, true) > 0);
389    assert(compare_vals(0, 0) == 0);
390    assert(compare_vals(0, "foo") < 0);
391    assert(compare_vals(0, [2,3,4]) < 0);
392    assert(compare_vals(0, [0:3]) < 0);
393
394    assert(compare_vals(1, undef) > 0);
395    assert(compare_vals(1, true) > 0);
396    assert(compare_vals(1, 1) == 0);
397    assert(compare_vals(1, "foo") < 0);
398    assert(compare_vals(1, [2,3,4]) < 0);
399    assert(compare_vals(1, [0:3]) < 0);
400
401    assert(compare_vals("foo", undef) > 0);
402    assert(compare_vals("foo", true) > 0);
403    assert(compare_vals("foo", 1) > 0);
404    assert(compare_vals("foo", "foo") == 0);
405    assert(compare_vals("foo", [2,3,4]) < 0);
406    assert(compare_vals("foo", [0:3]) < 0);
407
408    assert(compare_vals([2,3,4], undef) > 0);
409    assert(compare_vals([2,3,4], true) > 0);
410    assert(compare_vals([2,3,4], 1) > 0);
411    assert(compare_vals([2,3,4], "foo") > 0);
412    assert(compare_vals([2,3,4], [2,3,4]) == 0);
413    assert(compare_vals([2,3,4], [0:3]) < 0);
414
415    assert(compare_vals([0:3], undef) > 0);
416    assert(compare_vals([0:3], true) > 0);
417    assert(compare_vals([0:3], 1) > 0);
418    assert(compare_vals([0:3], "foo") > 0);
419    assert(compare_vals([0:3], [2,3,4]) > 0);
420    assert(compare_vals([0:3], [0:3]) == 0);
421}
422test_compare_vals();
423
424
425module test_compare_lists() {
426    assert(compare_lists([2,3,4], [2,3,4,5]) < 0);
427    assert(compare_lists([2,3,4,5], [2,3,4,5]) == 0);
428    assert(compare_lists([2,3,4,5], [2,3,4]) > 0);
429    assert(compare_lists([2,3,4,5], [2,3,5,5]) < 0);
430
431    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5]]) == 0);
432    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4,5], [3,4,5]]) < 0);
433    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,4,5,6]]) < 0);
434    assert(compare_lists([[2,3,4,5],[3,4,5]], [[2,3,4], [3,4,5]]) > 0);
435    assert(compare_lists([[2,3,4],[3,4,5,6]], [[2,3,4], [3,4,5]]) > 0);
436    assert(compare_lists([[2,3,4],[3,5,5]], [[2,3,4], [3,4,5]]) > 0);
437    assert(compare_lists([[2,3,4],[3,4,5]], [[2,3,4], [3,5,5]]) < 0);
438
439    assert(compare_lists("cat", "bat") > 0);
440    assert(compare_lists(["cat"], ["bat"]) > 0);
441}
442test_compare_lists();
443
444
445
446module test_min_index() {
447    assert(min_index([5,3,9,6,2,7,8,2,1])==8);
448    assert(min_index([5,3,9,6,2,7,8,2,7],all=true)==[4,7]);
449}
450test_min_index();
451
452
453module test_max_index() {
454    assert(max_index([5,3,9,6,2,7,8,9,1])==2);
455    assert(max_index([5,3,9,6,2,7,8,9,7],all=true)==[2,7]);
456}
457test_max_index();
458
459