File: test_random_access_set.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 (203 lines) | stat: -rw-r--r-- 8,518 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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/string.h>
#include <aws/http/private/random_access_set.h>

#include <aws/testing/aws_test_harness.h>

static uint64_t s_hash_string_ptr(const void *item) {
    const struct aws_string *str = *(const struct aws_string **)item;
    return aws_hash_string((void *)str);
}

static bool s_hash_string_ptr_eq(const void *a, const void *b) {
    const struct aws_string *str_a = *(const struct aws_string **)a;
    const struct aws_string *str_b = *(const struct aws_string **)b;
    return aws_string_eq(str_a, str_b);
}

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

    struct aws_random_access_set list_with_map;
    ASSERT_SUCCESS(aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, aws_ptr_eq, NULL, 0));
    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_sanitize_test, s_random_access_set_sanitize_fn)

static int s_random_access_set_insert_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
    AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
    AWS_STATIC_STRING_FROM_LITERAL(foobar, "foobar");

    struct aws_random_access_set list_with_map;
    /* With only 1 initial element. */
    ASSERT_SUCCESS(
        aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
    bool added = true;
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &bar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
    ASSERT_TRUE(added);

    /* You cannot have duplicates */
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
    ASSERT_FALSE(added);

    /* Check the size */
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 3);

    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_insert_test, s_random_access_set_insert_fn)

static int s_random_access_set_get_random_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");

    struct aws_random_access_set list_with_map;
    /* Insert a pointer of pointer of string to the structure */
    ASSERT_SUCCESS(
        aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
    /* Get the pointer of pointer to the string from the struct */
    struct aws_string **left_element = NULL;
    /* Fail to get any, when there is nothing in it. */
    ASSERT_FAILS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
    bool added = false;
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
    ASSERT_TRUE(added);

    /* Check the size */
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
    ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
    ASSERT_TRUE(aws_string_eq(*left_element, foo));

    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_get_random_test, s_random_access_set_get_random_fn)

static int s_random_access_set_exist_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
    AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");

    struct aws_random_access_set list_with_map;
    ASSERT_SUCCESS(
        aws_random_access_set_init(&list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, NULL, 1));
    bool added = false;
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
    ASSERT_TRUE(added);

    bool exist = false;
    ASSERT_SUCCESS(aws_random_access_set_exist(&list_with_map, &foo, &exist));
    ASSERT_TRUE(exist);

    ASSERT_SUCCESS(aws_random_access_set_exist(&list_with_map, &bar, &exist));
    ASSERT_FALSE(exist);

    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_exist_test, s_random_access_set_exist_fn)

static int s_random_access_set_remove_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    (void)ctx;
    AWS_STATIC_STRING_FROM_LITERAL(foo, "foo");
    AWS_STATIC_STRING_FROM_LITERAL(bar, "bar");
    AWS_STATIC_STRING_FROM_LITERAL(foobar, "foobar");

    struct aws_random_access_set list_with_map;
    /* With only 1 initial element. */
    ASSERT_SUCCESS(
        aws_random_access_set_init(&list_with_map, allocator, aws_hash_string, aws_hash_callback_string_eq, NULL, 1));
    bool added = false;
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, bar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foobar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foo, &added));
    ASSERT_TRUE(added);

    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foo));
    /* Check the size */
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 2);

    /* Should success and do nothing */
    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foo));

    /* Remove all beside foobar, so, if we get one random, it will be foobar */
    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, bar));
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
    struct aws_string *left_element = NULL;
    ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
    ASSERT_TRUE(aws_string_eq(left_element, foobar));

    /* Remove last thing and make sure everything should still work */
    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, foobar));
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 0);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, foo, &added));
    ASSERT_TRUE(added);
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);
    ASSERT_SUCCESS(aws_random_access_set_random_get_ptr(&list_with_map, (void **)&left_element));
    ASSERT_TRUE(aws_string_eq(left_element, foo));

    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_remove_test, s_random_access_set_remove_fn)

static void s_aws_string_destroy_callback(void *key) {
    struct aws_string *str = *(struct aws_string **)key;
    aws_string_destroy(str);
}

static int s_random_access_set_owns_element_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    /* If we copy the aws string itself, the underlying data will be copied as a pointer, if the data is
     * less than the size of a pointer, we will be fine. The long string will test against it. */
    struct aws_string *foo = aws_string_new_from_c_str(allocator, "foo123456");
    struct aws_string *bar = aws_string_new_from_c_str(allocator, "bar7894156132121");
    struct aws_string *foobar = aws_string_new_from_c_str(allocator, "foobar970712389709123");

    struct aws_random_access_set list_with_map;
    /* With only 1 initial element. Add clean up for the string */
    ASSERT_SUCCESS(aws_random_access_set_init(
        &list_with_map, allocator, s_hash_string_ptr, s_hash_string_ptr_eq, s_aws_string_destroy_callback, 1));
    bool added = false;
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &bar, &added));
    ASSERT_TRUE(added);
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foo, &added));
    ASSERT_TRUE(added);

    /* You cannot have duplicates */
    ASSERT_SUCCESS(aws_random_access_set_add(&list_with_map, &foobar, &added));
    ASSERT_FALSE(added);

    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, &foo));
    ASSERT_SUCCESS(aws_random_access_set_remove(&list_with_map, &foobar));

    /* Check the size */
    ASSERT_UINT_EQUALS(aws_random_access_set_get_size(&list_with_map), 1);

    aws_random_access_set_clean_up(&list_with_map);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(random_access_set_owns_element_test, s_random_access_set_owns_element_fn)