/*****************************************************************************
  $Id: log,v 1.1.1.1 2004/10/16 15:22:51 nthiery Exp $
  File:		lib/COMBINAT/TEST/decomposableObjects.tst
  Authors:	Sbastien Cellier <snoteip@yahoo.fr>
		Nicolas M. Thiry <nthiery@users.sourceforge.net>
  Credits:	Some tests are taken from the documentation of cs 1.5
  Created:	16/05/2002
  Purpose:	Test file for the library 'combinat::decomposableObjects'
  License:	LGPL
*****************************************************************************/
// Warning : in the example of the doc E=exp(1) is forbidden. Should be corrected.

prog::testinit("decomposableObjects", 57.5):

// Some parameters.
//****************************
// To print the progression... 
// showProgress := TRUE:
// do not test the list if it's longer that maxTestList
maxTestList := 500: // 5040=7! would be good but the watchdog does not allows that...
maxTestRand := 50:  

if showProgress=TRUE then
    unprotect(prog):
    oldtestfunc := prog::testfunc:
    prog::testfunc := proc() begin print(args()); eval(hold(oldtestfunc)(args())); end_proc:
    protect(prog):
end_if:

// TODO: use domain names without capital letters
// (e.g. binaryTrees instead of BinaryTrees)
// Procedure to get a temporary file name
tmpnam :=
proc() : DOM_PROC
    option escape;
    local i;
begin
    if version() <> [2,0,0] then
	proc() : DOM_STRING
	    local fd, fn;
	begin
	    fd := fopen(TempFile, Raw):
	    fn := fname(fd):
	    fclose(fd):
	    return(fn);
	end_proc;
    else
        // MuPAD 2.0.0 that does not have the TempFile option
	i := 0;
	if domtype(util) = DOM_DOMAIN or traperror(module(util)) = 0 then
	    module::displace("stdmod");
	    // If the dynamic module util is there
	    proc() : DOM_STRING
	    begin
		i := i+1;
		return("/tmp/mupad-".util::kernelPid()."-".util::time().i);
	    end_proc;
	else
	    proc() : DOM_STRING
	    begin
		i := i+1;
		return("/tmp/mupad-tmpnam".i);
	    end_proc;
	end_if;
    end_if;
end_proc():


alias(CS=combinat::decomposableObjects):

checkCoherenceDefaultOptions :=
table(
      CheckList=TRUE,
      CheckRand=TRUE,
      CheckC = if shell <> FAIL then
		   TRUE;
	       else
		   warning("module shell not available; not checking C code generation");
		   FALSE;
	       end_if
     ):

// Get around a kernel crash in MuPAD 2.0.0
// when the module stdmod is loaded before another module
%if version()=[2,0,0] then
    module::displace(stdmod):
end_if:

testRand := 
/**
    Check the coherence of the list function with the random.
    That is for the 
**/
proc(objs, size)
    local res, number, choice, i, nTest, repetitions;
begin
    res    := table();
    count  := 0;
    number := objs::count(size);
    if number > 10 then // heuristic. I Should look in a 
// probability book from where the sqrt law is a good approximation 
	nTest  := ceil(number*sqrt(number)*2);
    else
	nTest  := ceil(number*sqrt(number)*5);
    end_if;
    for i from 1 to nTest do
	choice := objs::random(size);
	if domtype(res[choice]) = DOM_INT then 
	     res[choice] := res[choice]+1;
	else res[choice] := 1;
	end_if;
    end_for;
// Should do some statistical tests here.
// for the moment I only test that the hits objects is the full list.
    if nops(res) < number then print(res); end_if;
    sort(map([op(res)], op, 1));
end_proc:

checkCoherence :=
proc(M: DOM_DOMAIN, maxn: Type::NonNegInt)
/**
    Check the coherence of the counting and generating functions
    of the domain M for n in 1..maxn.
    It also checks that there are no duplicates.
    Finally, it runs prog::check on the generated code.
**/
    local n, r, l, options, checkC, sourcefilename, programfilename;
begin
    options := combinat::getOptions(checkCoherenceDefaultOptions, 2, Error);
    if options[CheckC] then
	sourcefilename  := tmpnam();
	programfilename := tmpnam();
	if traperror(M::generateCode(sourcefilename)) <> 0 then
	    warning("Error while generating C code;not checking the C code generation");
	elif // This call to the compiler is certainly not portable ...
	     traperror(shell::system("cc -x c ".sourcefilename." -o ".programfilename." -lgmp")) <> 0 then
	    warning("could not compile the C code; not checking the C code generation");
	    options[CheckC]:=FALSE;
	end_if;
	shell::removeFile(sourcefilename):
    end_if;
    for n from 0 to maxn do
	r := M::count(n);
	if options[CheckList] and r < maxTestList then
	    if showProgress=TRUE then print(NoNL, "".n."(".r.")"); end_if;
	    prog::test(nops((l := M::list(n))),       r);
	    if showProgress=TRUE then print(NoNL, "L"); end_if;
	    prog::test(nops({op(l)}), r);
	    if options[CheckRand] and r < maxTestRand then
		prog::test(bool(sort(l) = sort(testRand(M, n))));
		if showProgress=TRUE then print(NoNL, "R"); end_if;
	    end_if;
	    if showProgress=TRUE then print(NoNL, " "); end_if;
	end_if;

	if options[CheckC] then
	    prog::test(text2expr(shell::system(programfilename." ".n." 0")),r);
	end_if;
    end_for;
    if options[CheckList] and showProgress=TRUE then print(NoNL, "\n"); end_if;
    if options[CheckC] then
	shell::removeFile(programfilename):
    end_if;
    map(M::unrank_functions, prog::check, 3):
    map(M::random_functions, prog::check, 3):
    map(M::count_functions,  prog::check, 3):
end_proc:


//////////////////////////////////////////////////////////////////////////////
prog::testfunc("typeStandardSpecification"):
//////////////////////////////////////////////////////////////////////////////

P:=CS({A=Atom}):
prog::test(testtype(table(A = Atom()), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(A = Epsilon()), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(A = Atom(A)), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(A = A), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = Prod(A)), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = Prod(A, B)), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = Prod(A, A)), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(A = Delta(A, 1)), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(), P::typeStandardSpecification),
           TRUE):
prog::test(testtype({d=1}, P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = 1), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = B), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = Alias(B)), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(A = Int(B)), P::typeStandardSpecification),
           FALSE):
prog::test(testtype(table(B = Atom(B), A = Int(B)), P::typeStandardSpecification),
           TRUE):
prog::test(testtype(table(A = Alias(B), B = Atom(B)), P::typeStandardSpecification),
           TRUE):

delete(i):
//////////////////////////////////////////////////////////////////////////////
prog::testfunc("optimizeStandardSpecification"):
//////////////////////////////////////////////////////////////////////////////

// Put the a standard specification in a standard form that is unique
// in a given MuPAD session
normalForm :=
proc(t)
    local aliases, nonAliases, rest, classes, production, A, B, class, m, x;
begin
    [aliases, nonAliases, rest] := split(t, has, Alias);
    for production in nonAliases do
	A := lhs(production);
	classes[A] := [A]
    end_for;
    for production in aliases do
	A := lhs(production);
	B := op(rhs(production), 1);
	classes[B] := append(classes[B],A)
    end_for;
    classes := map([op(classes)], op, 2);
    aliases := table();
    for class in classes do
	// Pickup a canonical element of this class (here the smallest
	// element of the class w.r.t. the system order and set alias
	// from the others to this one
	class := sort(class, sysorder);
	m := op(class,1);
	class := [op(class, 2..nops(class))];
	for x in class do
	    aliases[x] := m;
	end_for;
    end_for;
    table(op(map(aliases, Alias)), op(subs([op(nonAliases)], [op(aliases)])));
end:

prog::test(normalForm(table(A=Prod(B,C), F=Union(A,B))), 
	   table(A=Prod(B,C), F=Union(A,B))):
prog::test(bool(normalForm(table(A=Alias(B), B=Atom)) <>
	   table(A=Atom, B=Atom))):
prog::test(bool(normalForm(table(A=Alias(B), B=Atom, F=Union(A,C))) =
		normalForm(table(B=Alias(A), A=Atom, F=Union(B,C))))):

prog::test(normalForm(P::optimizeStandardSpecification(table(e = Prod(c, d), a = Alias(b), b = Alias(c), g = Union(e, e), h = Union(f, f), i = Atom(), c = Alias(d), f = Prod(d, b), d = Atom()))),
           normalForm(table(a=Alias(d),b=Alias(d),c=Alias(d),i=Alias(d),e=Alias(f),g=Alias(h),h=Union(f, f),f=Prod(d, d),d=Atom()))):

