root/user/uw-testbin/lib/testutils.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. print_location
  2. test_equal
  3. test_equal_one_of
  4. test_positive
  5. test_negative
  6. test_not_equal
  7. test_reset_stats
  8. test_verbose_on
  9. test_verbose_off
  10. test_print_stats
  11. main

   1 
   2 /*
   3  * Title    : testutils
   4  * Author   : Tim Brecht
   5  * Date     : Thu Dec 23 21:19:00 EST 2004
   6  * Modified : Wed  1 Jan 2014 14:17:19 EST
   7  *            Added TEST_EQUAL_ONE_OF
   8  *            Fri Jun 28 22:37:24 EDT 2013 for use with OS161
   9  *
  10  * Some simple utilities that can be helpful for testing.
  11  * Feel free to add/modify this code to suit your needs.
  12  * NOTE: this hasn't been fully tested.
  13  *
  14  * See test-testutil, file1 and file2 for example use cases.
  15  */
  16 
  17 #include <stdio.h>
  18 #include "testutils.h"
  19 
  20 /* Used to track the number of tests and failures */
  21 static int test_num = 0;
  22 static int num_failures = 0;
  23 static int verbose = 0;
  24 
  25 /* Use to print the source file, function and line */
  26 static void
  27 print_location(const char *file, const char *func, int line, const char *in_test)
  28 {
  29   printf("   %s : function = %s, line = %d test was %s\n", file, func, line, in_test);
  30 }
  31 
  32 /* Check if val is equal to expected_val, if not it is an error */
  33 void
  34 test_equal(int val, int expected_val, const char *str, 
  35             const char *file, const char *func, int line)
  36 {
  37   int failed = 0;
  38   char const *out = "SUCCESS";
  39 
  40   test_num++;
  41 
  42   if (val != expected_val) {
  43     num_failures++;
  44     failed = 1;
  45     out = "FAILURE";
  46   }
  47 
  48   if (failed || verbose) {
  49     printf("%s ON TEST = %d : Got %d : expected %d\n", 
  50       out, test_num, val, expected_val);
  51 
  52     if (failed) {
  53       printf("    %s\n", str);
  54     }
  55     print_location(file, func, line, __FUNCTION__);
  56   }
  57 
  58 } /* test_equal */
  59 
  60 /* Check if val is equal to expected_val, if not it is an error */
  61 void
  62 test_equal_one_of(int val, int expected_val1, int expected_val2, const char *str, 
  63             const char *file, const char *func, int line)
  64 {
  65   int failed = 0;
  66   char const *out = "SUCCESS";
  67 
  68   test_num++;
  69 
  70   if (val != expected_val1 && val != expected_val2) {
  71     num_failures++;
  72     failed = 1;
  73     out = "FAILURE";
  74   }
  75 
  76   if (failed || verbose) {
  77     printf("%s ON TEST = %d : Got %d : expected one of %d or %d\n", 
  78       out, test_num, val, expected_val1, expected_val2);
  79 
  80     if (failed) {
  81       printf("    %s\n", str);
  82     }
  83     print_location(file, func, line, __FUNCTION__);
  84   }
  85 
  86 } /* test_equal */
  87 
  88 /* Check if val is > 0, if not it is an error */
  89 void
  90 test_positive(int val, const char *str,
  91               const char *file, const char *func, int line)
  92 {
  93   int failed = 0;
  94   char const *out = "SUCCESS";
  95 
  96   test_num++;
  97 
  98   if (val <= 0) {
  99     num_failures++;
 100     failed = 1;
 101     out = "FAILURE";
 102   }
 103 
 104   if (failed || verbose) {
 105     printf("%s ON TEST = %d : Got %d : expected Positive Value\n", 
 106       out, test_num, val);
 107 
 108     if (failed) {
 109       printf("    %s\n", str);
 110     }
 111     print_location(file, func, line, __FUNCTION__);
 112   }
 113 }
 114 
 115 /* Check if val is < 0, if not it is an error */
 116 void
 117 test_negative(int val, const char *str,
 118               const char *file, const char *func, int line)
 119 {
 120   int failed = 0;
 121   char const *out = "SUCCESS";
 122 
 123   test_num++;
 124 
 125   if (val >= 0) {
 126     num_failures++;
 127     failed = 1;
 128     out = "FAILURE";
 129   }
 130 
 131   if (failed || verbose) {
 132     printf("%s ON TEST = %d : Got %d : expected Negative Value\n", 
 133       out, test_num, val);
 134 
 135     if (failed) {
 136       printf("    %s\n", str);
 137     }
 138     print_location(file, func, line, __FUNCTION__);
 139   }
 140 }
 141 
 142 /* Check if val1 is not equal to val2, if not it is an error */
 143 void
 144 test_not_equal(int val1, int val2, const char *str,
 145               const char *file, const char *func, int line)
 146 {
 147   int failed = 0;
 148   char const *out = "SUCCESS";
 149 
 150   test_num++;
 151 
 152   if (val1 == val2) {
 153     num_failures++;
 154     failed = 1;
 155     out = "FAILURE";
 156   }
 157 
 158   if (failed || verbose) {
 159     printf("%s ON TEST = %d : Got %d : Expected anything but %d\n", 
 160       out, test_num, val1, val2);
 161 
 162     if (failed) {
 163       printf("    %s\n", str);
 164     }
 165     print_location(file, func, line, __FUNCTION__);
 166   }
 167 }
 168 
 169 /* Reset the statistic counters */
 170 void
 171 test_reset_stats(void)
 172 {
 173   num_failures = 0;
 174   test_num = 0;
 175 }
 176 
 177 void
 178 test_verbose_on(void)
 179 {
 180   printf("TEST VERBOSE ON\n");
 181   verbose = 1;
 182 }
 183 
 184 void
 185 test_verbose_off(void)
 186 {
 187   printf("TEST VERBOSE OFF\n");
 188   verbose = 0;
 189 }
 190 
 191 void
 192 test_print_stats(const char *file, const char* func, int line)
 193 {
 194   printf("TEST STATS for %s : from function = %s, line = %d\n", 
 195     file, func, line);
 196   printf("    Number of failures = %d    Number of successes = %d   Number of Tests = %d\n",
 197     num_failures, test_num - num_failures, test_num);
 198   printf("\n");
 199 }
 200 
 201 // #define UNIT_TEST
 202 #ifdef UNIT_TEST
 203 int
 204 main()
 205 {
 206    TEST_VERBOSE_ON();
 207 
 208    TEST_EQUAL(1, 1, "Should pass\n");
 209    TEST_EQUAL(1, 2, "Should fail\n");
 210    TEST_EQUAL_ONE_OF(1, 1, 2, "Should pass\n");
 211    TEST_EQUAL_ONE_OF(1, 2, 1, "Should pass\n");
 212    TEST_EQUAL_ONE_OF(1, 2, 3, "Should fail\n");
 213    TEST_NOT_EQUAL(1, 2, "Should pass\n");
 214    TEST_NOT_EQUAL(2, 1, "Should pass\n");
 215    TEST_NOT_EQUAL(2, 2, "Should fail\n");
 216 
 217    TEST_NEGATIVE(-1, "Should pass\n");
 218    TEST_NEGATIVE(0, "Should fail\n");
 219    TEST_NEGATIVE(1, "Should fail\n");
 220    TEST_POSITIVE(1, "Should pass\n");
 221    TEST_POSITIVE(0, "Should fail\n");
 222    TEST_POSITIVE(-1, "Should fail\n");
 223 
 224    TEST_STATS();
 225    printf("Should have 7 passes and 7 failures\n");
 226 }
 227 #endif /* UNIT_TEST */

/* [<][>][^][v][top][bottom][index][help] */