diff options
author | Werner Koch <wk@gnupg.org> | 2015-06-22 19:28:33 +0200 |
---|---|---|
committer | Werner Koch <wk@gnupg.org> | 2015-06-22 19:28:33 +0200 |
commit | d37f47081d41584efc0c397432811f9cfa5d5867 (patch) | |
tree | e2b8ba8914a1b731f4ff4c29396635824662a650 /common/t-stringhelp.c | |
parent | gpg: Fix regression due to recent commit 6500f33 (diff) | |
download | gnupg2-d37f47081d41584efc0c397432811f9cfa5d5867.tar.xz gnupg2-d37f47081d41584efc0c397432811f9cfa5d5867.zip |
common: Add function strtokenize.
* common/stringhelp.c: Include assert.h.
(strtokenize): New.
* common/t-stringhelp.c (test_strtokenize): New.
Signed-off-by: Werner Koch <wk@gnupg.org>
Diffstat (limited to 'common/t-stringhelp.c')
-rw-r--r-- | common/t-stringhelp.c | 141 |
1 files changed, 141 insertions, 0 deletions
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c index 6c4723754..13f3afa74 100644 --- a/common/t-stringhelp.c +++ b/common/t-stringhelp.c @@ -538,6 +538,146 @@ test_strsplit (void) } } + + +static void +test_strtokenize (void) +{ + struct { + const char *s; + const char *delim; + const char *fields_expected[10]; + } tv[] = { + { + "", ":", + { "", NULL } + }, + { + "a", ":", + { "a", NULL } + }, + { + ":", ":", + { "", "", NULL } + }, + { + "::", ":", + { "", "", "", NULL } + }, + { + "a:b:c", ":", + { "a", "b", "c", NULL } + }, + { + "a:b:", ":", + { "a", "b", "", NULL } + }, + { + "a:b", ":", + { "a", "b", NULL } + }, + { + "aa:b:cd", ":", + { "aa", "b", "cd", NULL } + }, + { + "aa::b:cd", ":", + { "aa", "", "b", "cd", NULL } + }, + { + "::b:cd", ":", + { "", "", "b", "cd", NULL } + }, + { + "aa: : b:cd ", ":", + { "aa", "", "b", "cd", NULL } + }, + { + " aa: : b: cd ", ":", + { "aa", "", "b", "cd", NULL } + }, + { + " ", ":", + { "", NULL } + }, + { + " :", ":", + { "", "", NULL } + }, + { + " : ", ":", + { "", "", NULL } + }, + { + ": ", ":", + { "", "", NULL } + }, + { + ": x ", ":", + { "", "x", NULL } + }, + { + "a:bc:cde:fghi:jklmn::foo:", ":", + { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL } + }, + { + ",a,bc,,def,", ",", + { "", "a", "bc", "", "def", "", NULL } + }, + { + " a ", " ", + { "", "a", "", NULL } + }, + { + " ", " ", + { "", "", NULL } + }, + { + "", " ", + { "", NULL } + } + }; + + int tidx; + + for (tidx = 0; tidx < DIM(tv); tidx++) + { + char **fields; + int field_count; + int field_count_expected; + int i; + + for (field_count_expected = 0; + tv[tidx].fields_expected[field_count_expected]; + field_count_expected ++) + ; + + fields = strtokenize (tv[tidx].s, tv[tidx].delim); + if (!fields) + fail (tidx * 1000); + else + { + for (field_count = 0; fields[field_count]; field_count++) + ; + if (field_count != field_count_expected) + fail (tidx * 1000); + else + { + for (i = 0; i < field_count_expected; i++) + if (strcmp (tv[tidx].fields_expected[i], fields[i])) + { + printf ("For field %d, expected '%s', but got '%s'\n", + i, tv[tidx].fields_expected[i], fields[i]); + fail (tidx * 1000 + i + 1); + } + } + } + + xfree (fields); + } +} + + int main (int argc, char **argv) { @@ -551,6 +691,7 @@ main (int argc, char **argv) test_make_filename_try (); test_make_absfilename_try (); test_strsplit (); + test_strtokenize (); xfree (home_buffer); return 0; |