
/* Functions that aid in testing */

/* Round to specified number of digits */
RoundTo(x_IsNumber, prec_IsPositiveInteger) <-- N(Round(x*10^prec)*10^(-prec));

/* Logging functions */
// curline:=0;

/*
Function("StartTests",{})
[
  curline:=0;
];
*/
/*
Function("NextTest",{aLeft})
[
// curline++;
WriteString("
Test suite for ":aLeft:" : "
           );
  NewLine();
];
*/
/*
Function("Testing",{aLeft})
[
 WriteString(aLeft); NewLine();
];
*/

Function("KnownFailure",{expr})
[
  Local(rfail);
  Echo({"Known failure: ", expr});
  Set(rfail,Eval(expr));
  If(rfail,Echo({"Failure resolved!"}));
];
HoldArg("KnownFailure",expr);

Function("Verify",{aLeft,aRight})
[
	Local(result);
	result := Eval(aLeft);	// to save time
  If (Not(Equals(result,aRight)),
    [
      WriteString("******************");
      NewLine();
      Write(aLeft);
      WriteString(" evaluates to ");
	  Write(result);
	  WriteString(" which differs from ");
      Write(aRight);
      NewLine();
      WriteString("******************");
      NewLine();
      False;
    ],
    True
  );
];
HoldArg("Verify",aLeft);
UnFence("Verify",2);
/*
HoldArg("Verify",aRight);
*/

Function("LogicVerify",{aLeft,aRight})
[
  If(aLeft != aRight,
    Verify(CanProve(aLeft => aRight),True)
  );
];


f1(x,n,m):=(x^n-1)*(x^m-1);
f2(x,n,m):=x^(n+m)-(x^n)-(x^m)+1;

VerifyArithmetic(x,n,m):=
[
/*
  WriteString("Checking arithmetic with ");
  Write(x);Space();
  Write(n);Space();
  Write(m);NewLine();
*/
  Verify(f1(x,n,m),f2(x,n,m));
];
RandVerifyArithmetic(_n)<--
[
 While(n>0)
 [
   n--;
   VerifyArithmetic(MathFloor(300*Random()),MathFloor(80*Random()),MathFloor(90*Random()));
 ];
];

VerifyDiv(_u,_v) <--
[
  Local(q,r);
  q:=Div(u,v);
  r:=Rem(u,v);

  Verify(Expand(u),Expand(q*v+r));
];

10 # EchoInternal(string_IsString) <--
[
  WriteString(string);
];

20 # EchoInternal(_item) <--
[
  Write(item);Space();
];

10 # Echo(list_IsList)<--
[
  ForEach(item,list) EchoInternal(item);
  NewLine();
];
20 # Echo(_item)<--
[
  EchoInternal(item);
  NewLine();
];


Function("BenchCall",{expr}) 
[
  Echo({"In&gt ",expr});
  WriteString("<font color=ff0000>");
  Eval(expr);
  WriteString("</font>");
  True;
];
HoldArg("BenchCall",expr);

Function("BenchShow",{expr}) 
[
  Echo({"In&gt ",expr});
  WriteString("<font color=ff0000> ");
  Echo({"Out&gt ",Eval(expr),"</font>"});
  True;
];
HoldArg("BenchShow",expr);




/* Testing Yacas functionality by checking expressions against correct
   answer.
   Use with algebraic expressions only, since we need Simplify() for that to work.
 */

Function ("TestYacas", {expr, ans})
[
	Local(diff);
	diff := Simplify(Eval(expr)-Eval(ans));
		If (Simplify(diff)=0, True,
			[
			  WriteString("******************");
			  NewLine();
			  Write(expr);
			  WriteString(" evaluates to ");
			  NewLine();
			  Write(Eval(expr));
			  NewLine();
			  WriteString(" which differs from ");
			  NewLine();
			  Write(Eval(ans));
			  NewLine();
			  WriteString(" by ");
			  NewLine();
			  Write(diff);
			  NewLine();
			  WriteString("******************");
			  NewLine();
			  False;
			 ]
			);
];

HoldArg("TestYacas", expr);
HoldArg("TestYacas", ans);







