diff tests.c @ 735:31bc1ed07cf5

Renaming BOOL->bool and TRUE/FALSE to true/false, and using stdbool.h if available.
author Matti Hamalainen <ccr@tnsp.org>
date Wed, 07 Dec 2022 12:14:39 +0200
parents 388d72f4189d
children e2873f764b63
line wrap: on
line diff
--- a/tests.c	Wed Dec 07 11:58:54 2022 +0200
+++ b/tests.c	Wed Dec 07 12:14:39 2022 +0200
@@ -30,7 +30,7 @@
 typedef struct
 {
     char *header;
-    BOOL
+    bool
         shown,	// Has test result value been shown?
         failed;	// Did the test fail? (used by some tests to show extra info)
 } test_ctx;
@@ -65,21 +65,21 @@
 static const int arg_nopts = sizeof(arg_opts) / sizeof(arg_opts[0]);
 
 
-BOOL tprintv(const int level, const char *fmt, va_list ap)
+bool tprintv(const int level, const char *fmt, va_list ap)
 {
     if (level <= th_verbosity)
     {
         vfprintf(stdout, fmt, ap);
-        return TRUE;
+        return true;
     }
     else
-        return FALSE;
+        return false;
 }
 
 
-BOOL tprint(const int level, const char *fmt, ...)
+bool tprint(const int level, const char *fmt, ...)
 {
-    BOOL retv;
+    bool retv;
     va_list ap;
     va_start(ap, fmt);
     retv = tprintv(level, fmt, ap);
@@ -95,7 +95,7 @@
 }
 
 
-BOOL arg_handle_opt(const int optN, char *optArg, char *currArg)
+bool arg_handle_opt(const int optN, char *optArg, char *currArg)
 {
     switch (optN)
     {
@@ -110,7 +110,7 @@
 
     case 2:
         {
-            BOOL ret = TRUE;
+            bool ret = true;
             char *pos, *pstr, *next;
             pos = pstr = th_strdup(optArg);
             memset(sets_enabled, 0, sizeof(sets_enabled));
@@ -128,7 +128,7 @@
                     else
                     {
                         THERR("Invalid test number #%d, out of range [%d .. %d]\n", val, 1, SET_MAX_TESTS);
-                        ret = FALSE;
+                        ret = false;
                     }
                     th_free(tmp);
                 }
@@ -147,10 +147,10 @@
 
     default:
         THERR("Unknown option '%s'.\n", currArg);
-        return FALSE;
+        return false;
     }
 
-    return TRUE;
+    return true;
 }
 
 
@@ -177,19 +177,19 @@
 }
 
 
-void test_result_msg_v(test_ctx *ctx, BOOL check, const char *fmt, va_list ap)
+void test_result_msg_v(test_ctx *ctx, bool check, const char *fmt, va_list ap)
 {
     if (check)
     {
         if (!ctx->shown && tprint(2, "%s: OK\n", ctx->header))
-            ctx->shown = TRUE;
+            ctx->shown = true;
 
         tests_passed++;
     }
     else
     {
         if (!ctx->shown && tprint(0, "%s: FAIL\n", ctx->header))
-            ctx->shown = TRUE;
+            ctx->shown = true;
 
         if (fmt != NULL)
         {
@@ -199,12 +199,12 @@
         }
         tests_failed++;
 
-        ctx->failed = TRUE;
+        ctx->failed = true;
     }
 }
 
 
-BOOL test_result_msg(test_ctx *ctx, BOOL check, const char *fmt, ...)
+bool test_result_msg(test_ctx *ctx, bool check, const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
@@ -214,7 +214,7 @@
 }
 
 
-BOOL test_result(test_ctx *ctx, BOOL check)
+bool test_result(test_ctx *ctx, bool check)
 {
     test_result_msg_v(ctx, check, NULL, NULL);
     return check;
@@ -251,7 +251,7 @@
 
     if (ctx.failed && !th_printf_debug && th_verbosity >= 1)
     {
-        th_printf_debug = TRUE;
+        th_printf_debug = true;
         va_copy(tmp, ap); ret1 = th_vsnprintf(buf1, len, fmt, tmp);
         va_copy(tmp, ap); ret2 = vsnprintf(buf2, len, fmt, tmp);
     }
@@ -293,7 +293,7 @@
 }
 
 
