|
| 1 | + /* Copyright 2021 Google LLC |
| 2 | + * |
| 3 | + * This is part of the Google Cloud IoT Device SDK for Embedded C. |
| 4 | + * It is licensed under the BSD 3-Clause license; you may not use this file |
| 5 | + * except in compliance with the License. |
| 6 | + * |
| 7 | + * You may obtain a copy of the License at: |
| 8 | + * https://opensource.org/licenses/BSD-3-Clause |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#include "iotc_macros.h" |
| 18 | +#include "iotc_tt_testcase_management.h" |
| 19 | +#include "tinytest.h" |
| 20 | +#include "tinytest_macros.h" |
| 21 | + |
| 22 | +#include "iotc.h" |
| 23 | +#include "iotc_allocator.h" |
| 24 | + |
| 25 | +#include <errno.h> |
| 26 | +#include <stdio.h> |
| 27 | +#include <stdlib.h> |
| 28 | +#include <string.h> |
| 29 | +#include <time.h> |
| 30 | + |
| 31 | +#ifndef IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
| 32 | + |
| 33 | +#endif // IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
| 34 | + |
| 35 | +IOTC_TT_TESTGROUP_BEGIN(utest_memory_calloc) |
| 36 | + |
| 37 | +/* Happy cases. */ |
| 38 | +IOTC_TT_TESTCASE( |
| 39 | + utest__iotc_memory_calloc__num_1__size_8, |
| 40 | + { |
| 41 | + void* ptr = __iotc_calloc( /*num=*/1, /*size=*/8); |
| 42 | + tt_want_ptr_op(NULL, !=, ptr); |
| 43 | + __iotc_free(ptr); |
| 44 | + }) |
| 45 | + |
| 46 | +IOTC_TT_TESTCASE( |
| 47 | + utest__iotc_memory_calloc___num_255__size_55, |
| 48 | + { |
| 49 | + void* ptr = __iotc_calloc( /*num=*/255, /*size=*/55); |
| 50 | + tt_want_ptr_op(NULL, !=, ptr); |
| 51 | + __iotc_free(ptr); |
| 52 | + }) |
| 53 | + |
| 54 | +IOTC_TT_TESTCASE( |
| 55 | + utest__iotc_memory_calloc___num_1000000__size_88, |
| 56 | + { |
| 57 | + void* ptr = __iotc_calloc( /*num=*/1000000, /*size=*/88); |
| 58 | + tt_want_ptr_op(NULL, !=, ptr); |
| 59 | + __iotc_free(ptr); |
| 60 | + }) |
| 61 | + |
| 62 | +/* Edge cases. */ |
| 63 | +IOTC_TT_TESTCASE( |
| 64 | + utest__iotc_memory_calloc__num_0__size_8, |
| 65 | + { |
| 66 | + void* ptr = __iotc_calloc( /*num=*/0, /*size=*/8); |
| 67 | + tt_want_ptr_op(NULL, ==, ptr); |
| 68 | + __iotc_free(ptr); |
| 69 | + }) |
| 70 | + |
| 71 | +IOTC_TT_TESTCASE( |
| 72 | + utest__iotc_memory_calloc__num_1__size_0, |
| 73 | + { |
| 74 | + void* ptr = __iotc_calloc( /*num=*/1, /*size=*/0); |
| 75 | + tt_want_ptr_op(NULL, ==, ptr); |
| 76 | + __iotc_free(ptr); |
| 77 | + }) |
| 78 | + |
| 79 | +IOTC_TT_TESTCASE( |
| 80 | + utest__iotc_memory_calloc__num_0__size_0, |
| 81 | + { |
| 82 | + void* ptr = __iotc_calloc( /*num=*/0, /*size=*/0); |
| 83 | + tt_want_ptr_op(NULL, ==, ptr); |
| 84 | + __iotc_free(ptr); |
| 85 | + }) |
| 86 | + |
| 87 | +IOTC_TT_TESTCASE( |
| 88 | + utest__iotc_memory_calloc__num_max__size_0, |
| 89 | + { |
| 90 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX, /*size=*/0); |
| 91 | + tt_want_ptr_op(NULL, ==, ptr); |
| 92 | + __iotc_free(ptr); |
| 93 | + }) |
| 94 | + |
| 95 | +IOTC_TT_TESTCASE( |
| 96 | + utest__iotc_memory_calloc__num_0__size_max, |
| 97 | + { |
| 98 | + void* ptr = __iotc_calloc( /*num=*/0, /*size=*/SIZE_MAX); |
| 99 | + tt_want_ptr_op(NULL, ==, ptr); |
| 100 | + __iotc_free(ptr); |
| 101 | + }) |
| 102 | + |
| 103 | +/* Edge cases - SIZE_MAX |
| 104 | + Note: FreeRTOS heap size isn't large enough for SIZE_MAX allocations, |
| 105 | + so these tests are disabled for those targets. */ |
| 106 | +#ifndef IOTC_TARGET_FREERTOS |
| 107 | +IOTC_TT_TESTCASE( |
| 108 | + utest__iotc_memory_calloc__num_1__size_max, |
| 109 | + { |
| 110 | + void* ptr = __iotc_calloc( /*num=*/1, /*size=*/SIZE_MAX); |
| 111 | + tt_want_ptr_op(NULL, ==, ptr); |
| 112 | + __iotc_free(ptr); |
| 113 | + }) |
| 114 | + |
| 115 | +IOTC_TT_TESTCASE( |
| 116 | + utest__iotc_memory_calloc__num_max_minus_1__size_1, |
| 117 | + { |
| 118 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX-1, /*size=*/1); |
| 119 | + tt_want_ptr_op(NULL, ==, ptr); |
| 120 | + __iotc_free(ptr); |
| 121 | + }) |
| 122 | + |
| 123 | +IOTC_TT_TESTCASE( |
| 124 | + utest__iotc_memory_calloc__overflow__num_half_max__size_2, |
| 125 | + { |
| 126 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX/2, /*size=*/2); |
| 127 | + tt_want_ptr_op(NULL, ==, ptr); |
| 128 | + __iotc_free(ptr); |
| 129 | + }) |
| 130 | + |
| 131 | +IOTC_TT_TESTCASE( |
| 132 | + utest__iotc_memory_calloc__overflow__num_2__size_half_max, |
| 133 | + { |
| 134 | + void* ptr = __iotc_calloc( /*num=*/2, /*size=*/SIZE_MAX/2); |
| 135 | + tt_want_ptr_op(NULL, ==, ptr); |
| 136 | + __iotc_free(ptr); |
| 137 | + }) |
| 138 | + |
| 139 | +IOTC_TT_TESTCASE( |
| 140 | + utest__iotc_memory_calloc__num_1__size_max_minus_1, |
| 141 | + { |
| 142 | + void* ptr = __iotc_calloc( /*num=*/1, /*size=*/SIZE_MAX-1); |
| 143 | + tt_want_ptr_op(NULL, ==, ptr); |
| 144 | + __iotc_free(ptr); |
| 145 | + }) |
| 146 | + |
| 147 | +IOTC_TT_TESTCASE( |
| 148 | + utest__iotc_memory_calloc__num_size_max_minus_1__size_1, |
| 149 | + { |
| 150 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX-1, /*size=*/1); |
| 151 | + tt_want_ptr_op(NULL, ==, ptr); |
| 152 | + __iotc_free(ptr); |
| 153 | + }) |
| 154 | +#endif /* IOTC_TARGET_FREERTOS */ |
| 155 | + |
| 156 | +/* Overflow cases. */ |
| 157 | +IOTC_TT_TESTCASE( |
| 158 | + utest__iotc_memory_calloc__overflow__num_3__size_half_max, |
| 159 | + { |
| 160 | + void* ptr = __iotc_calloc( /*num=*/3, /*size=*/SIZE_MAX/2); |
| 161 | + tt_want_ptr_op(NULL, ==, ptr); |
| 162 | + __iotc_free(ptr); |
| 163 | + }) |
| 164 | + |
| 165 | +IOTC_TT_TESTCASE( |
| 166 | + utest__iotc_memory_calloc__overflow__num_half_max__size_3, |
| 167 | + { |
| 168 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX/2, /*size=*/3) ; |
| 169 | + tt_want_ptr_op(NULL, ==, ptr); |
| 170 | + __iotc_free(ptr); |
| 171 | + }) |
| 172 | + |
| 173 | +IOTC_TT_TESTCASE( |
| 174 | + utest__iotc_memory_calloc__overflow__num_half_max_plus_1__size_2, |
| 175 | + { |
| 176 | + void* ptr = __iotc_calloc( /*num=*/SIZE_MAX/2+1, /*size=*/2) ; |
| 177 | + tt_want_ptr_op(NULL, ==, ptr); |
| 178 | + __iotc_free(ptr); |
| 179 | + }) |
| 180 | + |
| 181 | +IOTC_TT_TESTCASE( |
| 182 | + utest__iotc_memory_calloc__overflow__num_3__size_half_max_plus_1, |
| 183 | + { |
| 184 | + void* ptr = __iotc_calloc( /*num=*/3, /*size=*/SIZE_MAX/2+1) ; |
| 185 | + tt_want_ptr_op(NULL, ==, ptr); |
| 186 | + __iotc_free(ptr); |
| 187 | + }) |
| 188 | + |
| 189 | +IOTC_TT_TESTGROUP_END |
| 190 | + |
| 191 | +#ifndef IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
| 192 | +#define IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
| 193 | +#include __FILE__ |
| 194 | +#undef IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
| 195 | +#endif // IOTC_TT_TESTCASE_ENUMERATION__SECONDPREPROCESSORRUN |
0 commit comments