changeset 365:c39f8e22ca1c

Refactor testing a bit.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 25 Feb 2016 14:58:36 +0200
parents f2e6e3352f2d
children 21bbb2dc4fac
files tests.c
diffstat 1 files changed, 53 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/tests.c	Thu Feb 25 14:48:25 2016 +0200
+++ b/tests.c	Thu Feb 25 14:58:36 2016 +0200
@@ -6,10 +6,20 @@
 
 
 #define SET_BUF_SIZE           128
+#define SET_BUF_SIZE_2         ((SET_BUF_SIZE) + 32)
 #define SET_MAX_TESTS          64
 #define SET_SENTINEL_BYTE      0x0e5
 
 
+enum
+{
+    TST_SUPERFLUOUS = 0x0001,
+    TST_CORNERCASE  = 0x0002,
+    TST_OVERFLOW    = 0x0004,
+    TST_ALL         = 0xffff,
+};
+
+
 typedef struct
 {
     char *header, *res;
@@ -18,10 +28,11 @@
 
 
 // Globals
-int tests_failed, tests_passed, tests_total, tests_sets, tests_nenabled;
-int tests_enabled[SET_MAX_TESTS];
+int tests_failed, tests_passed, tests_total, sets_total, sets_nenabled;
+int sets_enabled[SET_MAX_TESTS];
 
-char buf1[SET_BUF_SIZE+2], buf2[SET_BUF_SIZE+2];
+char buf1[SET_BUF_SIZE_2], buf2[SET_BUF_SIZE_2];
+int optFlags = TST_ALL;
 
 
 // Define option arguments
@@ -29,7 +40,8 @@
 {
     { 0, '?', "help",       "Show this help", OPT_NONE },
     { 1, 'v', "verbose",    "Be more verbose", OPT_NONE },
-    { 2, 't', "tests",      "Perform tests -t <set>[,<set2>..]", OPT_ARGREQ },
+    { 2, 's', "sets",       "Perform test sets -s <set>[,<set2>..]", OPT_ARGREQ },
+    { 3, 't', "tests",      "Perform only tests (see below)", OPT_ARGREQ },
 };
 
 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]);
@@ -83,7 +95,7 @@
             BOOL ret = TRUE;
             char *pos, *pstr, *next;
             pos = pstr = th_strdup(optArg);
-            memset(tests_enabled, 0, sizeof(tests_enabled));
+            memset(sets_enabled, 0, sizeof(sets_enabled));
 
             do {
                 next = strchr(pos, ',');
@@ -95,7 +107,7 @@
                 {
                     int val = atoi(tmp);
                     if (val > 0 && val <= SET_MAX_TESTS)
-                        tests_enabled[val - 1] = 1;
+                        sets_enabled[val - 1] = 1;
                     else
                     {
                         THERR("Invalid test number #%d, out of range [%d .. %d]\n", val, 1, SET_MAX_TESTS);
@@ -112,6 +124,10 @@
         }
         break;
 
+    case 3:
+        optFlags = FALSE;
+        break;
+
     default:
         THERR("Unknown option '%s'.\n", currArg);
         return FALSE;
@@ -206,8 +222,8 @@
     test_init(&ctx);
     test_start(&ctx, "th_vsnprintf(%" TH_PRIu_SIZE_T ", \"%s\", %s)", len, fmt, msg);
 
-    memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf1[SET_BUF_SIZE+1] = 0;
-    memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE+2); buf2[SET_BUF_SIZE+1] = 0;
+    memset(buf1, SET_SENTINEL_BYTE, SET_BUF_SIZE_2); buf1[SET_BUF_SIZE_2-1] = 0;
+    memset(buf2, SET_SENTINEL_BYTE, SET_BUF_SIZE_2); buf2[SET_BUF_SIZE_2-1] = 0;
 
     va_copy(tmp, ap); ret1 = th_vsnprintf(buf1, len, fmt, tmp);
     va_copy(tmp, ap); ret2 = vsnprintf(buf2, len, fmt, tmp);
@@ -215,8 +231,11 @@
     test_result_msg(&ctx, ret1 == ret2, "retval mismatch %d [th] != %d [libc]", ret1, ret2);
     test_result_msg(&ctx, strcmp(buf1, buf2) == 0, "result mismatch '%s' [th] != '%s' [libc]", buf1, buf2);
 
+    if (optFlags & TST_OVERFLOW)
+    {
     test_result_msg(&ctx, (unsigned char) buf1[len] == SET_SENTINEL_BYTE, "buffer #1 overflow, sentinel 0x%02x", buf1[len]);
     test_result_msg(&ctx, (unsigned char) buf2[len] == SET_SENTINEL_BYTE, "buffer #2 overflow, sentinel 0x%02x", buf2[len]);
+    }
 
     test_end(&ctx);
 }
