home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / MAGAZINE / DDJ9309.ZIP / FPALIB.ZIP / FMTEST.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  10KB  |  359 lines

  1. /* Extended IEEE Compatible Floating Point Arithmetic Library
  2. **
  3. ** Version 1.1
  4. ** Copyright (C) 1990, by Fred Motteler
  5. ** All Rights Reserved
  6. **
  7. ** This is the main test routine for the extended floating point arithmetic
  8. ** package.  It also tests the extended integer arithmetic package.
  9. **
  10. ** Floating Point Format:
  11. **
  12. ** |S| n bit signed exponent | m bit fraction |
  13. **
  14. ** The format is compatible with IEEE Standard for Binary Floating
  15. ** when:
  16. **        n = 8,   m = 23        single precision
  17. **        n = 11,  m = 52        double precision
  18. **
  19. ** If n = 15 and m = 64, this format is almost compatible with IEEE
  20. ** extended precision format.  The main difference are
  21. **    1) IEEE extended precision format is a 12 byte format with 10 bytes of
  22. **       information.  Bits 64 thru 80 are zero.
  23. **    2) The most significant bit of the mantissa (implied in single and
  24. **       double precision IEEE formats) is explicitly included.
  25. **
  26. ** Each routine that returns a floating point result returns a condition code
  27. ** value:
  28. **
  29. **    0 0 0 0 N Z I n
  30. **    Where N = negative (FFNEG)
  31. **          Z = zero (FFZERO)
  32. **          I = infinite (FFINF)
  33. **          n = not a number (FFNAN)
  34. **
  35. ** Each routine that returns a integer result returns a condition code
  36. ** value:
  37. **
  38. **    0 0 0 0 Z V S C
  39. **    Where Z = zero (ZERO)
  40. **          V = overflow (OVERFLOW)
  41. **          S = sign (SIGN)
  42. **          C = carry (CARRY)
  43. **
  44. ** This is a table driven tester.  It does a comprehensive test of:
  45. **    1. Integer math operations
  46. **    2. Integer / ASCII string conversions
  47. **    3. Float math operations
  48. **    4. Float / ASCII decimal conversions
  49. **    5. Integer / Float conversions
  50. **    6. Float / Float conversions
  51. **    7. ASCII string / Integer conversions
  52. **    8. ASCII string / Float conversions
  53. **
  54. ** All numeric input and output values are specified as ASCII strings.
  55. ** This greatly simplifies setting up test cases, and provides a good test
  56. ** of the string conversion routines.
  57. **
  58. ** Note that these routines recognize zero and infinite values. Not a number,
  59. ** or denormalized input parameters are not recogized.  They blindly assume
  60. ** that the arguments are valid floating point numbers.
  61. **
  62. ** These routines will return valid zero, infinite, and not a number values.
  63. */
  64.  
  65. #include <stdio.h>
  66. #ifndef MWC
  67. #include <stdlib.h>
  68. #endif
  69.  
  70. #include "imlib.h"
  71. #include "ffmlib.h"
  72. #include "fmlib.h"
  73. #include "fmtest.h"
  74.  
  75. #ifdef MEMTEST
  76. /* Dynamic memory allocation test defines. */
  77. #define FMALLSIZE 100            /* Max. number of alloc's to track. */
  78. #define FMALLFUNC 16            /* Max. length of function id's. */
  79.  
  80. /* Memory allocation table.  This is used to make sure that ALL dynamically
  81. ** allocated memory is actually freed. */
  82.  
  83. struct alloc_tab {
  84.     unsigned char *alloc_addrBP;    /* Memory allocation pointer */
  85.     char func_nameAB[FMALLFUNC];    /* Name of routine allocating memory */
  86.     } fmalltabAH[FMALLSIZE];        /* Allow up to 100 memory alloc's */
  87.  
  88. /*
  89. ** Function:    void fmallinit(void)
  90. **
  91. ** This function initializes the memory allocation tracking table.
  92. */
  93. void
  94. #ifdef PROTOTYPES
  95. fmallinit(void)
  96. #else
  97. fmallinit()
  98. #endif
  99. {
  100.     int i, j;
  101.  
  102.     for (i = 0; i < FMALLSIZE; i++)
  103.     {
  104.     fmalltabAH[i].alloc_addrBP = NULL;
  105.     for (j = 0; j < FMALLFUNC; j++)
  106.         fmalltabAH[i].func_nameAB[j] = 0;
  107.     }
  108.     return;
  109. }
  110.  
  111. /*
  112. ** Function:    int fmallreport(void)
  113. **
  114. ** This function printf's a list of memory left allocated.  This function
  115. ** returns the number of memory parcels allocated.
  116. **
  117. ** In order to keep the table from filling up, all allocated parcels
  118. ** are free'd and the table is reinitialized.
  119. */
  120. int
  121. #ifdef PROTOTYPES
  122. fmallreport(void)
  123. #else
  124. fmallreport()
  125. #endif
  126. {
  127.     int i;
  128.     int countN;                /* Allocated parcel count. */
  129.  
  130.     countN = 0;
  131.     for (i = 0; i < FMALLSIZE; i++)
  132.     {
  133.     if (fmalltabAH[i].alloc_addrBP != NULL)
  134.     {
  135.         countN++;
  136.         printf("Left over allocated memory: %s    address: %lx\n",
  137.             fmalltabAH[i].func_nameAB,
  138.             fmalltabAH[i].alloc_addrBP);
  139.         free(fmalltabAH[i].alloc_addrBP);
  140.         fmalltabAH[i].alloc_addrBP = NULL;
  141.         fmalltabAH[i].func_nameAB[0] = '\0';
  142.     }
  143.     }
  144.     return(countN);
  145. }
  146.  
  147. /*
  148. ** Function:    unsigned char *fmalloc(unsigned int sizeN,
  149. **                       char *func_nameBP)
  150. **
  151. ** This function allocates a block of contiguous memory with byte length given
  152. ** by sizeN.  Then it enters the allocated memory's address and the
  153. ** "func_nameBP" string name into the memory allocation table.
  154. */
  155. unsigned char *
  156. #ifdef PROTOTYPES
  157. fmalloc(unsigned int sizeN, char *func_nameBP)
  158. #else
  159. fmalloc(sizeN, func_nameBP)
  160. unsigned int sizeN;
  161. char *func_nameBP;
  162. #endif
  163. {
  164.     int i;
  165.     unsigned char *tempBP;
  166.  
  167.     /* Allocate the desired memory, if possible. */
  168.     if ((tempBP = (unsigned char *) malloc(sizeN)) == NULL)
  169.     {
  170.     printf("Out of memory in: %s\n", func_nameBP);
  171.     return(NULL);
  172.     }
  173.  
  174.     /* Search the allocation table for a free spot.  When found, record this
  175.      * memory allocation. */
  176.     for (i = 0; i < FMALLSIZE; i++)
  177.     {
  178.     if (fmalltabAH[i].alloc_addrBP == NULL)
  179.     {
  180.         fmalltabAH[i].alloc_addrBP = tempBP;
  181.         strncpy(fmalltabAH[i].func_nameAB, func_nameBP, FMALLFUNC);
  182.         return(tempBP);
  183.     }
  184.     }
  185.  
  186.     printf("Memory allocation table full, %s allocation not recorded.\n",
  187.         func_nameBP);
  188.     return(tempBP);
  189. }
  190.  
  191. /*
  192. ** Function:    unsigned char *fcalloc(int sizeN, int objectN,
  193. **                       char *func_nameBP)
  194. **
  195. ** This function allocates a block of zeroed contiguous memory with byte
  196. ** length given by sizeN.  Then it enters the allocated memory's address
  197. ** and the "func_nameBP" string name into the memory allocation table.
  198. */
  199. unsigned char *
  200. #ifdef PROTOTYPES
  201. fcalloc(unsigned int sizeN, unsigned int objectN, char *func_nameBP)
  202. #else
  203. fcalloc(sizeN, objectN, func_nameBP)
  204. unsigned int sizeN;
  205. unsigned int objectN;
  206. char *func_nameBP;
  207. #endif
  208. {
  209.     int i;
  210.     unsigned char *tempBP;
  211.  
  212.     /* Allocate the desired memory, if possible. */
  213.     if ((tempBP = (unsigned char *) calloc(sizeN, objectN)) == NULL)
  214.     {
  215.     printf("Out of memory in: %s\n", func_nameBP);
  216.     return(NULL);
  217.     }
  218.  
  219.     /* Search the allocation table for a free spot.  When found, record this
  220.      * memory allocation. */
  221.     for (i = 0; i < FMALLSIZE; i++)
  222.     {
  223.     if (fmalltabAH[i].alloc_addrBP == NULL)
  224.     {
  225.         fmalltabAH[i].alloc_addrBP = tempBP;
  226.         strncpy(fmalltabAH[i].func_nameAB, func_nameBP, FMALLFUNC);
  227.         return(tempBP);
  228.     }
  229.     }
  230.  
  231.     printf("Memory allocation table full, %s allocation not recorded.\n",
  232.         func_nameBP);
  233.     return(tempBP);
  234. }
  235.  
  236. /*
  237. ** Function:    void ffree(unsigned char *pntrBP)
  238. **
  239. ** This function frees the dynamically allocated memory block pointed to by 
  240. ** pntrBP.  It then search the memory allocation table for a matching address.
  241. ** If the address is found, then the corresponding entry in the table is
  242. ** cleared.
  243. */
  244. void
  245. #ifdef PROTOTYPES
  246. ffree(unsigned char *pntrBP)
  247. #else
  248. ffree(pntrBP)
  249. unsigned char *pntrBP;
  250. #endif
  251. {
  252.     int i;
  253.  
  254.     free( pntrBP );
  255.     for (i = 0; i < FMALLSIZE; i++)
  256.     {
  257.     if (fmalltabAH[i].alloc_addrBP == pntrBP)
  258.     {
  259.         fmalltabAH[i].alloc_addrBP = NULL;
  260.         fmalltabAH[i].func_nameAB[0] = '\0';
  261.         return;
  262.     }
  263.     }
  264.  
  265.     printf("Free address not found in allocation table: %lx\n", pntrBP);
  266.     return;
  267. }
  268. #endif
  269.  
  270. /*
  271. ** Function:    void foutput(char *stringBP, unsigned char *floatBP,
  272. **                 int lengthN)
  273. **
  274. ** This function dumps the "object" pointed to by floatBP as a string of
  275. ** hex characters.  The byte length of the object is given by lengthN.
  276. ** The string pointed to by stringBP is used to prefix the dumped value.
  277. ** The string and hex value are followed by a carriage return.
  278. */
  279. void
  280. #ifdef PROTOTYPES
  281. foutput(char *stringBP, unsigned char *floatBP, int lengthN)
  282. #else
  283. foutput(stringBP, floatBP, lengthN)
  284. char *stringBP;
  285. unsigned char *floatBP;
  286. int lengthN;
  287. #endif
  288. {
  289.     int i;
  290.     unsigned char tempB;
  291.  
  292.     printf(stringBP);
  293.     for (i = 0; i < lengthN; i++)
  294.     {
  295.     tempB = *floatBP++;
  296.     if (tempB < 16)
  297.         printf("0%1x",tempB);
  298.     else
  299.         printf("%2x", tempB);
  300.     }
  301.     printf("\n");
  302.     return;
  303. }
  304.  
  305. /*
  306. ** Function:    int main()
  307. **
  308. ** This is a table driven tester.  It does a comprehensive test of:
  309. **    1. Integer math operations
  310. **    2. Integer / ASCII string conversions
  311. **    3. Float math operations
  312. **    4. Float / ASCII decimal conversions
  313. **    5. Integer / Float conversions
  314. **    6. Float / Float conversions
  315. **    7. ASCII string / Integer conversions
  316. **    8. ASCII string / Float conversions
  317. **
  318. ** All numeric input and output values are specified as ASCII strings.
  319. ** This greatly simplifies setting up test cases, and provides a good test
  320. ** of the string conversion routines.
  321. */
  322. int
  323. #ifdef PROTOTYPES
  324. main(void)
  325. #else
  326. main()
  327. #endif
  328. {
  329.     int testN;
  330.     int errcountN;
  331.  
  332.     printf("Arbitrary Precision Floating Point Arithmetic Library Test Routine\n");
  333.     printf("Version 1.1\n");
  334.     printf("Copyright (C) 1989,1991 by Fred Motteler\n");
  335.     printf("All Rights Reserved\n\n");
  336.  
  337.     testN = 0;
  338.     errcountN = 0;
  339.  
  340.     /* Test basic operations for integer and float operations. */
  341.     errcountN += fmtst1(&testN);
  342.  
  343. #ifndef PARTEST
  344.     /* Test conversion operations for int to float, float to int, and
  345.      * float to float. */
  346.     errcountN += fmtst2(&testN);
  347.  
  348.     /* Test conversion operation for string to int and string to float. */
  349.     errcountN += fmtst3(&testN);
  350. #endif
  351.  
  352.     printf("Total number of errors encountered: %d", errcountN);
  353.     if (errcountN == 0)
  354.     printf(" {:-)]\n");
  355.     else
  356.     printf(" {:-(]\n");
  357.     exit(errcountN);
  358. }
  359.