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

  1. /* TESTS.C
  2.  
  3.     Vrsn  Date   Comment
  4.     ----|-------|--------------------------------------------------------------
  5.     0.00 18jan95
  6.     0.01 19jan95 Error messages added and division test debugged.
  7.     0.02 17feb95 Now includes tests.h (unnecessary, but worthwhile for
  8.                  consistency checks).
  9.     0.03 12jun96 Changed error messages for ENDISM check.
  10.  
  11.     Reuben Thomas
  12.  
  13.  
  14.     Test that C Beetle has been compiled properly. The following tests are run:
  15.  
  16.     1. The C compiler uses twos-complement arithmetic.
  17.     2. BYTE, CELL and UCELL are correctly set.
  18.     3. ARSHIFT performs an arithmetic right shift on signed quantities.
  19.     4. The C compiler performs ordinary modular arithmetic on overflow.
  20.     5. MOD and DIV perform floored division.
  21.     6. ENDISM is set correctly.
  22.  
  23. */
  24.  
  25.  
  26. #include <stdio.h>
  27. #include <limits.h>
  28. #include "beetle.h"
  29. #include "tests.h"
  30.  
  31.  
  32. int twos_complement(void)
  33. {
  34.     if (!(-1) != 0) {
  35.         printf("Beetle cannot work as the C compiler does not use "
  36.             "twos-complement arithmetic\n");
  37.         return 1;
  38.     }
  39.     return 0;
  40. }
  41.  
  42. int types(void)
  43. {
  44.     int ret = 0;
  45.  
  46.     if (sizeof(BYTE) != 1 || (BYTE)(0xff) < 0) {
  47.         printf("Type BYTE is incorrectly defined in bportab.h\n");
  48.         ret = 1;
  49.     }
  50.  
  51.     if (sizeof(CELL) != 4 || (CELL)(0x80000000) > 0) {
  52.         printf("Type CELL is incorrectly defined in bportab.h\n");
  53.         ret = 1;
  54.     }
  55.  
  56.     if (sizeof(UCELL) != 4  || (UCELL)(0x80000000) < 0) {
  57.         printf("Type UCELL is incorrectly defined in bportab.h\n");
  58.         ret = 1;
  59.     }
  60.  
  61.     return ret;
  62. }
  63.  
  64. int arshift(void)
  65. {
  66.     CELL x = -1;
  67.  
  68. #ifdef LRSHIFT
  69.     if (x >> 1 == -1) {
  70.         printf("LRSHIFT could be undefined in bportab.h\n");
  71. #else
  72.     if (ARSHIFT(x, 1) != -1) {
  73.         printf("LRSHIFT should be defined in bportab.h\n");
  74. #endif
  75.         return 1;
  76.     }
  77.     return 0;
  78. }
  79.  
  80. int overflow(void)
  81. {
  82.     signed long x = LONG_MAX;
  83.  
  84.     if (x + 1 != LONG_MIN || x - (-1) != LONG_MIN || x * 2 != -2) {
  85.         printf("Beetle cannot work as the C compiler does not use "
  86.             "modular arithmetic overflow\n");
  87.         return 1;
  88.     }
  89.     return 0;
  90. }
  91.  
  92. int division(void)
  93. {
  94.     CELL x = -10, y = 7, t;
  95.  
  96.     if (DIV(x, y) != -2 || MOD(x, y, t) != 4) {
  97. #ifdef FLOORED
  98.         printf("FLOORED should be undefined in bportab.h\n");
  99. #else
  100.         printf("FLOORED should be defined in bportab.h\n");
  101. #endif
  102.         return 1;
  103.     }
  104.     return 0;
  105. }
  106.  
  107. int endism(void)
  108. {
  109.     CELL x = 1;
  110.     BYTE *p = (BYTE *)&x;
  111.  
  112.     if (!((*p == 1) ^ ENDISM)) {
  113. #ifdef BIG_ENDIAN
  114.         printf("BIG_ENDIAN should not be defined\n");
  115. #else
  116.         printf("BIG_ENDIAN should be defined\n");
  117. #endif
  118.         return 1;
  119.     }
  120.     return 0;
  121. }
  122.  
  123.  
  124. int tests(void)
  125. {
  126.     return (twos_complement() | types() | arshift() | overflow() | division() |
  127.        endism());
  128. }
  129.