:i	static char memtest_sccs_id[] = "%W% %G%";
#define VBS_SLOW 1
#define VBS_FAST 0
#include "mem.h"
/*
* Here's a test program for the above memory-allocation routines.
*/
#define JUNK1 10
#define JUNK2 20
#define JUNK3 30
#define MAXCHUNK 15
/*
* Here are some global arrays to free.  The ints are here so that  the
* char  arrays  won't  be  adjacent  on  any machine (though this does
* assume that the compiler will lay things out in the  order  that  we
* declare them here).
*/
char junk1[JUNK1];
int  junki1;	/* Filler */
char junk2[JUNK2];
int  junki2;	/* Filler */
char junk3[JUNK3];
int  junki3;	/* Filler */
char junk123[JUNK1+JUNK2+JUNK3];
char* ch[MAXCHUNK+1];
int  errs = 0;	/* Count the failures */

main(ac,av,ev)
	int   ac;
	char**av;
	char**ev;
{	int   a, i;
	char *cp0, *cp1, *cp2, *cp3, *cp4;
	char *d;
	Ulong u;

:9	printf("memtest ac=%d av=%08X...\n",ac,av); fflush(stdout);
:9	u = MemAlign((Ulong)15,(Ulong)8); printf("MemAlign(15,8)=%lu.\n",a);
:9	u = MemAlign((Ulong)99,(Ulong)8); printf("MemAlign(99,8)=%lu.\n",a);

:9	printf("memtest Vinit ac=%d av=%08X...\n",ac,av); fflush(stdout);
:1	ac = Vinit(ac,av);
:9	printf("memtest Vinit done.\n"); fflush(stdout);
/*
* Test the basic mem tools:
*/
:1	V2 "=============================" D;
:1	V4 "Alignments:" D;
:1	V4 "  MEMU=%d.",MEMU D;
:1	V4 "  MEMALIGN( 0,MEMU)= %d.",MEMALIGN( 0,MEMU) D;
:1	V4 "  MEMALIGN( 5,MEMU)= %d.",MEMALIGN( 5,MEMU) D;
:1	V4 "  MEMALIGN(15,MEMU)= %d.",MEMALIGN(15,MEMU) D;
:1	V4 "  MEMALIGN(16,MEMU)= %d.",MEMALIGN(16,MEMU) D;
:1	V4 "  MEMALIGN(17,MEMU)= %d.",MEMALIGN(17,MEMU) D;
:1	V4 "  MEMALIGN(99,MEMU)= %d.",MEMALIGN(99,MEMU) D;

/*
* Test the ability to free a random chunk of memory:
*/
:1	V2 "=============================" D;
:1	V2 "The following 3 chunks should be freed without problems:" D;
	if (!FreeChunk(junk2,JUNK2,"junk2")) errs++;
	if (!FreeChunk(junk1,JUNK1,"junk1")) errs++;
	if (!FreeChunk(junk3,JUNK3,"junk3")) errs++;
/*
* Test the diagnostics for attempts to free overlapping chunks:
*/
:1	V2 "=============================" D;
:1	V2 "The following 5 chunks should be rejected due to overlaps:" D;
	if (FreeChunk(junk2,JUNK2,"junk2")) errs++;
	if (FreeChunk(junk2+6,JUNK2,"junk2+6")) errs++;
	if (FreeChunk(junk2-4,JUNK2,"junk2-4")) errs++;
	if (FreeChunk(junk2+JUNK2-1,1,"junk2+JUNK2-1")) errs++;
	if (FreeChunk(junk2-1,2,"junk2-1")) errs++;
/*
* The junk123[] block is freed in three pieces, which should end up
* merged into one big free block.
*/
:1	V2 "=============================" D;
:1	V2 "The following 3 chunks should be merged into one big JUNK1 chunk:" D;
	if (!FreeChunk(junk123+JUNK1,JUNK2,"junk123-2")) errs++;
	if (!FreeChunk(junk123,JUNK1,"junk123-1")) errs++;
	if (!FreeChunk(junk123+JUNK1+JUNK2,JUNK3,"junk123-3")) errs++;
/*
* Now we try to get this chunk in pieces, using SizChunk:
*/
:1	V2 "=============================" D;
:1	V2 "The following Get and Siz should get the last chunk:" D;
	cp0 = GetChunk(JUNK1,d="JUNK1");
:1	if (Vlvl>1) DmpChunk(HdrChunk(cp0,d),d);
	cp0 = SizChunk(cp0,JUNK1,JUNK1+JUNK2,d="JUNK1+2");
:1	if (Vlvl>1) DmpChunk(HdrChunk(cp0,d),d);
	cp0 = SizChunk(cp0,0,JUNK1+JUNK2+JUNK3,d="JUNK1+2+3");
:1	if (Vlvl>1) DmpChunk(HdrChunk(cp0,d),d);
	cp0 = SizChunk(cp0,0,JUNK1+JUNK2+JUNK3+100,d="JUNK1+2+3+100");
:1	if (Vlvl>1) DmpChunk(HdrChunk(cp0,d),d);
/*
*/
:1	V2 "=============================" D;
:1	V2 "Try to get a big chunk:" D;
	cp1 = GetChunk(2*(JUNK1+JUNK2+JUNK3),"big chunk");
/*
*/
:1	V2 "=============================" D;
:1	V2 "We should be able to get some of these chunks now." D;
	cp2 = GetAChunk(JUNK2,1,"JUNK2");
	cp3 = GetAChunk(JUNK3,2,"JUNK3");
	cp4 = GetChunk(JUNK1+JUNK2,"JUNK1+JUNK2");

	for (i = 0; i <= MAXCHUNK; i++) {
:2		if (i == 0)
:2			V2 "We should get an error for %d bytes:",i D;
		if (!(ch[i] = GetAChunk(i, i, "ch")))
			if (i) errs++;
:2		V2 "Got ch[%d] = %08X %d bytes.",i,ch[i],i D;
		if (i && !ch[i]) errs++;
		if (i >= 5)
			if (!IRelChunk(ch[i-4],"ch"))
				errs++;
	}
	exit(errs);
}
