OKlibrary  0.2.1.6
Lists.mac
Go to the documentation of this file.
00001 /* Oliver Kullmann, 20.2.2008 (Swansea) */
00002 /* Copyright 2008, 2009, 2010, 2011, 2012, 2013 Oliver Kullmann
00003 This file is part of the OKlibrary. OKlibrary is free software; you can redistribute
00004 it and/or modify it under the terms of the GNU General Public License as published by
00005 the Free Software Foundation and included in this library; either version 3 of the
00006 License, or any later version. */
00007 
00022 oklib_include("OKlib/ComputerAlgebra/TestSystem/Lisp/Asserts.mac")$
00023 oklib_include("OKlib/ComputerAlgebra/DataStructures/Lisp/HashMaps.mac")$
00024 oklib_include("OKlib/ComputerAlgebra/Hypergraphs/Lisp/SetSystems.mac")$
00025 
00026 kill(f)$
00027 
00028 /* ***************
00029    * List access *
00030    ***************
00031 */
00032 
00033 okltest_firste(f) := block([x],
00034   assert(f([0]) = 0),
00035   assert(f([1,2,3]) = 1),
00036   assert(f(x) = x),
00037   assert(f(0) = 0),
00038   true)$
00039 
00040 okltest_seconde(f) := block([x],
00041   assert(f([1,2]) = 2),
00042   assert(f([1,7,3]) = 7),
00043   assert(f(x) = x),
00044   assert(f(0) = 0),
00045   true)$
00046 
00047 okltest_thirde(f) := block([x],
00048   assert(f([1,2,3]) = 3),
00049   assert(f([1,2,7,4]) = 7),
00050   assert(f(x) = x),
00051   assert(f(0) = 0),
00052   true)$
00053 
00054 okltest_fourthe(f) := block([x],
00055   assert(f([1,2,3,4]) = 4),
00056   assert(f([1,2,3,7,5]) = 7),
00057   assert(f(x) = x),
00058   assert(f(0) = 0),
00059   true)$
00060 
00061 
00062 /* ***********************
00063    * Apply-functionality *
00064    ***********************
00065 */
00066 
00067 okltest_uaapply(f) := block(
00068   assert(f(append,[]) = []),
00069   if oklib_test_level = 0 then return(true),
00070   assert(f(append,create_list([],i,1,100000)) = []),
00071   true)$
00072 
00073 okltest_lappend(f) := block(
00074   assert(f([]) = []),
00075   assert(f([[]]) = []),
00076   assert(f([[1],[],[2],[],[3],[]]) = [1,2,3]),
00077   if oklib_test_level = 0 then return(true),
00078   assert(f(create_list([],i,1,100000)) = []),
00079   true)$
00080 
00081 
00082 /* *********************
00083    * Map-functionality *
00084    *********************
00085 */
00086 
00087 okltest_map2(f) := block([add1 : lambda([x], x+1), a,b,c],
00088   assert(f(add1,[]) = []),
00089   assert(f(add1, [{}]) = [{}]),
00090   assert(f(add1, [{1,2},{3,4},{5},{}]) = [{2,3},{4,5},{6},{}]),
00091   assert(f(add1, [[1,[2,3]],[3]]) = [[2,[3,4]],[4]]),
00092   assert(f(add1, [[1,{2,3}],[3]]) = [[2,{2,3}+1],[4]]),
00093   assert(f(add1, {{1,2},{3,4}}) = {{2,3},{4,5}}),
00094   assert(f(add1, a(b(c,c),b(c))) = a(b(c+1,c+1),b(c+1))),
00095   assert(f(lambda([x],0), {{1,2},{3,4}}) = {{0}}),
00096   true)$
00097 
00098 
00099 /* *******************************
00100    * Testing properties of lists *
00101    *******************************
00102 */
00103 
00104 okltest_every_s(f) := block([c],
00105   assert(f(identity,[]) = true),
00106   assert(f(identity,[false]) = false),
00107   assert(f(identity,[true]) = true),
00108   assert(f(identity,[true,false]) = false),
00109   assert(f(identity,[true,true]) = true),
00110   c : 0,
00111   assert(f(lambda([x],(c:c+1,false)),[1,2]) = false),
00112   assert(c = 1),
00113   c : 0,
00114   assert(every(lambda([x],(c:c+1,false)),[1,2]) = false),
00115   assert(c = 2),
00116   true)$
00117 
00118 okltest_everynary_s(f) := block([c2],
00119   assert(okltest_every_s(f) = true),
00120   assert(f("<",[],[]) = true),
00121   assert(f("<",[1,2],[2,3]) = true),
00122   assert(f("<",[1,2],[2,2]) = false),
00123   c : 0,
00124   assert(f(lambda([x,y],(c:c+1,is(x<y))),[1,2,3],[2,2,4]) = false),
00125   assert(c = 2),
00126   true)$
00127 
00128 okltest_some_s(f) := (
00129   assert(f(identity,[]) = false),
00130   assert(f(identity,[false]) = false),
00131   assert(f(identity,[true]) = true),
00132   assert(f(identity,[true,false]) = true),
00133   assert(f(identity,[false,false]) = false),
00134   c : 0,
00135   assert(f(lambda([x],(c:c+1,true)),[1,2]) = true),
00136   assert(c = 1),
00137   c : 0,
00138   assert(every(lambda([x],(c:c+1,true)),[1,2]) = true),
00139   assert(c = 2),
00140   assert(okltest_every_s(buildq([f],lambda([p,L],not f(lambda([x],not p(x)),L))))),
00141   assert(okltest_every_s(buildq([f],lambda([_newpred,M],
00142     not f(lambda([x], not _newpred(x)), M))))),
00143   block(
00144    [localf : buildq([f],lambda([SetSys], f(lambda([S], not emptyp(S)), SetSys)))],
00145     assert(f(localf,[]) = false),
00146     assert(f(localf,[{{}}]) = false),
00147     assert(f(localf,[{{}},{{},{1}}]) = true)
00148   ),
00149   true)$
00150 
00151 okltest_somenary_s(f) := block([c2],
00152   assert(okltest_some_s(f) = true),
00153   assert(f("<",[],[]) = false),
00154   assert(f("<",[1,2],[2,3]) = true),
00155   assert(f("<",[1,2],[2,2]) = true),
00156   assert(f("<",[3,2],[2,2]) = false),
00157   c : 0,
00158   assert(f(lambda([x,y],(c:c+1,is(x<y))),[1,2,3],[2,2,4]) = true),
00159   assert(c = 1),
00160   true)$
00161 
00162 okltest_listnorep_p(f) := (
00163   assert(f(0) = false),
00164   assert(f({}) = false),
00165   assert(f([]) = true),
00166   assert(f([1]) = true),
00167   assert(f([1,2]) = true),
00168   assert(f([1,1]) = false),
00169   true)$
00170 
00171 okltest_lconstant_p(f) := (
00172   assert(f([]) = true),
00173   assert(f([1]) = true),
00174   assert(f([[]]) = true),
00175   assert(f([1,1]) = true),
00176   assert(f([1,2]) = false),
00177   assert(f([1,[]]) = false),
00178   true)$
00179 
00180 okltest_strictascending_p(f) := (
00181   assert(f([]) = true),
00182   assert(f([1]) = true),
00183   assert(f([1,2]) = true),
00184   assert(f([2,1]) = false),
00185   true)$
00186 
00187 okltest_strictdescending_p(f) := (
00188   assert(okltest_strictascending_p(buildq([f],lambda([L],f(reverse(L))))) = true),
00189   assert(f([]) = true),
00190   assert(f([1]) = true),
00191   assert(f([1,2]) = false),
00192   assert(f([2,1]) = true),
00193   true)$
00194 
00195 okltest_ascending_p(f) := (
00196   assert(f([]) = true),
00197   assert(f([1]) = true),
00198   assert(f([1,2]) = true),
00199   assert(f([2,1]) = false),
00200   assert(f([1,1]) = true),
00201   true)$
00202 
00203 okltest_descending_p(f) := (
00204   assert(okltest_ascending_p(buildq([f],lambda([L],f(reverse(L))))) = true),
00205   assert(f([]) = true),
00206   assert(f([1]) = true),
00207   assert(f([1,2]) = false),
00208   assert(f([2,1]) = true),
00209   assert(f([2,2]) = true),
00210   true)$
00211 
00212 okltest_prefix_p(f) := (
00213   assert(f([],[]) = true),
00214   assert(f([],[1]) = true),
00215   assert(f([1],[]) = false),
00216   assert(f([1],[1]) = true),
00217   assert(f([1],[2]) = false),
00218   assert(f([1],[2,1]) = false),
00219   assert(f([1],[1,2]) = true),
00220   assert(f([1,3,-9], [1,3,-9,5]) = true),
00221   assert(f([1,3,-9], [1,3,0,-9]) = false),
00222   assert(f([1,3,-9], [0,1,3,-9]) = false),
00223   true)$
00224 
00225 /* *******************************************
00226    * Summations and products over lists/sets *
00227    *******************************************
00228 */
00229 
00230 okltest_sum_l(f) := (
00231   assert(f([]) = 0),
00232   assert(f([1]) = 1),
00233   assert(f([1,2,0,1]) = 4),
00234   assert(okltest_sum_s(buildq([f],lambda([S],f(listify(S)))))),
00235   true)$
00236 
00237 okltest_sum_s(f) := (
00238   assert(f({}) = 0),
00239   assert(f({1}) = 1),
00240   assert(f({1,2}) = 3),
00241   true)$
00242 
00243 okltest_gsum_l(f) := (
00244   assert(f(lambda([x],x^2), [1,2]) = 5),
00245   assert(okltest_sum_l(buildq([f],lambda([L],f(identity,L))))),
00246   true)$
00247 
00248 okltest_gsum_s(f) := (
00249   assert(f(lambda([x],x^2), {1,2}) = 5),
00250   assert(okltest_sum_s(buildq([f],lambda([S],f(identity,S))))),
00251   true)$
00252 
00253 okltest_prod_l(f) := (
00254   assert(f([]) = 1),
00255   assert(f([1]) = 1),
00256   assert(f([1,2,3]) = 6),
00257   assert(okltest_prod_s(buildq([f],lambda([S],f(listify(S)))))),
00258   true)$
00259 
00260 okltest_prod_s(f) := (
00261   assert(f({}) = 1),
00262   assert(f({1}) = 1),
00263   assert(f({1,2,3}) = 6),
00264   true)$
00265 
00266 okltest_gprod_l(f) := (
00267   assert(f(lambda([x],x^2), [1,2]) = 4),
00268   assert(okltest_prod_l(buildq([f],lambda([L],f(identity,L))))),
00269   true)$
00270 
00271 okltest_gprod_s(f) := (
00272   assert(f(lambda([x],x^2), {1,2}) = 4),
00273   assert(okltest_prod_s(buildq([f],lambda([S],f(identity,S))))),
00274   true)$
00275 
00276 
00277 /* *************************************
00278    * Finding special elements in lists *
00279    *************************************
00280 */
00281 
00282 okltest_first_smallest_l(f) := (
00283   assert(f([{}]) = {}),
00284   assert(f([{1},[]]) = []),
00285   assert(f([{1,2},[1],{2}]) = [1]),
00286   true)$
00287 
00288 okltest_first_smallest_lb_l(f) := (
00289   assert(f([{1,2}],1) = {1,2}),
00290   assert(f([[1,2,3],{1,2,3,4},[1,2],[1,2,3]],2) = [1,2]),
00291   assert(okltest_first_smallest_l(buildq([f],lambda([L],f(L,0))))),
00292   true)$
00293 
00294 okltest_first_smallest_f_l(f) := (
00295   assert(f(identity,[]) = [inf]),
00296   assert(f(identity,[1]) = [1,1]),
00297   assert(f(identity,[-1]) = [-1,-1]),
00298   assert(f(lambda([x],x*x),[2,-2]) = [4,2]),
00299   assert(f(lambda([x],x*x),[2,-2,1]) = [1,1]),
00300   assert(okltest_smallest_f_l(buildq([f], lambda([_f,L], block([res:f(_f,L)], if res = [inf] then inf else first(res))))) = true),
00301   true)$
00302 
00303 okltest_first_largest_f_l(f) := (
00304   assert(f(identity,[]) = [minf]),
00305   assert(f(identity,[1]) = [1,1]),
00306   assert(f(identity,[-1]) = [-1,-1]),
00307   assert(f(lambda([x],x*x),[2,-1]) = [4,2]),
00308   assert(f(lambda([x],x*x),[2,-2,3]) = [9,3]),
00309   assert(okltest_largest_f_l(buildq([f], lambda([_f,L], block([res:f(_f,L)], if res = [minf] then minf else first(res))))) = true),
00310   true)$
00311 
00312 okltest_smallest_f_l(f) := block([F],
00313   assert(f(F,[]) = inf),
00314   assert(f(identity,[3,2,1]) = 1),
00315   assert(f(lambda([x],x^2), [-3,1,2]) = 1),
00316   assert(f(lambda([x],x^2), [-5,2,3]) = 4),
00317   if oklib_test_level = 0 then return(true),
00318   assert(f(lambda([x],x^2), create_list(x,x,-50000,50000)) = 0),
00319   true)$
00320 
00321 okltest_largest_f_l(f) := block([F],
00322   assert(f(F,[]) = minf),
00323   assert(f(identity,[3,2,1]) = 3),
00324   assert(f(lambda([x],x^2), [-3,1,2]) = 9),
00325   if oklib_test_level = 0 then return(true),
00326   assert(f(lambda([x],x^2), create_list(x,x,-50000,50000)) = 50000^2),
00327   true)$
00328 
00329 okltest_find_first_l(f) := ([pred],
00330   assert(f(pred,[]) = inf),
00331   assert(f(evenp,[1]) = inf),
00332   assert(f(evenp,[2]) = 1),
00333   assert(f(evenp,[1,3,5,1]) = inf),
00334   assert(f(evenp,[2,4,6,2]) = 1),
00335   assert(f(evenp,[1,3,5,2,4,6,2]) = 4),
00336   assert(f(evenp,[1,3,4]) = 3),
00337   assert(okltest_find_firste_l(buildq([f], lambda([x,L], f(lambda([x_], is(x_ = x)),L)))) = true),
00338   true)$
00339 
00340 okltest_find_firste_l(f) := (
00341   assert(f(0,[]) = inf),
00342   assert(f(0,[0]) = 1),
00343   assert(f(0,[0,1,2,0]) = 1),
00344   assert(f(1,[0,1,2,0]) = 2),
00345   assert(f(3,[0,1,2,0]) = inf),
00346   true)$
00347 
00348 okltest_find_last_l(f) := ([pred],
00349   assert(f(pred,[]) = minf),
00350   assert(f(evenp,[1]) = minf),
00351   assert(f(evenp,[2]) = 1),
00352   assert(f(evenp,[1,3,5,1]) = minf),
00353   assert(f(evenp,[2,4,6,2]) = 4),
00354   assert(f(evenp,[1,3,5,2,4,6,2]) = 7),
00355   assert(f(evenp,[2,1,3,5]) = 1),
00356   assert(okltest_find_laste_l(buildq([f], lambda([x,L], f(lambda([x_], is(x_ = x)),L)))) = true),
00357   true)$
00358 
00359 okltest_find_laste_l(f) := (
00360   assert(f(0,[]) = minf),
00361   assert(f(0,[0]) = 1),
00362   assert(f(0,[0,1,2,4]) = 1),
00363   assert(f(1,[0,1,2,0]) = 2),
00364   assert(f(3,[0,1,2,0]) = minf),
00365   true)$
00366 
00367 okltest_elementp_l(f) := block([x],
00368   assert(f(x,[]) = false),
00369   assert(f(x,[1]) = false),
00370   assert(f(x,[x]) = true),
00371   assert(f(1,[0,2,3,1,4]) = true),
00372   assert(f(1,[0,2,3,4,1]) = true),
00373   assert(f(1,[1,2,3,4]) = true),
00374   assert(f(1,[0,2,3,4]) = false),
00375   true)$
00376 
00377 
00378 /* **************************
00379    * Sublist constructions *
00380    **************************
00381 */
00382 
00383 okltest_take_elements(f) := (
00384   assert(f(0,[]) = []),
00385   assert(f(0,[1,2]) = []),
00386   assert(f(1,[1,2]) = [1]),
00387   assert(f(2,[1,2]) = [1,2]),
00388   assert(f(3,[1,2]) = [2]),
00389   assert(f(4,[1,2]) = []),
00390   block([L1,L2],
00391    L1 : [1,2,3],
00392    L2 : f(2,L1),
00393    assert(L2 = [1,2]),
00394    L2[1] : 77,
00395    assert(L1 = [1,2,3])),
00396   true)$
00397 
00398 okltest_sublist_indices_el(f) := (
00399   assert(f([],1) = []),
00400   assert(f([1],1) = [1]),
00401   assert(f([1],2) = []),
00402   assert(f([1,2,3,1],1) = [1,4]),
00403   true)$
00404 
00405 okltest_remove_element(f) := (
00406   assert(f(1,[1]) = []),
00407   assert(f(1,[1,2]) = [2]),
00408   assert(f(2,[1,2]) = [1]),
00409   block([L1,L2],
00410    L1 : [1,2,3],
00411    L2 : f(2,L1),
00412    assert(L2 = [1,3]),
00413    L2[1] : 77,
00414    assert(L1 = [1,2,3])),
00415   true)$
00416 
00417 okltest_remove_elements(f) := block([L],
00418   assert(f([],L) = L),
00419   assert(f([1],[]) = []),
00420   assert(f([1],[1]) = []),
00421   assert(f([1],[1,1,2,1,2]) = [2,2]),
00422   assert(f([1,2,3],[1,4,2,1,5,2]) = [4,5]),
00423   true)$
00424 
00425 okltest_sublist_indicator(f) := (
00426   assert(f([],okl_make_array(fixnum,0)) = []),
00427   assert(f([],okl_make_array(fixnum,1)) = []),
00428   assert(f([1],okl_make_array(fixnum,1)) = [1]),
00429   assert(f([1,2],okl_make_array(fixnum,2)) = [1,2]),
00430   block([A : okl_make_array(fixnum,4)],
00431    assert(f([1,2,3,4],A) = [1,2,3,4]),
00432    A[1] : 1,
00433    assert(f([1,2,3,4],A) = [2,3,4]),
00434    A[4] : 2,
00435    assert(f([1,2,3,4],A) = [2,3]),
00436    A[2] : -1,
00437    assert(f([1,2,3,4],A) = [3]),
00438    A[3] : 6,
00439    assert(f([1,2,3,4],A) = [])
00440   ),
00441   block([L : [1,2,3], A : okl_make_array(fixnum,3), L2],
00442    A[2] : 1,
00443    L2 : f(L,A),
00444    assert(L2 = [1,3]),
00445    L2[1] : 77,
00446    assert(L[1] = 1)
00447   ),
00448   true)$
00449 
00450 okltest_remove_consrep(f) := (
00451   assert(f([]) = []),
00452   assert(f([1]) = [1]),
00453   assert(f([1,1]) = [1]),
00454   assert(f([1,1,2]) = [1,2]),
00455   assert(f([1,2,2]) = [1,2]),
00456   assert(f([1,1,2,1,2,2,3,1,2,4,4,3,3,2,2,2,1,1,1,1,4]) = [1,2,1,2,3,1,2,4,3,2,1,4]),
00457   true)$
00458 
00459 
00460 /* *********************
00461    * List partitioning *
00462    *********************
00463 */
00464 
00465 okltest_partition_elements(f) := (
00466   for n : 1 thru 3 do
00467     assert(f([],n) = []),
00468   for n : 1 thru 3 do
00469     assert(f([1],n) = [[1]]),
00470   for n : 1 thru 3 do
00471     assert(f([1,2],n) = if n=1 then [[1],[2]] else [[1,2]]),
00472   block([L1,L2],
00473    L1 : [1,2,3],
00474    L2 : f(L1,2),
00475    assert(L2 = [[1,2],[3]]),
00476    L2[1] : 77,
00477    assert(L1 = [1,2,3])),
00478   true)$
00479 
00480 okltest_partition_list(f) := block([P],
00481   assert(f([],P) = [[],[]]),
00482   assert(f([1,1],evenp) = [[],[1,1]]),
00483   assert(f([1,3,4,6],evenp) = [[4,6],[1,3]]),
00484   assert(f([1,2,3,4,5],oddp) = [[1,3,5],[2,4]]),
00485   for n : 0 thru 6 do block([L : create_list(1,i,1,n), res],
00486     res : f(L,evenp),
00487     assert(res = [[],L]),
00488     assert(f(L,oddp) = reverse(res))
00489   ),
00490   for n : 0 thru 6 do block([L : create_list(i,i,1,n), res],
00491     res : f(L,evenp),
00492     assert(res = [create_list(2*i,i,1,floor(n/2)), create_list(2*i+1,i,0,if evenp(n) then n/2-1 else (n-1)/2)]),
00493     assert(f(L,oddp) = reverse(res))
00494   ),
00495   true)$
00496 
00497 okltest_partition_list_eq(f) := block(
00498   assert(f([],identity) = []),
00499   assert(f([1,2,3,4,5],identity) = [[1],[2],[3],[4],[5]]),
00500   assert(f([5,2,4,3,1],identity) = [[1],[2],[3],[4],[5]]),
00501   assert(f([1,2,3,4,5],lambda([x],0)) = [[1,2,3,4,5]]),
00502   assert(f([5,2,4,3,1],lambda([x],0)) = [[5,2,4,3,1]]),
00503   assert(f([1,2,3,4,5],lambda([x],floor(x/2))) = [[1],[2,3],[4,5]]),
00504   assert(f([5,2,4,3,1],lambda([x],floor(x/2))) = [[1],[2,3],[5,4]]),
00505   assert(f([1,2,3,4,5],lambda([x],ceiling(x/2))) = [[1,2],[3,4],[5]]),
00506   assert(f([5,2,4,3,1],lambda([x],ceiling(x/2))) = [[2,1],[4,3],[5]]),
00507   true)$
00508 
00509 okltest_split_list(f) := block(
00510   assert(f([],1) = [[]]),
00511   assert(f([1],0) = [[1]]),
00512   assert(f([1],1) = [[],[]]),
00513   assert(f([1,1],1) = [[],[],[]]),
00514   assert(f([0,1,1,0,1,1,0,1,0,1,1],1) = [[0],[],[0],[],[0],[0],[],[]]),
00515   assert(f([0,1,1,0,1,1,0,1,0,1,1],0) = [[],[1,1],[1,1],[1],[1,1]]),
00516   assert(f([1,2,3,0,4,5,6,0,7,8,9],0) = [[1,2,3],[4,5,6],[7,8,9]]),
00517   assert(f([1,2,3,0,4,5,6,0,7,8,9,0],0) = [[1,2,3],[4,5,6],[7,8,9],[]]),
00518   true)$
00519 
00520 okltest_split_list_epo(f) := block(
00521   assert(okltest_split_list(
00522       buildq([f], lambda([L,e],
00523           f(L, buildq([e],lambda([a], is(a = e)))))))),
00524   assert(f([1,3,5,0,7,9,11,2,13,15,17,4,19], evenp) =
00525     [[1,3,5],[7,9,11],[13,15,17],[19]]),
00526   true)$
00527 
00528 /* **********************
00529    * List constructions *
00530    **********************
00531 */
00532 
00533 okltest_rotate(f) := (
00534   assert(f([1,2,3],1) = [3,1,2]),
00535   assert(f([1,2,3],2) = [2,3,1]),
00536   for n : -3 thru 3 do
00537     assert(f([],n) = []),
00538   for n : -3 thru 3 do
00539     assert(f([1,2,3],3*n) = [1,2,3]),
00540   for n : -3 thru 3 do
00541     assert(f([1,2,3],n) = f([1,2,3],-3+n)),
00542   block([L1,L2],
00543    L1 : [1,2,3],
00544    L2 : f(L1,2),
00545    assert(L2 = [2,3,1]),
00546    L2[1] : 77,
00547    assert(L1 = [1,2,3])),
00548   true)$
00549 
00550 okltest_stable_unique(f) := (
00551   assert(f([]) = []),
00552   assert(f([1]) = [1]),
00553   assert(f([1,2]) = [1,2]),
00554   assert(f([2,1]) = [2,1]),
00555   assert(f([1,1]) = [1]),
00556   assert(f([1,2,3]) = [1,2,3]),
00557   assert(f([2,1,3]) = [2,1,3]),
00558   assert(f([1,3,2]) = [1,3,2]),
00559   assert(f([1,2,1]) = [1,2]),
00560   assert(f([2,2,1]) = [2,1]),
00561   assert(f([3,1,1]) = [3,1]),
00562   assert(f([1,1,3]) = [1,3]),
00563   assert(f([1,1,1]) = [1]),
00564   assert(f([1,2,3,4,1,5,2,6,3,7,4,8,5]) = [1,2,3,4,5,6,7,8]),
00565   true)$
00566 
00567 okltest_interleave_l(f) := block(
00568   for i : 1 thru 20 do
00569     assert(apply(f, create_list([],j,0,i)) = []),
00570   assert(f([1],[2]) = [1,2]),
00571   assert(f([1,3],[2,4]) = [1,2,3,4]),
00572   assert(f([1,3],[2,4,5]) = [1,2,3,4]),
00573   assert(f([1,4],[2,5],[3,6]) = [1,2,3,4,5,6]),
00574   assert(f([1,4,7],[2,5],[3,6]) = [1,2,3,4,5,6]),
00575   true)$
00576 
00577 okltest_paddingfront_l(f) := (
00578   assert(f(0,[],0) = []),
00579   assert(f(0,[],-1) = []),
00580   assert(f(0,[],1) = [0]),
00581   assert(f(0,[1,2],0) = [1,2]),
00582   assert(f(0,[1,2],1) = [1,2]),
00583   assert(f(0,[1,2],2) = [1,2]),
00584   assert(f(0,[1,2],3) = [0,1,2]),
00585   assert(f(0,[1,2],4) = [0,0,1,2]),
00586   true)$
00587 
00588 okltest_paddingback_l(f) := (
00589   assert(f(0,[],0) = []),
00590   assert(f(0,[],-1) = []),
00591   assert(f(0,[],1) = [0]),
00592   assert(f(0,[1,2],0) = [1,2]),
00593   assert(f(0,[1,2],1) = [1,2]),
00594   assert(f(0,[1,2],2) = [1,2]),
00595   assert(f(0,[1,2],3) = [1,2,0]),
00596   assert(f(0,[1,2],4) = [1,2,0,0]),
00597   true)$
00598 
00599 
00600 /* *************************
00601    * Random parts of lists *
00602    *************************
00603 */
00604 
00605 /* ************************
00606    * Numerical operations *
00607    ************************
00608 */
00609 
00610 okltest_count_l(f) := (
00611   assert(f(0,[]) = 0),
00612   assert(f(0,[1]) = 0),
00613   assert(f(0,[0]) = 1),
00614   assert(f(0,[1,2,0,0,1,-1,0,3]) = 3),
00615   true)$
00616 
00617 okltest_countgt_l(f) := (
00618   assert(f(0,[]) = 0),
00619   assert(f(0,[1]) = 1),
00620   assert(f(0,[0]) = 0),
00621   assert(f(0,[-1]) = 0),
00622   assert(f(0,[1,2,0,0,1,-1,0,3]) = 4),
00623   true)$
00624 
00625 okltest_countlt_l(f) := (
00626   assert(f(0,[]) = 0),
00627   assert(f(0,[1]) = 0),
00628   assert(f(0,[0]) = 0),
00629   assert(f(0,[-1]) = 1),
00630   assert(f(0,[1,2,0,0,1,-1,0,3]) = 1),
00631   true)$
00632 
00633 okltest_countpred_l(f) := (
00634   assert(f(identity,[]) = 0),
00635   assert(f(evenp,[0,2,3,4,5]) = 3),
00636   assert(okltest_count_l(buildq([f], lambda([x,L], f(lambda([y],is(y=x)),L)))) = true),
00637   true)$
00638 
00639 okltest_countntconst_l(f) := (
00640   assert(f(0,[]) = 0),
00641   assert(f(0,[0]) = 0),
00642   assert(f(0,[0,0]) = 1),
00643   assert(f(0,[0,0,0,1,0,0,1,2,2,1,1,0]) = 2),
00644   true)$
00645 
00646 okltest_size_constintervals_l(f) := (
00647   assert(f([]) = []),
00648   assert(f([0]) = [1]),
00649   assert(f([0,0]) = [2]),
00650   assert(f([0,1,1,0,0,2,2,1,0]) = [1,2,2,2,1,1]),
00651   true)$
00652 
00653 okltest_Delta_l(f) := (
00654   assert(f([]) = []),
00655   assert(f([7]) = []),
00656   assert(f([1,3]) = [2]),
00657   assert(f([1,4,7,9,2,0]) = [3,3,2,-7,-2]),
00658   true)$
00659 
00660 okltest_count_extremals_l(f) := (
00661   assert(f([]) = 0),
00662   assert(f([0]) = 1),
00663   assert(f([0,0]) = 1),
00664   assert(f([0,0,1]) = 2),
00665   assert(f([0,0,1,1,2]) = 2),
00666   assert(f([0,0,1,1,2,2,3,4,5]) = 2),
00667   assert(f([0,0,1,1,2,2,3,4,5,5,4]) = 3),
00668   assert(f([0,0,1,1,2,2,3,4,5,5,4,3,-1]) = 3),
00669   assert(f([0,0,1,1,2,2,3,4,5,5,4,3,-1,-1,0,0,6]) = 4),
00670   true)$
00671 
00672 okltest_m2l_r(f) := (
00673   assert(f(matrix([])) = []),
00674   assert(f(matrix([1,2,3])) = [1,2,3]),
00675   assert(f(matrix([1,2,3],[4,5,6])) = [1,2,3,4,5,6]),
00676   assert(f(matrix([1,2,3],[4,5,6],[7,8,9])) = [1,2,3,4,5,6,7,8,9]),
00677   assert(f([]) = []),
00678   assert(f([[]]) = []),
00679   assert(f([[1,2,3]]) = [1,2,3]),
00680   assert(f([[1,2,3],[4,5,6]]) = [1,2,3,4,5,6]),
00681   assert(f([[1,2,3],[4,5,6],[7,8,9]]) = [1,2,3,4,5,6,7,8,9]),
00682   assert(f([[1,2,3],[4,5,6],[7,8,9],[]]) = [1,2,3,4,5,6,7,8,9]),
00683   assert(f([[1,2,3],[4,5,6],[7,8,9],[10]]) = [1,2,3,4,5,6,7,8,9,10]),
00684   true)$
00685 
00686 okltest_transpose_l(f) := (
00687   for i : 0 thru 5 do
00688     assert(f([],i) = []),
00689   assert(f([1,2,3,4],1) = [1,2,3,4]),
00690   assert(f([1,2,3,4],2) = [1,3,2,4]),
00691   assert(f([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16],4) = [1,5,9,13,2,6,10,14,3,7,11,15,4,8,12,16]), 
00692   true)$
00693 
00694 okltest_matrixcolumns(f) := (
00695   assert(f(matrix()) = []),
00696   assert(f(matrix([1],[2],[3])) = [[1,2,3]]),
00697   assert(f(matrix([1,2,3])) = [[1],[2],[3]]),
00698   assert(f(matrix([1,2,3],[4,5,6])) = [[1,4],[2,5],[3,6]]),
00699   assert(f(matrix([1,2,3],[4,5,6],[7,8,9])) = [[1,4,7],[2,5,8],[3,6,9]]),
00700   true)$
00701 
00702 okltest_columns2matrix(f) := (
00703   assert(f([]) = matrix()),
00704   assert(f([[1,2,3]]) = matrix([1],[2],[3])),
00705   assert(f([[1],[2],[3]]) = matrix([1,2,3])),
00706   assert(f([[1,4],[2,5],[3,6]]) = matrix([1,2,3],[4,5,6])),
00707   assert(f([[1,4,7],[2,5,8],[3,6,9]]) = matrix([1,2,3],[4,5,6],[7,8,9])),
00708   true)$
00709 
00710 
00711 /* ***********
00712    * Sorting *
00713    ***********
00714 */
00715 
00716 okltest_sort_length(f) := (
00717   assert(f([]) = []),
00718   for n : 0 thru 5 do block([L : create_list(setn(i),i,1,n)],
00719     assert(f(reverse(L)) = L)
00720   ),
00721   assert(f([{1,2},{2,3}]) = [{1,2},{2,3}]),
00722   true)$
00723 
00724 okltest_sort_length_desc(f) := (
00725   /* XXX */
00726   true)$
00727 
00728 okltest_sort_length_part(f) := block([x,y],
00729   assert(f([]) = []),
00730   assert(f([{}]) = [[{}]]),
00731   assert(f([[x],[y],[x,y]]) = [[[x],[y]],[[x,y]]]),
00732   assert(f([[x,y],[x],[y]]) = [[[x],[y]],[[x,y]]]),
00733   assert(f([{1,2,3},{1,2},{4,5,6},{2,3},{1},{}]) = [[{}],[{1}],[{1,2},{2,3}],[{1,2,3},{4,5,6}]]),
00734   true)$
00735 
00736 okltest_sort_length_desc_part(f) := block([x,y],
00737   assert(f([]) = []),
00738   assert(f([{}]) = [[{}]]),
00739   assert(f([[x],[y],[x,y]]) = [[[x,y]],[[x],[y]]]),
00740   assert(f([[x,y],[x],[y]]) = [[[x,y]],[[x],[y]]]),
00741   assert(f([{1,2,3},{1,2},{4,5,6},{2,3},{1},{}]) = [[{1,2,3},{4,5,6}],[{1,2},{2,3}],[{1}],[{}]]),
00742   true)$
00743 
00744 okltest_l2order_p(f) := block(
00745   assert(f([])(1,2) = und),
00746   assert(f([1])(1,1) = true),
00747   assert(f([1])(1,2) = und),
00748   assert(f([1])(2,1) = und),
00749   assert(f([1,2])(1,2) = true),
00750   assert(f([1,2])(1,1) = true),
00751   assert(f([1,2])(2,2) = true),
00752   assert(f([1,2])(2,1) = false),
00753   assert(f([1,2,3,4])(1,2) = true),
00754   assert(f([1,2,3,4])(2,3) = true),
00755   assert(f([1,2,3,4])(3,4) = true),
00756   assert(f([1,2,3,4])(2,4) = true),
00757   assert(f([1,2,3,4])(1,4) = true),
00758   assert(f([1,2,3,4])(2,1) = false),
00759   assert(f([1,2,3,4])(4,4) = true),
00760   assert(f([1,2,3,4])(4,2) = false),
00761   true)$
00762 
00763 
00764