aboutsummaryrefslogtreecommitdiffstats
path: root/common/t-stringhelp.c
diff options
context:
space:
mode:
authorWerner Koch <[email protected]>2017-07-17 11:00:44 +0000
committerWerner Koch <[email protected]>2017-07-17 13:53:16 +0000
commit849467870ee1c10e0a7b1e89cfc9e8214e4963fe (patch)
treec0c02cb105b0b928236636e453767425d85dd8f8 /common/t-stringhelp.c
parenttests: Improve 'shell.scm' script. (diff)
downloadgnupg-849467870ee1c10e0a7b1e89cfc9e8214e4963fe.tar.gz
gnupg-849467870ee1c10e0a7b1e89cfc9e8214e4963fe.zip
common: New function split_fields_colon.
* common/stringhelp.c (split_fields_colon): New. * common/t-stringhelp.c (test_split_fields_colon): New test. (main): Call that test. Signed-off-by: Werner Koch <[email protected]>
Diffstat (limited to 'common/t-stringhelp.c')
-rw-r--r--common/t-stringhelp.c76
1 files changed, 76 insertions, 0 deletions
diff --git a/common/t-stringhelp.c b/common/t-stringhelp.c
index 189fed1f9..7c6fb8022 100644
--- a/common/t-stringhelp.c
+++ b/common/t-stringhelp.c
@@ -761,6 +761,81 @@ test_split_fields (void)
}
+static void
+test_split_fields_colon (void)
+{
+ struct {
+ const char *s;
+ int nfields;
+ const char *fields_expected[10];
+ } tv[] = {
+ {
+ "a:bc:cde:fghi:jklmn: foo ", 6,
+ { "a", "bc", "cde", "fghi", "jklmn", " foo ", NULL }
+ },
+ {
+ " a:bc: def ", 2,
+ { " a", "bc", NULL }
+ },
+ {
+ " a:bc :def ", 3,
+ { " a", "bc ", "def ", NULL }
+ },
+ {
+ " a:bc: def ", 4,
+ { " a", "bc", " def ", NULL }
+ },
+ {
+ "", 0,
+ { NULL }
+ }
+ };
+
+ int tidx;
+ char *fields[10];
+ int field_count_expected, nfields, field_count, i;
+ char *s2;
+
+ for (tidx = 0; tidx < DIM(tv); tidx++)
+ {
+ nfields = tv[tidx].nfields;
+ assert (nfields <= DIM (fields));
+
+ /* Count the fields. */
+ for (field_count_expected = 0;
+ tv[tidx].fields_expected[field_count_expected];
+ field_count_expected ++)
+ ;
+ if (field_count_expected > nfields)
+ field_count_expected = nfields;
+
+ /* We need to copy s since split_fields modifies in place. */
+ s2 = xstrdup (tv[tidx].s);
+ field_count = split_fields_colon (s2, fields, nfields);
+
+ if (field_count != field_count_expected)
+ {
+ printf ("%s: tidx %d: expected %d, got %d\n",
+ __func__, tidx, field_count_expected, field_count);
+ fail (tidx * 1000);
+ }
+ else
+ {
+ for (i = 0; i < field_count_expected; i ++)
+ if (strcmp (tv[tidx].fields_expected[i], fields[i]))
+ {
+ printf ("%s: tidx %d, field %d: expected '%s', got '%s'\n",
+ __func__,
+ tidx, i, tv[tidx].fields_expected[i], fields[i]);
+ fail (tidx * 1000 + i + 1);
+ }
+ }
+
+ xfree (s2);
+ }
+}
+
+
static char *
stresc (char *s)
{
@@ -996,6 +1071,7 @@ main (int argc, char **argv)
test_strsplit ();
test_strtokenize ();
test_split_fields ();
+ test_split_fields_colon ();
test_compare_version_strings ();
test_format_text ();