-BOOL test_set_start(const char *str)
+bool test_set_start(const char *str)
 {
     if (sets_enabled[sets_total++])
     {
@@ -304,10 +304,10 @@
             "======================================================\n",
             sets_total, str);
 
-        return TRUE;
+        return true;
     }
     else
-        return FALSE;
+        return false;
 }
 
 
@@ -498,7 +498,7 @@
     char *v_str1 = NULL;
     unsigned int v_uint1;
     int v_int1;
-    BOOL v_bool1, v_bool2;
+    bool v_bool1, v_bool2;
     th_llist_t *v_str_list = NULL;
 
     // Create v_str_list
@@ -515,7 +515,7 @@
     th_cfg_add_hexvalue(&sect1, "hexval", &v_uint1, 0x11223344);
 
     th_cfg_add_comment(&sect1, "A boolean value");
-    th_cfg_add_bool(&sect1, "boolval", &v_bool1, FALSE);
+    th_cfg_add_bool(&sect1, "boolval", &v_bool1, false);
 
     th_cfg_add_comment(&sect1, "A string list");
     th_cfg_add_string_list(&sect1, "string_list", &v_str_list);
@@ -524,7 +524,7 @@
 
     sect1 = NULL;
     th_cfg_add_comment(&sect1, "Another section");
-    th_cfg_add_bool(&sect1, "boolval", &v_bool2, TRUE);
+    th_cfg_add_bool(&sect1, "boolval", &v_bool2, true);
 
     sect2 = NULL;
     th_cfg_add_comment(&sect2, "Section inside a section");
@@ -591,10 +591,10 @@
 } test_regex_def2;
 
 
-BOOL test_regex_list_matches(const th_char_t *str,
+bool test_regex_list_matches(const th_char_t *str,
     const th_regex_match_t *matches,
     const th_char_t * const *expected,
-    const BOOL testOnly)
+    const bool testOnly)
 {
     size_t nmatch = 0;
     char *match = NULL;
@@ -618,7 +618,7 @@
         }
         else
         {
-            BOOL seqMatch = strcmp(match, expected[nmatch]) == 0;
+            bool seqMatch = strcmp(match, expected[nmatch]) == 0;
             if (testOnly && !seqMatch)
                 goto error;
 
@@ -636,11 +636,11 @@
         th_free(match);
     }
 
-    return TRUE;
+    return true;
 
 error:
     th_free(match);
-    return FALSE;
+    return false;
 }
 
 
@@ -667,7 +667,7 @@
     for (const test_regex_def1 *def = list; def->str != NULL; def++)
     {
         size_t nmatches;
-        BOOL matchOK;
+        bool matchOK;
 
         tprint(3, "\n----------------------------------------\n");
         if ((res = th_regex_match(expr, def->str,
@@ -679,7 +679,7 @@
             goto out;
         }
 
-        matchOK = test_regex_list_matches(def->str, matches, def->expected, TRUE);
+        matchOK = test_regex_list_matches(def->str, matches, def->expected, true);
         if (th_verbosity < 1 && !matchOK)
         {
             tprint(0,
@@ -703,7 +703,7 @@
         }
 #endif
 
-        test_regex_list_matches(def->str, matches, def->expected, FALSE);
+        test_regex_list_matches(def->str, matches, def->expected, false);
 
         th_regex_free_matches(matches);
         matches = NULL;
@@ -749,7 +749,7 @@
             def->pattern, def->str,
             nmatches);
 
-        test_regex_list_matches(def->str, matches, def->expected, FALSE);
+        test_regex_list_matches(def->str, matches, def->expected, false);
 
 out:
         th_regex_free_matches(matches);
@@ -931,50 +931,52 @@
     //
     if (test_set_start("String compare #1"))
     {
-        TEST2(th_strcasecmp, "aSdFq", "asdfq", TRUE);
-        TEST2(th_strcasecmp, "aSdFq", "asFfq", FALSE);
-        TEST2(th_strcasecmp, "abcde", "abcde", TRUE);
-        TEST2(th_strcasecmp, "öäå", "öäå", TRUE);
-        TEST2(th_strcasecmp, "aöäå", "aöäå", TRUE);
+        TEST2(th_strcasecmp, "aSdFq", "asdfq", true);
+        TEST2(th_strcasecmp, "aSdFq", "asFfq", false);
+        TEST2(th_strcasecmp, "abcde", "abcde", true);
+        TEST2(th_strcasecmp, "öäå", "öäå", true);
+        TEST2(th_strcasecmp, "aöäå", "aöäå", true);
     }
 
     if (test_set_start("String compare #2"))
     {
-        TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, FALSE);
-        TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, TRUE);
-        TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, TRUE);
-        TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, TRUE);
-        TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, TRUE);
-        TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, TRUE);
-        TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, FALSE);
+        TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, false);
+        TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, true);
+        TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, true);
+        TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, true);
+        TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, true);
+        TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, true);
+        TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, false);
+        TEST3(th_strncasecmp, "max=", "max=4", 4, true);
+        TEST3(th_strncasecmp, "max=", "max", 4, false);
     }
 
     if (test_set_start("String compare #3"))
     {
-        TEST2C(th_strrcasecmp, "foo aSdFq", " asdfq", TRUE);
-        TEST2C(th_strrcasecmp, "aSdFq", " asdfq", FALSE);
-        TEST2C(th_strrcasecmp, "foo aSdFq baz", "asdfq", FALSE);
+        TEST2C(th_strrcasecmp, "foo aSdFq", " asdfq", true);
+        TEST2C(th_strrcasecmp, "aSdFq", " asdfq", false);
+        TEST2C(th_strrcasecmp, "foo aSdFq baz", "asdfq", false);
     }
 
     if (test_set_start("String matching #1"))
     {
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", TRUE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", TRUE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", FALSE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", FALSE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", TRUE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", TRUE);
-        TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", FALSE);
-        TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", FALSE);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", true);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", true);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", false);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", false);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", true);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", true);
+        TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", false);
+        TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", false);
     }
 
     if (test_set_start("String matching #2"))
     {
-        TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", FALSE);
-        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", TRUE);
-        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", TRUE);
-        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", FALSE);
-        TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", TRUE);
+        TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", false);
+        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", true);
+        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", true);
+        TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", false);
+        TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", true);
     }
 
     // Tests that test for things that do not work correctly yet
