diff options
author | Neal H. Walfield <neal@g10code.de> | 2015-03-12 13:03:50 +0100 |
---|---|---|
committer | Neal H. Walfield <neal@g10code.de> | 2015-03-23 19:58:25 +0100 |
commit | b18ffcb81a3839dbf09603d70ebb8b80f65892d3 (patch) | |
tree | 1392ba5325cd04475a20cf9889c7f93d8f37edb2 /common/t-stringhelp.c | |
parent | gpg: Consider a mailbox only userid in mail search mode. (diff) | |
download | gnupg2-b18ffcb81a3839dbf09603d70ebb8b80f65892d3.tar.xz gnupg2-b18ffcb81a3839dbf09603d70ebb8b80f65892d3.zip |
common: Add new helper function, strsplit.
* common/stringhelp.h (strsplit): New declaration.
* common/stringhelp.c (strsplit): New function.
* common/t-stringhelp.c (test_strsplit): New function.
(main): Call it here.
--
Signed-off-by: Neal H. Walfield <neal@g10code.de>
Diffstat (limited to 'common/t-stringhelp.c')
-rw-r--r-- | common/t-stringhelp.c | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c index dcd5a453f..f5b6cd99e 100644 --- a/common/t-stringhelp.c +++ b/common/t-stringhelp.c @@ -1,5 +1,6 @@ /* t-stringhelp.c - Regression tests for stringhelp.c * Copyright (C) 2007 Free Software Foundation, Inc. + * 2015 g10 Code GmbH * * This file is part of JNLIB, which is a subsystem of GnuPG. * @@ -478,6 +479,62 @@ test_make_absfilename_try (void) xfree (cwd); } +static void +test_strsplit (void) +{ + int test_count = 0; + void test (const char *s, char delim, char replacement, + const char *fields_expected[]) + { + char *s2; + int field_count; + char **fields; + int field_count_expected; + int i; + + /* Count the fields. */ + for (field_count_expected = 0; + fields_expected[field_count_expected]; + field_count_expected ++) + ; + + test_count ++; + + /* We need to copy s since strsplit modifies it in place. */ + s2 = xstrdup (s); + fields = strsplit (s2, delim, replacement, &field_count); + + if (field_count != field_count_expected) + fail (test_count * 1000); + + for (i = 0; i < field_count_expected; i ++) + if (strcmp (fields_expected[i], fields[i]) != 0) + { + printf ("For field %d, expected '%s', but got '%s'\n", + i, fields_expected[i], fields[i]); + fail (test_count * 1000 + i + 1); + } + + xfree (s2); + } + + { + const char *expected_result[] = + { "a", "bc", "cde", "fghi", "jklmn", "", "foo", "", NULL }; + test ("a:bc:cde:fghi:jklmn::foo:", ':', '\0', expected_result); + } + + { + const char *expected_result[] = + { "!a!bc!!def!", "a!bc!!def!", "bc!!def!", "!def!", "def!", "", NULL }; + test (",a,bc,,def,", ',', '!', expected_result); + } + + { + const char *expected_result[] = { "", NULL }; + test ("", ':', ',', expected_result); + } +} int main (int argc, char **argv) @@ -491,6 +548,7 @@ main (int argc, char **argv) test_xstrconcat (); test_make_filename_try (); test_make_absfilename_try (); + test_strsplit (); xfree (home_buffer); return 0; |