/********************************************
 * TESTSTKX - Test Extended Stack Functions *
 * Assumes free RAM from $7FFF to $807E     *
 ********************************************/

//use -h option on command line when compiling

#include <stddef.h02>
#include <stdlib.h02>
#include <stdio.h02>
#include <stdiox.h02>
#include <string.h02>
#include <memory.h02>
#include <stack.h02>
#include <stackx.h02>

char c,f,g,i;
char aa,xx,yy;
char zz,lo,hi;
const char r={6,7,8,9};
const char s="Test String";
char t[255];

main:

for (i=0;i<$20;i++) srclo[i]=0; //Initialize Pointers

putln("SETTING STACK START");
putln(" STKBGN(&$7FFF)");
stkbgn(&$7FFF); lo=stkslo; hi=stkshi;
setdst("stks"); cklohi(&$7FFF);

putln("SETTING STACK END");
putln(" STKEND(&$807E)\t");
stkend(&$807E); lo=stkelo; hi=stkehi;
setdst("stke"); cklohi(&$807E);

puts("Resetting Stack Pointer\t\t");
puts("stkrst()\t");
stkrst(); lo=stklo; hi=stkhi;
setdst("stk"); cklohi(0,&$7FFF);
chksiz(#FALSE, &0);

puts("Pushing Array onto Stack\t");
puts("stkpsh(@r,$r)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkpsh(@r,&r); printf(f,"bytes=%d:");
setdst(?,hi,lo); g=(memcmp(@r, &r)==0) ? #TRUE : #FALSE;
psorfl(f & g); chksiz(#TRUE, &5); chkptr(&$8004); 

puts("Pushing String onto Stack\t");
puts("stkstr(&s)\t");
aa,hi,lo=stkptr(); //Save Stack Pointer
f=stkstr(&s); printf(f,"bytes=%d:");
setdst(?,hi,lo); g=(strcmp(&s)==0) ? #TRUE : #FALSE;
psorfl(f & g); chksiz(#TRUE, &18); chkptr(&$8011);

puts("Swapping Top and Second Entry\t");
puts("stkswp()\t");
f=stkswp(); printf(f,"bytes=%d:");
g=(f==4) ? #TRUE : #FALSE;
psorfl(f & g); chksiz(#TRUE, &18); chkptr(&$8011);

puts("Popping Array off Stack\t\t");
puts("stkpop(&t)\t"); 
f=stkpop(&t); prtary(f); puts(": ");
g=(memcmp(f,&r)==0) ? #TRUE : #FALSE;
psorfl(f & g); chksiz(#TRUE, &13); chkptr(&$800C);

puts("Popping String off Stack\t");
puts("stkpop(&t)\t"); 
f=stkpop(&t); setdst(&t); printf("\"%s\":");
g=(strcmp(&s)==0) ? #TRUE : #FALSE;
psorfl(f & g); chksiz(#FALSE, &0); chkptr(&$7FFF);

putln("Tests Complete");

goto exit;

if (anykey() == #esckey) goto exit;

void chkptr(aa,yy,xx) {
  puts("\tChecking Stack Pointer\t\t\t");
  puts("address=$"); prbyte(stkhi); prbyte(stklo); putc(':');
  xx = (stklo == xx) ? #TRUE : #FALSE;
  yy = (stkhi == yy) ? #TRUE : #FALSE;
  psorfl(xx & yy);
}

void prtary(aa) {
  putc('{'); 
  for (i=0;i<aa;i++) {
    if (i) putc(',');
    putdec(t[i]);
  }
  putc('}');
}

void cklohi(aa,yy,xx) {
  putdst(); puts("lo=$"); prbyte(lo); putc(' ');
  putdst(); puts("hi=$"); prbyte(hi); putc(':');
  xx = (lo == xx) ? #TRUE : #FALSE;
  yy = (hi == yy) ? #TRUE : #FALSE;
  psorfl(xx & yy);
}

void chksiz(aa,yy,xx) {
  puts("\tChecking Stack Size\t");
  puts("stksiz()\t");
  zz, dsthi, dstlo = stksiz();
  printf(zz, "Sts=$%x Size=$%w:");
  if (zz == aa and dsthi == yy and dstlo == xx ) pass(); 
  else {fail(); goto exit;}
}

void psorfl(aa) {if (aa) pass(); else {fail(); goto exit;}}
void pass() {putln(" Pass");}
void fail() {putln(" Fail");}


puts("Dropping Swapped Entry off Stack\t");
puts("stkdrp()\t");
f=stkdrp(); printf(f,"bytes=%d:");
g=(f==4) ? #TRUE : #FALSE;
psorfl(f & g);
chkptr(&$8016);

