File: calloc_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 (77 lines) | stat: -rw-r--r-- 2,699 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
/**
 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0.
 */

#include <aws/common/common.h>
#include <aws/common/math.h>
#include <aws/testing/aws_test_harness.h>
#include <stdlib.h>

static void *s_calloc_stub(struct aws_allocator *allocator, size_t num, size_t size) {
    allocator->impl = (void *)(num * size);
    return calloc(num, size);
}

static void s_mem_release_stub(struct aws_allocator *allocator, void *ptr) {
    allocator->impl = 0;
    free(ptr);
}

static int s_test_calloc_on_given_allocator(struct aws_allocator *allocator, bool using_calloc_stub_impl) {
    /* Check that calloc gives 0ed memory */
    char *p = aws_mem_calloc(allocator, 2, 4);
    ASSERT_NOT_NULL(p);
    for (size_t i = 0; i < 2 * 4; ++i) {
        ASSERT_TRUE(p[i] == 0);
    }
    if (using_calloc_stub_impl) {
        ASSERT_TRUE((intptr_t)allocator->impl == 8);
    }
    aws_mem_release(allocator, p);
    return AWS_OP_SUCCESS;
}

AWS_TEST_CASE(test_calloc_override, s_test_calloc_override_fn)
static int s_test_calloc_override_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    struct aws_allocator my_alloc = {
        .mem_calloc = s_calloc_stub,
        .mem_release = s_mem_release_stub,
    };
    return s_test_calloc_on_given_allocator(&my_alloc, true);
}

AWS_TEST_CASE(test_calloc_fallback_from_default_allocator, s_test_calloc_fallback_from_default_allocator_fn)
static int s_test_calloc_fallback_from_default_allocator_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_allocator my_alloc = *aws_default_allocator();
    my_alloc.mem_calloc = NULL;
    return s_test_calloc_on_given_allocator(&my_alloc, false);
}

AWS_TEST_CASE(test_calloc_fallback_from_given, s_test_calloc_fallback_from_given_fn)
static int s_test_calloc_fallback_from_given_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;

    struct aws_allocator my_alloc = *allocator;
    my_alloc.mem_calloc = NULL;
    return s_test_calloc_on_given_allocator(&my_alloc, false);
}

AWS_TEST_CASE(test_calloc_from_default_allocator, s_test_calloc_from_default_allocator_fn)
static int s_test_calloc_from_default_allocator_fn(struct aws_allocator *allocator, void *ctx) {
    (void)allocator;
    (void)ctx;
    return s_test_calloc_on_given_allocator(aws_default_allocator(), false);
}

AWS_TEST_CASE(test_calloc_from_given_allocator, s_test_calloc_from_given_allocator_fn)
static int s_test_calloc_from_given_allocator_fn(struct aws_allocator *allocator, void *ctx) {
    (void)ctx;
    return s_test_calloc_on_given_allocator(allocator, false);
}