home *** CD-ROM | disk | FTP | other *** search
/ Monster Media 1994 #1 / monster.zip / monster / MAGAZINE / DDJ9309.ZIP / FPALIB.ZIP / FMTEST2.C < prev    next >
C/C++ Source or Header  |  1993-05-12  |  9KB  |  281 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 part 2 of a simple extended floating point arithmetic package
  8. ** tester.
  9. **
  10. ** This is a table driven tester.  It does a comprehensive test of:
  11. **    1. Integer / ASCII string conversions
  12. **    2. Float / ASCII decimal conversions
  13. **    3. Integer / Float conversions
  14. **    4. Float / Integer conversions
  15. **    5. Float / Float conversions
  16. **
  17. ** Where reasonable, all numeric input and output values are specified
  18. ** as ASCII strings.  This greatly simplifies setting up test cases, and
  19. ** provides a good test of the string conversion routines.
  20. ** Note that these routines recognize zero, but NOT infinite, not a number,
  21. ** or denormalized input parameters.  They blindly assume that the arguments
  22. ** are valid floating point numbers.
  23. **
  24. ** These routines will return valid zero, infinite, and not a number values.
  25. */
  26. #include <stdio.h>
  27. #ifndef MWC
  28. #include <stdlib.h>
  29. #endif
  30. #include "imlib.h"
  31. #include "ffmlib.h"
  32. #include "fmlib.h"
  33. #include "fmtest.h"
  34.  
  35. /* Include the tables containing the tester data.  There are three data
  36. ** tables that contain test cases for:
  37. **
  38. **    integer to float conversions
  39. **    float to integer conversions
  40. **    float to float conversions
  41. */
  42. #include "fmtest2.h"
  43.  
  44. /*
  45. ** Function:    int fmtst2(int *testPN)
  46. **
  47. ** This is a table driven tester.  It does a comprehensive test of:
  48. **    1. Integer / ASCII string conversions
  49. **    2. Float / ASCII decimal conversions
  50. **    3. Integer / Float conversions
  51. **    4. Float / Integer conversions
  52. **    5. Float / Float conversions
  53. **
  54. ** testPN is a pointer to an integer used to total the error count.
  55. **
  56. ** Where reasonable, all numeric input and output values are specified
  57. ** as ASCII strings.  This greatly simplifies setting up test cases, and
  58. ** provides a good test of the string conversion routines.
  59. */
  60. int
  61. #ifdef PROTOTYPES
  62. fmtst2(int *testPN)
  63. #else
  64. fmtst2(testPN)
  65. int *testPN;
  66. #endif
  67. {
  68.     int i, fracbitsN, expbitsN, totalenN, radixN;
  69.     int intlenN;
  70.     int infracbitsN, inexpbitsN, intotalenN;
  71.     int outfracbitsN, outexpbitsN, outtotalenN;
  72. #ifdef MEMTEST
  73.     int memparcelN;
  74. #endif
  75.     unsigned char op1AB[128], op2AB[128];
  76.     unsigned char condcodeB;
  77.     char resultAB[128], quotientAB[128];
  78.     int errcountN;
  79.  
  80.     errcountN = 0;
  81.  
  82. #ifdef MEMTEST
  83.     fmallinit();
  84. #endif
  85.  
  86.     printf("************************************************\n");
  87.     printf("** Integer to floating point conversion tests **\n");
  88.     printf("************************************************\n");
  89.     i = 0;
  90.     while (iftableAH[i].operationS != NULL)
  91.     {
  92.     /* Determine the byte length of the input integer */
  93.     intlenN = iftableAH[i].intinlenN;
  94.  
  95.     /* Determine the length and type of the output floating point number */
  96.     expbitsN = iftableAH[i].fltoutexpN;
  97.     fracbitsN = iftableAH[i].fltoutfracN;
  98.     totalenN = iftableAH[i].fltoutlenN;
  99.  
  100.     printf("Operation %d: %s\n", (*testPN)++, (iftableAH[i].operationS));
  101. #ifdef TEST
  102.     printf("Input integer length: %d\n", intlenN);
  103.     printf("Output float exponent bits: %d, Mantissa bits: %d, Total byte length: %d\n",
  104.         expbitsN, fracbitsN, totalenN);
  105. #endif
  106.  
  107.     /* Convert the first operand from a string to an integer. */
  108.     strtoint(iftableAH[i].intinBP, strlen(iftableAH[i].intinBP), op1AB,
  109.          intlenN, 10);
  110. #ifdef TEST
  111.     printf("Integer as string: %s\n", iftableAH[i].intinBP);
  112.     foutput("Integer in binary: ", op1AB, intlenN);
  113. #endif
  114.  
  115.     /* Perform the conversion. */
  116.     condcodeB = intoflt(op1AB, intlenN, op2AB, fracbitsN, expbitsN);
  117.  
  118. #ifdef TEST
  119.     foutput("Result as float: ", op2AB, totalenN);
  120. #endif
  121.     /* Convert the result back to a string. */
  122.     fltostr(op2AB, fracbitsN, expbitsN, resultAB, 128);
  123. #ifdef TEST
  124.     printf("Result as string: %s\n", resultAB);
  125.     printf("Result should be: %s\n", iftableAH[i].fltoutBP);
  126.     printf("Result condition code: 0x%x\n", condcodeB);
  127.     printf("Condition code should be: 0x%x\n", iftableAH[i].ccresultN);
  128. #endif
  129.     if ((strcmp(resultAB, iftableAH[i].fltoutBP) == 0) &&
  130.         (((int) condcodeB) == iftableAH[i].ccresultN))
  131.     {
  132.         printf(" [(-:} OK {:-)]\n");
  133.     }
  134.     else
  135.     {
  136.         printf(" [)-:}***** ERROR *****{:-(]\n");
  137.         errcountN++;
  138.     }
  139. #ifdef MEMTEST
  140.     if ((memparcelN = fmallreport()) != 0)
  141.     {
  142.         printf("%d memory blocks not free'd\n", memparcelN);
  143.         errcountN += memparcelN;
  144.     }
  145. #endif
  146.     i++;
  147.     }
  148.  
  149.     printf("************************************************\n");
  150.     printf("** Floating point to integer conversion tests **\n");
  151.     printf("************************************************\n");
  152.     i = 0;
  153.     while (fitableAH[i].operationS != NULL)
  154.     {
  155.     /* Determine the length and type of the input floating point number */
  156.     expbitsN = fitableAH[i].fltinexpN;
  157.     fracbitsN = fitableAH[i].fltinfracN;
  158.     totalenN = fitableAH[i].fltinlenN;
  159.  
  160.     /* Determine the byte length of the output integer */
  161.     intlenN = fitableAH[i].intoutlenN;
  162.  
  163.     printf("Operation %d: %s\n", (*testPN)++, (fitableAH[i].operationS));
  164. #ifdef TEST
  165.     printf("Input float exponent bits: %d, Mantissa bits: %d, Total byte length: %d\n",
  166.         expbitsN, fracbitsN, totalenN );
  167.     printf("Output integer length: %d\n", intlenN);
  168. #endif
  169.  
  170.     /* Convert the first operand from a string to a float. */
  171.     strtoflt(fitableAH[i].fltinBP, strlen(fitableAH[i].fltinBP), op1AB,
  172.          fracbitsN, expbitsN );
  173. #ifdef TEST
  174.     printf("Float as string: %s\n", fitableAH[i].fltinBP);
  175.     foutput("Float in binary: ", op1AB, totalenN );
  176. #endif
  177.  
  178.     /* Perform the conversion. */
  179.     condcodeB = fltoint(op1AB, fracbitsN, expbitsN, op2AB, intlenN);
  180.  
  181. #ifdef TEST
  182.     foutput("Result as integer: ", op2AB, intlenN);
  183. #endif
  184.     /* Convert the result back to a string. */
  185.     intostr(op2AB, intlenN, resultAB, 128, 10);
  186. #ifdef TEST
  187.     printf("Result as string: %s\n", resultAB);
  188.     printf("Result should be: %s\n", fitableAH[i].intoutBP);
  189.     printf("Result condition code: 0x%x\n", condcodeB);
  190.     printf("Condition code should be: 0x%x\n", fitableAH[i].ccresultN);
  191. #endif
  192.     if ((strcmp(resultAB, fitableAH[i].intoutBP) == 0) &&
  193.         (((int) condcodeB) == fitableAH[i].ccresultN))
  194.     {
  195.         printf(" [(-:} OK {:-)]\n");
  196.     }
  197.     else
  198.     {
  199.         printf(" [)-:}***** ERROR *****{:-(]\n");
  200.         errcountN++;
  201.     }
  202. #ifdef MEMTEST
  203.     if ((memparcelN = fmallreport()) != 0)
  204.     {
  205.         printf("%d memory blocks not free'd\n", memparcelN);
  206.         errcountN += memparcelN;
  207.     }
  208. #endif
  209.     i++;
  210.     }
  211.  
  212.     printf("*******************************************************\n");
  213.     printf("** Floating point to floating point conversion tests **\n");
  214.     printf("*******************************************************\n");
  215.     i = 0;
  216.     while (fftableAH[i].operationS != NULL)
  217.     {
  218.     /* Determine the length and type of the input floating point number */
  219.     inexpbitsN = fftableAH[i].fltinexpN;
  220.     infracbitsN = fftableAH[i].fltinfracN;
  221.     intotalenN = fftableAH[i].fltinlenN;
  222.  
  223.     /* Determine the length and type of the output floating point number */
  224.     outexpbitsN = fftableAH[i].fltoutexpN;
  225.     outfracbitsN = fftableAH[i].fltoutfracN;
  226.     outtotalenN = fftableAH[i].fltoutlenN;
  227.  
  228.     printf("Operation %d: %s\n", (*testPN)++, (fftableAH[i].operationS));
  229. #ifdef TEST
  230.     printf(" Input float exponent bits: %d, Mantissa bits: %d, Total byte length: %d\n",
  231.         inexpbitsN, infracbitsN, intotalenN );
  232.     printf("Output float exponent bits: %d, Mantissa bits: %d, Total byte length: %d\n",
  233.         outexpbitsN, outfracbitsN, outtotalenN );
  234. #endif
  235.  
  236.     /* Convert the first operand from a string to a float. */
  237.     strtoflt(fftableAH[i].fltinBP, strlen(fftableAH[i].fltinBP), op1AB,
  238.          infracbitsN, inexpbitsN );
  239. #ifdef TEST
  240.     printf("Input float as string: %s\n", fftableAH[i].fltinBP);
  241.     foutput("Input float in binary: ", op1AB, intotalenN);
  242. #endif
  243.  
  244.     /* Perform the conversion. */
  245.     condcodeB = fltoflt(op1AB, infracbitsN, inexpbitsN,
  246.                 op2AB, outfracbitsN, outexpbitsN);
  247.  
  248. #ifdef TEST
  249.     foutput("Result as float: ", op2AB, outtotalenN);
  250. #endif
  251.     /* Convert the result back to a string. */
  252.     fltostr(op2AB, outfracbitsN, outexpbitsN, resultAB, 128);
  253. #ifdef TEST
  254.     printf("Result as string: %s\n", resultAB);
  255.     printf("Result should be: %s\n", fftableAH[i].fltoutBP);
  256.     printf("Result condition code: 0x%x\n", condcodeB);
  257.     printf("Condition code should be: 0x%x\n", fftableAH[i].ccresultN);
  258. #endif
  259.     if ((strcmp(resultAB, fftableAH[i].fltoutBP) == 0) &&
  260.         (((int) condcodeB) == fftableAH[i].ccresultN))
  261.     {
  262.         printf(" [(-:} OK {:-)]\n");
  263.     }
  264.     else
  265.     {
  266.         printf(" [)-:}***** ERROR *****{:-(]\n");
  267.         errcountN++;
  268.     }
  269. #ifdef MEMTEST
  270.     if ((memparcelN = fmallreport()) != 0)
  271.     {
  272.         printf("%d memory blocks not free'd\n", memparcelN);
  273.         errcountN += memparcelN;
  274.     }
  275. #endif
  276.     i++;
  277.     }
  278.  
  279.     return(errcountN);
  280. }
  281.