@@ -982,9 +984,9 @@
     if ((optFlags & TST_BROKEN) &&
         test_set_start("Invalid UTF-8 handling"))
     {
-        TEST2(th_strcasecmp, "ÖÄÅ", "öäå", FALSE); // SHOULD match
-        TEST3(th_strncasecmp, "Aäöå", "aöå", 2, TRUE); // should NOT match
-        TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", FALSE); // should match
+        TEST2(th_strcasecmp, "ÖÄÅ", "öäå", false); // SHOULD match
+        TEST3(th_strncasecmp, "Aäöå", "aöå", 2, true); // should NOT match
+        TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match
     }
 
     //
@@ -1006,15 +1008,15 @@
 
         snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz);
 #if TH_ARCH == 32
-        TEST2(strcmp, tmp, "0000000011223344h", TRUE);
+        TEST2(strcmp, tmp, "0000000011223344h", true);
 #else
-        TEST2(strcmp, tmp, "aabbccdd11223344h", TRUE);
+        TEST2(strcmp, tmp, "aabbccdd11223344h", true);
 #endif
 
         snprintf(tmp, sizeof(tmp), "%08" PRIx32 "h", u32);
-        TEST2(strcmp, tmp, "aabbccddh", TRUE);
+        TEST2(strcmp, tmp, "aabbccddh", true);
         snprintf(tmp, sizeof(tmp), "%16" PRIx64 "h", u64);
-        TEST2(strcmp, tmp, "aabbccdd11223344h", TRUE);
+        TEST2(strcmp, tmp, "aabbccdd11223344h", true);
     }
 
     //
@@ -1032,11 +1034,11 @@
     {
         unsigned int tmpUint;
 
-        TEST1(th_get_hex_triplet("0fac11", &tmpUint) == TRUE);
+        TEST1(th_get_hex_triplet("0fac11", &tmpUint) == true);
         TEST1A("0x%06x", tmpUint, ==, 0x0fac11);
-        TEST1(th_get_hex_triplet("120fac11", &tmpUint) == TRUE);
+        TEST1(th_get_hex_triplet("120fac11", &tmpUint) == true);
         TEST1A("0x%06x", tmpUint, ==, 0x120fac11);
-        TEST1(th_get_hex_triplet("x120fac11", &tmpUint) == FALSE);
+        TEST1(th_get_hex_triplet("x120fac11", &tmpUint) == false);
     }
 
     //