/*****************************************
 * TESTBLK - Test Module block Functions *
 *****************************************/

//todo: Test blknxt() and blkprv()

//Specify System Header using -H option
#include <stddef.h02>
#include <stdlib.h02>
#include <intlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <test.h02>
#include <memory.h02>
#include <block.h02>

char c, i, n, r, z;
int chkint, savptr;
char number[5];
const char numbrs = "zero one  two  threefour five six  seveneightnine ten";
const char sorted = {8,5,4,9,1,7,6,3,2,0};
const char name1 = "Dick", name2 = "Jane";
char block[255]; //Array to Use as Block
char temp[127];
char seglo[9],seghi[9];
char aa,xx,yy,zz;
int yx;

struct ssgmt {char key[6]; char flag; char value;};
struct ssgmt sgmt;
struct ssgmt seg1;
struct ssgmt seg2;
struct ssgmt tseg;

char cmpptr(chkint) {putadr(blkptr); return cmpadr(blkptr);}
void chkadr(yx) {putadr(yx);cmpadr(yx);psflln(<yx & >yx);}
char equals(aa,zz) {A = (aa == zz) ? #TRUE : #FALSE;}
char cmpadr(yx) {xx = equals(<chkint, <yx); yy = equals(>chkint, <yx); return xx & yy;}
void clrseg() {setdst(); memset(0, @sgmt);}
char chkmem() {A = equals(memcmp(), 0);}  
void prtadr() {putwrd(); putspc();}
void prbadr() {putadr(blkptr); prtadr(blkend); putwrd(blkptr);newlin();}

main:

puts("blkbgn = &$4321; "); blkbgn = &$4321;
puts("blkend = &$4567; "); blkend = &$4567; 
putln("blklen = @sgmt;"); blklen = @sgmt;

puts("blkrst();       ");
blkrst();
chkint = &$4321; chkadr(blkptr);

puts("blknxt();       ");
blknxt();
chkint = &$432A; chkadr(blkptr);

puts("blkprv();       ");
blkprv();
chkint = &$4321; chkadr(blkptr);

puts("blkset('@');    ");
blkset('@'); r=cmpptr(&$4321);
while (blkget(1,&sgmt)) {if (sgmt[0]<>'@') r=#FALSE;} psflln(r);

newlin();//if (anykey()==#ESCKEY) goto exit;

setdst(&seg1.key); strcpy(&name1);seg1.flag=#TRUE; seg1.value = 1;
puts("seg1=");prtseg(&seg1);
setdst(&seg2.key); strcpy(&name2);seg2.flag=#FALSE;seg2.value = 2; 
puts("seg2=");prtseg(&seg2);
setdst(&sgmt);memcpy(@seg2,&seg2);
puts("sgmt=");prtseg(&sgmt);
newlin();

puts("blkput(@seg1,&seg1); ");
blkrst(); blkput(@seg1,&seg1); r=cmpptr(&$432A);
memdst(&$4321); r = chkmem(@seg1,&seg1) & r; 
psflln(r);
puts(" _blk=");prtseg(&$4321);
newlin();

puts("blkswp(@sgmt,&sgmt); ");
blkrst(); blkswp(@sgmt,&sgmt); r = cmpptr(&$4321);
memdst(&$4321); r = chkmem(@seg2,&seg2) & r; 
memdst(&sgmt);  r = chkmem(@seg1,&seg1) & r; 
psflln(r);
puts(" _blk=");prtseg(&$4321);
puts(" sgmt=");prtseg(&sgmt);

anykey();

puts("blkput(@sgmt, &sgmt);\n"); blkset(0); blkrst(); r = #TRUE;
for (i=0; i<10; i++) {
  printf(i,"%d: "); nsgmt(i,&sgmt); savptr=blkptr;
  blkput(@sgmt,&sgmt); seglo[i]=<savptr; seghi[i]=>savptr;
  putadr(savptr); puts(" _blk="); prtseg(savptr);
  setdst(savptr); if (memcmp(@sgmt,&sgmt)) r = #FALSE;
}
puts("blkput:"); psflln(r); newlin();

puts("blkget(@temp, &temp);\n"); blkrst(); r = #TRUE;
for (i=0; i<10; i++) {
  printf(i,"%d: "); nsgmt(i,&sgmt); memclr(@temp, &temp); 
  putadr(blkptr); r = blkget(@temp, &temp);
  setdst(&temp); if (memcmp(@sgmt, &sgmt)) r = #FALSE;
  puts(" temp="); prtseg(&temp);
}
puts("blkget:"); psflln(r); anykey();

puts("blkmem(@sgmt, &sgmt);\n"); r = #TRUE;
for (i=9; i:+; i--) {
  printf(i,"%d: "); nsgmt(i,&sgmt);
  blkmem(@sgmt,&sgmt);  
  if (<dstptr<>seglo[i]) r = #FALSE; 
  if (>dstptr<>seghi[i]) r = #FALSE; 
  putadr(dstptr); puts(" _blk="); prtseg(dstptr);
}
puts("blkmem:"); psflln(r); newlin();

puts("blkstr(@sgmt, &sgmnt);\n"); r = #TRUE;
for (i=0; i<10; i++) {
  printf(i,"%d: "); nsgmt(i,&sgmt); memclr(@temp, &temp);
  setdst(&temp); blkstr(&sgmt.key);
  if (<srcptr<>seglo[i]) r = #FALSE; 
  if (>srcptr<>seghi[i]) r = #FALSE; 
  putadr(srcptr); puts(" temp="); prtseg(&temp);
  if (memcmp(@sgmt, &sgmt)) r = #FALSE;
  i++; if (i=9) i=0; //evens then odds
}
puts("blkstr:"); psflln(r); anykey();

puts("blksrt(&temp);\n"); r = #TRUE; blksrt(&temp);blkrst();
for (i=0; i<10; i++) {
  printf(i,"%d: "); nsgmt(sorted[i],&sgmt); memclr(@temp, &temp); 
  putadr(blkptr); r = blkget(@temp, &temp);
  setdst(&temp); if (memcmp(@sgmt, &sgmt)) r = #FALSE;
  puts(" temp="); prtseg(&temp);
}
puts("blksrt:"); psflln(r); newlin();

putln("Tests complete.");

goto exit;

void prtseg(aa,yy,xx) {
  setdst(&tseg); memcpy(@tseg,yy,xx);
  setdst(&tseg.key); printf("key=\"%s\",");
  puts("flag=");if (tseg.flag) puts("TRUE"); else puts("FALSE");
  printf(tseg.value,",value=%d\n");
}


//Copy Textual Representation of n to temp
void numstr(n,yy,xx) {
  strdst(&temp); strcut(mult(n,5),&numbrs); //Copy Representation
  temp[5] = 0; //Cut off remainder of String
  z=strchr(' ', &temp); if (z:+) temp[z]=0; //and terminate at space
  setdst(.,yy,xx); strcpy(&temp);
}

//Build String Segment in Temp
void nsgmt(n,yx) {
  memclr(@tseg,&tseg);
  numstr(n,&tseg.key); 
  tseg.flag = trufls(n);
  tseg.value = n;
  setdst(yx); memcpy(@tseg,&tseg);  
}

//Print Addresses
void pradrs() {
  puts(" SRC: $");putwrd(srcptr);
  puts(" DST: $");putwrd(dstptr);
  puts(" BLK: $");putwrd(blkptr);
  newlin();
}


