TEST(String, SplitCommand) { char **argv; /* test with a NULL/empty string */ POINTERS_EQUAL(NULL, string_split_command (NULL, ';')); POINTERS_EQUAL(NULL, string_split_command ("", ';')); /* string with one command */ argv = string_split_command ("abc", ';'); CHECK(argv); STRCMP_EQUAL("abc", argv[0]); POINTERS_EQUAL(NULL, argv[1]); string_free_split_command (argv); /* string with 3 commands */ argv = string_split_command ("abc;de;fghi", ';'); CHECK(argv); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split_command (argv); /* string with 3 commands (containing spaces) */ argv = string_split_command (" abc ; de ; fghi ", ';'); CHECK(argv); STRCMP_EQUAL("abc ", argv[0]); STRCMP_EQUAL("de ", argv[1]); STRCMP_EQUAL("fghi ", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split_command (argv); /* separator other than ';' */ argv = string_split_command ("abc,de,fghi", ','); CHECK(argv); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split_command (argv); /* free split with NULL */ string_free_split_command (NULL); }
TEST(String, SplitCommand) { char **argv; POINTERS_EQUAL(NULL, string_split_command (NULL, ';')); POINTERS_EQUAL(NULL, string_split_command ("", ';')); argv = string_split_command ("abc;de;fghi", ';'); CHECK(argv); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split_command (argv); /* free split with NULL */ string_free_split_command (NULL); }
TEST(String, Split) { char **argv, *str; int argc; POINTERS_EQUAL(NULL, string_split (NULL, NULL, 0, 0, NULL)); POINTERS_EQUAL(NULL, string_split (NULL, "", 0, 0, NULL)); POINTERS_EQUAL(NULL, string_split ("", NULL, 0, 0, NULL)); POINTERS_EQUAL(NULL, string_split ("", "", 0, 0, NULL)); argc = 1; POINTERS_EQUAL(NULL, string_split (NULL, NULL, 0, 0, &argc)); LONGS_EQUAL(0, argc); argc = 1; POINTERS_EQUAL(NULL, string_split (NULL, "", 0, 0, &argc)); LONGS_EQUAL(0, argc); argc = 1; POINTERS_EQUAL(NULL, string_split ("", NULL, 0, 0, &argc)); LONGS_EQUAL(0, argc); argc = 1; POINTERS_EQUAL(NULL, string_split ("", "", 0, 0, &argc)); LONGS_EQUAL(0, argc); /* free split with NULL */ string_free_split (NULL); string_free_split_shared (NULL); string_free_split_command (NULL); /* standard split */ argv = string_split (" abc de fghi ", " ", 0, 0, &argc); LONGS_EQUAL(3, argc); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split (argv); /* max 2 items */ argv = string_split (" abc de fghi ", " ", 0, 2, &argc); LONGS_EQUAL(2, argc); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); POINTERS_EQUAL(NULL, argv[2]); string_free_split (argv); /* keep eol */ argv = string_split (" abc de fghi ", " ", 1, 0, &argc); LONGS_EQUAL(3, argc); STRCMP_EQUAL("abc de fghi", argv[0]); STRCMP_EQUAL("de fghi", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split (argv); /* keep eol and max 2 items */ argv = string_split (" abc de fghi ", " ", 1, 2, &argc); LONGS_EQUAL(2, argc); STRCMP_EQUAL("abc de fghi", argv[0]); STRCMP_EQUAL("de fghi", argv[1]); POINTERS_EQUAL(NULL, argv[2]); string_free_split (argv); /* split with shared strings */ argv = string_split_shared (" abc de abc ", " ", 0, 0, &argc); LONGS_EQUAL(3, argc); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("abc", argv[2]); POINTERS_EQUAL(NULL, argv[3]); /* same content == same pointer for shared strings */ POINTERS_EQUAL(argv[0], argv[2]); string_free_split_shared (argv); /* build string with split string */ str = string_build_with_split_string (NULL, NULL); POINTERS_EQUAL(NULL, str); argv = string_split (" abc de fghi ", " ", 0, 0, &argc); str = string_build_with_split_string ((const char **)argv, NULL); STRCMP_EQUAL("abcdefghi", str); free (str); str = string_build_with_split_string ((const char **)argv, ""); STRCMP_EQUAL("abcdefghi", str); free (str); str = string_build_with_split_string ((const char **)argv, ";;"); STRCMP_EQUAL("abc;;de;;fghi", str); free (str); string_free_split (argv); /* split command */ POINTERS_EQUAL(NULL, string_split_command (NULL, ';')); POINTERS_EQUAL(NULL, string_split_command ("", ';')); argv = string_split_command ("abc;de;fghi", ';'); LONGS_EQUAL(3, argc); STRCMP_EQUAL("abc", argv[0]); STRCMP_EQUAL("de", argv[1]); STRCMP_EQUAL("fghi", argv[2]); POINTERS_EQUAL(NULL, argv[3]); string_free_split_command (argv); }