P:=CS([a=b, b=c, g=Union(e,e), h=Union(f,f), i=Atom, c=d, f=Prod(d,b), d=Atom, j=Prod(c,d), e=Alias(j, Sequence)]):

prog::test(normalForm(P::standardSpecification()),
           normalForm(table(c=Alias(d),b=Alias(d),a=Alias(d),j=Alias(f),e=Alias(f, Sequence),d=Atom(d),f=Prod(d, d),i=Atom(i),h=Union(f, f),g=Union(e, e)))):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("standardSpecification: errors"):
//////////////////////////////////////////////////////////////////////////////

bla := combinat::decomposableObjects([T=Multiset(S)]):
if prog::TestNoTestargs = FALSE then
prog::test(bla::standardSpecification(), TrapError=1028):
end_if:
bla := combinat::decomposableObjects([T=Multiset(T,T)]):
prog::test(bla::standardSpecification(), TrapError=1028):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("standardSpecificationCount"):
//////////////////////////////////////////////////////////////////////////////
prog::test(normalForm(P::standardSpecificationCount()),
           normalForm(table(e=Alias(f),j=Alias(f),a=Alias(d),b=Alias(d),c=Alias(d),i=Alias(d),g=Alias(h),h=Union(f, f),f=Prod(d, d),d=Atom(1)))):

