home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_10 / 1010052a < prev    next >
Text File  |  1992-08-12  |  2KB  |  101 lines

  1. /* Listing 5 */
  2.  
  3. #include <stdio.h>
  4.  
  5. #define TEST
  6. #include "Listing 3"
  7.  
  8. static int c;
  9.  
  10. int getLength(void);
  11.  
  12. /* target function */
  13. int complexDecision(int a, int b)
  14. {
  15.   if (((a > b) && (a < getLength())) || (a == 15))
  16.      c = a + b;
  17.   else
  18.      c = 0;
  19.   return c;
  20. }
  21.  
  22. /******** begin test code **********/
  23. #if defined( TEST )
  24.  
  25. static int length;
  26.  
  27. struct testParameters
  28. {
  29.   int     a;            /* input parameter */
  30.   int     b;            /* input parameter */
  31.   int     length;       /* function call return */
  32.   int     returnValue;  /* target return value */
  33.   char    *errorMsg;  /* message if error detected */
  34. };
  35.  
  36. #define MAX 32767       /* 16 bit integer boundary */
  37.  
  38. struct testParameters testCase[] =
  39. {
  40.   {  1,  0,MAX,  1,"a > b and a < length" },
  41.   {  0,  0,MAX,  0,"a = b and a < length" },
  42.   { -1,  0,MAX,  0,"a < b and a < length" },
  43.   { -1, -1,MAX,  0, "a and b = -1 and a < length" },
  44.   {MAX,  0,MAX,  0, "a > b and a = length" },
  45.   {  0,  0,  0,  0, "a, b and length = 0" },
  46.   { -1, -1, -1,  0, "a, b and length = -1" },
  47.   {MAX,MAX,MAX,  0, "a, b, and length = MAX" },
  48.   { 15, 15,MAX, 30, "a = b, a < length and a = 15" },
  49.   {15,MAX,MAX,-32754,"a < b, a < length and a = 15" },
  50.   {15,MAX,MAX,-32754,"a = b, a < length and a = 15" },
  51.   { 15, 14, 14, 29, "a > b, a > length and a = 15" },
  52.   { 15,  0, 15, 15, "a > b, a = length and a = 15" },
  53.   { 15, -1,MAX, 14, "a > b, a < length and a = 15" },
  54.   { 15,  1, 16, 16, "a > b, a < length and a = 15" }
  55. };
  56.  
  57. /* function to test the complexDecision() function */
  58. int testDecision(void)
  59. {
  60.   int   i;
  61.   int   result;
  62.  
  63.   /* macro to print header and initialize error
  64.      counter */
  65.   StartTest("complexDecision()");
  66.  
  67.   /* loop through test cases */
  68.   for(i = 0; i < 15; i++)
  69.   {
  70.      /* set dummy function return value */
  71.      length = testCase[i].length;
  72.  
  73.      /* call the target function */
  74.      result = complexDecision(testCase[i].a,
  75.                               testCase[i].b);
  76.  
  77.      /* check for correct return value */
  78.      if(result != testCase[i].returnValue)
  79.         ErrorMsg(i, testCase[i].errorMsg);
  80.  
  81.      /* verify correct side effect */
  82.      if(result != c)
  83.         ErrorMsg(i, "c is incorrect");
  84.   }
  85.  
  86.   /* macro to print results and return error count */
  87.   EndTest;
  88. }
  89.  
  90. main()
  91. {
  92.   testDecision();
  93. }
  94.  
  95. /* dummy function */
  96. int getLength( void )
  97. {
  98.   return length;
  99. }
  100. #endif
  101.