/**************************************************
 * TESTSLIB - Test Library stdlib.h02 for py65mon *
 **************************************************/
 
//Specify System Header using -H option
#include <stddef.h02>
#include <stdlib.h02>

char aa, yy, xx;

void prtcma(aa) {putchr(','); prbyte(aa);}
void prtlin() {putstr(); newlin();}
void prtok() {prtlin(" OK");}
void prtopr(aa,yy,xx) {prbyte(aa); putchr(yy); prbyte(xx); putchr("=");}

main:

tstabs: //Test abs()
char onum, anum, cnum;

  putstr("ABS()");
  onum = 0; 
  do { //test abs() positive numbers  
    anum = abs(onum);
    if (onum <> anum) goto abserr;
    onum++;
  } while (onum < $80);
  do { //test abs() negative numbers
    anum = abs(onum);
    cnum = -onum;
    if (anum <> cnum) goto abserr;
    onum++;
  } while (onum);
  prtok();

tstmnx: //Test min() and max()
char lnum, rnum, nnum, xnum;

  putstr("MAX(), MIN()");
  lnum = 0; do {
    rnum = 0; do {
      xnum = max(lnum,rnum);
      nnum = min(lnum,rnum);
      if (lnum > rnum) {
        if (xnum <> lnum) maxerr();
        if (nnum <> rnum) minerr();
      } else {
        if (xnum <> rnum) maxerr();
        if (nnum <> lnum) minerr();
      }
    } while (rnum);
  } while (lnum);
  prtok();

tstmlt: //Test mult()
char mltplr, mltpnd, acmlsb, acmmsb, acmlst;
char prodct, ovrflw;

  putstr("MULT()");
  mltplr = 1; 
  do {
    if (!mltplr & $F) putchr('.');
    mltpnd = 1; acmlst = 0;
    acmlsb = 0; acmmsb = 0;
    do {
      acmlsb = acmlsb + mltplr;
      if (acmlsb<acmlst) acmmsb++;
      acmlst = acmlsb;
      prodct,ovrflw = mult(mltplr,mltpnd);
      if (prodct <> acmlsb) goto mlterr;
      mltpnd++;
    } while (mltpnd);
    mltplr++;
  } while (mltplr);
  prtok();

tstdiv: //Test div()
char maxmpd, divdnd, divisr, quotnt;

putstr("DIV()");
mltplr = 255;
maxmpd = 1;
drloop:
  mltpnd = 1;
  acmlsb = 0;
  ddloop:
    acmlsb = acmlsb + mltplr;
    prodct = mult(mltplr, mltpnd);
    quotnt = div(prodct, mltpnd);
    if (quotnt <> mltplr) goto derror;
    mltpnd++;
    if (mltpnd < maxmpd) goto ddloop;
  mltplr>>;
  maxmpd<<;
  if (mltplr <> 0) goto drloop; 
  prtok();

tstrnd: //Test rand() and rands()
char countr, rndnum, rndtbl[255];

  putstr("RAND()");
  rands(1); //Seed Random Number Generator;

  countr = 0;
  
  riloop:
    rndtbl[countr] = 0;
    countr++;
    if (countr <> 0) goto riloop;
  
  rnloop:
    rndnum = rand();
    if (rndtbl[rndnum] > 0) goto rnderr;
    rndtbl[rndnum] = $FF;
    countr++;
    if (countr < 255) goto rnloop;
    prtok();