prog::test(P::isContextFree(), TRUE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("unstandard"):
//////////////////////////////////////////////////////////////////////////////
/*
testdom := CS({}):
prog::test(testdom::unstandard(Prod(A,B)),
	   Prod(A, B)):
prog::test(testdom::unstandard(Prod(A,Prod(A,B))),
	   Prod(A, Prod(A, B))):
prog::test(testdom::unstandard(Prod(A,ProdPrd(A,B))),
	   Prod(A, A, B)):
prog::test(testdom::unstandard(Prod(A,ProdSeq(A,B))),
	   Prod(A, Sequence(A, B))):
prog::test(testdom::unstandard(Prod(A,ProdSeq(A,Epsilon))),
	   Prod(A, Sequence(A))):
prog::test(testdom::unstandard(Prod(A,ProdSeq(A,ProdSeqPrd(A,B)))),
	   Prod(A, Sequence(A, ProdSeqPrd(A, B)))):
prog::test(testdom::unstandard(Prod(A,ProdSeq(A,ProdPrdSeq(A,B)))),
	   Prod(A, Sequence(A, A, B))):
*/

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Union"):
//////////////////////////////////////////////////////////////////////////////

// Empty unions
bla := combinat::decomposableObjects([A=Union()]):
prog::test([bla::count(i) $ i = 0..10],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test(bla::valuation,
           table(A=infinity)):
prog::test(bla::degree,
           table(A=-infinity)):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Alias"):
//////////////////////////////////////////////////////////////////////////////

bla := combinat::decomposableObjects([A=Alias(Z)]):
prog::test(bla::random(1), Z):
prog::test(bla::unrank(1, 1), Z):
prog::test(bla::list(1), [Z]):

bla := combinat::decomposableObjects([A=Alias(Z, f)]):
prog::test(bla::random(1), f(Z)):
prog::test(bla::unrank(1, 1), f(Z)):
prog::test(bla::list(1), [f(Z)]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("BoxedProd"):
//////////////////////////////////////////////////////////////////////////////

bla := combinat::decomposableObjects([A=BoxedProd(Z,Z)], Labelled):
prog::test([bla::list(i) $ i = 0..3],
           [[], [], [Prod(Z(1), Z(2))], []]):
prog::test(bla::standardSpecification(),
           table(Z=Atom(Z),A=MultX(NonTerminal2),NonTerminal2=Prod(NonTerminal1, Z),NonTerminal1=DivX(Z))):

bla := combinat::decomposableObjects([A=BoxedProd(Z,Z)]):
prog::test(bla::standardSpecification(), TrapError=1028):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Binary Trees"):
//////////////////////////////////////////////////////////////////////////////

BinaryTrees:=CS({B=Union(Z,Prod(B,B))}):
prog::test([BinaryTrees::count(i) $ i=0..10 ],
	   [0,1,1,2,5,14,42,132,429,1430,4862]):
prog::test([BinaryTrees::random(i) $i=1..2],[Z,Prod(Z, Z)]):
prog::check(BinaryTrees,3):
checkCoherence(BinaryTrees, 7):
prog::test(BinaryTrees::isContextFree()):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Motzkin Trees"):
//////////////////////////////////////////////////////////////////////////////

MotzkinTrees:=CS({M=Union(Z,Prod(Z,M),Prod(Z,M,M))}):
prog::test([MotzkinTrees::count(i) $ i=0..10 ], 
	   [0,1,1,2,4,9,21,51,127,323,835]):
prog::test([MotzkinTrees::random(2)],[Prod(Z, Z)]):
checkCoherence(MotzkinTrees, 7):
prog::test(MotzkinTrees::isContextFree(), TRUE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Shroeder Trees"):
//////////////////////////////////////////////////////////////////////////////

ShroederTrees:=CS([S=Union(Z,Prod(S,U)), U=Union(Prod(Z,S),Prod(S,U))]):
prog::test([ShroederTrees::count(i) $ i=0..10 ], 
	   [0,1,0,1,1,3,6,15,36,91,232]):
prog::test([ShroederTrees::random(3)],[Prod(Z, Prod(Z, Z))]):
// This grammar is ambiguous!
// checkCoherence(ShroederTrees, 9):
prog::test([nops(ShroederTrees::list(i)) $i=0..6],
	   [ShroederTrees::count(i) $i=0..6]):
prog::test(ShroederTrees::isContextFree(), TRUE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Sequences"):
//////////////////////////////////////////////////////////////////////////////

Sequences0 := CS({S=Sequence(Z)}):
prog::test([Sequences0::random(i)    $ i=0..10],
	   [Sequence(Z $ i) $ i=0..10]):
checkCoherence(Sequences0, 10):
prog::test(Sequences0::isContextFree(), TRUE):

Sequences1 := CS([S=Sequence(Z),Z=Union(A,B),A=Atom,B=Atom]):
prog::test([Sequences1::count(i) $ i=0..10 ], 
	   [1,2,4,8,16,32,64,128,256,512,1024]):
SEED:=3:
prog::test([Sequences1::random(3)],
	   [Sequence(A, A, A)]):
prog::test(Sequences1::list(0),
	   [Sequence()]):
prog::test(Sequences1::list(1),
	   [Sequence(A), Sequence(B)]):
prog::test(Sequences1::list(2),
	   [Sequence(A, A), Sequence(A, B), Sequence(B, A), Sequence(B, B)]):
prog::test(Sequences1::list(3),
	   [Sequence(A, A, A), Sequence(A, A, B), Sequence(A, B, A),
	    Sequence(A, B, B), Sequence(B, A, A), Sequence(B, A, B),
	    Sequence(B, B, A), Sequence(B, B, B)]):
checkCoherence(Sequences1, 6):

Sequences2 := CS([S=Sequence(Z, MinLength=3), Z=Union(A,B),A=Atom,B=Atom],
		 Boustrophedonic=FALSE):
prog::test([Sequences2::count(i) $ i=0..10 ], 
	   [0, 0, 0, 8, 16, 32, 64, 128, 256, 512, 1024]):
SEED:=1:
prog::test([Sequences2::random(3)],
	   [Sequence(B, A, B)]):
checkCoherence(Sequences2, 6):

Sequences3 := CS([S=Sequence(Z, Length =3), Z=Union(A,B), A=Atom, B=Atom]):
prog::test([Sequences3::count(i) $ i=0..10 ], 
	   [0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]):
SEED:=2:
prog::test([Sequences3::random(3)],
	   [Sequence(A, A, A)]):
checkCoherence(Sequences3, 6):

Sequences4 := CS([S=Sequence(Z, MaxLength=3), Z=Union(A,B),A=Atom, B=Atom]):
prog::test([Sequences4::count(i) $ i=0..10 ], 
	   [1, 2, 4, 8, 0, 0, 0, 0, 0, 0, 0]):
SEED:=2:
prog::test([Sequences4::random(3)],
	   [Sequence(A, A, B)]):
checkCoherence(Sequences4, 10):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Sequences with MinLength"):
//////////////////////////////////////////////////////////////////////////////

Sequences := CS([A = Sequence(Z, MinLength=0)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
prog::test(Sequences::standardSpecification(),
           table(Z=Atom(Z),A=Alias(NonTerminal3, Sequence),NonTerminal1=Alias(NonTerminal4, op),NonTerminal4=Prod(Z, NonTerminal3),NonTerminal3=Union(NonTerminal2, NonTerminal1),NonTerminal2=Epsilon())):

Sequences := CS([A = Sequence(Z, MinLength=1)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
prog::test(Sequences::standardSpecification(),
           table(Z=Atom(Z),A=Alias(NonTerminal1, Sequence),NonTerminal1=Alias(NonTerminal4, op),NonTerminal4=Prod(Z, NonTerminal3),NonTerminal3=Union(NonTerminal2, NonTerminal1),NonTerminal2=Epsilon())):

Sequences := CS([A = Sequence(Z, MinLength=2)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
prog::test([Sequences::list(i) $ i = 0..5],
           [[], [], [Sequence(Z, Z)], [Sequence(Z, Z, Z)],
	    [Sequence(Z, Z, Z, Z)], [Sequence(Z, Z, Z, Z, Z)]]):
prog::test(Sequences::standardSpecification(),
           table(Z=Atom(Z),A=Alias(NonTerminal5, Sequence@op),NonTerminal5=Prod(Z, NonTerminal1),NonTerminal1=Alias(NonTerminal4, op),NonTerminal4=Prod(Z, NonTerminal3),NonTerminal3=Union(NonTerminal2, NonTerminal1),NonTerminal2=Epsilon())):

Sequences := CS([A = Sequence(Z, MinLength=3)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Sequences with MinLength and MaxLength"):
//////////////////////////////////////////////////////////////////////////////

Sequences := CS([A = Sequence(Z, MinLength=1, MaxLength=1)]):
prog::test(Sequences::standardSpecification(),
           table(Z=Atom(Z),A=Alias(Z, Sequence))):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test([Sequences::list(i) $ i = 0..5],
           [[], [Sequence(Z)], [], [], [], []]):

Sequences := CS([A = Sequence(Z, MinLength=1, MaxLength=2)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test([Sequences::list(i) $ i = 0..5],
           [[], [Sequence(Z)], [Sequence(Z, Z)], [], [], []]):

Sequences := CS([A = Sequence(Z, MinLength=0, MaxLength=2)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test([Sequences::list(i) $ i = 0..5],
           [[Sequence()], [Sequence(Z)], [Sequence(Z, Z)], [], [], []]):

Sequences := CS([A = Sequence(Z, MinLength=2, MaxLength=2)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]):

Sequences := CS([A = Sequence(Z, MinLength=2, MaxLength=3)]):
prog::test([Sequences::count(i) $ i = 0..10],
           [0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("General Trees"):
//////////////////////////////////////////////////////////////////////////////

GeneralTrees := CS({T=Prod(Z,Sequence(T))}):
prog::test([GeneralTrees::count(i) $i=0..10],
	   [0, 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]):
prog::test([GeneralTrees::random(2)],
	   [Prod(Z, Sequence(Prod(Z, Sequence())))]):
checkCoherence(GeneralTrees, 6):
prog::test(GeneralTrees::isContextFree(), TRUE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Empty decomposable class"):
//////////////////////////////////////////////////////////////////////////////

EmptyClass := CS([A=Prod(Z,A,A)]):
EmptyClass::makeValuation():
prog::test(EmptyClass::valuation[A], infinity):
prog::test([EmptyClass::count(i) $i=0..10],
	   [0 $ 11]):
checkCoherence(EmptyClass, 6):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Theta"):
//////////////////////////////////////////////////////////////////////////////

Theta1 := CS({S=Theta(Z)}):
prog::test([Theta1::count(i) $ i=0..10 ], 
	   [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test(Theta1::generator, FAIL):
prog::test(Theta1::unrank,    FAIL):
prog::test(domtype(Theta1::unrankFailureReason), DOM_STRING):
prog::test(contains(Theta1::interface, hold(unrankFailureReason))):
prog::test(not contains(Theta1::interface, hold(unrank))):
checkCoherence(Theta1, 10, CheckList=FALSE):
prog::test(Theta1::isContextFree(), FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Int"):
//////////////////////////////////////////////////////////////////////////////

Int1 := CS({S=Int(Z)}):
prog::test([Int1::count(i) $ i=0..10 ], 
	   [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
prog::test(Int1::generator, FAIL):
prog::test(Int1::unrank,    FAIL):
checkCoherence(Int1, 10, CheckList=FALSE):
prog::test(Int1::isContextFree(), FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("DivX"):
//////////////////////////////////////////////////////////////////////////////

bla:=combinat::decomposableObjects([T=DivX(Z)], Labelled):
prog::test(bla::list(0), [Z(Mark(1))]):
prog::test([bla::count(i) $ i = 0..10],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

bla:=combinat::decomposableObjects([T=DivX(DivX(Z))], Labelled):
prog::test([bla::count(i) $ i = 0..10],
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

bla:=combinat::decomposableObjects([T=DivX(DivX(Prod(Z,Z)))], Labelled):
prog::test(bla::list(0),
           [Prod(Z(Mark(2)), Z(Mark(1))), Prod(Z(Mark(1)), Z(Mark(2)))]):
prog::test([bla::count(i) $ i = 0..10],
           [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

bla:=combinat::decomposableObjects([T=Prod(DivX(Z),DivX(Z))], Labelled):
prog::test(bla::list(0),
           [Prod(Z(Mark(1)), Z(Mark(2)))]):
prog::test([bla::count(i) $ i = 0..10],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

TDiv := CS([BLA = DivX(DivX(DivX(DivX(Sequence(Z)))))], Labelled):
prog::test(TDiv::count(0), 24):
// According to Nicolas, there should be different marks here.
prog::test(sort(TDiv::list(0)),
           sort( // Note: the generation order seems to have changed!
	   [Sequence(Z(Mark(4)), Z(Mark(3)), Z(Mark(2)), Z(Mark(1))),
	    Sequence(Z(Mark(3)), Z(Mark(4)), Z(Mark(2)), Z(Mark(1))),
	    Sequence(Z(Mark(2)), Z(Mark(4)), Z(Mark(3)), Z(Mark(1))),
	    Sequence(Z(Mark(1)), Z(Mark(4)), Z(Mark(3)), Z(Mark(2))),
	    Sequence(Z(Mark(4)), Z(Mark(2)), Z(Mark(3)), Z(Mark(1))),
	    Sequence(Z(Mark(3)), Z(Mark(2)), Z(Mark(4)), Z(Mark(1))),
	    Sequence(Z(Mark(2)), Z(Mark(3)), Z(Mark(4)), Z(Mark(1))),
	    Sequence(Z(Mark(1)), Z(Mark(3)), Z(Mark(4)), Z(Mark(2))),
	    Sequence(Z(Mark(4)), Z(Mark(1)), Z(Mark(3)), Z(Mark(2))),
	    Sequence(Z(Mark(3)), Z(Mark(1)), Z(Mark(4)), Z(Mark(2))),
	    Sequence(Z(Mark(2)), Z(Mark(1)), Z(Mark(4)), Z(Mark(3))),
	    Sequence(Z(Mark(1)), Z(Mark(2)), Z(Mark(4)), Z(Mark(3))),
	    Sequence(Z(Mark(4)), Z(Mark(3)), Z(Mark(1)), Z(Mark(2))),
	    Sequence(Z(Mark(3)), Z(Mark(4)), Z(Mark(1)), Z(Mark(2))),
	    Sequence(Z(Mark(2)), Z(Mark(4)), Z(Mark(1)), Z(Mark(3))),
	    Sequence(Z(Mark(1)), Z(Mark(4)), Z(Mark(2)), Z(Mark(3))),
	    Sequence(Z(Mark(4)), Z(Mark(2)), Z(Mark(1)), Z(Mark(3))),
	    Sequence(Z(Mark(3)), Z(Mark(2)), Z(Mark(1)), Z(Mark(4))),
	    Sequence(Z(Mark(2)), Z(Mark(3)), Z(Mark(1)), Z(Mark(4))),
	    Sequence(Z(Mark(1)), Z(Mark(3)), Z(Mark(2)), Z(Mark(4))),
	    Sequence(Z(Mark(4)), Z(Mark(1)), Z(Mark(2)), Z(Mark(3))),
	    Sequence(Z(Mark(3)), Z(Mark(1)), Z(Mark(2)), Z(Mark(4))),
	    Sequence(Z(Mark(2)), Z(Mark(1)), Z(Mark(3)), Z(Mark(4))),
	    Sequence(Z(Mark(1)), Z(Mark(2)), Z(Mark(3)), Z(Mark(4)))])):


bla:=combinat::decomposableObjects([T=MultX(Z)], Labelled):
prog::test(bla::list(2),
           [Z(2)]):
prog::test(bla::list(1),
           []):
prog::test(bla::list(0),
           []):


//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Powerset"):
//////////////////////////////////////////////////////////////////////////////

for n from 0 to 4 do
    bla := combinat::decomposableObjects([A=Powerset(Union(Atom(Z.i) $ i =1..n))]):
    prog::test([bla::count(k) $ k=0..10], [binomial(n,k) $ k = 0..10]) ;
end_for:

bla:=combinat::decomposableObjects([T=Powerset(Sequence(Z), MinLength=2, MaxLength=3, MinPart=2, MaxPart=3)]):
prog::test([bla::list(i) $ i = 0..10],
           [[], [], [], [], [],
	    [Powerset(Sequence(Z, Z), Sequence(Z, Z, Z))],
	    [], [], [], [], []]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Multiset unlabelled"):
//////////////////////////////////////////////////////////////////////////////

Multiset1 := CS({S=Multiset(Z)}):
prog::test([Multiset1::count(i) $ i=0..10 ],
	   [binomial(i+0,0) $i=0..10]):
checkCoherence(Multiset1, 10, CheckC=FALSE, CheckRand=FALSE):
prog::test(Multiset1::unrank, FAIL):
prog::test(Multiset1::isContextFree(), FALSE):

Multiset2 := CS([S=Multiset(Z), Z=Union(A,B), A=Atom, B=Atom]):
prog::test([Multiset2::count(i) $ i=0..10 ], 
	   [binomial(i+1,1) $i=0..10]):
checkCoherence(Multiset2, 10, CheckC=FALSE, CheckRand=FALSE):

Multiset3 := CS([S=Multiset(Z), Z=Union(A,B,C), A=Atom, B=Atom, C=Atom]):
prog::test([Multiset3::count(i) $ i=0..10 ], 
	   [binomial(i+2,2) $i=0..10]):
checkCoherence(Multiset3, 10, CheckC=FALSE, CheckRand=FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Multiset with length conditions"):
//////////////////////////////////////////////////////////////////////////////

// Labelled
Multisets := CS([A = Multiset(Z, MinLength=3)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]):
Multisets := CS([A = Multiset(Z, MinLength=3)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]):
Multisets := CS([A = Multiset(Z)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
Multisets := CS([A = Multiset(Z, MinLength=1)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
Multisets := CS([A = Multiset(Z, MinLength=2)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]):
Multisets := CS([A = Multiset(Z, MinLength=2, MaxLength=2)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]):
Multisets := CS([A = Multiset(Z, MinLength=2, MaxLength=5)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0]):
Multisets := CS([A = Multiset(Z, MinLength=1, MaxLength=5)], Labelled):
prog::test([Multisets::count(i) $ i = 0..10],
           [0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0]):

// Unlabelled
Multisets:=combinat::decomposableObjects([T=Multiset(Z, MinLength=2)]):
prog::test(Multisets::list(0), []):
prog::test(Multisets::list(1), []):
prog::test(Multisets::list(2), [Multiset(Z, Z)]):
prog::test(Multisets::list(3), [Multiset(Z, Z, Z)]):
prog::test(Multisets::count,  FAIL):
prog::test(Multisets::unrank, FAIL):
prog::test(Multisets::random, FAIL):

Multisets:=combinat::decomposableObjects([T=Multiset(Z, MinLength=2, MaxLength=3)]):
prog::test(Multisets::list(0), []):
prog::test(Multisets::list(0), []):
prog::test(Multisets::list(1), []):
prog::test(Multisets::list(2), [Multiset(Z, Z)]):
prog::test(Multisets::list(3), [Multiset(Z, Z, Z)]):
prog::test(Multisets::list(4), []):

Multisets:=combinat::decomposableObjects([T=Multiset(Sequence(Z),
					       MinLength=2, MaxLength=3,
					       MinPart=2, MaxPart=3)]):
prog::test([Multisets::list(i) $ i = 0..10],
           [[], [], [], [],
	    [Multiset(Sequence(Z, Z), Sequence(Z, Z))],
	    [Multiset(Sequence(Z, Z), Sequence(Z, Z, Z))],
	    [Multiset(Sequence(Z, Z, Z), Sequence(Z, Z, Z)),
	     Multiset(Sequence(Z, Z), Sequence(Z, Z), Sequence(Z, Z))],
	    [Multiset(Sequence(Z, Z), Sequence(Z, Z), Sequence(Z, Z, Z))],
	    [Multiset(Sequence(Z, Z), Sequence(Z, Z, Z), Sequence(Z, Z, Z))],
	    [Multiset(Sequence(Z, Z, Z), Sequence(Z, Z, Z), Sequence(Z, Z, Z))],
	    []]):


//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Partitions"):
//////////////////////////////////////////////////////////////////////////////
delete(P): delete(N):
Partitions1 := CS([P=Multiset(N),N=Sequence(Z,MinLength=1)]):
prog::test([Partitions1::count(i) $ i=0..10 ], 
	   [1,1,2,3,5,7,11,15,22,30,42]):
prog::test(Partitions1::list(4),
           [Multiset(Sequence(Z, Z, Z, Z)), Multiset(Sequence(Z), Sequence(Z, Z, Z)), Multiset(Sequence(Z, Z), Sequence(Z, Z)), Multiset(Sequence(Z), Sequence(Z), Sequence(Z, Z)), Multiset(Sequence(Z), Sequence(Z), Sequence(Z), Sequence(Z))]):
checkCoherence(Partitions1, 10, CheckC=FALSE, CheckRand=FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Partitions2"):

entier:=()->1:entier(0):=0:
Partitions2:=CS([P=Multiset(N),N=Predefined(entier)]):
prog::test([Partitions2::count(i) $ i=0..10 ], 
	   [1,1,2,3,5,7,11,15,22,30,42]):
prog::test(Partitions2::list,      FAIL):
prog::test(Partitions2::generator, FAIL):
prog::test(Partitions2::random,    FAIL):
prog::test(Partitions2::unrank,    FAIL):
checkCoherence(Partitions2, 10, CheckC=FALSE, CheckList=FALSE, CheckRand=FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Partitions3"):
isodd:=x->x mod 2:
Partitions3:= CS([P=Multiset(N),N=Predefined(isodd)]):
prog::test([Partitions3::count(i) $ i=0..10],
	   [1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10]):

checkCoherence(Partitions3, 10, CheckList=FALSE, CheckC=FALSE, CheckRand=FALSE):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Unordered trees"):
//////////////////////////////////////////////////////////////////////////////

UT1 := CS([C=Prod(Z, Multiset(C))]):
prog::test([UT1::count(n) $ n=0..10],
	   [0, 1, 1, 2, 4, 9, 20, 48, 115, 286, 719]): // maple/combstruct
prog::test(UT1::list(3),
           [Prod(Z, Multiset(Prod(Z, Multiset(Prod(Z, Multiset()))))), Prod(Z, Multiset(Prod(Z, Multiset()), Prod(Z, Multiset())))]):
checkCoherence(UT1, 10, CheckRand=FALSE):

UT2 := CS([C=Prod(Z, Multiset(C))], Labelled):
prog::test([UT2::count(n) $ n=0..10],
	   [0, n^(n-1) $ n=1..10]):
prog::test(UT2::list(3),
           [Prod(Z(1), Multiset(Prod(Z(2),Multiset()),Prod(Z(3),Multiset()))),
	    Prod(Z(1), Multiset(Prod(Z(2),Multiset(Prod(Z(3),Multiset()))))),
	    Prod(Z(1), Multiset(Prod(Z(3),Multiset(Prod(Z(2),Multiset()))))),
	    Prod(Z(2), Multiset(Prod(Z(1),Multiset()),Prod(Z(3),Multiset()))),
	    Prod(Z(2), Multiset(Prod(Z(1),Multiset(Prod(Z(3), Multiset()))))),
	    Prod(Z(2), Multiset(Prod(Z(3),Multiset(Prod(Z(1), Multiset()))))),
	    Prod(Z(3), Multiset(Prod(Z(1),Multiset()),Prod(Z(2),Multiset()))),
	    Prod(Z(3), Multiset(Prod(Z(1),Multiset(Prod(Z(2),Multiset()))))),
	    Prod(Z(3), Multiset(Prod(Z(2),Multiset(Prod(Z(1),Multiset())))))
	   ]):
checkCoherence(UT2, 10, CheckC=FALSE): // labelled C code generation not implemented

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Cycle unlabelled"):
//////////////////////////////////////////////////////////////////////////////

Cycle1 := CS([C=Cycle(Z)]):
prog::test([Cycle1::count(n) $ n=0..10],
	   [1 $n=0..10]):
prog::test(Cycle1::list(0),
           [Cycle()]):
prog::test(Cycle1::list(1),
           [Cycle(Z)]):
prog::test(Cycle1::list(2),
           [Cycle(Z, Z)]):
prog::test(Cycle1::list(3),
           [Cycle(Z, Z, Z)]):
prog::test(Cycle1::list(4),
           [Cycle(Z, Z, Z, Z)]):
prog::test(Cycle1::list(5),
           [Cycle(Z, Z, Z, Z, Z)]):
checkCoherence(Cycle1, 10, CheckC=FALSE, CheckRand=FALSE):
  // C not implemented for Delta(...,numlib::phi)
  // Random elements are not normalized

Cycle1b := CS([W = Cycle(Union(Atom(A), Atom(B)))]):
prog::test(Cycle1b::list(0),
           [Cycle()]):
prog::test(Cycle1b::list(1),
           [Cycle(A), Cycle(B)]):
prog::test(Cycle1b::list(2),
           [Cycle(A, A), Cycle(A, B), Cycle(B, B)]):
prog::test(Cycle1b::list(3),
           [Cycle(A, A, A), Cycle(A, A, B), Cycle(A, B, B), Cycle(B, B, B)]):
prog::test(Cycle1b::list(4),
           [Cycle(A, A, A, A), Cycle(A, A, A, B), Cycle(A, B, A, B), Cycle(A, A, B, B),
	    Cycle(A, B, B, B), Cycle(B, B, B, B)]):
prog::test(Cycle1b::list(5),
	   [Cycle(A, A, A, A, A), Cycle(A, A, A, A, B), Cycle(A, A, B, A, B), Cycle(A, A, A, B, B),
	    Cycle(A, B, A, B, B), Cycle(A, A, B, B, B), Cycle(A, B, B, B, B), Cycle(B, B, B, B, B)]):

Cycle2 := CS([A=Cycle(T), T=Union(Z, Prod(T,T))]):
prog::test([Cycle2::count(n) $ n=0..20],
	   [1, 1, 2, 4, 10, 26, 80, 246, 810, 2704, 9252, 32066, 112720, 
	    400024, 1432860, 5170604, 18784170, 68635478, 252088496, 
	    930138522, 3446167860]): // maple/combstruct
// Note that Cycle() is accepted, while this is not the case with combstruct!

checkCoherence(Cycle2, 10, CheckC=FALSE, CheckRand=FALSE):
	// C not implemented for Delta(...,numlib::phi)
	// Random elements are not normalized

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Cycle labelled"):
//////////////////////////////////////////////////////////////////////////////

Cycle3 := CS([C=Cycle(Z)], Labelled):
prog::test([Cycle3::count(n) $ n=0..10],
	   [1, (n-1)! $n=1..10]):
checkCoherence(Cycle3, 10, CheckC=FALSE): // C not implemented for labelled

Cycle4 := CS([A=Cycle(T), T=Union(Z, Prod(T,T))], Labelled):
prog::test([Cycle4::count(n) $ n=0..10],
	   [ 1, 1, 3, 20, 210, 3024, 55440, 1235520, 32432400, 
	    980179200, 33522128640]): // maple/combstruct
checkCoherence(Cycle4, 10, CheckC=FALSE): // C not implemented for labelled

Cycles := CS([A = Cycle(Z)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [1, 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]):
Cycles := CS([A = Cycle(Z, MinLength=1)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]):
Cycles := CS([A = Cycle(Z, MinLength=2)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 0, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880]):
Cycles := CS([A = Cycle(Z, MinLength=3)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 0, 0, 2, 6, 24, 120, 720, 5040, 40320, 362880]):
Cycles := CS([A = Cycle(Z, MaxLength=0)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MaxLength=1)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MaxLength=2)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MaxLength=3)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [1, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MinLength=1, MaxLength=3)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 1, 1, 2, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MinLength=2, MaxLength=3)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 0, 1, 2, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MinLength=3, MaxLength=3)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0]):
Cycles := CS([A = Cycle(Z, MinLength=1, MaxLength=1)], Labelled):
prog::test([Cycles::count(i) $ i = 0..10],
           [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):


//////////////////////////////////////////////////////////////////////////////
prog::testfunc("PermutationsCycles"):
//////////////////////////////////////////////////////////////////////////////

// Permutations in disjoint cycle representation
PermCycles := CS([Permutation=Powerset(Cycle(Z, MinLength=1))], Labelled):
prog::test([PermCycles::count(n) $ n=0..10],
	   [ n!                  $ n=0..10]):
// Same thing, with pretty printing
PermCycles := CS([Permutation=Alias(PowerSet(Alias(Cycle(Atom(id), MinLength=1),
						   c -> if nops(c) = 1 then
							    null()
							else
							    [op(c)]
							end_if)),
				    DOM_LIST@op)],
		 Labelled):
prog::test(sort(PermCycles::list(4)),
	   sort( // Note: the generation order seems to have changed!
	   [[], [[3, 4]], [[2, 3, 4]], [[2, 4, 3]], [[2, 3]], [[2, 4]],
	    [[1, 2, 3, 4]], [[1, 3, 2, 4]], [[1, 4, 2, 3]], [[1, 2, 4, 3]],
	    [[1, 3, 4, 2]], [[1, 4, 3, 2]], [[1, 2]],
	    [[1, 3]], [[1, 4]], [[1, 2], [3, 4]], [[1, 3], [2, 4]],
	    [[1, 4], [2, 3]], [[1, 2, 3]], [[1, 2, 4]], [[1, 3, 4]],
	    [[1, 3, 2]], [[1, 4, 2]], [[1, 4, 3]]])):
checkCoherence(PermCycles, 10, CheckC=FALSE): // C not implemented for labelled

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Lyndon"):
//////////////////////////////////////////////////////////////////////////////

bla := combinat::decomposableObjects([W = Lyndon(Atom(A))]):
prog::test(bla::list(0),
           []):
prog::test(bla::list(1),
           [Lyndon(A)]):
prog::test(bla::list(2),
           []):
prog::test(bla::list(3),
           []):
prog::test(bla::list(4),
           []):
prog::test(bla::list(5),
           []):

bla := CS([W = Lyndon(Union(Atom(A), Atom(B)))]):
prog::test(bla::list(0),
           []):
prog::test(bla::list(1),
           [Lyndon(A), Lyndon(B)]):
prog::test(bla::list(2),
           [Lyndon(A, B)]):
prog::test(bla::list(3),
           [Lyndon(A, A, B), Lyndon(A, B, B)]):
prog::test(bla::list(4),
           [Lyndon(A, A, A, B), Lyndon(A, A, B, B), Lyndon(A, B, B, B)]):
prog::test(bla::list(5),
           [Lyndon(A, A, A, A, B), Lyndon(A, A, B, A, B), Lyndon(A, A, A, B, B),
	    Lyndon(A, B, A, B, B), Lyndon(A, A, B, B, B), Lyndon(A, B, B, B, B)]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Domain"):
//////////////////////////////////////////////////////////////////////////////

domain oddIntegers
    inherits Dom::BaseDomain;
    category Cat::GradedCombinatorialClass;
    axiom Ax::systemRep;

    size := id;
    count := n -> if n mod 2 = 1 then 1 else 0 end_if;
    list  := n -> if n mod 2 = 1 then [n] else [] end_if;
end_domain:
domain doubledEvenIntegers
    inherits Dom::BaseDomain;
    category Cat::GradedCombinatorialClass;
    axiom Ax::systemRep;

    size := _plus@op;
    count := n -> if n mod 4 = 0 then 1 else 0 end_if;
    list  := n -> if n mod 4 = 0 then [[n/2 $ 2]] else [] end_if;
end_domain:
domain doubledOddIntegers
    inherits Dom::BaseDomain;
    category Cat::GradedCombinatorialClass;
    axiom Ax::systemRep;

    size := _plus@op;
    count := n -> if n mod 4 = 2 then 1 else 0 end_if;
    list  := n -> if n mod 4 = 2 then [[n/2 $ 2]] else [] end_if;
end_domain:

// partitions with even numbers of even parts
bla := combinat::decomposableObjects([P=Multiset(doubledEvenIntegers)]):
prog::test(bla::list(4),
           [Multiset([2, 2])]):
prog::test(bla::list(5),
           []):
prog::test(bla::list(12),
           [Multiset([6, 6]), Multiset([2, 2], [4, 4]), Multiset([2, 2], [2, 2], [2, 2])]):
prog::test(bla::unrank, FAIL):

// partitions with even numbers of even parts, pretty printed
bla := combinat::decomposableObjects

                                    CS
                                                               Time: 1 msec
     ([P=Alias(Multiset(Alias(doubledEvenIntegers, op)),
	       revert@sort@DOM_LIST@op)]):
prog::test(bla::list(8),
           [[4, 4], [2, 2, 2, 2]]):
Error: Unknown slot "bla::list" [slot];
during evaluation of 'prog::test'
prog::test(bla::list(12),
           [[6, 6], [4, 4, 2, 2], [2, 2, 2, 2, 2, 2]]):
Error: Unknown slot "bla::list" [slot];
during evaluation of 'prog::test'

// partitions with even numbers of even parts, and at least m odd numbers
// of odd parts, pretty printed
m := 2:
bla := combinat::decomposableObjects

                                    CS
                                                               Time: 0 msec
     ([P=Alias(Prod(Alias(Multiset(Alias(doubledEvenIntegers, op)), op),
		    Alias(Multiset(Alias(doubledOddIntegers,  op)), op),
		    Alias(Powerset(      oddIntegers, MinLength=m), op)),
	       revert@sort@DOM_LIST@op)]):
prog::test(bla::list(10),
           [[9, 1], [7, 3], [7, 1, 1, 1], [5, 3, 1, 1], [5, 1, 1, 1, 1, 1],
	    [3, 3, 3, 1], [3, 1, 1, 1, 1, 1, 1, 1], [5, 2, 2, 1], [3, 2, 2, 1, 1, 1]]):
Error: Unknown slot "bla::list" [slot];
during evaluation of 'prog::test'
prog::test(bla::unrank, FAIL):
Error in test 'Domain' 5: bla::unrank = FAIL [TrapError = 1118: Unknown slot "bla::unrank" [slot]]
prog::test(bla::count,  FAIL):
Error in test 'Domain' 6: bla::count = FAIL [TrapError = 1118: Unknown slot "bla::count" [slot]]

// UngradedCombinatorialClass
domain byList
    inherits Dom::BaseDomain;
    category Cat::CombinatorialClass;
    axiom Ax::systemRep;

    count := () -> 2;
    list  := () -> [1,2];
end_domain:
bla := combinat::decomposableObjects([P = Alias(Prod(Domain(byList) $ 3), DOM_LIST@op)]):
prog::test([bla::count(i) $ i=0..10], [8, 0 $ 10]):
prog::test(bla::list(0), combinat::cartesianProduct::list(byList::list() $ 3)):
prog::test([bla::list(i) $ i=1..10], [[] $ 10]):

// graded and ungraded classes. 
bla := combinat::decomposableObjects([P = Alias(Prod(Domain(byList), 
						     oddIntegers), DOM_LIST@op)]):
prog::test([bla::count(i) $ i = 0..10],
           [0, 2, 0, 2, 0, 2, 0, 2, 0, 2, 0]):
prog::test([bla::list(i) $ i = 0..10],
           [[], [[1, 1], [2, 1]], [], [[1, 3], [2, 3]], [], [[1, 5], [2, 5]], 
	    [], [[1, 7], [2, 7]], [], [[1, 9], [2, 9]], []]):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Compatibility aliases"):
//////////////////////////////////////////////////////////////////////////////
bla := CS([A=hold(Set)(Z)]):
prog::test(bla::random(4), hold(Set)(Z,Z,Z,Z)):

bla := CS([A=hold(Set)(Z)], Labelled):
prog::test(bla::random(4), hold(Set)(Z(1),Z(2),Z(3),Z(4))):
bla := CS([A=PowerSet(Z)], Labelled):
prog::test(bla::random(4), PowerSet(Z(1),Z(2),Z(3),Z(4))):

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Labelled trees"):
//////////////////////////////////////////////////////////////////////////////
bla := CS([Bla = Union(Alias(Z, op), 
		       Alias(Prod(Bla, Bla), DOM_LIST@op))], 
	  Labelled=TRUE):
prog::test(bla::list(0),
           []):
prog::test(bla::list(1),
           [1]):
prog::test(bla::list(2),
           [[1, 2], [2, 1]]):
prog::test(sort(bla::list(3)),
	   sort( // Note: the generation order seems to have changed!
           [[1, [2, 3]], [2, [1, 3]], [3, [1, 2]], [1, [3, 2]], [2, [3, 1]], [3, [2, 1]], 
	    [[1, 2], 3], [[1, 3], 2], [[2, 3], 1], [[2, 1], 3], [[3, 1], 2], [[3, 2], 1]])):
prog::test(sort(bla::list(4)),
	   sort(
           [[1, [2, [3, 4]]], [2, [1, [3, 4]]], [3, [1, [2, 4]]], [4, [1, [2, 3]]], [1, [3, [2, 4]]], 
	    [2, [3, [1, 4]]], [3, [2, [1, 4]]], [4, [2, [1, 3]]], [1, [4, [2, 3]]], [2, [4, [1, 3]]], 
	    [3, [4, [1, 2]]], [4, [3, [1, 2]]], [1, [2, [4, 3]]], [2, [1, [4, 3]]], [3, [1, [4, 2]]], 
	    [4, [1, [3, 2]]], [1, [3, [4, 2]]], [2, [3, [4, 1]]], [3, [2, [4, 1]]], [4, [2, [3, 1]]], 
	    [1, [4, [3, 2]]], [2, [4, [3, 1]]], [3, [4, [2, 1]]], [4, [3, [2, 1]]], [1, [[2, 3], 4]], 
	    [2, [[1, 3], 4]], [3, [[1, 2], 4]], [4, [[1, 2], 3]], [1, [[2, 4], 3]], [2, [[1, 4], 3]], 
	    [3, [[1, 4], 2]], [4, [[1, 3], 2]], [1, [[3, 4], 2]], [2, [[3, 4], 1]], [3, [[2, 4], 1]], 
	    [4, [[2, 3], 1]], [1, [[3, 2], 4]], [2, [[3, 1], 4]], [3, [[2, 1], 4]], [4, [[2, 1], 3]],
	    [1, [[4, 2], 3]], [2, [[4, 1], 3]], [3, [[4, 1], 2]], [4, [[3, 1], 2]], [1, [[4, 3], 2]],
	    [2, [[4, 3], 1]], [3, [[4, 2], 1]], [4, [[3, 2], 1]], [[1, [2, 3]], 4], [[1, [2, 4]], 3],
	    [[1, [3, 4]], 2], [[2, [3, 4]], 1], [[2, [1, 3]], 4], [[2, [1, 4]], 3], [[3, [1, 4]], 2],
	    [[3, [2, 4]], 1], [[3, [1, 2]], 4], [[4, [1, 2]], 3], [[4, [1, 3]], 2], [[4, [2, 3]], 1],
	    [[1, [3, 2]], 4], [[1, [4, 2]], 3], [[1, [4, 3]], 2], [[2, [4, 3]], 1], [[2, [3, 1]], 4],
	    [[2, [4, 1]], 3], [[3, [4, 1]], 2], [[3, [4, 2]], 1], [[3, [2, 1]], 4], [[4, [2, 1]], 3], 
	    [[4, [3, 1]], 2], [[4, [3, 2]], 1], [[[1, 2], 3], 4], [[[1, 2], 4], 3], [[[1, 3], 4], 2],
	    [[[2, 3], 4], 1], [[[1, 3], 2], 4], [[[1, 4], 2], 3], [[[1, 4], 3], 2], [[[2, 4], 3], 1],
	    [[[2, 3], 1], 4], [[[2, 4], 1], 3], [[[3, 4], 1], 2], [[[3, 4], 2], 1], [[[2, 1], 3], 4],
	    [[[2, 1], 4], 3], [[[3, 1], 4], 2], [[[3, 2], 4], 1], [[[3, 1], 2], 4], [[[4, 1], 2], 3],
	    [[[4, 1], 3], 2], [[[4, 2], 3], 1], [[[3, 2], 1], 4], [[[4, 2], 1], 3], [[[4, 3], 1], 2],
	    [[[4, 3], 2], 1], [[1, 2], [3, 4]], [[1, 3], [2, 4]], [[1, 4], [2, 3]], [[2, 3], [1, 4]],
	    [[2, 4], [1, 3]], [[3, 4], [1, 2]], [[1, 2], [4, 3]], [[1, 3], [4, 2]], [[1, 4], [3, 2]],
	    [[2, 3], [4, 1]], [[2, 4], [3, 1]], [[3, 4], [2, 1]], [[2, 1], [3, 4]], [[3, 1], [2, 4]],
	    [[4, 1], [2, 3]], [[3, 2], [1, 4]], [[4, 2], [1, 3]], [[4, 3], [1, 2]], [[2, 1], [4, 3]],
	    [[3, 1], [4, 2]], [[4, 1], [3, 2]], [[3, 2], [4, 1]], [[4, 2], [3, 1]], [[4, 3], [2, 1]]])):
checkCoherence(bla, 5, CheckC=FALSE): // C not implemented for labelled

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Various Labelled Objects"):
//////////////////////////////////////////////////////////////////////////////

// Here is a list of labelled objects
///////////////////////////////////////////////////////////////////////////
// this is the smallest example to understand for unrank with Theta <-> Int.
es := combinat::decomposableObjects([Bla = MultX(Prod(DivX(Prod(Z, Z)), Z))], Labelled=TRUE):
se := combinat::decomposableObjects([Bla = Set(Z)], Labelled=TRUE):
cy := combinat::decomposableObjects([Bla = Cycle(Z)], Labelled=TRUE):
cypa := combinat::decomposableObjects([Bla = Cycle(Prod(Z,Z))], Labelled=TRUE):
sepa := combinat::decomposableObjects([Bla = Set(Prod(Z,Z))], Labelled=TRUE):
sese :=  combinat::decomposableObjects([Bla = Set(Set(Z))], Labelled=TRUE):
secy :=  combinat::decomposableObjects([Bla = Set(Cycle(Z))], Labelled=TRUE):
//cycy :=  combinat::decomposableObjects([Bla = Cycle(Cycle(Z))], Labelled=TRUE):
//cyse :=  combinat::decomposableObjects([Bla = Cycle(Set(Z))], Labelled=TRUE):
fgraph := combinat::decomposableObjects([Bla = Alias(Set(Cycle(A)), DOM_LIST@op), 
					 A=Alias(Prod(Z,Alias(Set(A), 
							      z -> if z=Set() then null() else DOM_LIST(op(z)) end_if)
						     ), DOM_LIST@op)], Labelled=TRUE):
// These three are good for testing the case valuationC=degreeC which does not
// happen is the previous ones.
// Exercise: Guess what is the result ?
eq := combinat::decomposableObjects([Bla = Union(Z, 
						 MultX(Prod(Bla, DivX(Z))),
						 MultX(Prod(DivX(Z), Bla)))],
				    Labelled=TRUE):
// Hint: the same but more readable.
eqr := combinat::decomposableObjects([Blo = Alias(Bla, DOM_LIST),
				      Bla = Alias(Union(A, 
							MultX(Prod(Bla, DivX(A))),
							MultX(Prod(DivX(A), Bla))), op),
				      A = Alias(Z, op)],
				     Labelled=TRUE):				     
// This one is not so different and also funny. Guess what is the result ?
ri := combinat::decomposableObjects([Bla = Union(Z, 
						 MultX(Prod(DivX(Z), Bla)),
						 MultX(Prod(Bla, DivX(Z))),
						 MultX(Prod(Bla, DivX(Z), Bla)))],
				    Labelled=TRUE):
// Another funny one, very close to the preceding.
dt := combinat::decomposableObjects([Bla = Union(Epsilon, MultX(Prod(DivX(Z), Bla, Bla)))],
				    Labelled=TRUE):
// Yet Another pretty one. Hint: try the two entries Alt and Tan.
// Guess what is the exponential generating series.
tt := combinat::decomposableObjects([Tan  = Alias(Blbb, DOM_LIST@op),
				     Alt  = Alias(Union(Blhh, Blbh, Blhb, Blbb), DOM_LIST@op),
				     Blhh = Alias(Union(A, MultX(Prod(Blhh, DivX(A), Blhh))), op), 
				     Blbh = Alias(Union(MultX(Prod(DivX(A), Blhh)),
							MultX(Prod(Blbh, DivX(A), Blhh))), op),
				     Blhb = Alias(Union(MultX(Prod(Blhh, DivX(A))),
							MultX(Prod(Blhh, DivX(A), Blhb))), op),
				     Blbb = Alias(Union(A,
							MultX(Prod(Blbh, DivX(A))),
							MultX(Prod(DivX(A), Blhb)),
							MultX(Prod(Blbh, DivX(A), Blhb))), op),
				     A    = Alias(Z, op)],
				    Labelled=TRUE):

listObj := [hold(es), hold(se), hold(cy), hold(cypa), hold(sepa), hold(sese), hold(secy), //hold(cycy), hold(cyse), 
 hold(fgraph), hold(dt), hold(eq), hold(eqr), hold(ri), hold(dt), hold(tt)]:

for obj in listObj do 
    prog::testfunc("Various Labelled Objects : ".expr2text(obj)):
    checkCoherence(eval(obj), 10, CheckC=FALSE): // C not implemented for labelled
end_for:

//////////////////////////////////////////////////////////////////////////////
prog::testfunc("Weighted enumeration"):
//////////////////////////////////////////////////////////////////////////////

// Weighted epsilons
bla := combinat::decomposableObjects([A=Epsilon(A, 0.0)]):
prog::test([bla::count(i) $ i = 0..5],
           [0.0, 0, 0, 0, 0, 0]):
bla := combinat::decomposableObjects([A=Epsilon(A, 0.0, 0)]):
prog::test([bla::count(i) $ i = 0..5],
           [0.0, 0, 0, 0, 0, 0]):

// Weighted atoms
bla := combinat::decomposableObjects([A=Atom(A, 0.0)]):
prog::test([bla::count(i) $ i = 0..5],
           [0, 0.0, 0, 0, 0, 0]):
bla := combinat::decomposableObjects([A=Atom(A, 0.0, 0)]):
prog::test([bla::count(i) $ i = 0..5],
           [0, 0.0, 0, 0, 0, 0]):


// counting ordered trees by number of internal nodes
S:=combinat::decomposableObjects([T = Union(Atom(Leaf,1),
					    Prod(Atom(Node,q), Sequence(T, MinLength=1)))],
				 CoeffRing = Dom::ExpressionField()):
prog::test([expand(S::count(i)) $ i = 0..5],
           [0, 1, q, q + q^2, q + 3*q^2 + q^3, q + 6*q^2 + 6*q^3 + q^4]):

// Approximate counting of complete binary trees
S:=combinat::decomposableObjects([T = Union(Atom(Leaf,1.0), Prod(Atom(Node,1.0), T, T))],
				 Products=Naive):
prog::test([expand(S::count(i)) $ i = 0..10],
           [0, 1.0, 0, 1.0, 0, 2.0, 0, 5.0, 0, 14.0, 0]):

// Approximate counting of complete binary trees
S:=combinat::decomposableObjects([T = Union(Atom(Leaf), Prod(Atom(Node), T, T))], Float):
prog::test([expand(S::count(i)) $ i = 0..10],
           [0, 1.0, 0, 1.0, 0, 2.0, 0, 5.0, 0, 14.0, 0]):

/* TODO: check that all the tests below are already done

read("cs.mu"):
prog::testinit("cs"):

prog::testfunc("cs::count and draw for Elementary Structure"):
spec:={A=Epsilon,B=Atom,C=Union(A,B)}:
cs::compile(spec):
prog::test([cs::count([A, spec], size=i) $ i=0..5 ], [1,0,0,0,0,0]):
prog::test([cs::draw( [C, spec], size=0)], [Epsilon]):
prog::test([cs::count([B, spec], size=i) $ i=0..5 ], [0,1,0,0,0,0]): 
prog::test([cs::draw( [C, spec], size=1)], [B]):
prog::test([cs::count([C, spec], size=i) $ i=0..5 ], [1,1,0,0,0,0]):

prog::testfunc("cs::count and draw for binary trees"):
specBinaryTrees:={B=Union(Z,Prod(B,B))}:
cs::compile(specBinaryTrees):
prog::test([cs::count([B, specBinaryTrees], size=i) $ i=0..10 ],
	   [0,1,1,2,5,14,42,132,429,1430,4862]):
prog::test([cs::draw([B,specBinaryTrees],size=i)$i=1..2],[Z,Prod(Z, Z)]):

prog::testfunc("cs::count and draw for Motzkin Trees"):
specMotzkinTrees:={M=Union(Z,Prod(Z,M),Prod(Z,M,M))}:
cs::compile(specMotzkinTrees):
prog::test([cs::count([M, specMotzkinTrees], size=i) $ i=0..10 ], 
	   [0,1,1,2,4,9,21,51,127,323,835]):
prog::test([cs::draw([M,specMotzkinTrees],size=2)],[Prod(Z, Z)]):

prog::testfunc("cs::count and draw for Shroeder Trees"):
specShroederTrees:={S=Union(Z,Prod(S,U)), U=Union(Prod(Z,S),Prod(S,U))}:
cs::compile(specShroederTrees):
prog::test([cs::count([S, specShroederTrees], size=i) $ i=0..10 ], 
	   [0,1,0,1,1,3,6,15,36,91,232]):
prog::test([cs::draw([S,specShroederTrees],size=3)],[Prod(Z, Prod(Z, Z))]):

prog::testfunc("cs::count and draw for Sequence"):
specSequence1 := {S=Sequence(Z),Z=Union(A,B),A=Atom,B=Atom}:
cs::compile(specSequence1):
prog::test([cs::count([S, specSequence1], size=i) $ i=0..10 ], 
	   [1,2,4,8,16,32,64,128,256,512,1024]):
SEED:=3:
prog::test([cs::draw([S,specSequence1],size=3)],
	   [Sequence(A, A, A)]):

specSequence2 := {S=Sequence(Z, card >=3), Z=Union(A,B), A=Atom, B=Atom}:
cs::compile(specSequence2):
prog::test([cs::count([S, specSequence2], size=i) $ i=0..10 ], 
	   [0, 0, 0, 8, 16, 32, 64, 128, 256, 512, 1024]):
SEED:=1:
prog::test([cs::draw([S,specSequence2],size=3)],
	   [Sequence(B, A, B)]):

specSequence3 := {S=Sequence(Z, card =3), Z=Union(A,B), A=Atom, B=Atom}:
cs::compile(specSequence3):
prog::test([cs::count([S, specSequence3], size=i) $ i=0..10 ], 
	   [0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0]):
SEED:=2:
prog::test([cs::draw([S,specSequence3],size=3)],
	   [Sequence(A, A, A)]):

specSequence4 := {S=Sequence(Z, card <=3), Z=Union(A,B), A=Atom, B=Atom}:
cs::compile(specSequence4):
prog::test([cs::count([S, specSequence4], size=i) $ i=0..10 ], 
	   [1, 2, 4, 8, 0, 0, 0, 0, 0, 0, 0]):
SEED:=2:
prog::test([cs::draw([S,specSequence4],size=3)],
	   [Sequence(A, A, B)]):

prog::testfunc("cs::count and draw for General trees"):
specGeneral:={T=Prod(Z,Sequence(T))}:
cs::compile(specGeneral):
prog::test([cs::count([T,specGeneral],size=i)$i=0..10],
	   [0, 1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]):
prog::test([cs::draw([T,specGeneral],size=2)],
	   [Prod(Z, Sequence(Prod(Z, Epsilon)))]):

prog::testfunc("cs::count for Theta"):
specTheta1 := {S=Theta(Z)}:
cs::compile(specTheta1):
prog::test([cs::count([S, specTheta1], size=i) $ i=0..10 ], 
	   [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

prog::testfunc("cs::count for Int"):
specInt1 := {S=Int(Z)}:
cs::compile(specInt1):
prog::test([cs::count([S, specInt1], size=i) $ i=0..10 ], 
	   [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0]):

prog::testfunc("cs::count for Set"): // Set means Multiset ?
specSet1 := {S=Set(Z)}:
cs::compile(specSet1):
prog::test([cs::count([S, specSet1], size=i) $ i=0..10 ],
	   [binomial(i+0,0) $i=0..10]):

specSet2 := {S=Set(Z), Z=Union(A,B), A=Atom, B=Atom}:
cs::compile(specSet2):
prog::test([cs::count([S, specSet2], size=i) $ i=0..10 ], 
	   [binomial(i+1,1) $i=0..10]):

specSet3 := {S=Set(Z), Z=Union(A,B,C), A=Atom, B=Atom, C=Atom}:
cs::compile(specSet3):
prog::test([cs::count([S, specSet3], size=i) $ i=0..10 ], 
	   [binomial(i+2,2) $i=0..10]):

prog::testfunc("cs::count for partitions"):
specPartitions1:={P=Set(N),N=Sequence(Z,card>=1)}:
cs::compile(specPartitions1):
prog::test([cs::count([P, specPartitions1], size=i) $ i=0..10 ], 
	   [1,1,2,3,5,7,11,15,22,30,42]):
entier:=()->1:entier(0):=0:
specPartitions2:={P=Set(N),N=Predefined(entier)}:
prog::test([cs::count([P, specPartitions2], size=i) $ i=0..10 ], 
	   [1,1,2,3,5,7,11,15,22,30,42]):

isodd:=x->x mod 2:
specPartitions3:={P=Set(N),N=Predefined(isodd)}:
cs::compile(specPartitions3):
prog::test([cs::count([P,specPartitions3],size=i)$i=0..10],
	   [1, 1, 1, 2, 2, 3, 4, 5, 6, 8, 10]):

prog::testfunc("cs::speciftoalgeq,algeqtodiffeq and diffeqtorec for Motzkin trees and words"):
specMotzkinWords   := {M=Union(V,Prod(Z,M),Prod(Z,Z,M,M)), Z=Atom, V=Epsilon}:
specNFMotzkinWords := cs::standardForm1(specMotzkinWords, unlabelled):
prog::test([cs::speciftoalgeq(specMotzkinWords,M)],
	   [M*z-M+M^2*z^2+1]):
specNFMotzkinTrees := cs::standardForm1(specMotzkinTrees, unlabelled):
eq:=cs::speciftoalgeq(specNFMotzkinTrees,M):
prog::test([eq],
	   [z-M+M*z+M^2*z]):
deq:=cs::algeqtodiffeq(eq,M(z)):
prog::test([deq],
	   [2*z-M(z)+z*M(z)-z*D(M)(z)+2*z^2*D(M)(z)+3*z^3*D(M)(z)]):
prog::test([cs::diffeqtorec(deq, M(z), v(n))],
	   [v(n-1)-v(n)-n*v(n)+2*(n-1)*v(n-1)+3*(n-2)*v(n-2)]):

prog::testfunc("cs::speciftoalgeq diffeqtorec and algeqtodiffeq for Red-Black trees"):
specRedBlackTree   := {R=Union(Z,Prod(Z,B,B)),B=Union(Z,Prod(Z,R,R,R))}:
specNFRedBlackTree := cs::standardForm1(specRedBlackTree,unlabelled):
prog::test([cs::speciftoalgeq(specNFRedBlackTree,B)],
	   [B*(-1) + z + z^4 + B^2*z^4*3 + B^4*z^4*3 + B^6*z^4]):
prog::test([cs::speciftoalgeq(specNFRedBlackTree,R)],
	   [R*(-1) + z + z^3 + R^3*z^3*2 + R^6*z^3]):

prog::testfunc("other examples of use of diff,algeq"):
prog::test([cs::diffeqtorec(y(z)-a*D(y)(z), y(z), v(n))],
	   [v(n)-a*(n+1)*v(n+1)]):
deq:=cs::algeqtodiffeq(y - z - y^2,y(z)):
prog::test([deq],
	   [4*z*D(y)(z)-2*y(z)-D(y)(z)+1]):
prog::test([cs::diffeqtorec(deq, y(z), v(n))],
	   [4*n*v(n)-2*v(n)-(n+1)*v(n+1)]):
deq:=cs::algeqtodiffeq(y - z - z*y - z*y^3,y(z)):
prog::test([deq],
	   [y(z)*2 + z*y(z)*(-2) + z*D(y)(z)*(-2) + z^2*D(y)(z)*12 +
	    z^3*D(y)(z)*(-18) + z^4*D(y)(z)*62 + z^2*D(D(y))(z)*(-4) + 
	    z^3*D(D(y))(z)*12 + z^4*D(D(y))(z)*(-12) + z^5*D(D(y))(z)*31]):
prog::test([cs::diffeqtorec(deq, y(z), v(n))],
	   [v(n)*2 + n*v(n)*(-2) + v(n - 1)*(-2) + n*v(n)*(n - 1)*(-4) +
	    (n - 1)*v(n - 1)*12 + (n - 2)*v(n - 2)*(-18) + 
	    (n - 3)*v(n - 3)*62 + (n - 1)*(n - 2)*v(n - 1)*12 +
	    (n - 2)*(n - 3)*v(n - 2)*(-12) + 
	    (n - 3)*(n - 4)*v(n - 3)*31]):

prog::testexit():
*/

//////////////////////////////////////////////////////////////////////////////
// Global checking of the package

prog::testfunc(combinat::decomposableObjects):

prog::check(BinaryTrees, 3):

prog::check(combinat::decomposableObjectsTools::gmp, 3):
prog::testexit():
Info: 1090 tests, 2 errors, runtime factor  96.6  (expected  57.5)
Warning: expected time and test time differ  68.0 % [prog::testexit]
Info: All tests 1090 [prog::testexit]
