Debian Codegraph @

crt/aws-c-common/tests/byte_cursor_find_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 #include <aws/testing/aws_test_harness.h>
8
9 static int s_test_byte_cursor_find_str_fn(struct aws_allocator *allocator, void *ctx) {
10 (void)allocator;
11 (void)ctx;
12
13 const char *string_with_match = "This is a string and we want to find a substring of it.";
14 const char *to_find = "and we want";
15
16 struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
17 struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);
18
19 struct aws_byte_cursor find_res;
20 AWS_ZERO_STRUCT(find_res);
21
22 ASSERT_SUCCESS(aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));
23 ASSERT_BIN_ARRAYS_EQUALS(to_find_cur.ptr, to_find_cur.len, find_res.ptr, to_find_cur.len);
24 ASSERT_UINT_EQUALS(string_with_match_cur.len - (find_res.ptr - string_with_match_cur.ptr), find_res.len);
25 ASSERT_PTR_EQUALS(string_with_match_cur.ptr + (find_res.ptr - string_with_match_cur.ptr), find_res.ptr);
26
27 return AWS_OP_SUCCESS;
28 }
29
30 AWS_TEST_CASE(test_byte_cursor_find_str, s_test_byte_cursor_find_str_fn)
31
32 static int s_test_byte_cursor_find_str_not_found_fn(struct aws_allocator *allocator, void *ctx) {
33 (void)allocator;
34 (void)ctx;
35
36 const char *string_with_match = "This is a string and we want to find a substring of it.";
37 const char *to_find = "and we went";
38
39 struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
40 struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);
41
42 struct aws_byte_cursor find_res;
43 AWS_ZERO_STRUCT(find_res);
44
45 ASSERT_ERROR(
46 AWS_ERROR_STRING_MATCH_NOT_FOUND, aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));
47
48 return AWS_OP_SUCCESS;
49 }
50
51 AWS_TEST_CASE(test_byte_cursor_find_str_not_found, s_test_byte_cursor_find_str_not_found_fn)
52
53 static int s_test_byte_cursor_find_str_longer_than_input_fn(struct aws_allocator *allocator, void *ctx) {
54 (void)allocator;
55 (void)ctx;
56
57 const char *string_with_match = "This ";
58 const char *to_find = "and we want";
59
60 struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
61 struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);
62
63 struct aws_byte_cursor find_res;
64 AWS_ZERO_STRUCT(find_res);
65
66 ASSERT_ERROR(
67 AWS_ERROR_STRING_MATCH_NOT_FOUND, aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));
68
69 return AWS_OP_SUCCESS;
70 }
71
72 AWS_TEST_CASE(test_byte_cursor_find_str_longer_than_input, s_test_byte_cursor_find_str_longer_than_input_fn)