Mercurial > hg > th-libs
comparison tests.c @ 764:0abba6091bd1
Clean up the tests a bit, rename macros, cosmetics.
author | Matti Hamalainen <ccr@tnsp.org> |
---|---|
date | Thu, 09 Feb 2023 23:59:00 +0200 |
parents | 1cb9454ec569 |
children | 600a3c08747f |
comparison
equal
deleted
inserted
replaced
763:c6b2546d3ae3 | 764:0abba6091bd1 |
---|---|
318 THERR("Assertion '" # xcond "' failed.\n"); \ | 318 THERR("Assertion '" # xcond "' failed.\n"); \ |
319 return -1; \ | 319 return -1; \ |
320 } \ | 320 } \ |
321 } while (0) | 321 } while (0) |
322 | 322 |
323 #define TEST1(fun) do { \ | 323 #define TEST_1A(fun) do { \ |
324 test_ctx ctx; \ | 324 test_ctx ctx; \ |
325 test_start(&ctx, # fun ); \ | 325 test_start(&ctx, # fun ); \ |
326 test_result(&ctx, fun); \ | 326 test_result(&ctx, fun); \ |
327 test_end(&ctx); \ | 327 test_end(&ctx); \ |
328 } while (0) | 328 } while (0) |
329 | 329 |
330 #define TEST1A(fmt, fun, fcmp, fres) do { \ | 330 #define TEST_1B(fmt, fun, fcmp, fres) do { \ |
331 test_ctx ctx; \ | 331 test_ctx ctx; \ |
332 test_start(&ctx, #fun " " #fcmp " " fmt " (" fmt ")", fres, fun); \ | 332 test_start(&ctx, #fun " " #fcmp " " fmt " (" fmt ")", fres, fun); \ |
333 test_result(&ctx, fun fcmp fres ); \ | 333 test_result(&ctx, fun fcmp fres ); \ |
334 test_end(&ctx); \ | 334 test_end(&ctx); \ |
335 } while (0) | 335 } while (0) |
336 | 336 |
337 #define TEST2(fun, str1, str2, ret) do { \ | 337 #define TEST_2A(fun, str1, str2, ret) do { \ |
338 test_ctx ctx; \ | 338 test_ctx ctx; \ |
339 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ | 339 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ |
340 test_result(&ctx, ( fun (str1, str2) == 0) == ret); \ | 340 test_result(&ctx, ( fun (str1, str2) == 0) == ret); \ |
341 test_end(&ctx); \ | 341 test_end(&ctx); \ |
342 } while (0) | 342 } while (0) |
343 | 343 |
344 #define TEST2B(fun, str1, str2, ret) do { \ | 344 #define TEST_2B(fun, str1, str2, ret) do { \ |
345 test_ctx ctx; \ | 345 test_ctx ctx; \ |
346 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ | 346 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ |
347 test_result(&ctx, fun (str1, str2) == ret); \ | 347 test_result(&ctx, fun (str1, str2) == ret); \ |
348 test_end(&ctx); \ | 348 test_end(&ctx); \ |
349 } while (0) | 349 } while (0) |
350 | 350 |
351 #define TEST2C(fun, str1, str2, ret) do { \ | 351 #define TEST_2C(fun, str1, str2, ret) do { \ |
352 test_ctx ctx; \ | 352 test_ctx ctx; \ |
353 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ | 353 test_start(&ctx, # fun "('%s', '%s')", str1, str2); \ |
354 test_result(&ctx, (fun (str1, str2) != NULL) == ret); \ | 354 test_result(&ctx, (fun (str1, str2) != NULL) == ret); \ |
355 test_end(&ctx); \ | 355 test_end(&ctx); \ |
356 } while (0) | 356 } while (0) |
357 | 357 |
358 #define TEST3(fun, str1, str2, len, ret) do { \ | 358 #define TEST_3A(fun, str1, str2, len, ret) do { \ |
359 test_ctx ctx; \ | 359 test_ctx ctx; \ |
360 test_start(&ctx, # fun "('%s', '%s', %d)", str1, str2, len); \ | 360 test_start(&ctx, # fun "('%s', '%s', %d)", str1, str2, len); \ |
361 test_result(&ctx, ( fun (str1, str2, len) == 0) == ret); \ | 361 test_result(&ctx, ( fun (str1, str2, len) == 0) == ret); \ |
362 test_end(&ctx); \ | |
363 } while (0) | |
364 | |
365 #define TEST_3B(fun, str1, str2, sbool, ret) do { \ | |
366 test_ctx ctx; \ | |
367 test_start(&ctx, # fun "('%s', '%s', %s)", str1, str2, sbool ? "true" : "false"); \ | |
368 test_result(&ctx, fun (str1, str2, sbool) == ret); \ | |
362 test_end(&ctx); \ | 369 test_end(&ctx); \ |
363 } while (0) | 370 } while (0) |
364 | 371 |
365 | 372 |
366 void test_config_values(th_cfgitem_t *cfg) | 373 void test_config_values(th_cfgitem_t *cfg) |
910 th_stat_data stat; | 917 th_stat_data stat; |
911 #ifdef TH_PLAT_UNIX | 918 #ifdef TH_PLAT_UNIX |
912 int res; | 919 int res; |
913 #endif | 920 #endif |
914 | 921 |
915 TEST1A("%s", home, !=, NULL); | 922 TEST_1B("%s", home, !=, NULL); |
916 TEST1A("%s", cfg, !=, NULL); | 923 TEST_1B("%s", cfg, !=, NULL); |
917 | 924 |
918 TEST1(th_stat_path(home, &stat) == true); | 925 TEST_1A(th_stat_path(home, &stat) == true); |
919 TEST1A("%d", (stat.flags & TH_IS_DIR), !=, 0); | 926 TEST_1B("%d", (stat.flags & TH_IS_DIR), !=, 0); |
920 | 927 |
921 #ifdef TH_PLAT_UNIX | 928 #ifdef TH_PLAT_UNIX |
922 TEST1(th_stat_path("/nonexist", &stat) == false); | 929 TEST_1A(th_stat_path("/nonexist", &stat) == false); |
923 TEST1(th_stat_path("/root", &stat) == true); | 930 TEST_1A(th_stat_path("/root", &stat) == true); |
924 TEST1A("%d", (stat.flags & TH_IS_WRITABLE), ==, 0); | 931 TEST_1B("%d", (stat.flags & TH_IS_WRITABLE), ==, 0); |
925 | 932 |
926 TEST1((res = th_mkdir_path("/tmp/thlibtest/foobar/baz", 0)) == THERR_OK); | 933 TEST_1A((res = th_mkdir_path("/tmp/thlibtest/foobar/baz", 0)) == THERR_OK); |
927 TEST1A("%d", res, ==, THERR_OK); | 934 TEST_1B("%d", res, ==, THERR_OK); |
928 if (res == THERR_OK) | 935 if (res == THERR_OK) |
929 { | 936 { |
930 TEST1(th_stat_path("/tmp/thlibtest/foobar/baz", &stat) == true); | 937 TEST_1A(th_stat_path("/tmp/thlibtest/foobar/baz", &stat) == true); |
931 TEST1A("%d", (stat.flags & TH_IS_DIR), !=, 0); | 938 TEST_1B("%d", (stat.flags & TH_IS_DIR), !=, 0); |
932 } | 939 } |
933 #endif | 940 #endif |
934 } | 941 } |
935 | 942 |
936 // | 943 // |
1067 // | 1074 // |
1068 // String matching functions | 1075 // String matching functions |
1069 // | 1076 // |
1070 if (test_set_start("String compare #1")) | 1077 if (test_set_start("String compare #1")) |
1071 { | 1078 { |
1072 TEST2(th_strcasecmp, "aSdFq", "asdfq", true); | 1079 TEST_2A(th_strcasecmp, "aSdFq" , "asdfq" , true); |
1073 TEST2(th_strcasecmp, "aSdFq", "asFfq", false); | 1080 TEST_2A(th_strcasecmp, "aSdFq" , "asFfq" , false); |
1074 TEST2(th_strcasecmp, "abcde", "abcde", true); | 1081 TEST_2A(th_strcasecmp, "abcde" , "abcde" , true); |
1075 TEST2(th_strcasecmp, "öäå", "öäå", true); | 1082 TEST_2A(th_strcasecmp, "öäå" , "öäå" , true); |
1076 TEST2(th_strcasecmp, "aöäå", "aöäå", true); | 1083 TEST_2A(th_strcasecmp, "aöäå" , "aöäå" , true); |
1077 } | 1084 } |
1078 | 1085 |
1079 if (test_set_start("String compare #2")) | 1086 if (test_set_start("String compare #2")) |
1080 { | 1087 { |
1081 TEST3(th_strncasecmp, "aSdFq", "asFfqB", 4, false); | 1088 TEST_3A(th_strncasecmp, "aSdFq" , "asFfqB" , 4, false); |
1082 TEST3(th_strncasecmp, "aSdFq", "asFfqQ", 2, true); | 1089 TEST_3A(th_strncasecmp, "aSdFq" , "asFfqQ" , 2, true); |
1083 TEST3(th_strncasecmp, "aSdFq", "asDfq", 3, true); | 1090 TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 3, true); |
1084 TEST3(th_strncasecmp, "aSdFq", "asDfq", 2, true); | 1091 TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 2, true); |
1085 TEST3(th_strncasecmp, "aSdFq", "asDfq", 0, true); | 1092 TEST_3A(th_strncasecmp, "aSdFq" , "asDfq" , 0, true); |
1086 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 0, true); | 1093 TEST_3A(th_strncasecmp, "aSdFq" , "QsDfq" , 0, true); |
1087 TEST3(th_strncasecmp, "aSdFq", "QsDfq", 1, false); | 1094 TEST_3A(th_strncasecmp, "aSdFq" , "QsDfq" , 1, false); |
1088 TEST3(th_strncasecmp, "max=", "max=4", 4, true); | 1095 TEST_3A(th_strncasecmp, "max=" , "max=4" , 4, true); |
1089 TEST3(th_strncasecmp, "max=", "max", 4, false); | 1096 TEST_3A(th_strncasecmp, "max=" , "max" , 4, false); |
1090 } | 1097 } |
1091 | 1098 |
1092 if (test_set_start("String compare #3")) | 1099 if (test_set_start("String compare #3")) |
1093 { | 1100 { |
1094 TEST2C(th_strrcasecmp, "foo aSdFq", " asdfq", true); | 1101 TEST_2C(th_strrcasecmp, "foo aSdFq", " asdfq", true); |
1095 TEST2C(th_strrcasecmp, "aSdFq", " asdfq", false); | 1102 TEST_2C(th_strrcasecmp, "aSdFq", " asdfq", false); |
1096 TEST2C(th_strrcasecmp, "foo aSdFq baz", "asdfq", false); | 1103 TEST_2C(th_strrcasecmp, "foo aSdFq baz", "asdfq", false); |
1097 } | 1104 } |
1098 | 1105 |
1099 if (test_set_start("String matching #1")) | 1106 if (test_set_start("String matching #1")) |
1100 { | 1107 { |
1101 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lol", true); | 1108 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lol" , true); |
1102 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo*", true); | 1109 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lo*" , true); |
1103 TEST2B(th_strmatch, "abba ABBAkukka lol", "*lo", false); | 1110 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*lo" , false); |
1104 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba", false); | 1111 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abba" , false); |
1105 TEST2B(th_strmatch, "abba ABBAkukka lol", "*bba*", true); | 1112 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "*bba*" , true); |
1106 TEST2B(th_strmatch, "abba ABBAkukka lol", "abba*", true); | 1113 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abba*" , true); |
1107 TEST2B(th_strmatch, "abba ABBAkukka lol", "abbak*", false); | 1114 TEST_2B(th_strmatch, "abba ABBAkukka lol" , "abbak*" , false); |
1108 TEST2B(th_strmatch, "abba ABBAöökukka lol", "*abbaö?", false); | 1115 TEST_2B(th_strmatch, "abba ABBAöökukka lol" , "*abbaö?" , false); |
1109 } | 1116 } |
1110 | 1117 |
1111 if (test_set_start("String matching #2")) | 1118 if (test_set_start("String matching #2")) |
1112 { | 1119 { |
1113 TEST2B(th_strcasematch, "abba ABBAkukka lol", "abbak*", false); | 1120 TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "abbak*" , false); |
1114 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak*", true); | 1121 TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*abbak*" , true); |
1115 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*ab?ak*", true); | 1122 TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*ab?ak*" , true); |
1116 TEST2B(th_strcasematch, "abba ABBAkukka lol", "*abbak?", false); | 1123 TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "*abbak?" , false); |
1117 TEST2B(th_strcasematch, "abba ABBAkukka lol", "?bba?abba*", true); | 1124 TEST_2B(th_strcasematch, "abba ABBAkukka lol" , "?bba?abba*" , true); |
1118 } | 1125 } |
1119 | 1126 |
1120 // Tests that test for things that do not work correctly yet | 1127 // Tests that test for things that do not work correctly yet |
1121 // Unicode / multibyte UTF-8 causes problems here | 1128 // Unicode / multibyte UTF-8 causes problems here |
1122 if ((optFlags & TST_BROKEN) && | 1129 if ((optFlags & TST_BROKEN) && |
1123 test_set_start("Invalid UTF-8 handling")) | 1130 test_set_start("Invalid UTF-8 handling")) |
1124 { | 1131 { |
1125 TEST2(th_strcasecmp, "ÖÄÅ", "öäå", false); // SHOULD match | 1132 TEST_2A(th_strcasecmp, "ÖÄÅ", "öäå", false); // if it worked, SHOULD match |
1126 TEST3(th_strncasecmp, "Aäöå", "aöå", 2, true); // should NOT match | 1133 TEST_3A(th_strncasecmp, "Aäöå", "aöå", 2, true); // if worked, it should NOT match |
1127 TEST2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match | 1134 TEST_2B(th_strmatch, "öriÖRI! lol", "?ri?RI!*", false); // should match |
1128 } | 1135 } |
1129 | 1136 |
1130 // | 1137 // |
1131 // printf() PRI* format specifiers, also a compile time test | 1138 // printf() PRI* format specifiers, also a compile time test |
1132 // | 1139 // |
1144 # error Unsupported TH_ARCH value. | 1151 # error Unsupported TH_ARCH value. |
1145 #endif | 1152 #endif |
1146 | 1153 |
1147 snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz); | 1154 snprintf(tmp, sizeof(tmp), "%16" PRIx_SIZE_T "h", usiz); |
1148 #if TH_ARCH == 32 | 1155 #if TH_ARCH == 32 |
1149 TEST2(strcmp, tmp, "0000000011223344h", true); | 1156 TEST_2A(strcmp, tmp, "0000000011223344h", true); |
1150 #else | 1157 #else |
1151 TEST2(strcmp, tmp, "aabbccdd11223344h", true); | 1158 TEST_2A(strcmp, tmp, "aabbccdd11223344h", true); |
1152 #endif | 1159 #endif |
1153 | 1160 |
1154 snprintf(tmp, sizeof(tmp), "%08" PRIx32 "h", u32); | 1161 snprintf(tmp, sizeof(tmp), "%08" PRIx32 "h", u32); |
1155 TEST2(strcmp, tmp, "aabbccddh", true); | 1162 TEST_2A(strcmp, tmp, "aabbccddh", true); |
1156 snprintf(tmp, sizeof(tmp), "%16" PRIx64 "h", u64); | 1163 snprintf(tmp, sizeof(tmp), "%16" PRIx64 "h", u64); |
1157 TEST2(strcmp, tmp, "aabbccdd11223344h", true); | 1164 TEST_2A(strcmp, tmp, "aabbccdd11223344h", true); |
1158 } | 1165 } |
1159 | 1166 |
1160 // | 1167 // |
1161 // Configuration file handling | 1168 // Configuration file handling |
1162 // | 1169 // |
1177 // | 1184 // |
1178 if (test_set_start("String functions")) | 1185 if (test_set_start("String functions")) |
1179 { | 1186 { |
1180 unsigned int tmpUint; | 1187 unsigned int tmpUint; |
1181 | 1188 |
1182 TEST1(th_get_hex_triplet("0fac11", &tmpUint) == true); | 1189 TEST_1A(th_get_hex_triplet("0fac11", &tmpUint) == true); |
1183 TEST1A("0x%06x", tmpUint, ==, 0x0fac11); | 1190 TEST_1B("0x%06x", tmpUint, ==, 0x0fac11); |
1184 TEST1(th_get_hex_triplet("120fac11", &tmpUint) == true); | 1191 TEST_1A(th_get_hex_triplet("120fac11", &tmpUint) == true); |
1185 TEST1A("0x%06x", tmpUint, ==, 0x120fac11); | 1192 TEST_1B("0x%06x", tmpUint, ==, 0x120fac11); |
1186 TEST1(th_get_hex_triplet("x120fac11", &tmpUint) == false); | 1193 TEST_1A(th_get_hex_triplet("x120fac11", &tmpUint) == false); |
1187 } | 1194 } |
1188 | 1195 |
1189 // | 1196 // |
1190 // Regular expressions | 1197 // Regular expressions |
1191 // | 1198 // |