home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / RiscOS / APP / DEVS / FORTH / BEETLE / BEETLE.ZIP / Beetle / exceptst.c < prev    next >
C/C++ Source or Header  |  1997-04-22  |  3KB  |  98 lines

  1. /* EXCEPTST.C
  2.  
  3.     Vrsn  Date   Comment
  4.     ----|-------|---------------------------------------------------------------
  5.     0.00 25mar95
  6.     0.01 01apr95 Incorrect printf format changed.
  7.     0.02 26may96 Incorrect return code for test 5 changed. Added test 13 to test
  8.              for invalid contents of 'THROW.
  9.  
  10.     Reuben Thomas
  11.  
  12.  
  13.     Test the Beetle-generated exceptions and HALT codes.
  14.  
  15. */
  16.  
  17.  
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include "beetle.h"     /* main header */
  21. #include "btests.h"    /* Beetle tests header */
  22. #include "opcodes.h"    /* opcode enumeration */
  23. #include "debug.h"      /* debugging functions */
  24.  
  25.  
  26. int test[] = { 16, 20, 28, 40, 52, 56, 60, 64, 72, 76, 80, 84 };
  27. int result[] = { -258, -9, 100, 0, -258, -23, -10, -9, -9, -23, -256, -257 };
  28. int bad[] = { -1, 28, 28, 28, 28, 1, 64, 16384, 76, 80, 84, 88 };
  29. int address[] = { -4, 16384, 0, 0, 1, 1, 0, 16384, -4, 1, 0, 0 };
  30.  
  31.  
  32. int main(void)
  33. {
  34.     int i;
  35.     CELL res;
  36.  
  37.     init_beetle((BYTE *)malloc(16384), 4096, 16);
  38.     S0 = SP;    /* save base of stack */
  39.  
  40.     here = EP;    /* start assembling at 16 */
  41.     start_ass();
  42.     ass(O_ZERO); ass(O_SPSTORE); ass(O_DUP); ass(O_NEXT00); /* test 1 */
  43.     ass(O_LITERALI); ilit(MEMORY);  /* test 2 */
  44.     ass(O_SPSTORE); ass(O_TOR); ass(O_NEXT00); ass(O_NEXT00);
  45.     ass(O_CELL); ass(O_SPSTORE); ass(O_DUP); ass(O_DROP);   /* test 3 */
  46.     ass(O_LITERALI); ilit(100);    /* reset 'THROW, overwritten by the DUP above */
  47.     ass(O_HALT); ass(O_NEXT00); ass(O_NEXT00); ass(O_NEXT00);
  48.     ass(O_LITERALI); ilit(MEMORY);  /* test 4 */
  49.     ass(O_MINUSCELL); ass(O_SPSTORE); ass(O_TOR); ass(O_ZERO);
  50.     ass(O_HALT); ass(O_NEXT00); ass(O_NEXT00); ass(O_NEXT00);
  51.     ass(O_ONE); ass(O_PLUSCELL); ass(O_SPSTORE); ass(O_DUP);    /* test 5 */
  52.     ass(O_ONE); ass(O_EXECUTE);    ass(O_NEXT00); ass(O_NEXT00);    /* test 6 */
  53.     ass(O_ONE); ass(O_ZERO); ass(O_SLASH); ass(O_NEXT00);   /* test 7 */
  54.     ass(O_BRANCH); ass(O_NEXT00); ass(O_NEXT00); ass(O_NEXT00);    /* test 8 */
  55.     lit(MEMORY - CELL_W);
  56.     ass(O_MCELL); ass(O_FETCH);    ass(O_NEXT00); ass(O_NEXT00);    /* test 9 */
  57.     ass(O_ONE); ass(O_FETCH); ass(O_NEXT00); ass(O_NEXT00); /* test 10 */
  58.     ass(0x60);    ass(O_NEXT00); ass(O_NEXT00); ass(O_NEXT00);    /* test 11 */
  59.     ass(O_MCELL); ass(O_LIB);    /* test 12 */
  60.     ass(O_ONE); ass(O_DUP); ass(O_ZERO); ass(O_STORE);    /* test 13 */
  61.     ass(O_THROW);
  62.     end_ass();
  63.  
  64.     here = (CELL *)(M0 + 100);    /* start assembling at 100 */
  65.     start_ass();
  66.     ass(O_HALT);
  67.     end_ass();
  68.  
  69.     *THROW = 100;   /* set address of exception handler */
  70.  
  71.     for (i = 0; i < 12; i++) {
  72.         SP = S0;    /* reset stack pointer */
  73.  
  74.         EP = (CELL *)(M0 + test[i]);
  75.         NEXT;   /* load first instruction word */
  76.         res = run();
  77.  
  78. #ifdef B_DEBUG
  79.         printf("Test %d\n", i + 1);
  80.         printf("Return code is %d; should be %d\n", res, result[i]);
  81.         if (result[i] != 0) printf("'BAD = %d; should be %d\n", BAD, bad[i]);
  82.         if (result[i] <= -258 || result[i] == 9 || result[i] == -23)
  83.             printf("-ADDRESS = %d; should be %d\n", ADDRESS, address[i]);
  84.         putchar('\n');
  85. #endif
  86.         if (result[i] != res || (result[i] != 0 && bad[i] != BAD) ||
  87.             ((result[i] <= -258 || result[i] == 9 || result[i] == -23) &&
  88.             address[i] != ADDRESS)) {
  89.             printf("Error in ExceptsT: test %d failed; EP = %d\n", i + 1,
  90.                 (BYTE *) EP - M0);
  91.             exit(1);
  92.         }
  93.     }
  94.  
  95.     printf("ExceptsT ran OK\n");
  96.     return 0;
  97. }
  98.