tstros: //Test rotatl(), rotatr(), swap()
char lbyte, rbyte, obyte, sbyte, tbyte, scount;

  putstr("ROTATL(), SHIFTL()");
  obyte = 0; 
  do {
    if (!obyte & $F) putchr('.');
    scount = 0; sbyte = obyte; tbyte = obyte; 
    do {
      rbyte = rotatl(obyte, scount); if (rbyte <> tbyte) goto rtlerr;
      lbyte = shiftl(obyte, scount); if (lbyte <> sbyte) goto shlerr;
      sbyte<<;
      temp0 = tbyte; tbyte<<; if (temp0:-) tbyte++;
      scount++; if (scount == 8) scount = $F8;
    } while (scount);  
    obyte ++;
  } while (obyte);
  prtok();
  
  putstr("ROTATR(), SHIFTR()");
  obyte = 0; 
  do {
    if (!obyte & $F) putchr('.');
    scount = 0; sbyte = obyte; tbyte = obyte; 
    do {
      rbyte = rotatr(obyte, scount); if (rbyte <> tbyte) goto rtrerr;
      lbyte = shiftr(obyte, scount); if (lbyte <> sbyte) goto shrerr;
      sbyte>>;
      temp0 = tbyte; tbyte>>; if (temp0 & 1) tbyte = tbyte | $80;
      scount++; if (scount == 8) scount = $F8;
    } while (scount);  
    obyte ++;
  } while (obyte);
  prtok();
  
  putstr("SWAP()");
  lbyte=0; do {
    rbyte = 0; do {
      obyte = lbyte & $f0; obyte = rbyte & $0f + lbyte;
      tbyte = lbyte & $0f; tbyte = rbyte & $f0 + lbyte;
      sbyte = swap(obyte);
      if (sbyte <> tbyte) goto swperr;      
      rbyte = rbyte + $11;
    } while (rbyte & $0f);
  } while (lbyte & $0f);
  prtok();

tstc2a: //Test ctoa();
char srcchr, dstchr, tststr[4];

  putstr("ATOC(), CTOA()");
  srcchr = 0; do {
    ctoa(srcchr, &tststr);
    dstchr = atoc(&tststr);
    if (srcchr <> dstchr) goto c2aerr;
    srcchr++;
  } while (srcchr);
  prtok();

goto exit;

abserr:
  putstr("ABS("); prbyte(onum); putstr(")="); prbyte(anum); prtcma(cnum); newlin();
  goto exit;

maxerr:
  putstr("MAX("); prbyte(lnum); prtcma(rnum); putstr(")="); prbyte(xnum); newlin();
  goto exit;

minerr:
  putstr("MIN("); prbyte(lnum); prtcma(rnum); putstr(")="); prbyte(nnum); newlin();
  goto exit;

mlterr:
  newlin(); prtopr(mltplr,'*',mltpnd); prbyte(prodct); prtcma(acmlsb); newlin();
  goto exit;

derror:
  newlin(); prtopr(divdnd,'/',divisr); prbyte(quotnt); prtcma(mltplr); newlin();
  goto exit;

rnderr:
  newlin(); putstr("RAND()="); prbyte(rndnum); prtcma(countr); newlin();
  goto exit;

rtlerr:
  newlin(); putstr("ROTATL("); prbyte(obyte); prtcma(scount); putstr(")="); 
  prbyte(rbyte); prtcma(tbyte); newlin();
  goto exit;

rtrerr:
  newlin(); putstr("ROTATR("); prbyte(obyte); prtcma(scount); putstr(")="); 
  prbyte(rbyte); prtcma(tbyte); newlin();
  goto exit;

shlerr:
  newlin(); putstr("SHIFTL("); prbyte(obyte); prtcma(scount); putstr(")="); 
  prbyte(lbyte); prtcma(sbyte); newlin();
  goto exit;

shrerr:
  newlin(); putstr("SHIFTR("); prbyte(obyte); prtcma(scount); putstr(")="); 
  prbyte(lbyte); prtcma(sbyte); newlin();
  goto exit;

swperr:
  newlin(); putstr("SWAP("); prbyte(obyte); putstr(")="); 
  prbyte(sbyte); prtcma(tbyte); newlin();
  goto exit;

c2aerr:
  putstr("CTOA("); prbyte(srcchr); putstr(")="); prbyte(dstchr); newlin();
  goto exit;
