| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
#include <aws/common/common.h> |
| 7 |
#include <aws/common/math.h> |
| 8 |
#include <aws/testing/aws_test_harness.h> |
| 9 |
#include <stdlib.h> |
| 10 |
|
| 11 |
static void *s_calloc_stub(struct aws_allocator *allocator, size_t num, size_t size) { |
| 12 |
allocator->impl = (void *)(num * size); |
| 13 |
return calloc(num, size); |
| 14 |
} |
| 15 |
|
| 16 |
static void s_mem_release_stub(struct aws_allocator *allocator, void *ptr) { |
| 17 |
allocator->impl = 0; |
| 18 |
free(ptr); |
| 19 |
} |
| 20 |
|
| 21 |
static int s_test_calloc_on_given_allocator(struct aws_allocator *allocator, bool using_calloc_stub_impl) { |
| 22 |
|
| 23 |
char *p = aws_mem_calloc(allocator, 2, 4); |
| 24 |
ASSERT_NOT_NULL(p); |
| 25 |
for (size_t i = 0; i < 2 * 4; ++i) { |
| 26 |
ASSERT_TRUE(p[i] == 0); |
| 27 |
} |
| 28 |
if (using_calloc_stub_impl) { |
| 29 |
ASSERT_TRUE((intptr_t)allocator->impl == 8); |
| 30 |
} |
| 31 |
aws_mem_release(allocator, p); |
| 32 |
return AWS_OP_SUCCESS; |
| 33 |
} |
| 34 |
|
| 35 |
AWS_TEST_CASE(test_calloc_override, s_test_calloc_override_fn) |
| 36 |
static int s_test_calloc_override_fn(struct aws_allocator *allocator, void *ctx) { |
| 37 |
(void)allocator; |
| 38 |
(void)ctx; |
| 39 |
struct aws_allocator my_alloc = { |
| 40 |
.mem_calloc = s_calloc_stub, |
| 41 |
.mem_release = s_mem_release_stub, |
| 42 |
}; |
| 43 |
return s_test_calloc_on_given_allocator(&my_alloc, true); |
| 44 |
} |
| 45 |
|
| 46 |
AWS_TEST_CASE(test_calloc_fallback_from_default_allocator, s_test_calloc_fallback_from_default_allocator_fn) |
| 47 |
static int s_test_calloc_fallback_from_default_allocator_fn(struct aws_allocator *allocator, void *ctx) { |
| 48 |
(void)allocator; |
| 49 |
(void)ctx; |
| 50 |
|
| 51 |
struct aws_allocator my_alloc = *aws_default_allocator(); |
| 52 |
my_alloc.mem_calloc = NULL; |
| 53 |
return s_test_calloc_on_given_allocator(&my_alloc, false); |
| 54 |
} |
| 55 |
|
| 56 |
AWS_TEST_CASE(test_calloc_fallback_from_given, s_test_calloc_fallback_from_given_fn) |
| 57 |
static int s_test_calloc_fallback_from_given_fn(struct aws_allocator *allocator, void *ctx) { |
| 58 |
(void)allocator; |
| 59 |
(void)ctx; |
| 60 |
|
| 61 |
struct aws_allocator my_alloc = *allocator; |
| 62 |
my_alloc.mem_calloc = NULL; |
| 63 |
return s_test_calloc_on_given_allocator(&my_alloc, false); |
| 64 |
} |
| 65 |
|
| 66 |
AWS_TEST_CASE(test_calloc_from_default_allocator, s_test_calloc_from_default_allocator_fn) |
| 67 |
static int s_test_calloc_from_default_allocator_fn(struct aws_allocator *allocator, void *ctx) { |
| 68 |
(void)allocator; |
| 69 |
(void)ctx; |
| 70 |
return s_test_calloc_on_given_allocator(aws_default_allocator(), false); |
| 71 |
} |
| 72 |
|
| 73 |
AWS_TEST_CASE(test_calloc_from_given_allocator, s_test_calloc_from_given_allocator_fn) |
| 74 |
static int s_test_calloc_from_given_allocator_fn(struct aws_allocator *allocator, void *ctx) { |
| 75 |
(void)ctx; |
| 76 |
return s_test_calloc_on_given_allocator(allocator, false); |
| 77 |
} |