# HG changeset patch # User Matti Hamalainen # Date 1675979940 -7200 # Node ID 0abba6091bd1a218637b12de5751e48209f7a00f # Parent c6b2546d3ae3b3c7065d8b10732206ebef462ced Clean up the tests a bit, rename macros, cosmetics. diff -r c6b2546d3ae3 -r 0abba6091bd1 tests.c --- a/tests.c Thu Feb 09 23:55:04 2023 +0200 +++ b/tests.c Thu Feb 09 23:59:00 2023 +0200 @@ -320,48 +320,55 @@ } \ } while (0) -#define TEST1(fun) do { \ +#define TEST_1A(fun) do { \ test_ctx ctx; \ test_start(&ctx, # fun ); \ test_result(&ctx, fun); \ test_end(&ctx); \ } while (0) -#define TEST1A(fmt, fun, fcmp, fres) do { \ +#define TEST_1B(fmt, fun, fcmp, fres) do { \ test_ctx ctx; \ test_start(&ctx, #fun " " #fcmp " " fmt " (" fmt ")", fres, fun); \ test_result(&ctx, fun fcmp fres ); \ test_end(&ctx); \ } while (0) -#define TEST2(fun, str1, str2, ret) do { \ +#define TEST_2A(fun, str1, str2, ret) do { \ test_ctx ctx; \ test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ test_result(&ctx, ( fun (str1, str2) == 0) == ret); \ test_end(&ctx); \ } while (0) -#define TEST2B(fun, str1, str2, ret) do { \ +#define TEST_2B(fun, str1, str2, ret) do { \ test_ctx ctx; \ test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ test_result(&ctx, fun (str1, str2) == ret); \ test_end(&ctx); \ } while (0) -#define TEST2C(fun, str1, str2, ret) do { \ +#define TEST_2C(fun, str1, str2, ret) do { \ test_ctx ctx; \ test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ test_result(&ctx, (fun (str1, str2) != NULL) == ret); \ test_end(&ctx); \ } while (0) -#define TEST3(fun, str1, str2, len, ret) do { \ +#define TEST_3A(fun, str1, str2, len, ret) do { \ test_ctx ctx; \ test_start(&ctx, # fun "('%s', '%s', %d)", str1, str2, len); \ test_result(&ctx, ( fun (str1, str2, len) == 0) == ret); \ test_end(&ctx); \ } while (0) +#define TEST_3B(fun, str1, str2, sbool, ret) do { \ + test_ctx ctx; \ + test_start(&ctx, # fun "('%s', '%s', %s)", str1, str2, sbool ? "true" : "false"); \ + test_result(&ctx, fun (str1, str2, sbool) == ret); \ + test_end(&ctx); \ + } while (0) + void test_config_values(th_cfgitem_t *cfg) { @@ -912,23 +919,23 @@ int res; #endif - TEST1A("%s", home, !=, NULL); - TEST1A("%s", cfg, !=, NULL); + TEST_1B("%s", home, !=, NULL); + TEST_1B("%s", cfg, !=, NULL); - TEST1(th_stat_path(home, &stat) == true); - TEST1A("%d", (stat.flags & TH_IS_DIR), !=, 0); + TEST_1A(th_stat_path(home, &stat) == true); + TEST_1B("%d", (stat.flags & TH_IS_DIR), !=, 0); #ifdef TH_PLAT_UNIX - TEST1(th_stat_path("/nonexist", &stat) == false); - TEST1(th_stat_path("/root", &stat) == true); - TEST1A("%d", (stat.flags & TH_IS_WRITABLE), ==, 0); + TEST_1A(th_stat_path("/nonexist", &stat) == false); + TEST_1A(th_stat_path("/root", &stat) == true); + TEST_1B("%d", (stat.flags & TH_IS_WRITABLE), ==, 0); - TEST1((res = th_mkdir_path("/tmp/thlibtest/foobar/baz", 0)) == THERR_OK); - TEST1A("%d", res, ==, THERR_OK); + TEST_1A((res = th_mkdir_path("/tmp/thlibtest/foobar/baz", 0)) == THERR_OK); + TEST_1B("%d", res, ==, THERR_OK); if (res == THERR_OK) { - TEST1(th_stat_path("/tmp/thlibtest/foobar/baz", &stat) == true); - TEST1A("%d", (stat.flags & TH_IS_DIR), !=, 0); + TEST_1A(th_stat_path("/tmp/thlibtest/foobar/baz", &stat) == true); + TEST_1B("%d", (stat.flags & TH_IS_DIR), !=, 0); } #endif } @@ -1069,52 +1076,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); + TEST_2A(th_strcasecmp, "aSdFq" , "asdfq" , true); + TEST_2A(th_strcasecmp, "aSdFq" , "asFfq" , false); + TEST_2A(th_strcasecmp, "abcde" , "abcde" , true); + TEST_2A(th_strcasecmp, "öäå" , "öäå" , true); + TEST_2A(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, "max=", "max=4", 4, true); - TEST3(th_strncasecmp, "max=", "max", 4, false); + TEST_3A(th_strncasecmp, "aSdFq" , "asFfqB" , 4, false); + TEST_3A(th_strncasecmp, "aSdFq" , "asFfqQ" , 2, true); + TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 3, true); + TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 2, true); + TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 0, true); + TEST_3A(th_strncasecmp, "aSdFq" , "QsDfq" , 0, true); + TEST_3A(th_strncasecmp, "aSdFq" , "QsDfq" , 1, false); + TEST_3A(th_strncasecmp, "max=" , "max=4" , 4, true); + TEST_3A(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); + TEST_2C(th_strrcasecmp, "foo aSdFq", " asdfq", true); + TEST_2C(th_strrcasecmp, "aSdFq", " asdfq", false); + TEST_2C(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); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lol" , true); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lo*" , true); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lo" , false); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abba" , false); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*bba*" , true); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abba*" , true); + TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abbak*" , false); + TEST_2B(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); + TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "abbak*" , false); + TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*abbak*" , true); + TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*ab?ak*" , true); + TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*abbak?" , false); + TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "?bba?abba*" , true); } // Tests that test for things that do not work correctly yet @@ -1122,9 +1129,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 + TEST_2A(th_strcasecmp, "ÖÄÅ", "öäå", false); // if it worked, SHOULD match + TEST_3A(th_strncasecmp, "Aäöå", "aöå", 2, true); // if worked, it should NOT match + TEST_2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match } // @@ -1146,15 +1153,15 @@ snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz); #if TH_ARCH == 32 - TEST2(strcmp, tmp, "0000000011223344h", true); + TEST_2A(strcmp, tmp, "0000000011223344h", true); #else - TEST2(strcmp, tmp, "aabbccdd11223344h", true); + TEST_2A(strcmp, tmp, "aabbccdd11223344h", true); #endif snprintf(tmp, sizeof(tmp), "%08" PRIx32 "h", u32); - TEST2(strcmp, tmp, "aabbccddh", true); + TEST_2A(strcmp, tmp, "aabbccddh", true); snprintf(tmp, sizeof(tmp), "%16" PRIx64 "h", u64); - TEST2(strcmp, tmp, "aabbccdd11223344h", true); + TEST_2A(strcmp, tmp, "aabbccdd11223344h", true); } // @@ -1179,11 +1186,11 @@ { unsigned int tmpUint; - TEST1(th_get_hex_triplet("0fac11", &tmpUint) == true); - TEST1A("0x%06x", tmpUint, ==, 0x0fac11); - TEST1(th_get_hex_triplet("120fac11", &tmpUint) == true); - TEST1A("0x%06x", tmpUint, ==, 0x120fac11); - TEST1(th_get_hex_triplet("x120fac11", &tmpUint) == false); + TEST_1A(th_get_hex_triplet("0fac11", &tmpUint) == true); + TEST_1B("0x%06x", tmpUint, ==, 0x0fac11); + TEST_1A(th_get_hex_triplet("120fac11", &tmpUint) == true); + TEST_1B("0x%06x", tmpUint, ==, 0x120fac11); + TEST_1A(th_get_hex_triplet("x120fac11", &tmpUint) == false); } //