view testlist.rhope @ 75:0083b2f7b3c7

Partially working implementation of List. Modified build scripts to allow use of other compilers. Fixed some bugs involving method implementations on different types returning different numbers of outputs. Added Fold to the 'builtins' in the comipler.
author Mike Pavone <pavone@retrodev.com>
date Tue, 06 Jul 2010 07:52:59 -0400
parents
children 004f0fc8941f
line wrap: on
line source


Make List[list, cur, num:out]
{
	If[[cur]=[num]]
	{ out <- list }
	{
		out <- Make List[[list]Append[cur], [cur]+[1], num]
	}
}

Make List Step[list, cur index, curval, num, step:out]
{
	Print["Make List Step"]
	{ Print[cur index]
	{ Print[curval]
	{
	If[[curval]=[num]]
	{ out <- list }
	{
		next <- [list]Set[cur index, curval]
		Print[[next]Length]
		{
		out <- Make List Step[next, [cur index]+[step], [curval]+[1], num, step]
		}
	}
	}}}
}

Sum[list,index,cur:out]
{
	[list]Index[index]
	{
		out <- Sum[list,[index]+[1],[cur]+[~]]
	}{
		out <- cur
	}
}

Calc Sum[num:out]
{
	If[[[[num]/[2]]*[2]] = [num]]
	{
		out <- [[num]-[1]]*[[num]/[2]]
	}{
		out <- [[[num]-[2]]*[[[num]-[1]]/[2]]]+[[num]-[1]]
	}
}

Test Size[size:out]
{
	list <- Make List[List[], 0, size]

	If[[[list]Length] != [size]]
	{
		out <- No
		Print["Length should be:"]
		{ Print[size]
		{ Print["but was:"]
		{ Print[[list]Length] }}}
	}{
		tlast <- [size]-[1]
		If[[[list]Last] != [tlast]]
		{
			out <- No
			Print["Last should be:"]
			{ Print[tlast]
			{ Print["but was:"]
			{ Print[[list]Last] }}}
		}{
			sum <- Sum[list,0,0]
			ssum <- Calc Sum[size]
			If[[sum]=[ssum]]
			{
				out <- Yes
				Print["Test succeeded for size:"]
				{ Print[size] }
			}{
				out <- No
				Print["Sum is:"]
				{ Print[sum]
				{ Print["but should be:"]
				{ Print[ssum] }}}
			}
		}
	}
}

Do Test[size:success,failure]
{
	success <- If[Test Size[size]] {}
	{ failure <- size }
}

Test Next[size,step:success,failure]
{
	list <- Make List Step[List[], 0, 0, size, step]
	
	If[[[list]Length] != [size]]
	{
		failure <- size
		Print["Length should be:"]
		{ Print[size]
		{ Print["but was:"]
		{ Print[[list]Length] }}}
	}{
		sum <- Fold[+[?], 0, list]
		ssum <- Calc Sum[size]
		If[[sum]=[ssum]]
		{
			success <- Yes
			Print["Test succeeded for size:"]
			{ Print[size] }
		}{
			failure <- size
			Print["Sum is:"]
			{ Print[sum]
			{ Print["but should be:"]
			{ Print[ssum] }}}
		}
	}
}

Test First[index:success,failure]
{
	f <- [[List[]]Set[index, 0]]First
	{
		success <- If[[~]=[index]] {}
		{ 
			Print["First returned:"]
			{ Print[f]
			{ Print["Should have returned:"]
			{ Print[index] }}}
			failure <- index
		}
	}{
		Print["First set \"none\" output on List with 1 element at index:"]
		{ Print[index] }
		failure <- index
	}
}

Main[:out]
{
	,out <- Do Test[8i32]
	{ ,out <- Do Test[16i32]
	{ ,out <- Do Test[24i32]
	{ ,out <- Do Test[32i32]
	{ 
		Print["Basic append/retrieve tests succeeded"]
		do ftest <- Yes
	}}}}

	Val[do ftest]
	{
		[List[]]First
		{
			Print["Calling First on empty list populated first output!"]
			out <- 33i32
		}{
			Test First[0i32]
			{
				,out <- Test First[7i32]
				{ ,out <- Test First[15i32]
				{ ,out <- Test First[23i32]
				{ ,out <- Test First[31i32] 
				{
					Print["Tests of First method successful"]
					,out <- Test Next[33i32, 1i32]
					{ ,out <- Test Next[5i32, 2i32]
					{ ,out <- Test Next[17i32, 3i32]
					{
						Print["Test of Next method successful"]
						out <- 0i32
					}}}
				}
				}}}
			}{
				out <- 1i32
			}
		}
	}
}