Debian Codegraph @

crt/aws-c-common/tests/split_test.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/byte_buf.h>
7
8 #include <aws/testing/aws_test_harness.h>
9
10 AWS_TEST_CASE(test_char_split_happy_path, s_test_char_split_happy_path_fn)
11 static int s_test_char_split_happy_path_fn(struct aws_allocator *allocator, void *ctx) {
12 (void)ctx;
13
14 const char str_to_split[] = "testa;testb;testc";
15
16 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
17
18 struct aws_array_list output;
19 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
20 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
21 ASSERT_INT_EQUALS(3, aws_array_list_length(&output));
22
23 struct aws_byte_cursor value = {0};
24 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
25
26 char *expected = "testa";
27
28 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
29
30 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
31 expected = "testb";
32 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
33
34 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
35 expected = "testc";
36 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
37
38 aws_array_list_clean_up(&output);
39
40 return 0;
41 }
42
43 AWS_TEST_CASE(test_char_split_ends_with_token, s_test_char_split_ends_with_token_fn)
44 static int s_test_char_split_ends_with_token_fn(struct aws_allocator *allocator, void *ctx) {
45 (void)ctx;
46
47 const char str_to_split[] = "testa;testb;testc;";
48
49 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
50
51 struct aws_array_list output;
52 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
53 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
54 ASSERT_INT_EQUALS(4, aws_array_list_length(&output));
55
56 struct aws_byte_cursor value = {0};
57 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
58 char *expected = "testa";
59 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
60
61 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
62 expected = "testb";
63 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
64
65 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
66 expected = "testc";
67 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
68
69 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 3));
70 expected = "";
71 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
72
73 aws_array_list_clean_up(&output);
74
75 return 0;
76 }
77
78 AWS_TEST_CASE(test_char_split_begins_with_token, s_test_char_split_begins_with_token_fn)
79 static int s_test_char_split_begins_with_token_fn(struct aws_allocator *allocator, void *ctx) {
80 (void)ctx;
81
82 const char str_to_split[] = ";testa;testb;testc";
83
84 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
85
86 struct aws_array_list output;
87 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
88 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
89 ASSERT_INT_EQUALS(4, aws_array_list_length(&output));
90
91 struct aws_byte_cursor value = {0};
92
93 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
94 char *expected = "";
95 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
96
97 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
98 expected = "testa";
99
100 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
101
102 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
103 expected = "testb";
104 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
105
106 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 3));
107 expected = "testc";
108 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
109
110 aws_array_list_clean_up(&output);
111
112 return 0;
113 }
114
115 AWS_TEST_CASE(test_char_split_token_not_present, s_test_char_split_token_not_present_fn)
116 static int s_test_char_split_token_not_present_fn(struct aws_allocator *allocator, void *ctx) {
117 (void)ctx;
118
119 const char str_to_split[] = "testa";
120
121 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
122
123 struct aws_array_list output;
124 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
125 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
126 ASSERT_INT_EQUALS(1, aws_array_list_length(&output));
127
128 struct aws_byte_cursor value = {0};
129 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
130
131 char *expected = "testa";
132
133 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
134
135 aws_array_list_clean_up(&output);
136
137 return 0;
138 }
139
140 AWS_TEST_CASE(test_char_split_empty, s_test_char_split_empty_fn)
141 static int s_test_char_split_empty_fn(struct aws_allocator *allocator, void *ctx) {
142 (void)ctx;
143
144 const char str_to_split[] = "";
145
146 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
147
148 struct aws_array_list output;
149 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
150 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
151 ASSERT_INT_EQUALS(1, aws_array_list_length(&output));
152
153 struct aws_byte_cursor value = {0};
154 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
155 ASSERT_INT_EQUALS(0, value.len);
156
157 aws_array_list_clean_up(&output);
158
159 return 0;
160 }
161
162 AWS_TEST_CASE(test_char_split_zeroed, s_test_char_split_zeroed_fn)
163 static int s_test_char_split_zeroed_fn(struct aws_allocator *allocator, void *ctx) {
164 (void)ctx;
165
166 struct aws_byte_cursor to_split;
167 AWS_ZERO_STRUCT(to_split);
168
169 struct aws_array_list output;
170 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
171 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
172 ASSERT_INT_EQUALS(1, aws_array_list_length(&output));
173
174 struct aws_byte_cursor value = {0};
175 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
176 ASSERT_INT_EQUALS(0, value.len);
177
178 aws_array_list_clean_up(&output);
179
180 return 0;
181 }
182
183 AWS_TEST_CASE(test_char_split_adj_tokens, s_test_char_split_adj_tokens_fn)
184 static int s_test_char_split_adj_tokens_fn(struct aws_allocator *allocator, void *ctx) {
185 (void)ctx;
186
187 const char str_to_split[] = "testa;;testb;testc";
188
189 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
190
191 struct aws_array_list output;
192 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
193 ASSERT_SUCCESS(aws_byte_cursor_split_on_char(&to_split, ';', &output));
194 ASSERT_INT_EQUALS(4, aws_array_list_length(&output));
195
196 struct aws_byte_cursor value = {0};
197 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
198
199 char *expected = "testa";
200
201 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
202
203 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
204 expected = "";
205 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
206
207 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
208 expected = "testb";
209 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
210
211 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 3));
212 expected = "testc";
213 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
214
215 aws_array_list_clean_up(&output);
216
217 return 0;
218 }
219
220 AWS_TEST_CASE(test_char_split_with_max_splits, s_test_char_split_with_max_splits_fn)
221 static int s_test_char_split_with_max_splits_fn(struct aws_allocator *allocator, void *ctx) {
222 (void)ctx;
223
224 const char str_to_split[] = ";testa;testb;testc";
225
226 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
227
228 struct aws_array_list output;
229 ASSERT_SUCCESS(aws_array_list_init_dynamic(&output, allocator, 4, sizeof(struct aws_byte_cursor)));
230 ASSERT_SUCCESS(aws_byte_cursor_split_on_char_n(&to_split, ';', 2, &output));
231 ASSERT_INT_EQUALS(3, aws_array_list_length(&output));
232
233 struct aws_byte_cursor value = {0};
234
235 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
236 char *expected = "";
237 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
238
239 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
240 expected = "testa";
241
242 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
243
244 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
245 expected = "testb;testc";
246 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
247
248 aws_array_list_clean_up(&output);
249
250 return 0;
251 }
252
253 AWS_TEST_CASE(test_char_split_output_too_small, s_test_char_split_output_too_small_fn)
254 static int s_test_char_split_output_too_small_fn(struct aws_allocator *allocator, void *ctx) {
255 (void)allocator;
256 (void)ctx;
257
258 const char str_to_split[] = "testa;testb;testc;";
259
260 struct aws_byte_cursor to_split = aws_byte_cursor_from_c_str(str_to_split);
261
262 struct aws_array_list output;
263 struct aws_byte_cursor output_array[3] = {{0}};
264 aws_array_list_init_static(&output, output_array, 3, sizeof(struct aws_byte_cursor));
265 ASSERT_ERROR(AWS_ERROR_LIST_EXCEEDS_MAX_SIZE, aws_byte_cursor_split_on_char(&to_split, ';', &output));
266 ASSERT_INT_EQUALS(3, aws_array_list_length(&output));
267
268 struct aws_byte_cursor value = {0};
269
270 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 0));
271 char *expected = "testa";
272
273 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
274
275 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 1));
276 expected = "testb";
277 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
278
279 ASSERT_SUCCESS(aws_array_list_get_at(&output, &value, 2));
280 expected = "testc";
281 ASSERT_BIN_ARRAYS_EQUALS(expected, strlen(expected), value.ptr, value.len);
282
283 return 0;
284 }
285
286 AWS_TEST_CASE(test_byte_cursor_next_split, s_test_byte_cursor_next_split)
287 static int s_test_byte_cursor_next_split(struct aws_allocator *allocator, void *ctx) {
288 (void)allocator;
289 (void)ctx;
290
291 struct aws_byte_cursor to_split1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("testa;testb;testc;a");
292 struct aws_byte_cursor result1 = {0};
293 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split1, ';', &result1));
294 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "testa");
295
296 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split1, ';', &result1));
297 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "testb");
298
299 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split1, ';', &result1));
300 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "testc");
301
302 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split1, ';', &result1));
303 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "a");
304
305 ASSERT_FALSE(aws_byte_cursor_next_split(&to_split1, ';', &result1));
306 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "");
307
308 struct aws_byte_cursor to_split2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
309 struct aws_byte_cursor result2 = {0};
310 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split2, ';', &result2));
311 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result2, "");
312
313 ASSERT_FALSE(aws_byte_cursor_next_split(&to_split2, ';', &result2));
314 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result2, "");
315
316 struct aws_byte_cursor to_split3 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL(";;");
317 struct aws_byte_cursor result3 = {0};
318 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split3, ';', &result3));
319 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
320
321 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split3, ';', &result3));
322 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
323
324 ASSERT_TRUE(aws_byte_cursor_next_split(&to_split3, ';', &result3));
325 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
326
327 ASSERT_FALSE(aws_byte_cursor_next_split(&to_split3, ';', &result3));
328 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
329
330 return 0;
331 }
332
333 AWS_TEST_CASE(test_byte_cursor_next_split_multi, s_test_byte_cursor_next_split_multi)
334 static int s_test_byte_cursor_next_split_multi(struct aws_allocator *allocator, void *ctx) {
335 (void)allocator;
336 (void)ctx;
337
338 struct aws_byte_cursor to_split1 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("test-a--testb--testc--a");
339 struct aws_byte_cursor result1 = {0};
340 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split1, aws_byte_cursor_from_c_str("--"), &result1));
341 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "test-a");
342
343 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split1, aws_byte_cursor_from_c_str("--"), &result1));
344 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "testb");
345
346 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split1, aws_byte_cursor_from_c_str("--"), &result1));
347 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "testc");
348
349 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split1, aws_byte_cursor_from_c_str("--"), &result1));
350 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "a");
351
352 ASSERT_FALSE(aws_byte_cursor_next_split_on_cursor(&to_split1, aws_byte_cursor_from_c_str("--"), &result1));
353 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result1, "");
354
355 struct aws_byte_cursor to_split2 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("");
356 struct aws_byte_cursor result2 = {0};
357 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split2, aws_byte_cursor_from_c_str("--"), &result2));
358 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result2, "");
359
360 ASSERT_FALSE(aws_byte_cursor_next_split_on_cursor(&to_split2, aws_byte_cursor_from_c_str("--"), &result2));
361 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result2, "");
362
363 struct aws_byte_cursor to_split3 = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("----");
364 struct aws_byte_cursor result3 = {0};
365 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split3, aws_byte_cursor_from_c_str("--"), &result3));
366 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
367
368 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split3, aws_byte_cursor_from_c_str("--"), &result3));
369 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
370
371 ASSERT_TRUE(aws_byte_cursor_next_split_on_cursor(&to_split3, aws_byte_cursor_from_c_str("--"), &result3));
372 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
373
374 ASSERT_FALSE(aws_byte_cursor_next_split_on_cursor(&to_split3, aws_byte_cursor_from_c_str("--"), &result3));
375 ASSERT_CURSOR_VALUE_CSTRING_EQUALS(result3, "");
376
377 return 0;
378 }