os161-1.99
 All Data Structures
testutils.c
00001 
00002 /*
00003  * Title    : testutils
00004  * Author   : Tim Brecht
00005  * Date     : Thu Dec 23 21:19:00 EST 2004
00006  * Modified : Wed  1 Jan 2014 14:17:19 EST
00007  *            Added TEST_EQUAL_ONE_OF
00008  *            Fri Jun 28 22:37:24 EDT 2013 for use with OS161
00009  *
00010  * Some simple utilities that can be helpful for testing.
00011  * Feel free to add/modify this code to suit your needs.
00012  * NOTE: this hasn't been fully tested.
00013  *
00014  * See test-testutil, file1 and file2 for example use cases.
00015  */
00016 
00017 #include <stdio.h>
00018 #include "testutils.h"
00019 
00020 /* Used to track the number of tests and failures */
00021 static int test_num = 0;
00022 static int num_failures = 0;
00023 static int verbose = 0;
00024 
00025 /* Use to print the source file, function and line */
00026 static void
00027 print_location(const char *file, const char *func, int line, const char *in_test)
00028 {
00029   printf("   %s : function = %s, line = %d test was %s\n", file, func, line, in_test);
00030 }
00031 
00032 /* Check if val is equal to expected_val, if not it is an error */
00033 void
00034 test_equal(int val, int expected_val, const char *str, 
00035             const char *file, const char *func, int line)
00036 {
00037   int failed = 0;
00038   char const *out = "SUCCESS";
00039 
00040   test_num++;
00041 
00042   if (val != expected_val) {
00043     num_failures++;
00044     failed = 1;
00045     out = "FAILURE";
00046   }
00047 
00048   if (failed || verbose) {
00049     printf("%s ON TEST = %d : Got %d : expected %d\n", 
00050       out, test_num, val, expected_val);
00051 
00052     if (failed) {
00053       printf("    %s\n", str);
00054     }
00055     print_location(file, func, line, __FUNCTION__);
00056   }
00057 
00058 } /* test_equal */
00059 
00060 /* Check if val is equal to expected_val, if not it is an error */
00061 void
00062 test_equal_one_of(int val, int expected_val1, int expected_val2, const char *str, 
00063             const char *file, const char *func, int line)
00064 {
00065   int failed = 0;
00066   char const *out = "SUCCESS";
00067 
00068   test_num++;
00069 
00070   if (val != expected_val1 && val != expected_val2) {
00071     num_failures++;
00072     failed = 1;
00073     out = "FAILURE";
00074   }
00075 
00076   if (failed || verbose) {
00077     printf("%s ON TEST = %d : Got %d : expected one of %d or %d\n", 
00078       out, test_num, val, expected_val1, expected_val2);
00079 
00080     if (failed) {
00081       printf("    %s\n", str);
00082     }
00083     print_location(file, func, line, __FUNCTION__);
00084   }
00085 
00086 } /* test_equal */
00087 
00088 /* Check if val is > 0, if not it is an error */
00089 void
00090 test_positive(int val, const char *str,
00091               const char *file, const char *func, int line)
00092 {
00093   int failed = 0;
00094   char const *out = "SUCCESS";
00095 
00096   test_num++;
00097 
00098   if (val <= 0) {
00099     num_failures++;
00100     failed = 1;
00101     out = "FAILURE";
00102   }
00103 
00104   if (failed || verbose) {
00105     printf("%s ON TEST = %d : Got %d : expected Positive Value\n", 
00106       out, test_num, val);
00107 
00108     if (failed) {
00109       printf("    %s\n", str);
00110     }
00111     print_location(file, func, line, __FUNCTION__);
00112   }
00113 }
00114 
00115 /* Check if val is < 0, if not it is an error */
00116 void
00117 test_negative(int val, const char *str,
00118               const char *file, const char *func, int line)
00119 {
00120   int failed = 0;
00121   char const *out = "SUCCESS";
00122 
00123   test_num++;
00124 
00125   if (val >= 0) {
00126     num_failures++;
00127     failed = 1;
00128     out = "FAILURE";
00129   }
00130 
00131   if (failed || verbose) {
00132     printf("%s ON TEST = %d : Got %d : expected Negative Value\n", 
00133       out, test_num, val);
00134 
00135     if (failed) {
00136       printf("    %s\n", str);
00137     }
00138     print_location(file, func, line, __FUNCTION__);
00139   }
00140 }
00141 
00142 /* Check if val1 is not equal to val2, if not it is an error */
00143 void
00144 test_not_equal(int val1, int val2, const char *str,
00145               const char *file, const char *func, int line)
00146 {
00147   int failed = 0;
00148   char const *out = "SUCCESS";
00149 
00150   test_num++;
00151 
00152   if (val1 == val2) {
00153     num_failures++;
00154     failed = 1;
00155     out = "FAILURE";
00156   }
00157 
00158   if (failed || verbose) {
00159     printf("%s ON TEST = %d : Got %d : Expected anything but %d\n", 
00160       out, test_num, val1, val2);
00161 
00162     if (failed) {
00163       printf("    %s\n", str);
00164     }
00165     print_location(file, func, line, __FUNCTION__);
00166   }
00167 }
00168 
00169 /* Reset the statistic counters */
00170 void
00171 test_reset_stats(void)
00172 {
00173   num_failures = 0;
00174   test_num = 0;
00175 }
00176 
00177 void
00178 test_verbose_on(void)
00179 {
00180   printf("TEST VERBOSE ON\n");
00181   verbose = 1;
00182 }
00183 
00184 void
00185 test_verbose_off(void)
00186 {
00187   printf("TEST VERBOSE OFF\n");
00188   verbose = 0;
00189 }
00190 
00191 void
00192 test_print_stats(const char *file, const char* func, int line)
00193 {
00194   printf("TEST STATS for %s : from function = %s, line = %d\n", 
00195     file, func, line);
00196   printf("    Number of failures = %d    Number of successes = %d   Number of Tests = %d\n",
00197     num_failures, test_num - num_failures, test_num);
00198   printf("\n");
00199 }
00200 
00201 // #define UNIT_TEST
00202 #ifdef UNIT_TEST
00203 int
00204 main()
00205 {
00206    TEST_VERBOSE_ON();
00207 
00208    TEST_EQUAL(1, 1, "Should pass\n");
00209    TEST_EQUAL(1, 2, "Should fail\n");
00210    TEST_EQUAL_ONE_OF(1, 1, 2, "Should pass\n");
00211    TEST_EQUAL_ONE_OF(1, 2, 1, "Should pass\n");
00212    TEST_EQUAL_ONE_OF(1, 2, 3, "Should fail\n");
00213    TEST_NOT_EQUAL(1, 2, "Should pass\n");
00214    TEST_NOT_EQUAL(2, 1, "Should pass\n");
00215    TEST_NOT_EQUAL(2, 2, "Should fail\n");
00216 
00217    TEST_NEGATIVE(-1, "Should pass\n");
00218    TEST_NEGATIVE(0, "Should fail\n");
00219    TEST_NEGATIVE(1, "Should fail\n");
00220    TEST_POSITIVE(1, "Should pass\n");
00221    TEST_POSITIVE(0, "Should fail\n");
00222    TEST_POSITIVE(-1, "Should fail\n");
00223 
00224    TEST_STATS();
00225    printf("Should have 7 passes and 7 failures\n");
00226 }
00227 #endif /* UNIT_TEST */
 All Data Structures