00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include <stdio.h>
00018 #include "testutils.h"
00019
00020
00021 static int test_num = 0;
00022 static int num_failures = 0;
00023 static int verbose = 0;
00024
00025
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
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 }
00059
00060
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 }
00087
00088
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
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
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
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
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