aboutsummaryrefslogtreecommitdiffhomepage
path: root/test/core/support
diff options
context:
space:
mode:
authorGravatar David Garcia Quintas <dgq@google.com>2015-07-07 23:43:22 -0700
committerGravatar David Garcia Quintas <dgq@google.com>2015-07-08 01:58:25 -0700
commit074e2247ebd91b1fdb70e0b1164940253f2196fa (patch)
treed7839fbd70b44bb467828b04797e78b9d03d53b9 /test/core/support
parent14f1c271927976db4918cbb88529de65a874465a (diff)
Split function now takes slices as input, performs no allocs
Diffstat (limited to 'test/core/support')
-rw-r--r--test/core/support/string_test.c49
1 files changed, 33 insertions, 16 deletions
diff --git a/test/core/support/string_test.c b/test/core/support/string_test.c
index b7ee7cad3b..4fd11302d0 100644
--- a/test/core/support/string_test.c
+++ b/test/core/support/string_test.c
@@ -194,52 +194,69 @@ static void test_strjoin_sep(void) {
static void test_strsplit(void) {
gpr_slice_buffer* parts;
+ gpr_slice str;
+ gpr_slice sep;
+
LOG_TEST_NAME("test_strsplit");
- parts = gpr_strsplit("one, two, three, four", ", ");
+ parts = gpr_malloc(sizeof(gpr_slice_buffer));
+ gpr_slice_buffer_init(parts);
+
+ str = gpr_slice_from_copied_string("one, two, three, four");
+ sep = gpr_slice_from_copied_string(", ");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(4 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "two"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[2], "three"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[3], "four"));
- gpr_slice_buffer_destroy(parts);
- gpr_free(parts);
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
/* separator not present in string */
- parts = gpr_strsplit("one two three four", ", ");
+ str = gpr_slice_from_copied_string("one two three four");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(1 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "one two three four"));
- gpr_slice_buffer_destroy(parts);
- gpr_free(parts);
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
/* separator at the end */
- parts = gpr_strsplit("foo,", ",");
+ str = gpr_slice_from_copied_string("foo, ");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], "foo"));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
- gpr_slice_buffer_destroy(parts);
- gpr_free(parts);
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
/* separator at the beginning */
- parts = gpr_strsplit(",foo", ",");
+ str = gpr_slice_from_copied_string(", foo");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], "foo"));
- gpr_slice_buffer_destroy(parts);
- gpr_free(parts);
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
/* standalone separator */
- parts = gpr_strsplit(",", ",");
+ str = gpr_slice_from_copied_string(", ");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(2 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[1], ""));
- gpr_slice_buffer_destroy(parts);
- gpr_free(parts);
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
/* empty input */
- parts = gpr_strsplit("", ",");
+ str = gpr_slice_from_copied_string("");
+ gpr_slice_split(str, sep, parts);
GPR_ASSERT(1 == parts->count);
GPR_ASSERT(0 == gpr_slice_str_cmp(parts->slices[0], ""));
+ gpr_slice_buffer_reset_and_unref(parts);
+ gpr_slice_unref(str);
+
+ gpr_slice_unref(sep);
gpr_slice_buffer_destroy(parts);
gpr_free(parts);
}