/* wordsplit - a word splitter Copyright (C) 2009, 2010 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 . */ #ifdef HAVE_CONFIG_H # include #endif #include #include #include #include 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", text); errors++; } else if (ws.ws_wordc != j) { fprintf (stderr, "E: %s: wrong number of fields (%lu/%lu)\n", text, (unsigned long) ws.ws_wordc, (unsigned long) 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, (unsigned long) 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", (unsigned long) count); if (errors) printf ("Failures: %lu\n", (unsigned long) errors); return errors ? 1 : 0; }