/*

Multiplication / division unit testing

*/

program MulDiv;
var name : string = "MulDiv";
@include "common.tru"
var  
	sc, co : pointer; 			// screen and colour RAM pointers

/*--------------------------------------------------------------------------------------------

	VARIABLES FOR USE IN UNIT TESTS
	
----------------------------------------------------------------------------------------------*/

	v1, v2,v3,rb : byte;
	i1, i2,ri : integer;
	p1, p2,rp : pointer;




/*--------------------------------------------------------------------------------------------

	UNIT TESTS
	
----------------------------------------------------------------------------------------------*/

// Byte mul byte
procedure ByteMulByte01();
begin
	v1 := 15; 
	v2 := 9;
	rb:=v1*v2;
	text := "01:"; if rb=135 then PASS() else FAIL();
end;

// Byte mul byte power of two
procedure ByteMulBytePow202();
begin
	v1 := 15;
	rb:=v1*4;
	text := "02:"; if rb=60 then PASS() else FAIL();
end;

procedure ByteDivBytePow203();
begin
	v1 := 100;
	rb:=v1/4;
	text := "03:"; if rb=25 then PASS() else FAIL();
end;

// Byte mul div power
procedure ByteDivByte04();
begin
	v1 := 100;
	v2 := 5;
	rb:=v1/v2;
	text := "04:"; if rb=20 then PASS() else FAIL();
end;


// Byte mul mul
procedure ByteMulMul05();
begin
	v1 := 3;
	v2 := 4;
	v3 := 5;
	rb:=(v1-1)*v2*(v3+1);
	text := "05:"; if rb=48 then PASS() else FAIL();
end;

// Byte mul mul
procedure IntByteMul06();
begin
	i1 := 1010;
	v1 := 3;
	ri := i1*v1;
	text := "06:"; 
	if (ri=3030) then PASS() else FAIL();
end;


procedure IntMulPow07();
begin
	i1 := 1010;
	v1 := 4;
	ri := i1*v1;
	text := "07:"; 
	if (ri=4040) then PASS() else FAIL();
end;


procedure IntDivByte08();
begin
	i1 := 615;
	v1 := 5;
	v2 := i1/v1;
	text := "08:"; 
	if (v2=123) then PASS() else FAIL();
end;

procedure IntDivInt09();
begin
	i1 := 60000;
	i2 := 1000;
	i1 := i1/i2;
	text := "09:"; 
	if (i1=60) then PASS() else FAIL();
end;

procedure IntDivPow210();
begin
	i1 := 60000;
	i1 := i1/4;
	text := "10:"; 
	if (i1=15000) then PASS() else FAIL();
	
end;

procedure IntDivMix11();
begin
	i1 := 60000;
	v1:=100;
	i1:=i1/v1;
	text := "11:"; 
	if (i1=600) then PASS() else FAIL();
end;

procedure IntDivMix12();
begin
	i1 := 20000;
	v1:=200;
	v1:=i1/v1;
	text := "12:"; 
	if (v1=100) then PASS() else FAIL();
end;

procedure IntMulMix13();
begin
	i1 := 200;
	v1:=100;
	i1:=i1*v1;
	text := "13:"; 
	if (i1=20000) then PASS() else FAIL();
end;

// Fails. Integer * integer
procedure IntMulInt14();
begin
	i1 := 10;
	i2 := 10;
	i1:=i1*i2;
	text := "14:"; 
	if (i1=100) then PASS() else FAIL();
end;



// Fails. Integer * byte * byte
procedure IntMulAdvancedMix15();
begin
	i1 := 10;
	v1 := 10;
	v2 := 10;
	i1:=i1*v1*v2;
	text := "15:";
	if (i1=1000) then PASS() else FAIL();
end;

procedure ByteMulAdvanced16();
begin
	v1:=2;
	v2 := 3;
	v3 := 4;
	v1:=v1*v2*(v3-1);
	text := "16:";
	if (v1=18) then PASS() else FAIL();
end;

procedure IntMulConst17();
begin
	v1:=10;
	i1:=v1*5;
	text := "17:";
	if (i1=50) then PASS() else FAIL();


end;


procedure IntCombConst18();
begin
	v1:=10;
	i1:=200 + 1000+v1*5;
	text := "18:";
	if (i1=1250) then PASS() else FAIL();
end;


procedure IntCombConst19();
begin
	v1:=10;
	i1:=(200 + 1000+v1*5)*4;
	text := "19:";
	if (i1=5000) then PASS() else FAIL();
end;

procedure IntShift20();
begin
	i1:=10;
	i1:=i1<<7;
	text := "20:";
	if (i1=1280) then PASS() else FAIL();
//	DebugValue(i1);
end;

procedure IntShift21();
begin
	i1:=1280;
	i1:=i1>>7;
	text := "21:";
	if (i1=10) then PASS() else FAIL();
end;


//--------------------------------------------------------------------------------------------
/* MAIN PROGRAM */
procedure RunTests();
begin
	// Mul and div

	ByteMulByte01();
	ByteMulBytePow202();
	ByteDivBytePow203();
	ByteDivByte04();
	ByteMulMul05();
	IntByteMul06();
	IntMulPow07();
	IntDivByte08();
	IntDivInt09();
	IntDivPow210();
	IntDivMix11();
	IntDivMix12();
	IntMulMix13(); // fails?
	IntMulInt14(); // Not implemented, fails
	IntMulAdvancedMix15(); // fails
	ByteMulAdvanced16();
	IntMulConst17();
	IntCombConst18();
	IntCombConst19();
	IntShift20();
	IntShift21();
end;

begin
	Main();
	Loop();
end.
