File: byte_cursor_find_test.c

package info (click to toggle)
aws-crt-python 0.20.4%2Bdfsg-1~bpo12%2B1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-backports
  • size: 72,656 kB
  • sloc: ansic: 381,805; python: 23,008; makefile: 6,251; sh: 4,536; cpp: 699; ruby: 208; java: 77; perl: 73; javascript: 46; xml: 11
file content (72 lines) | stat: -rw-r--r-- 2,754 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/byte_buf.h>
#include <aws/testing/aws_test_harness.h>

static int s_test_byte_cursor_find_str_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const char *string_with_match = "This is a string and we want to find a substring of it.";
    const char *to_find = "and we want";

    struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
    struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);

    struct aws_byte_cursor find_res;
    AWS_ZERO_STRUCT(find_res);

    ASSERT_SUCCESS(aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));
    ASSERT_BIN_ARRAYS_EQUALS(to_find_cur.ptr, to_find_cur.len, find_res.ptr, to_find_cur.len);
    ASSERT_UINT_EQUALS(string_with_match_cur.len - (find_res.ptr - string_with_match_cur.ptr), find_res.len);
    ASSERT_PTR_EQUALS(string_with_match_cur.ptr + (find_res.ptr - string_with_match_cur.ptr), find_res.ptr);

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_byte_cursor_find_str, s_test_byte_cursor_find_str_fn)

static int s_test_byte_cursor_find_str_not_found_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const char *string_with_match = "This is a string and we want to find a substring of it.";
    const char *to_find = "and we went";

    struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
    struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);

    struct aws_byte_cursor find_res;
    AWS_ZERO_STRUCT(find_res);

    ASSERT_ERROR(
        AWS_ERROR_STRING_MATCH_NOT_FOUND, aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_byte_cursor_find_str_not_found, s_test_byte_cursor_find_str_not_found_fn)

static int s_test_byte_cursor_find_str_longer_than_input_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    const char *string_with_match = "This ";
    const char *to_find = "and we want";

    struct aws_byte_cursor string_with_match_cur = aws_byte_cursor_from_c_str(string_with_match);
    struct aws_byte_cursor to_find_cur = aws_byte_cursor_from_c_str(to_find);

    struct aws_byte_cursor find_res;
    AWS_ZERO_STRUCT(find_res);

    ASSERT_ERROR(
        AWS_ERROR_STRING_MATCH_NOT_FOUND, aws_byte_cursor_find_exact(&string_with_match_cur, &to_find_cur, &find_res));

    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_byte_cursor_find_str_longer_than_input, s_test_byte_cursor_find_str_longer_than_input_fn)