@@ -227,20 +246,28 @@
     test_ctx ctx;
     va_list ap, tmp;
     va_start(ap, fmt);
-    va_copy(tmp, ap); test_snprintf_do(0, msg, fmt, tmp);
-    va_copy(tmp, ap); test_snprintf_do(1, msg, fmt, tmp);
-    va_copy(tmp, ap); test_snprintf_do(2, msg, fmt, tmp);
-    va_copy(tmp, ap); test_snprintf_do(16, msg, fmt, tmp);
+
+    if (optFlags & TST_CORNERCASE)
+    {
+        va_copy(tmp, ap); test_snprintf_do(0, msg, fmt, tmp);
+        va_copy(tmp, ap); test_snprintf_do(1, msg, fmt, tmp);
+        va_copy(tmp, ap); test_snprintf_do(2, msg, fmt, tmp);
+        va_copy(tmp, ap); test_snprintf_do(16, msg, fmt, tmp);
+    }
+
     va_copy(tmp, ap); test_snprintf_do(SET_BUF_SIZE, msg, fmt, tmp);
 
     // Test th_strdup_vprintf()
-    test_init(&ctx);
-    test_start(&ctx, "th_strdup_vprintf('%s')", fmt);
-    va_copy(tmp, ap);
-    char *str = th_strdup_vprintf(fmt, tmp);
-    test_result_msg(&ctx, str != NULL, "result NULL");
-    th_free(str);
-    test_end(&ctx);
+    if (optFlags & TST_SUPERFLUOUS)
+    {
+        test_init(&ctx);
+        test_start(&ctx, "th_strdup_vprintf('%s')", fmt);
+        va_copy(tmp, ap);
+        char *str = th_strdup_vprintf(fmt, tmp);
+        test_result_msg(&ctx, str != NULL, "result NULL");
+        th_free(str);
+        test_end(&ctx);
+    }
 
     va_end(ap); 
     tprint(2,
@@ -250,14 +277,14 @@
 
 BOOL test_set_start(const char *str)
 {
-    if (tests_enabled[tests_sets++])
+    if (sets_enabled[sets_total++])
     {
-        tests_nenabled++;
+        sets_nenabled++;
         tprint(1,
             "======================================================\n"
             " Set #%d : %s tests\n"
             "======================================================\n",
-            tests_sets, str);
+            sets_total, str);
 
         return TRUE;
     }
@@ -309,9 +336,9 @@
         return -1;
     }
 
-    tests_failed = tests_passed = tests_total = tests_sets = tests_nenabled = 0;
+    tests_failed = tests_passed = tests_total = sets_total = sets_nenabled = 0;
     for (i1 = 0; i1 < SET_MAX_TESTS; i1++)
-        tests_enabled[i1] = 1;
+        sets_enabled[i1] = 1;
 
     //
     // Parse command line arguments
@@ -320,6 +347,7 @@
         arg_handle_opt, NULL, 0))
         return 0;
 
+    tprint(1, "Enabled test types are 0x%04x.\n", optFlags);
 
     //
     // Test series #1
@@ -470,7 +498,7 @@
 
     tprint(0,
         "%d tests failed, %d passed (%d main tests), %d test sets of %d sets total.\n\n",
-        tests_failed, tests_passed, tests_total, tests_nenabled, tests_sets);
+        tests_failed, tests_passed, tests_total, sets_nenabled, sets_total);
 
     return 0;
 }