/*LoadScriptOnce("texform");*/

NextTest("TeXForm()...");

/* it worketh no more...
Testing("Realistic example");
f:=Exp(I*lambda*eta)*w(T*(k+k1+lambda));
g:=Simplify(Subst(lambda,0) f+(k+k1)*(Differentiate(lambda)f)+k*k1*Differentiate(lambda)Differentiate(lambda)f );
Verify(TeXForm(g), ...);
*/

Verify(
TeXForm(Hold(Cos(A-B)*Sqrt(C+D)-(a+b)*c^d+2*I+Complex(a+b,a-b)/Complex(0,1)))
,"$\\cos ( A - B)  \\cdot \\sqrt{C + D} - ( a + b)  \\cdot c ^{d} + 2 \\cdot \\imath  + \\frac{a + b + \\imath  \\cdot ( a - b) }{\\imath } $"
);

Verify(
TeXForm(Hold(Exp(A*B)/C/D/(E+F)*G-(-(a+b)-(c-d))-b^(c^d) -(a^b)^c))
,"$\\frac{\\frac{\\frac{\\exp ( A \\cdot B) }{C} }{D} }{E + F}  \\cdot G - (  - ( a + b)  - ( c - d) )  - b ^{c ^{d}} - ( a ^{b})  ^{c}$"
);

Verify(
TeXForm(Hold(Cos(A-B)*Sin(a)*f(b,c,d*(e+1))*Sqrt(C+D)-(g(a+b)^(c+d))^(c+d)))
,"$\\cos ( A - B)  \\cdot \\sin a \\cdot f( b, c, d \\cdot ( e + 1) )  \\cdot \\sqrt{C + D} - ( g( a + b)  ^{c + d})  ^{c + d}$"
);

// testing latest features: \\cdot, %, (a/b)^n, BinomialCoefficient(), BesselI, OrthoH
Verify(
TeXForm(3*2^n+Hold(x*10!) + (x/y)^2 + BinomialCoefficient(x,y) + BesselI(n,x) + Maximum(a,b) + OrthoH(n,x))
, "$3\\cdot 2 ^{n} + x\\cdot 10! + ( \\frac{x}{y} )  ^{2} + {x \\choose y} + I _{n}( x)  + \\max ( a, b)  + H _{n}( x) $"
);

/* this fails because of a bug that Differentiate(x) f(y) does not go to 0 */ /*
Verify(
TeXForm(3*Differentiate(x)f(x,y,z)*Cos(Omega)*Modulo(Sin(a)*4,5/a^b))
,"$3 ( \\frac{\\partial}{\\partial x}f( x, y, z) )  ( \\cos \\Omega )  ( 4 ( \\sin a) ) \\bmod \\frac{5}{a ^{b}} $"
);
*/

Verify(
TeXForm(Hold(Differentiate(x)f(x)))
,"$\\frac{d}{d x}f( x) $");

Verify(
TeXForm(Hold(Not (c<0) And (a+b)*c>= -d^e And (c<=0 Or b+1>0) Or a!=0 And Not (p=q)))
,"$ \\neg c < 0\\wedge ( a + b)  \\cdot c\\geq  - d ^{e}\\wedge ( c\\leq 0\\vee b + 1 > 0) \\vee a\\neq 0\\wedge  \\neg p = q$"
);


Verify(
TeXForm((Differentiate(x)f(x,y,z))*Cos(Omega)*Modulo(Sin(a)*4,5/a^b))
,"$( \\frac{\\partial}{\\partial x}f( x, y, z) )  \\cdot \\cos \\Omega  \\cdot ( 4 \\cdot \\sin a) \\bmod \\frac{5}{a ^{b}} $"
);


Verify(
TeXForm(Pi+Exp(1)-Theta-Integrate(x,x1,3/g(Pi))2*theta(x)*Exp(1/x))
,"$\\pi  + \\exp ( 1)  - \\Theta  - \\int _{x_{1}} ^{\\frac{3}{g( \\pi ) }  } 2 \\cdot \\theta ( x)  \\cdot \\exp ( \\frac{1}{x} )  dx$"
);

Verify(
TeXForm({a[3]*b[5]-c[1][2],{a,b,c,d}})
,"$( a _{3} \\cdot b _{5} - c _{( 1, 2) }, ( a, b, c, d) ) $"
);

Bodied("aa", 200);
Infix("bar", 100);

Verify(
TeXForm(aa(x,y) z + 1 bar y!)
,"$aa( x, y) z + 1\\mathrm{ bar }y!$"
);

Verify(
TeXForm(x^(1/3)+x^(1/2))
, "$\\sqrt[3]{x} + \\sqrt{x}$"
);

/*
Verify(
TeXForm()
,"" 
);
*/

Verify(
CForm(Hold(Cos(A-B)*Sin(a)*func(b,c,d*(e+Pi))*Sqrt(Abs(C)+D)-(g(a+b)^(c+d))^(c+d)))
,"cos(A - B) * sin(a) * func(b, c, d * ( e + Pi) ) * sqrt(fabs(C) + D) - pow(pow(g(a + b), c + d), c + d)" 
);

Verify(
CForm(Hold([i:=0;While(i<10)[i++; a:=a+Floor(i);];]))
, "{
  i = 0;
  while(i < 10)
    {
      ++(i);
      a = a + floor(i);
      }
    ;
    ;
  }
"
);

/* Check that we can still force numbers to be floats in stead of integers if we want to */
Verify(
CForm(Hold([i:=0.;While(i<10.)[i++; a:=a+Floor(i);];]))
, "{
  i = 0.;
  while(i < 10.)
    {
      ++(i);
      a = a + floor(i);
      }
    ;
    ;
  }
"
);


Testing("IsCFormable");
Verify(
IsCFormable(e+Pi*Cos(A-B)/3-Floor(3.14)*2)
, True
);
Verify(
IsCFormable(e+Pi*Cos(A-B)/3-Floor(3.14)*2+bad'func(x+y))
, False
);
Verify(
IsCFormable(e+Pi*Cos(A-B)/3-Floor(3.14)*2+bad'func(x+y), {bad'func})
, True
);
Verify(
IsCFormable([i:=0;While(i<10)[i++; a:=a+i;];])
, True
);
Verify(
IsCFormable([i:=0;While(i<10)[i++; a:=a+i; {};];])
, False
);
