Loading...
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 204 205 206 207 208 209 210 211 | /* * Copyright (c) 2017 Intel Corporation * * SPDX-License-Identifier: Apache-2.0 */ /* * @file test dynamic memory allocation using C libraries * * This module verifies that the various dynamic memory allocation functions * works fine with minimal C library and newlib C library. * * IMPORTANT: The module only ensures that each supported library is present, * and that a bare minimum of its functionality is operating correctly. It does * NOT guarantee that ALL standards-defined functionality is present, nor does * it guarantee that ALL functionality provided is working correctly. */ #include <zephyr.h> #include <ztest.h> #include <stdlib.h> #include <errno.h> #define BUF_LEN 10 /** * @brief Test dynamic memory allocation using malloc * * @see malloc(), free() */ void test_malloc(void) { /* Initialize error number to avoid garbage value, in case of SUCCESS */ int *iptr = NULL; iptr = malloc(BUF_LEN * sizeof(int)); zassert_not_null((iptr), "malloc failed, errno: %d", errno); memset(iptr, 'p', BUF_LEN * sizeof(int)); free(iptr); iptr = NULL; } /** * @brief Test dynamic memory allocation free function * * @see free() */ void test_free(void) { /* * In free, if ptr is passed as NULL, no operation is performed * Just make sure, no exception occurs and test pass */ free(NULL); } /** * @brief Test dynamic memory allocation using calloc * * @see calloc(), free() */ #define CALLOC_BUFLEN (200) static ZTEST_BMEM unsigned char zerobuf[CALLOC_BUFLEN]; void test_calloc(void) { char *cptr = NULL; cptr = calloc(CALLOC_BUFLEN, sizeof(char)); zassert_not_null((cptr), "calloc failed, errno: %d", errno); zassert_true(((memcmp(cptr, zerobuf, CALLOC_BUFLEN)) == 0), "calloc failed to set zero value, errno: %d", errno); memset(cptr, 'p', CALLOC_BUFLEN); free(cptr); cptr = NULL; } /** * @brief Test dynamic memory allocation using realloc * * @see malloc(), realloc(), free() */ ZTEST_BMEM unsigned char filled_buf[BUF_LEN]; void test_realloc(void) { char orig_size = BUF_LEN; char new_size = BUF_LEN + BUF_LEN; char *ptr = NULL; char *reloc_ptr = NULL; ptr = malloc(orig_size); zassert_not_null((ptr), "malloc failed, errno: %d", errno); (void)memset(ptr, 'p', orig_size); reloc_ptr = realloc(ptr, new_size); zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno); zassert_not_null((ptr), "malloc/realloc failed, errno: %d", errno); ptr = reloc_ptr; (void)memset(filled_buf, 'p', BUF_LEN); zassert_true(((memcmp(ptr, filled_buf, BUF_LEN)) == 0), "realloc failed to copy malloc data, errno: %d", errno); free(ptr); ptr = NULL; } /** * @brief Test dynamic memory allocation using reallocarray * * @see malloc(), reallocarray(), free() */ #ifdef CONFIG_NEWLIB_LIBC void test_reallocarray(void) { /* reallocarray not implemented for newlib */ ztest_test_skip(); } #else void test_reallocarray(void) { char orig_size = BUF_LEN; char *ptr = NULL; ptr = malloc(orig_size); zassert_not_null((ptr), "malloc failed, errno: %d", errno); (void)memset(ptr, 'p', orig_size); char *reloc_ptr = reallocarray(ptr, 2, orig_size); zassert_not_null(reloc_ptr, "reallocarray failed"); zassert_not_null((ptr), "malloc/reallocarray failed, errno: %d", errno); ptr = reloc_ptr; (void)memset(filled_buf, 'p', BUF_LEN); zassert_true(((memcmp(ptr, filled_buf, BUF_LEN)) == 0), "realloc failed to copy malloc data, errno: %d", errno); free(ptr); ptr = NULL; } #endif #define MAX_LEN (10 * BUF_LEN) /** * @brief Test dynamic memory allocation functions * * @see malloc(), calloc(), realloc(), free() */ void test_memalloc_all(void) { char *mlc_ptr = NULL; char *clc_ptr = NULL; char *reloc_ptr = NULL; int orig_size = BUF_LEN; int new_size = MAX_LEN; mlc_ptr = malloc(orig_size); zassert_not_null((mlc_ptr), "malloc failed, errno: %d", errno); clc_ptr = calloc(100, sizeof(char)); zassert_not_null((clc_ptr), "calloc failed, errno: %d", errno); reloc_ptr = realloc(mlc_ptr, new_size); zassert_not_null(reloc_ptr, "realloc failed, errno: %d", errno); zassert_not_null((mlc_ptr), "malloc/realloc failed, errno: %d", errno); mlc_ptr = reloc_ptr; free(mlc_ptr); free(clc_ptr); mlc_ptr = NULL; clc_ptr = NULL; reloc_ptr = NULL; } /** * * @brief Test dynamic memory allocation upto maximum size * Negative test case * */ void test_memalloc_max(void) { char *ptr = NULL; ptr = malloc(0x7fffffff); zassert_is_null(ptr, "malloc passed unexpectedly"); free(ptr); ptr = NULL; } void test_main(void) { ztest_test_suite(test_c_lib_dynamic_memalloc, ztest_user_unit_test(test_malloc), ztest_user_unit_test(test_free), ztest_user_unit_test(test_calloc), ztest_user_unit_test(test_realloc), ztest_user_unit_test(test_reallocarray), ztest_user_unit_test(test_memalloc_all), ztest_user_unit_test(test_memalloc_max) ); ztest_run_test_suite(test_c_lib_dynamic_memalloc); } |