diff options
Diffstat (limited to 'tests/wsbatch.c')
-rw-r--r-- | tests/wsbatch.c | 146 |
1 files changed, 146 insertions, 0 deletions
diff --git a/tests/wsbatch.c b/tests/wsbatch.c new file mode 100644 index 0000000..5d986db --- /dev/null +++ b/tests/wsbatch.c @@ -0,0 +1,146 @@ +/* wordsplit - a word splitter + Copyright (C) 2009 Sergey Poznyakoff + + This program is free software; you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by the + Free Software Foundation; either version 3 of the License, or (at your + option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License along + with this program. If not, see <http://www.gnu.org/licenses/>. */ + +#ifdef HAVE_CONFIG_H +# include <config.h> +#endif + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <wordsplit.h> + +size_t errors = 0; +size_t count = 0; + +void +runtest (const char **input, struct wordsplit *wsinit, int flags) +{ + int i; + + for (i = 0; input[i]; i++) + { + size_t j; + const char *text = input[i]; + struct wordsplit ws = *wsinit; + i++; + for (j = 0; input[i+j]; j++); + + count++; + if (wordsplit (text, &ws, flags)) + { + fprintf (stderr, "E: %s: parse failed\n", input); + errors++; + } + else if (ws.ws_wordc != j) + { + fprintf (stderr, "E: %s: wrong number of fields (%lu/%lu)\n", + text, ws.ws_wordc, j); + errors++; + } + else + { + size_t k; + + for (k = 0; k < ws.ws_wordc; k++) + if (strcmp (ws.ws_wordv[k], input[i + k])) + { + fprintf (stderr, "E: %s: word %lu mismatch: %s/%s\n", + text, k, ws.ws_wordv[k], input[i + k]); + errors++; + break; + } + } + i += j; + } +} + +const char *testv[] = { + "a bcd", "a", "bcd", NULL, + "a bcd \tef 13 456", "a", "bcd", "ef", "13", "456", NULL, + "a \"s p a c e\" b", "a", "s p a c e", "b", NULL, + "a \"s p \\\" a \\\\ c e\" b", "a", "s p \" a \\ c e", "b", NULL, + "a\\tb \"a\\tb\" 'a\\tb'", "a\tb", "a\tb", "a\\tb", NULL, + "\\\\\\a\\b\\f\\n\\r\\t\\v", "\\\a\b\f\n\r\t\v", NULL, + "a w13ord #comment", "a", "w13ord", "#comment", NULL, + NULL +}; + +const char *testcmt[] = { + "a bcd", "a", "bcd", NULL, + "a w13ord #comment", "a", "w13ord", NULL, + "#comment", NULL, + NULL +}; + +const char *testcol[] = { + "gray:x:1000:100:Sergey Poznyakoff:/home/gray:/bin/bash", + "gray", + "x", + "1000", + "100", + "Sergey Poznyakoff", + "/home/gray", + "/bin/bash", + NULL, + + "mail::8:12:\"mail pseudo user\":/:\n", + "mail", + "", + "8", + "12", + "\"mail pseudo user\"", + "/", + "", + NULL, + + NULL +}; + +const char *testcol2[] = { + "set x=foo,y=bar", + "set", " ", + "x", "=", + "foo", ",", + "y", "=", "bar", + NULL, + + NULL +}; + +int +main () +{ + struct wordsplit ws; + + runtest (testv, &ws, WRDSF_DEFFLAGS); + + ws.ws_comment = "#"; + runtest (testcmt, &ws, WRDSF_DEFFLAGS|WRDSF_COMMENT); + + ws.ws_delim = ":\n"; + runtest (testcol, &ws, WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM); + + ws.ws_delim = " =,"; + runtest (testcol2, &ws, + WRDSF_NOVAR | WRDSF_NOCMD | WRDSF_DELIM | WRDSF_RETURN_DELIMS); + + printf ("Tests: %lu\n", count); + if (errors) + printf ("Failures: %lu\n", errors); + return errors ? 1 : 0; +} + |