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

void prtcma() {putchr(',');}
void prtlin() {putstr(); newlin();}
void prtok()  {prtlin(" ok");}

main:  

tstabs: 
  char onum, anum, cnum;
  putstr("ABS()");
  onum = 0;
aploop: 
  anum = abs(onum);
  if (onum <> anum) goto abserr;
  onum++;
  if (onum < $80) goto aploop;
anloop:
  anum = abs(onum);
  cnum = -onum;
  if (anum <> cnum) goto abserr;
  onum++; 
  if (onum > $00) goto anloop;
  prtok();

tstmlt:
  char mltplr, mltpnd, acmlsb, acmmsb, acmlst, prodct, ovrflw;
  putstr("MULT()");
  mltplr = 1;
mrloop: 
  mltpnd = 1; acmlst = 0; acmlsb = 0; acmmsb = 0;
mdloop: 
  acmlsb = acmlsb + mltplr;
  if (acmlsb < acmlst) acmmsb++;
  acmlst = acmlsb;
  prodct,ovrflw = mult(mltplr,mltpnd);
  if (prodct <> acmlsb) goto merror; 
  mltpnd++; if (mltpnd > 0) goto mdloop;
  mltplr++; if (mltplr > 0) goto mrloop;
  prtok();

tstdiv:
  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:
  char countr, rndnum, rndtbl[255];
  putstr("RAND()");
  rands(1); 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();

tstcvb:
  char cvbchr, cvblo, cvbhi;
  putstr("CVBCD()"); 
  cvbchr = 0; cvblo = 0; cvbhi = 0;
cvloop: 
  cvbcd(cvbchr); if (temp1 <> cvblo or temp2 <> cvbhi) goto cvberr;
  cvblo++; 
  if (cvblo & $0F > 9) {
    cvblo = cvblo & $F0 + $10;
    if (cvblo >= $A0) {cvblo = cvblo & $0F; cvbhi++;} 
  }
  cvbchr++; if (cvbchr) goto cvloop; 
  prtok();

tstc2a:
  char srcchr, dstchr, tststr[4];
  putstr("CTOA()"); 
  srcchr = 0;
caloop: 
  ctoa(srcchr, &tststr);
  dstchr = atoc(&tststr);
  if (srcchr <> dstchr) goto c2aerr;
  srcchr++; if (srcchr <> 0) goto caloop;
  prtok();

  goto exit;

abserr: 
  putstr("abs("); prbyte(onum); putchr(')'); putchr('=');
  prbyte(anum); putchr(','); prbyte(cnum); newlin();
  goto exit;

merror: 
  newlin();
  prbyte(mltplr); putchr('*'); prbyte(mltpnd); putchr('=');
  prbyte(prodct); putchr(','); prbyte(acmlsb); newlin();
  goto exit;

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

rnderr: 
  putstr("rand()="); prbyte(rndnum); putchr(','); prbyte(countr); newlin();
  goto exit;

cvberr: 
  putstr(" !cvbcd("); prbyte(cvbchr); putstr(")=$");
  prbyte(temp2); prbyte(temp1); newlin();
  goto exit;

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