1include <../std.scad>
  2
  3
  4// Section: List Query Operations
  5
  6module test_is_homogeneous(){
  7    assert(is_homogeneous([[1,["a"]], [2,["b"]]])==true);
  8    assert(is_homogeneous([[1,["a"]], [2,[true]]])==false);
  9    assert(is_homogeneous([[1,["a"]], [2,[true]]],1)==true);
 10    assert(is_homogeneous([[1,["a"]], [2,[true]]],2)==false);
 11    assert(is_homogeneous([[1,["a"]], [true,["b"]]])==false);
 12}
 13test_is_homogeneous();
 14
 15
 16module test_select() {
 17    l = [3,4,5,6,7,8,9];
 18    assert(select(l, 5, 6) == [8,9]);
 19    assert(select(l, 5, 8) == [8,9,3,4]);
 20    assert(select(l, 5, 2) == [8,9,3,4,5]);
 21    assert(select(l, -3, -1) == [7,8,9]);
 22    assert(select(l, 3, 3) == [6]);
 23    assert(select(l, 4) == 7);
 24    assert(select(l, -2) == 8);
 25    assert(select(l, [1:3]) == [4,5,6]);
 26    assert(select(l, [1,3]) == [4,6]);
 27}
 28test_select();
 29
 30
 31module test_slice() {
 32    l = [3,4,5,6,7,8,9];
 33    assert(slice(l, 5, 6) == [8,9]);
 34    assert(slice(l, 5, 8) == [8,9]);
 35    assert(slice(l, 5, 2) == []);
 36    assert(slice(l, -3, -1) == [7,8,9]);
 37    assert(slice(l, 3, 3) == [6]);
 38    assert(slice(l, 4) == [7,8,9]);
 39    assert(slice(l, -2) == [8,9]);
 40}
 41test_slice();
 42
 43
 44module test_last() {
 45    list = [1,2,3,4];
 46    assert(last(list)==4);
 47    assert(last([])==undef);
 48}
 49test_last();
 50
 51
 52module test_list_head() {
 53    list = [1,2,3,4];
 54    assert_equal(list_head(list), [1,2,3]);
 55    assert_equal(list_head([1]), []);
 56    assert_equal(list_head([]), []);
 57    assert_equal(list_head(list,-3), [1,2]);
 58    assert_equal(list_head(list,1), [1,2]);
 59    assert_equal(list_head(list,2), [1,2,3]);
 60    assert_equal(list_head(list,6), [1,2,3,4]);
 61    assert_equal(list_head(list,-6), []);
 62}
 63test_list_head();
 64
 65
 66module test_list_tail() {
 67    list = [1,2,3,4];
 68    assert_equal(list_tail(list), [2,3,4]);
 69    assert_equal(list_tail([1]), []);
 70    assert_equal(list_tail([]), []);
 71    assert_equal(list_tail(list,-3), [2,3,4]);
 72    assert_equal(list_tail(list,2), [3,4]);
 73    assert_equal(list_tail(list,3), [4]);
 74    assert_equal(list_tail(list,6), []);
 75    assert_equal(list_tail(list,-6), [1,2,3,4]);
 76}
 77test_list_tail();
 78
 79
 80module test_in_list() {
 81    assert(in_list("bar", ["foo", "bar", "baz"]));
 82    assert(!in_list("bee", ["foo", "bar", "baz"]));
 83    assert(in_list("bar", [[2,"foo"], [4,"bar"], [3,"baz"]], idx=1));
 84    assert(!in_list("bee", ["foo", "bar", ["bee"]]));
 85    assert(in_list(NAN, [NAN])==false);
 86    assert(!in_list(undef, [3,4,5]));
 87    assert(in_list(undef,[3,4,undef,5]));
 88    assert(!in_list(3,[]));
 89    assert(!in_list(3,[4,5,[3]]));
 90}
 91test_in_list();
 92
 93
 94
 95
 96// Section: Basic List Generation
 97
 98module test_repeat() {
 99    assert(repeat(1, 4) == [1,1,1,1]);
100    assert(repeat(8, [2,3]) == [[8,8,8], [8,8,8]]);
101    assert(repeat(0, [2,2,3]) == [[[0,0,0],[0,0,0]], [[0,0,0],[0,0,0]]]);
102    assert(repeat([1,2,3],3) == [[1,2,3], [1,2,3], [1,2,3]]);
103    assert(repeat(4, [2,-1]) == [[], []]);
104}
105test_repeat();
106
107
108module test_count() {
109    assert_equal(count(5), [0,1,2,3,4]);
110    assert_equal(count(5,3), [3,4,5,6,7]);
111    assert_equal(count(4,3,2), [3,5,7,9]);
112    assert_equal(count(5,0,0.25), [0, 0.25, 0.5, 0.75, 1.0]);
113}
114test_count();
115
116
117module test_reverse() {
118    assert(reverse([3,4,5,6]) == [6,5,4,3]);
119    assert(reverse("abcd") == "dcba");
120    assert(reverse([]) == []);
121}
122test_reverse();
123
124
125module test_list_rotate() {
126    assert(list_rotate([1,2,3,4,5],-2) == [4,5,1,2,3]);
127    assert(list_rotate([1,2,3,4,5],-1) == [5,1,2,3,4]);
128    assert(list_rotate([1,2,3,4,5],0) == [1,2,3,4,5]);
129    assert(list_rotate([1,2,3,4,5],1) == [2,3,4,5,1]);
130    assert(list_rotate([1,2,3,4,5],2) == [3,4,5,1,2]);
131    assert(list_rotate([1,2,3,4,5],3) == [4,5,1,2,3]);
132    assert(list_rotate([1,2,3,4,5],4) == [5,1,2,3,4]);
133    assert(list_rotate([1,2,3,4,5],5) == [1,2,3,4,5]);
134    assert(list_rotate([1,2,3,4,5],6) == [2,3,4,5,1]);
135    assert(list_rotate([],3) == []);
136    path = [[1,1],[-1,1],[-1,-1],[1,-1]];
137    assert(list_rotate(path,1) == [[-1,1],[-1,-1],[1,-1],[1,1]]);
138    assert(list_rotate(path,2) == [[-1,-1],[1,-1],[1,1],[-1,1]]);
139}
140test_list_rotate();
141
142
143
144module test_list_set() {
145    assert_equal(list_set([2,3,4,5], 2, 21), [2,3,21,5]);
146    assert_equal(list_set([2,3,4,5], [1,3], [81,47]), [2,81,4,47]);
147    assert_equal(list_set([2,3,4,5], [2], [21]), [2,3,21,5]);
148    assert_equal(list_set([1,2,3], [], []), [1,2,3]);
149    assert_equal(list_set([1,2,3], [1,5], [4,4]), [1,4,3,0,0,4]);
150    assert_equal(list_set([1,2,3], [1,5], [4,4],dflt=12), [1,4,3,12,12,4]);
151    assert_equal(list_set([1,2,3], [1,2], [4,4],dflt=12, minlen=5), [1,4,4,12,12]);
152    assert_equal(list_set([1,2,3], 1, 4, dflt=12, minlen=5), [1,4,3,12,12]);
153    assert_equal(list_set([1,2,3], [],[],dflt=12, minlen=5), [1,2,3,12,12]);
154    assert_equal(list_set([1,2,3], 5,9), [1,2,3,0,0,9]);
155    assert_equal(list_set([1,2,3], 5,9,dflt=12), [1,2,3,12,12,9]);    
156}
157test_list_set();
158
159
160module test_list_remove() {
161    assert(list_remove([3,6,9,12],1) == [3,9,12]);
162    assert(list_remove([3,6,9,12],[1]) == [3,9,12]);
163    assert(list_remove([3,6,9,12],[1,3]) == [3,9]);
164    assert(list_remove([3,6,9],[]) == [3,6,9]);
165    assert(list_remove([],[]) == []);
166    assert(list_remove([1,2,3], -1)==[1,2,3]);
167    assert(list_remove([1,2,3], 3)==[1,2,3]);    
168    assert(list_remove([1,2,3], [-1,3])==[1,2,3]);    
169    assert(list_remove([1,2,3], [-1,1,3])==[1,3]);    
170}
171test_list_remove();
172
173module test_list_remove_values() {
174    animals = ["bat", "cat", "rat", "dog", "bat", "rat"];
175    assert(list_remove_values(animals, "rat") == ["bat","cat","dog","bat","rat"]);
176    assert(list_remove_values(animals, "bat", all=true) == ["cat","rat","dog","rat"]);
177    assert(list_remove_values(animals, ["bat","rat"]) == ["cat","dog","bat","rat"]);
178    assert(list_remove_values(animals, ["bat","rat"], all=true) == ["cat","dog"]);
179    assert(list_remove_values(animals, ["tucan","rat"], all=true) == ["bat","cat","dog","bat"]);
180
181    test = [3,4,[5,6],7,5,[5,6],4,[6,5],7,[4,4]];
182    assert_equal(list_remove_values(test,4), [3, [5, 6], 7, 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
183    assert_equal(list_remove_values(test,[4,4]), [3, [5, 6], 7, 5, [5, 6], [6, 5], 7, [4, 4]]);
184    assert_equal(list_remove_values(test,[4,7]), [3, [5, 6], 5, [5, 6], 4, [6, 5], 7, [4, 4]]);
185    assert_equal(list_remove_values(test,[5,6]), [3, 4, [5, 6], 7, [5, 6], 4, [6, 5], 7, [4, 4]]);
186    assert_equal(list_remove_values(test,[[5,6]]), [3,4,7,5,[5,6],4,[6,5],7,[4,4]]);
187    assert_equal(list_remove_values(test,[[5,6]],all=true), [3,4,7,5,4,[6,5],7,[4,4]]);    
188    assert_equal(list_remove_values(test,4,all=true),  [3, [5, 6], 7, 5, [5, 6], [6, 5],7, [4, 4]]);
189    assert_equal(list_remove_values(test,[4,7],all=true), [3, [5, 6], 5, [5, 6], [6, 5], [4, 4]]);
190    assert_equal(list_remove_values(test,[]),test);
191    assert_equal(list_remove_values(test,[],all=true),test);
192    assert_equal(list_remove_values(test,99), test);
193    assert_equal(list_remove_values(test,99,all=true), test);
194    assert_equal(list_remove_values(test,[99,100],all=true), test);
195    assert_equal(list_remove_values(test,[99,100]), test);            
196  
197}
198test_list_remove_values();
199
200
201module test_list_insert() {
202    assert_equal(list_insert([3,6,9,12],1,5),[3,5,6,9,12]);
203    assert_equal(list_insert([3,6,9,12],[1,3],[5,11]),[3,5,6,9,11,12]);
204    assert_equal(list_insert([3],1,4), [3,4]);
205    assert_equal(list_insert([3],[0,1], [1,2]), [1,3,2]);
206    assert_equal(list_insert([1,2,3],[],[]),[1,2,3]);
207    assert_equal(list_insert([], 0, 4),[4]);
208}
209test_list_insert();
210
211
212module test_bselect() {
213    assert(bselect([3,4,5,6,7], [false,false,false,false,false]) == []);
214    assert(bselect([3,4,5,6,7], [false,true,true,false,true]) == [4,5,7]);
215    assert(bselect([3,4,5,6,7], [true,true,true,true,true]) == [3,4,5,6,7]);
216}
217test_bselect();
218
219
220module test_list_bset() {
221    assert(list_bset([false,true,false,true,false], [3,4]) == [0,3,0,4,0]);
222    assert(list_bset([false,true,false,true,false], [3,4], dflt=1) == [1,3,1,4,1]);
223}
224test_list_bset();
225
226
227module test_min_length() {
228    assert(min_length(["foobar", "bazquxx", "abcd"]) == 4);
229}
230test_min_length();
231
232
233module test_max_length() {
234    assert(max_length(["foobar", "bazquxx", "abcd"]) == 7);
235}
236test_max_length();
237
238
239module test_list_pad() {
240    assert(list_pad([4,5,6], 5, 8) == [4,5,6,8,8]);
241    assert(list_pad([4,5,6,7,8], 5, 8) == [4,5,6,7,8]);
242    assert(list_pad([4,5,6,7,8,9], 5, 8) == [4,5,6,7,8,9]);
243}
244test_list_pad();
245
246
247module test_idx() {
248    colors = ["red", "green", "blue", "cyan"];
249    assert([for (i=idx(colors)) i] == [0,1,2,3]);
250    assert([for (i=idx(colors,e=-2)) i] == [0,1,2]);
251    assert([for (i=idx(colors,s=1)) i] == [1,2,3]);
252    assert([for (i=idx(colors,s=1,e=-2)) i] == [1,2]);
253}
254test_idx();
255
256
257module test_shuffle() {
258    nums1 = count(100);
259    nums2 = shuffle(nums1,33);
260    nums3 = shuffle(nums2,99);
261    assert(sort(nums2)==nums1);
262    assert(sort(nums3)==nums1);
263    assert(nums1!=nums2);
264    assert(nums2!=nums3);
265    assert(nums1!=nums3);
266    str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
267    shufstr = shuffle(str,12);
268    assert(shufstr != str && sort(shufstr)==str);
269}
270test_shuffle();
271
272
273
274// Sets
275
276module test_set_union() {
277    assert_equal(
278        set_union([2,3,5,7,11], [1,2,3,5,8]),
279        [2,3,5,7,11,1,8]
280    );
281    assert_equal(
282        set_union([2,3,5,7,11], [1,2,3,5,8], get_indices=true),
283        [[5,0,1,2,6],[2,3,5,7,11,1,8]]
284    );
285}
286test_set_union();
287
288
289module test_set_difference() {
290    assert_equal(
291        set_difference([2,3,5,7,11], [1,2,3,5,8]),
292        [7,11]
293    );
294}
295test_set_difference();
296
297
298module test_set_intersection() {
299    assert_equal(
300        set_intersection([2,3,5,7,11], [1,2,3,5,8]),
301        [2,3,5]
302    );
303}
304test_set_intersection();
305
306
307// Arrays
308
309
310module test_force_list() {
311    assert_equal(force_list([3,4,5]), [3,4,5]);
312    assert_equal(force_list(5), [5]);
313    assert_equal(force_list(7, n=3), [7,7,7]);
314    assert_equal(force_list(4, n=3, fill=1), [4,1,1]);
315}
316test_force_list();
317
318
319module test_pair() {
320    assert(pair([3,4,5,6]) == [[3,4], [4,5], [5,6]]);
321    assert(pair("ABCD") == [["A","B"], ["B","C"], ["C","D"]]);
322    assert(pair([3,4,5,6],true) == [[3,4], [4,5], [5,6], [6,3]]);
323    assert(pair("ABCD",true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
324    assert(pair([3,4,5,6],wrap=true) == [[3,4], [4,5], [5,6], [6,3]]);
325    assert(pair("ABCD",wrap=true) == [["A","B"], ["B","C"], ["C","D"], ["D","A"]]);
326    assert_equal(pair([],wrap=true),[]);
327    assert_equal(pair([],wrap=false),[]);
328    assert_equal(pair([1],wrap=true),[]);
329    assert_equal(pair([1],wrap=false),[]);
330    assert_equal(pair([1,2],wrap=false),[[1,2]]);
331    assert_equal(pair([1,2],wrap=true),[[1,2],[2,1]]);
332}
333test_pair();
334
335
336module test_triplet() {
337    assert(triplet([3,4,5,6,7]) == [[3,4,5], [4,5,6], [5,6,7]]);
338    assert(triplet("ABCDE") == [["A","B","C"], ["B","C","D"], ["C","D","E"]]);
339    assert(triplet([3,4,5,6],true) == [[6,3,4],[3,4,5], [4,5,6], [5,6,3]]);
340    assert(triplet("ABCD",true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
341    assert(triplet("ABCD",wrap=true) == [["D","A","B"],["A","B","C"], ["B","C","D"], ["C","D","A"]]);
342    assert_equal(triplet([],wrap=true),[]);
343    assert_equal(triplet([],wrap=false),[]);    
344    assert_equal(triplet([1],wrap=true),[]);
345    assert_equal(triplet([1],wrap=false),[]);    
346    assert_equal(triplet([1,2],wrap=true),[]);
347    assert_equal(triplet([1,2],wrap=false),[]);    
348    assert_equal(triplet([1,2,3],wrap=true),[[3,1,2],[1,2,3],[2,3,1]]);
349    assert_equal(triplet([1,2,3],wrap=false),[[1,2,3]]);    
350}
351test_triplet();
352
353
354module test_combinations() {
355    assert(combinations([3,4,5,6]) ==  [[3,4],[3,5],[3,6],[4,5],[4,6],[5,6]]);
356    assert(combinations([3,4,5,6],n=3) == [[3,4,5],[3,4,6],[3,5,6],[4,5,6]]);
357}
358test_combinations();
359
360
361module test_repeat_entries() {
362    list = [0,1,2,3];
363    assert(repeat_entries(list, 6) == [0,0,1,2,2,3]);
364    assert(repeat_entries(list, 6, exact=false) == [0,0,1,1,2,2,3,3]);
365    assert(repeat_entries(list, [1,1,2,1], exact=false) == [0,1,2,2,3]);
366}
367test_repeat_entries();
368
369
370module test_list_to_matrix() {
371    v = [1,2,3,4,5,6];
372    assert(list_to_matrix(v,2) == [[1,2], [3,4], [5,6]]);
373    assert(list_to_matrix(v,3) == [[1,2,3], [4,5,6]]);
374    assert(list_to_matrix(v,4,0) == [[1,2,3,4], [5,6,0,0]]);
375}
376test_list_to_matrix();
377
378
379module test_flatten() {
380    assert(flatten([[1,2,3], [4,5,[6,7,8]]]) == [1,2,3,4,5,[6,7,8]]);
381    assert(flatten([]) == []);
382}
383test_flatten();
384
385
386module test_full_flatten() {
387    assert(full_flatten([[1,2,3], [4,5,[6,[7],8]]]) == [1,2,3,4,5,6,7,8]);
388    assert(full_flatten([]) == []);
389}
390test_full_flatten();
391
392
393module test_list_shape() {
394    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]]) == [2,2,3]);
395    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 0) == 2);
396    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9],[10,11,12]]], 2) == 3);
397    assert(list_shape([[[1,2,3],[4,5,6]],[[7,8,9]]]) == [2,undef,3]);
398    assert(list_shape([1,2,3,4,5,6,7,8,9]) == [9]);
399    assert(list_shape([[1],[2],[3],[4],[5],[6],[7],[8],[9]]) == [9,1]);
400    assert(list_shape([]) == [0]);
401    assert(list_shape([[]]) == [1,0]);
402    assert(list_shape([[],[]]) == [2,0]);
403    assert(list_shape([[],[1]]) == [2,undef]);
404}
405test_list_shape();
406
407
408// vim: expandtab tabstop=4 shiftwidth=4 softtabstop=4 nowrap