{"thread":{"id":"28545","subject":"[PATCH 1/2] test-ctype: macrofy","startedAt":"2011-10-01T16:36:14Z","lastAt":"2011-10-01T16:39:58Z","messageCount":2,"participants":["René Scharfe"],"isPatch":true,"patchVersion":1,"patchTotal":2},"messages":[{"id":"176647","messageId":"4E87417E.1060100@lsrfire.ath.cx","threadId":"28545","inReplyTo":null,"subject":"[PATCH 1/2] test-ctype: macrofy","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2011-10-01T16:36:14Z","receivedAt":"2011-10-01T16:36:14Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Rewrite test-ctype to use a global variable and a macro instead of\nwrapper functions for each character class and complicated structs\nwith loops going through them.  The resulting code may be uglier,\nbut that's OK for a test program, and it's actually easier to read\nand extend.  And much shorter.\n\nSigned-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n test-ctype.c |   79 +++++++++++++++------------------------------------------\n 1 files changed, 21 insertions(+), 58 deletions(-)\n\ndiff --git a/test-ctype.c b/test-ctype.c\nindex 033c749..b4d1f74 100644\n--- a/test-ctype.c\n+++ b/test-ctype.c\n@@ -1,78 +1,41 @@\n #include \"cache.h\"\n \n+static int rc;\n \n-static int test_isdigit(int c)\n+static void report_error(const char *class, int ch)\n {\n-\treturn isdigit(c);\n+\tprintf(\"%s classifies char %d (0x%02x) wrongly\\n\", class, ch, ch);\n+\trc = 1;\n }\n \n-static int test_isspace(int c)\n+static int is_in(const char *s, int ch)\n {\n-\treturn isspace(c);\n+\t/* We can't find NUL using strchr.  It's classless anyway. */\n+\tif (ch == '\\0')\n+\t\treturn 0;\n+\treturn !!strchr(s, ch);\n }\n \n-static int test_isalpha(int c)\n-{\n-\treturn isalpha(c);\n-}\n-\n-static int test_isalnum(int c)\n-{\n-\treturn isalnum(c);\n-}\n-\n-static int test_is_glob_special(int c)\n-{\n-\treturn is_glob_special(c);\n-}\n-\n-static int test_is_regex_special(int c)\n-{\n-\treturn is_regex_special(c);\n+#define TEST_CLASS(t,s) {\t\t\t\\\n+\tint i;\t\t\t\t\t\\\n+\tfor (i = 0; i < 256; i++) {\t\t\\\n+\t\tif (is_in(s, i) != t(i))\t\\\n+\t\t\treport_error(#t, i);\t\\\n+\t}\t\t\t\t\t\\\n }\n \n #define DIGIT \"0123456789\"\n #define LOWER \"abcdefghijklmnopqrstuvwxyz\"\n #define UPPER \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n \n-static const struct ctype_class {\n-\tconst char *name;\n-\tint (*test_fn)(int);\n-\tconst char *members;\n-} classes[] = {\n-\t{ \"isdigit\", test_isdigit, DIGIT },\n-\t{ \"isspace\", test_isspace, \" \\n\\r\\t\" },\n-\t{ \"isalpha\", test_isalpha, LOWER UPPER },\n-\t{ \"isalnum\", test_isalnum, LOWER UPPER DIGIT },\n-\t{ \"is_glob_special\", test_is_glob_special, \"*?[\\\\\" },\n-\t{ \"is_regex_special\", test_is_regex_special, \"$()*+.?[\\\\^{|\" },\n-\t{ NULL }\n-};\n-\n-static int test_class(const struct ctype_class *test)\n-{\n-\tint i, rc = 0;\n-\n-\tfor (i = 0; i < 256; i++) {\n-\t\tint expected = i ? !!strchr(test->members, i) : 0;\n-\t\tint actual = test->test_fn(i);\n-\n-\t\tif (actual != expected) {\n-\t\t\trc = 1;\n-\t\t\tprintf(\"%s classifies char %d (0x%02x) wrongly\\n\",\n-\t\t\t       test->name, i, i);\n-\t\t}\n-\t}\n-\treturn rc;\n-}\n-\n int main(int argc, char **argv)\n {\n-\tconst struct ctype_class *test;\n-\tint rc = 0;\n-\n-\tfor (test = classes; test->name; test++)\n-\t\trc |= test_class(test);\n+\tTEST_CLASS(isdigit, DIGIT);\n+\tTEST_CLASS(isspace, \" \\n\\r\\t\");\n+\tTEST_CLASS(isalpha, LOWER UPPER);\n+\tTEST_CLASS(isalnum, LOWER UPPER DIGIT);\n+\tTEST_CLASS(is_glob_special, \"*?[\\\\\");\n+\tTEST_CLASS(is_regex_special, \"$()*+.?[\\\\^{|\");\n \n \treturn rc;\n }\n-- \n1.7.7\n"},{"id":"176648","messageId":"4E87425E.10606@lsrfire.ath.cx","threadId":"28545","inReplyTo":"4E87417E.1060100@lsrfire.ath.cx","subject":"[PATCH 2/2] test-ctype: add test for is_pathspec_magic","fromName":"René Scharfe","fromEmail":"rene.scharfe@lsrfire.ath.cx","sentAt":"2011-10-01T16:39:58Z","receivedAt":"2011-10-01T16:39:58Z","isPatch":true,"sender":{"key":"l.s.r@web.de","avatar":"https://avatars.githubusercontent.com/u/26122331?v=4"},"body":"Signed-off-by: Rene Scharfe <rene.scharfe@lsrfire.ath.cx>\n---\n test-ctype.c |    1 +\n 1 files changed, 1 insertions(+), 0 deletions(-)\n\ndiff --git a/test-ctype.c b/test-ctype.c\nindex b4d1f74..707a821 100644\n--- a/test-ctype.c\n+++ b/test-ctype.c\n@@ -36,6 +36,7 @@ int main(int argc, char **argv)\n \tTEST_CLASS(isalnum, LOWER UPPER DIGIT);\n \tTEST_CLASS(is_glob_special, \"*?[\\\\\");\n \tTEST_CLASS(is_regex_special, \"$()*+.?[\\\\^{|\");\n+\tTEST_CLASS(is_pathspec_magic, \"!\\\"#%&',-/:;<=>@_`~\");\n \n \treturn rc;\n }\n-- \n1.7.7\n"}]}