Marco Zecchini
/
Example_RTOS
Rtos API example
mbed-os/TESTS/mbedmicro-rtos-mbed/MemoryPool/main.cpp@0:9fca2b23d0ba, 2019-02-23 (annotated)
- Committer:
- marcozecchini
- Date:
- Sat Feb 23 12:13:36 2019 +0000
- Revision:
- 0:9fca2b23d0ba
final commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
marcozecchini | 0:9fca2b23d0ba | 1 | /* mbed Microcontroller Library |
marcozecchini | 0:9fca2b23d0ba | 2 | * Copyright (c) 2017 ARM Limited |
marcozecchini | 0:9fca2b23d0ba | 3 | * |
marcozecchini | 0:9fca2b23d0ba | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
marcozecchini | 0:9fca2b23d0ba | 5 | * you may not use this file except in compliance with the License. |
marcozecchini | 0:9fca2b23d0ba | 6 | * You may obtain a copy of the License at |
marcozecchini | 0:9fca2b23d0ba | 7 | * |
marcozecchini | 0:9fca2b23d0ba | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
marcozecchini | 0:9fca2b23d0ba | 9 | * |
marcozecchini | 0:9fca2b23d0ba | 10 | * Unless required by applicable law or agreed to in writing, software |
marcozecchini | 0:9fca2b23d0ba | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
marcozecchini | 0:9fca2b23d0ba | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
marcozecchini | 0:9fca2b23d0ba | 13 | * See the License for the specific language governing permissions and |
marcozecchini | 0:9fca2b23d0ba | 14 | * limitations under the License. |
marcozecchini | 0:9fca2b23d0ba | 15 | */ |
marcozecchini | 0:9fca2b23d0ba | 16 | #include "mbed.h" |
marcozecchini | 0:9fca2b23d0ba | 17 | #include "greentea-client/test_env.h" |
marcozecchini | 0:9fca2b23d0ba | 18 | #include "unity.h" |
marcozecchini | 0:9fca2b23d0ba | 19 | #include "utest.h" |
marcozecchini | 0:9fca2b23d0ba | 20 | |
marcozecchini | 0:9fca2b23d0ba | 21 | using namespace utest::v1; |
marcozecchini | 0:9fca2b23d0ba | 22 | |
marcozecchini | 0:9fca2b23d0ba | 23 | /* Enum used to select block allocation method. */ |
marcozecchini | 0:9fca2b23d0ba | 24 | typedef enum |
marcozecchini | 0:9fca2b23d0ba | 25 | { |
marcozecchini | 0:9fca2b23d0ba | 26 | ALLOC, CALLOC |
marcozecchini | 0:9fca2b23d0ba | 27 | } AllocType; |
marcozecchini | 0:9fca2b23d0ba | 28 | |
marcozecchini | 0:9fca2b23d0ba | 29 | /* Structure for complex block type. */ |
marcozecchini | 0:9fca2b23d0ba | 30 | typedef struct |
marcozecchini | 0:9fca2b23d0ba | 31 | { |
marcozecchini | 0:9fca2b23d0ba | 32 | int a; |
marcozecchini | 0:9fca2b23d0ba | 33 | char b; |
marcozecchini | 0:9fca2b23d0ba | 34 | int c; |
marcozecchini | 0:9fca2b23d0ba | 35 | } COMPLEX_TYPE; |
marcozecchini | 0:9fca2b23d0ba | 36 | |
marcozecchini | 0:9fca2b23d0ba | 37 | /* Function to check if complex type object is cleared.*/ |
marcozecchini | 0:9fca2b23d0ba | 38 | bool comp_is_cleared(COMPLEX_TYPE *object) |
marcozecchini | 0:9fca2b23d0ba | 39 | { |
marcozecchini | 0:9fca2b23d0ba | 40 | if (object->a == 0 && object->b == 0 && object->c == 0) { |
marcozecchini | 0:9fca2b23d0ba | 41 | return true; |
marcozecchini | 0:9fca2b23d0ba | 42 | } |
marcozecchini | 0:9fca2b23d0ba | 43 | |
marcozecchini | 0:9fca2b23d0ba | 44 | return false; |
marcozecchini | 0:9fca2b23d0ba | 45 | } |
marcozecchini | 0:9fca2b23d0ba | 46 | |
marcozecchini | 0:9fca2b23d0ba | 47 | /* Function to check if complex type object holds specified values.*/ |
marcozecchini | 0:9fca2b23d0ba | 48 | bool comp_is_equal(COMPLEX_TYPE *object, int a, char b, int c) |
marcozecchini | 0:9fca2b23d0ba | 49 | { |
marcozecchini | 0:9fca2b23d0ba | 50 | if (object->a == a && object->b == b && object->c == c) { |
marcozecchini | 0:9fca2b23d0ba | 51 | return true; |
marcozecchini | 0:9fca2b23d0ba | 52 | } |
marcozecchini | 0:9fca2b23d0ba | 53 | |
marcozecchini | 0:9fca2b23d0ba | 54 | return false; |
marcozecchini | 0:9fca2b23d0ba | 55 | } |
marcozecchini | 0:9fca2b23d0ba | 56 | |
marcozecchini | 0:9fca2b23d0ba | 57 | /* Function to set complex type object fields.*/ |
marcozecchini | 0:9fca2b23d0ba | 58 | void comp_set(COMPLEX_TYPE *object, int a, char b, int c) |
marcozecchini | 0:9fca2b23d0ba | 59 | { |
marcozecchini | 0:9fca2b23d0ba | 60 | object->a = a; |
marcozecchini | 0:9fca2b23d0ba | 61 | object->b = b; |
marcozecchini | 0:9fca2b23d0ba | 62 | object->c = c; |
marcozecchini | 0:9fca2b23d0ba | 63 | } |
marcozecchini | 0:9fca2b23d0ba | 64 | |
marcozecchini | 0:9fca2b23d0ba | 65 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 66 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 67 | * |
marcozecchini | 0:9fca2b23d0ba | 68 | * Given MemoryPool object of the specified type and queue size has |
marcozecchini | 0:9fca2b23d0ba | 69 | * been successfully created. |
marcozecchini | 0:9fca2b23d0ba | 70 | * When max number of blocks is allocated from the pool. |
marcozecchini | 0:9fca2b23d0ba | 71 | * Then all allocations are successful. |
marcozecchini | 0:9fca2b23d0ba | 72 | * |
marcozecchini | 0:9fca2b23d0ba | 73 | * */ |
marcozecchini | 0:9fca2b23d0ba | 74 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 75 | void test_mem_pool_alloc_success(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 76 | { |
marcozecchini | 0:9fca2b23d0ba | 77 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 78 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 79 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 80 | |
marcozecchini | 0:9fca2b23d0ba | 81 | /* Test alloc()/calloc() methods - try to allocate max number of |
marcozecchini | 0:9fca2b23d0ba | 82 | blocks. All allocations should be successful. */ |
marcozecchini | 0:9fca2b23d0ba | 83 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 84 | /* Allocate memory block. */ |
marcozecchini | 0:9fca2b23d0ba | 85 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 86 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 87 | } else { |
marcozecchini | 0:9fca2b23d0ba | 88 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 89 | } |
marcozecchini | 0:9fca2b23d0ba | 90 | |
marcozecchini | 0:9fca2b23d0ba | 91 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 92 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 93 | |
marcozecchini | 0:9fca2b23d0ba | 94 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 95 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 96 | TEST_ASSERT_EQUAL(0, *p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 97 | } |
marcozecchini | 0:9fca2b23d0ba | 98 | |
marcozecchini | 0:9fca2b23d0ba | 99 | /* Init fields. */ |
marcozecchini | 0:9fca2b23d0ba | 100 | *p_blocks[i] = (i + 5); |
marcozecchini | 0:9fca2b23d0ba | 101 | } |
marcozecchini | 0:9fca2b23d0ba | 102 | |
marcozecchini | 0:9fca2b23d0ba | 103 | /* Check if blocks holds valid values. */ |
marcozecchini | 0:9fca2b23d0ba | 104 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 105 | TEST_ASSERT_EQUAL((i + 5), *p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 106 | } |
marcozecchini | 0:9fca2b23d0ba | 107 | } |
marcozecchini | 0:9fca2b23d0ba | 108 | |
marcozecchini | 0:9fca2b23d0ba | 109 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 110 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 111 | * |
marcozecchini | 0:9fca2b23d0ba | 112 | * Complex memory pool block type is used. |
marcozecchini | 0:9fca2b23d0ba | 113 | * |
marcozecchini | 0:9fca2b23d0ba | 114 | * Given MemoryPool object of the specified type and queue size has |
marcozecchini | 0:9fca2b23d0ba | 115 | * been successfully created. |
marcozecchini | 0:9fca2b23d0ba | 116 | * When max number of blocks is allocated from the pool. |
marcozecchini | 0:9fca2b23d0ba | 117 | * Then all allocations are successful. |
marcozecchini | 0:9fca2b23d0ba | 118 | * |
marcozecchini | 0:9fca2b23d0ba | 119 | * */ |
marcozecchini | 0:9fca2b23d0ba | 120 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 121 | void test_mem_pool_alloc_success_complex(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 122 | { |
marcozecchini | 0:9fca2b23d0ba | 123 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 124 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 125 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 126 | |
marcozecchini | 0:9fca2b23d0ba | 127 | /* Test alloc()/calloc() methods - try to allocate max number of |
marcozecchini | 0:9fca2b23d0ba | 128 | blocks. All allocations should be successful. */ |
marcozecchini | 0:9fca2b23d0ba | 129 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 130 | /* Allocate memory block. */ |
marcozecchini | 0:9fca2b23d0ba | 131 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 132 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 133 | } else { |
marcozecchini | 0:9fca2b23d0ba | 134 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 135 | } |
marcozecchini | 0:9fca2b23d0ba | 136 | |
marcozecchini | 0:9fca2b23d0ba | 137 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 138 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 139 | |
marcozecchini | 0:9fca2b23d0ba | 140 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 141 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 142 | TEST_ASSERT_EQUAL(true, comp_is_cleared(p_blocks[i])); |
marcozecchini | 0:9fca2b23d0ba | 143 | } |
marcozecchini | 0:9fca2b23d0ba | 144 | |
marcozecchini | 0:9fca2b23d0ba | 145 | /* Init fields. */ |
marcozecchini | 0:9fca2b23d0ba | 146 | comp_set(p_blocks[i], i + 1, i + 2, i + 3); |
marcozecchini | 0:9fca2b23d0ba | 147 | } |
marcozecchini | 0:9fca2b23d0ba | 148 | |
marcozecchini | 0:9fca2b23d0ba | 149 | /* Check if blocks holds valid values. */ |
marcozecchini | 0:9fca2b23d0ba | 150 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 151 | TEST_ASSERT_EQUAL(true, comp_is_equal(p_blocks[i], i + 1, i + 2, i + 3)); |
marcozecchini | 0:9fca2b23d0ba | 152 | } |
marcozecchini | 0:9fca2b23d0ba | 153 | } |
marcozecchini | 0:9fca2b23d0ba | 154 | |
marcozecchini | 0:9fca2b23d0ba | 155 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 156 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 157 | * |
marcozecchini | 0:9fca2b23d0ba | 158 | * Given MemoryPool has already max number of blocks allocated from the pool. |
marcozecchini | 0:9fca2b23d0ba | 159 | * When next block is allocated. |
marcozecchini | 0:9fca2b23d0ba | 160 | * Then allocation fails. |
marcozecchini | 0:9fca2b23d0ba | 161 | * |
marcozecchini | 0:9fca2b23d0ba | 162 | * */ |
marcozecchini | 0:9fca2b23d0ba | 163 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 164 | void test_mem_pool_alloc_fail(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 165 | { |
marcozecchini | 0:9fca2b23d0ba | 166 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 167 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 168 | T * p_extra_block; |
marcozecchini | 0:9fca2b23d0ba | 169 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 170 | |
marcozecchini | 0:9fca2b23d0ba | 171 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 172 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 173 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 174 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 175 | } else { |
marcozecchini | 0:9fca2b23d0ba | 176 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 177 | } |
marcozecchini | 0:9fca2b23d0ba | 178 | |
marcozecchini | 0:9fca2b23d0ba | 179 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 180 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 181 | } |
marcozecchini | 0:9fca2b23d0ba | 182 | |
marcozecchini | 0:9fca2b23d0ba | 183 | /* There are no more blocks available. Try to allocate another block. */ |
marcozecchini | 0:9fca2b23d0ba | 184 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 185 | p_extra_block = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 186 | } else { |
marcozecchini | 0:9fca2b23d0ba | 187 | p_extra_block = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 188 | } |
marcozecchini | 0:9fca2b23d0ba | 189 | |
marcozecchini | 0:9fca2b23d0ba | 190 | /* Show that memory pool block has NOT been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 191 | TEST_ASSERT_NULL(p_extra_block); |
marcozecchini | 0:9fca2b23d0ba | 192 | } |
marcozecchini | 0:9fca2b23d0ba | 193 | |
marcozecchini | 0:9fca2b23d0ba | 194 | /* Template for functional tests for free() function |
marcozecchini | 0:9fca2b23d0ba | 195 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 196 | * |
marcozecchini | 0:9fca2b23d0ba | 197 | * Given MemoryPool has all blocks allocated. |
marcozecchini | 0:9fca2b23d0ba | 198 | * When free operation is executed on the each allocated block. |
marcozecchini | 0:9fca2b23d0ba | 199 | * Then each deallocation is successfully performed. |
marcozecchini | 0:9fca2b23d0ba | 200 | * |
marcozecchini | 0:9fca2b23d0ba | 201 | * */ |
marcozecchini | 0:9fca2b23d0ba | 202 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 203 | void test_mem_pool_free_success(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 204 | { |
marcozecchini | 0:9fca2b23d0ba | 205 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 206 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 207 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 208 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 209 | |
marcozecchini | 0:9fca2b23d0ba | 210 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 211 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 212 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 213 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 214 | } else { |
marcozecchini | 0:9fca2b23d0ba | 215 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 216 | } |
marcozecchini | 0:9fca2b23d0ba | 217 | |
marcozecchini | 0:9fca2b23d0ba | 218 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 219 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 220 | } |
marcozecchini | 0:9fca2b23d0ba | 221 | |
marcozecchini | 0:9fca2b23d0ba | 222 | /* Free all memory blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 223 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 224 | status = mem_pool.free(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 225 | |
marcozecchini | 0:9fca2b23d0ba | 226 | /* Check operation status. */ |
marcozecchini | 0:9fca2b23d0ba | 227 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 228 | } |
marcozecchini | 0:9fca2b23d0ba | 229 | } |
marcozecchini | 0:9fca2b23d0ba | 230 | |
marcozecchini | 0:9fca2b23d0ba | 231 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 232 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 233 | * |
marcozecchini | 0:9fca2b23d0ba | 234 | * Basic memory pool block type is used. |
marcozecchini | 0:9fca2b23d0ba | 235 | * |
marcozecchini | 0:9fca2b23d0ba | 236 | * Given MemoryPool had all blocks allocated and one block has |
marcozecchini | 0:9fca2b23d0ba | 237 | * been freed (last). |
marcozecchini | 0:9fca2b23d0ba | 238 | * When next block is allocated. |
marcozecchini | 0:9fca2b23d0ba | 239 | * Then allocation is successful. |
marcozecchini | 0:9fca2b23d0ba | 240 | * |
marcozecchini | 0:9fca2b23d0ba | 241 | * */ |
marcozecchini | 0:9fca2b23d0ba | 242 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 243 | void test_mem_pool_free_realloc_last(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 244 | { |
marcozecchini | 0:9fca2b23d0ba | 245 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 246 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 247 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 248 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 249 | |
marcozecchini | 0:9fca2b23d0ba | 250 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 251 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 252 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 253 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 254 | } else { |
marcozecchini | 0:9fca2b23d0ba | 255 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 256 | } |
marcozecchini | 0:9fca2b23d0ba | 257 | |
marcozecchini | 0:9fca2b23d0ba | 258 | /* Init block. */ |
marcozecchini | 0:9fca2b23d0ba | 259 | *p_blocks[i] = 0xAB; |
marcozecchini | 0:9fca2b23d0ba | 260 | |
marcozecchini | 0:9fca2b23d0ba | 261 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 262 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 263 | } |
marcozecchini | 0:9fca2b23d0ba | 264 | |
marcozecchini | 0:9fca2b23d0ba | 265 | /* Free the last block. */ |
marcozecchini | 0:9fca2b23d0ba | 266 | status = mem_pool.free(p_blocks[numOfEntries - 1]); |
marcozecchini | 0:9fca2b23d0ba | 267 | |
marcozecchini | 0:9fca2b23d0ba | 268 | /* Check status. */ |
marcozecchini | 0:9fca2b23d0ba | 269 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 270 | |
marcozecchini | 0:9fca2b23d0ba | 271 | /* Try to allocate another block (one block is now available). */ |
marcozecchini | 0:9fca2b23d0ba | 272 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 273 | p_blocks[numOfEntries - 1] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 274 | } else { |
marcozecchini | 0:9fca2b23d0ba | 275 | p_blocks[numOfEntries - 1] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 276 | } |
marcozecchini | 0:9fca2b23d0ba | 277 | |
marcozecchini | 0:9fca2b23d0ba | 278 | /* Show that memory pool block has been now allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 279 | TEST_ASSERT_NOT_NULL(p_blocks[numOfEntries - 1]); |
marcozecchini | 0:9fca2b23d0ba | 280 | |
marcozecchini | 0:9fca2b23d0ba | 281 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 282 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 283 | TEST_ASSERT_EQUAL(0, *p_blocks[numOfEntries - 1]); |
marcozecchini | 0:9fca2b23d0ba | 284 | } |
marcozecchini | 0:9fca2b23d0ba | 285 | } |
marcozecchini | 0:9fca2b23d0ba | 286 | |
marcozecchini | 0:9fca2b23d0ba | 287 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 288 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 289 | * |
marcozecchini | 0:9fca2b23d0ba | 290 | * Complex memory pool block type is used. |
marcozecchini | 0:9fca2b23d0ba | 291 | * |
marcozecchini | 0:9fca2b23d0ba | 292 | * Given MemoryPool had all blocks allocated and one block has |
marcozecchini | 0:9fca2b23d0ba | 293 | * been freed (last). |
marcozecchini | 0:9fca2b23d0ba | 294 | * When next block is allocated. |
marcozecchini | 0:9fca2b23d0ba | 295 | * Then allocation is successful. |
marcozecchini | 0:9fca2b23d0ba | 296 | * |
marcozecchini | 0:9fca2b23d0ba | 297 | * */ |
marcozecchini | 0:9fca2b23d0ba | 298 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 299 | void test_mem_pool_free_realloc_last_complex(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 300 | { |
marcozecchini | 0:9fca2b23d0ba | 301 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 302 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 303 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 304 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 305 | |
marcozecchini | 0:9fca2b23d0ba | 306 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 307 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 308 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 309 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 310 | } else { |
marcozecchini | 0:9fca2b23d0ba | 311 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 312 | } |
marcozecchini | 0:9fca2b23d0ba | 313 | |
marcozecchini | 0:9fca2b23d0ba | 314 | /* Init block. */ |
marcozecchini | 0:9fca2b23d0ba | 315 | comp_set(p_blocks[i], i + 1, i + 2, i + 3); |
marcozecchini | 0:9fca2b23d0ba | 316 | |
marcozecchini | 0:9fca2b23d0ba | 317 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 318 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 319 | } |
marcozecchini | 0:9fca2b23d0ba | 320 | |
marcozecchini | 0:9fca2b23d0ba | 321 | /* Free the last block. */ |
marcozecchini | 0:9fca2b23d0ba | 322 | status = mem_pool.free(p_blocks[numOfEntries - 1]); |
marcozecchini | 0:9fca2b23d0ba | 323 | |
marcozecchini | 0:9fca2b23d0ba | 324 | /* Check status. */ |
marcozecchini | 0:9fca2b23d0ba | 325 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 326 | |
marcozecchini | 0:9fca2b23d0ba | 327 | /* Try to allocate another block (one block is now available). */ |
marcozecchini | 0:9fca2b23d0ba | 328 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 329 | p_blocks[numOfEntries - 1] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 330 | } else { |
marcozecchini | 0:9fca2b23d0ba | 331 | p_blocks[numOfEntries - 1] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 332 | } |
marcozecchini | 0:9fca2b23d0ba | 333 | |
marcozecchini | 0:9fca2b23d0ba | 334 | /* Show that memory pool block has been now allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 335 | TEST_ASSERT_NOT_NULL(p_blocks[numOfEntries - 1]); |
marcozecchini | 0:9fca2b23d0ba | 336 | |
marcozecchini | 0:9fca2b23d0ba | 337 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 338 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 339 | TEST_ASSERT_EQUAL(true, comp_is_cleared(p_blocks[numOfEntries - 1])); |
marcozecchini | 0:9fca2b23d0ba | 340 | } |
marcozecchini | 0:9fca2b23d0ba | 341 | } |
marcozecchini | 0:9fca2b23d0ba | 342 | |
marcozecchini | 0:9fca2b23d0ba | 343 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 344 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 345 | * |
marcozecchini | 0:9fca2b23d0ba | 346 | * Basic memory pool block type is used. |
marcozecchini | 0:9fca2b23d0ba | 347 | * |
marcozecchini | 0:9fca2b23d0ba | 348 | * Given MemoryPool had all blocks allocated and one block has |
marcozecchini | 0:9fca2b23d0ba | 349 | * been freed (first). |
marcozecchini | 0:9fca2b23d0ba | 350 | * When next block is allocated. |
marcozecchini | 0:9fca2b23d0ba | 351 | * Then allocation is successful. |
marcozecchini | 0:9fca2b23d0ba | 352 | * |
marcozecchini | 0:9fca2b23d0ba | 353 | * */ |
marcozecchini | 0:9fca2b23d0ba | 354 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 355 | void test_mem_pool_free_realloc_first(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 356 | { |
marcozecchini | 0:9fca2b23d0ba | 357 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 358 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 359 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 360 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 361 | |
marcozecchini | 0:9fca2b23d0ba | 362 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 363 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 364 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 365 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 366 | } else { |
marcozecchini | 0:9fca2b23d0ba | 367 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 368 | } |
marcozecchini | 0:9fca2b23d0ba | 369 | |
marcozecchini | 0:9fca2b23d0ba | 370 | /* Init block. */ |
marcozecchini | 0:9fca2b23d0ba | 371 | *p_blocks[i] = 0xAB; |
marcozecchini | 0:9fca2b23d0ba | 372 | |
marcozecchini | 0:9fca2b23d0ba | 373 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 374 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 375 | } |
marcozecchini | 0:9fca2b23d0ba | 376 | |
marcozecchini | 0:9fca2b23d0ba | 377 | /* Free the last block. */ |
marcozecchini | 0:9fca2b23d0ba | 378 | status = mem_pool.free(p_blocks[0]); |
marcozecchini | 0:9fca2b23d0ba | 379 | |
marcozecchini | 0:9fca2b23d0ba | 380 | /* Check status. */ |
marcozecchini | 0:9fca2b23d0ba | 381 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 382 | |
marcozecchini | 0:9fca2b23d0ba | 383 | /* Try to allocate another block (one block is now available). */ |
marcozecchini | 0:9fca2b23d0ba | 384 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 385 | p_blocks[0] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 386 | } else { |
marcozecchini | 0:9fca2b23d0ba | 387 | p_blocks[0] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 388 | } |
marcozecchini | 0:9fca2b23d0ba | 389 | |
marcozecchini | 0:9fca2b23d0ba | 390 | /* Show that memory pool block has been now allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 391 | TEST_ASSERT_NOT_NULL(p_blocks[0]); |
marcozecchini | 0:9fca2b23d0ba | 392 | |
marcozecchini | 0:9fca2b23d0ba | 393 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 394 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 395 | TEST_ASSERT_EQUAL(0, *p_blocks[0]); |
marcozecchini | 0:9fca2b23d0ba | 396 | } |
marcozecchini | 0:9fca2b23d0ba | 397 | } |
marcozecchini | 0:9fca2b23d0ba | 398 | |
marcozecchini | 0:9fca2b23d0ba | 399 | /* Template for functional tests for alloc(), calloc() functions |
marcozecchini | 0:9fca2b23d0ba | 400 | * of MemoryPool object. |
marcozecchini | 0:9fca2b23d0ba | 401 | * |
marcozecchini | 0:9fca2b23d0ba | 402 | * Complex memory pool block type is used. |
marcozecchini | 0:9fca2b23d0ba | 403 | * |
marcozecchini | 0:9fca2b23d0ba | 404 | * Given MemoryPool had all blocks allocated and one block has |
marcozecchini | 0:9fca2b23d0ba | 405 | * been freed (first). |
marcozecchini | 0:9fca2b23d0ba | 406 | * When next block is allocated. |
marcozecchini | 0:9fca2b23d0ba | 407 | * Then allocation is successful. |
marcozecchini | 0:9fca2b23d0ba | 408 | * |
marcozecchini | 0:9fca2b23d0ba | 409 | * */ |
marcozecchini | 0:9fca2b23d0ba | 410 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 411 | void test_mem_pool_free_realloc_first_complex(AllocType atype) |
marcozecchini | 0:9fca2b23d0ba | 412 | { |
marcozecchini | 0:9fca2b23d0ba | 413 | MemoryPool<T, numOfEntries> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 414 | T * p_blocks[numOfEntries]; |
marcozecchini | 0:9fca2b23d0ba | 415 | uint32_t i; |
marcozecchini | 0:9fca2b23d0ba | 416 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 417 | |
marcozecchini | 0:9fca2b23d0ba | 418 | /* Allocate all available blocks. */ |
marcozecchini | 0:9fca2b23d0ba | 419 | for (i = 0; i < numOfEntries; i++) { |
marcozecchini | 0:9fca2b23d0ba | 420 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 421 | p_blocks[i] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 422 | } else { |
marcozecchini | 0:9fca2b23d0ba | 423 | p_blocks[i] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 424 | } |
marcozecchini | 0:9fca2b23d0ba | 425 | |
marcozecchini | 0:9fca2b23d0ba | 426 | /* Init block. */ |
marcozecchini | 0:9fca2b23d0ba | 427 | comp_set(p_blocks[i], i + 1, i + 2, i + 3); |
marcozecchini | 0:9fca2b23d0ba | 428 | |
marcozecchini | 0:9fca2b23d0ba | 429 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 430 | TEST_ASSERT_NOT_NULL(p_blocks[i]); |
marcozecchini | 0:9fca2b23d0ba | 431 | } |
marcozecchini | 0:9fca2b23d0ba | 432 | |
marcozecchini | 0:9fca2b23d0ba | 433 | /* Free the last block. */ |
marcozecchini | 0:9fca2b23d0ba | 434 | status = mem_pool.free(p_blocks[0]); |
marcozecchini | 0:9fca2b23d0ba | 435 | |
marcozecchini | 0:9fca2b23d0ba | 436 | /* Check status. */ |
marcozecchini | 0:9fca2b23d0ba | 437 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 438 | |
marcozecchini | 0:9fca2b23d0ba | 439 | /* Try to allocate another block (one block is now available). */ |
marcozecchini | 0:9fca2b23d0ba | 440 | if (atype == ALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 441 | p_blocks[0] = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 442 | } else { |
marcozecchini | 0:9fca2b23d0ba | 443 | p_blocks[0] = mem_pool.calloc(); |
marcozecchini | 0:9fca2b23d0ba | 444 | } |
marcozecchini | 0:9fca2b23d0ba | 445 | |
marcozecchini | 0:9fca2b23d0ba | 446 | /* Show that memory pool block has been now allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 447 | TEST_ASSERT_NOT_NULL(p_blocks[0]); |
marcozecchini | 0:9fca2b23d0ba | 448 | |
marcozecchini | 0:9fca2b23d0ba | 449 | /* Check if Calloc clears the block. */ |
marcozecchini | 0:9fca2b23d0ba | 450 | if (atype == CALLOC) { |
marcozecchini | 0:9fca2b23d0ba | 451 | TEST_ASSERT_EQUAL(true, comp_is_cleared(p_blocks[0])); |
marcozecchini | 0:9fca2b23d0ba | 452 | } |
marcozecchini | 0:9fca2b23d0ba | 453 | } |
marcozecchini | 0:9fca2b23d0ba | 454 | |
marcozecchini | 0:9fca2b23d0ba | 455 | /* Robustness checks for free() function. |
marcozecchini | 0:9fca2b23d0ba | 456 | * |
marcozecchini | 0:9fca2b23d0ba | 457 | * Given block from the MemoryPool has been successfully deallocated. |
marcozecchini | 0:9fca2b23d0ba | 458 | * When free operation is executed on this block again. |
marcozecchini | 0:9fca2b23d0ba | 459 | * Then operation fails with osErrorResource status. |
marcozecchini | 0:9fca2b23d0ba | 460 | * |
marcozecchini | 0:9fca2b23d0ba | 461 | * */ |
marcozecchini | 0:9fca2b23d0ba | 462 | void test_mem_pool_free_on_freed_block() |
marcozecchini | 0:9fca2b23d0ba | 463 | { |
marcozecchini | 0:9fca2b23d0ba | 464 | MemoryPool<int, 1> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 465 | int * p_block; |
marcozecchini | 0:9fca2b23d0ba | 466 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 467 | |
marcozecchini | 0:9fca2b23d0ba | 468 | /* Allocate memory block. */ |
marcozecchini | 0:9fca2b23d0ba | 469 | p_block = mem_pool.alloc(); |
marcozecchini | 0:9fca2b23d0ba | 470 | |
marcozecchini | 0:9fca2b23d0ba | 471 | /* Show that memory pool block has been allocated. */ |
marcozecchini | 0:9fca2b23d0ba | 472 | TEST_ASSERT_NOT_NULL(p_block); |
marcozecchini | 0:9fca2b23d0ba | 473 | |
marcozecchini | 0:9fca2b23d0ba | 474 | /* Free memory block. */ |
marcozecchini | 0:9fca2b23d0ba | 475 | status = mem_pool.free(p_block); |
marcozecchini | 0:9fca2b23d0ba | 476 | |
marcozecchini | 0:9fca2b23d0ba | 477 | /* Check operation status. */ |
marcozecchini | 0:9fca2b23d0ba | 478 | TEST_ASSERT_EQUAL(osOK, status); |
marcozecchini | 0:9fca2b23d0ba | 479 | |
marcozecchini | 0:9fca2b23d0ba | 480 | /* Free memory block again. */ |
marcozecchini | 0:9fca2b23d0ba | 481 | status = mem_pool.free(p_block); |
marcozecchini | 0:9fca2b23d0ba | 482 | |
marcozecchini | 0:9fca2b23d0ba | 483 | /* Check operation status. */ |
marcozecchini | 0:9fca2b23d0ba | 484 | TEST_ASSERT_EQUAL(osErrorResource, status); |
marcozecchini | 0:9fca2b23d0ba | 485 | } |
marcozecchini | 0:9fca2b23d0ba | 486 | |
marcozecchini | 0:9fca2b23d0ba | 487 | /* Robustness checks for free() function. |
marcozecchini | 0:9fca2b23d0ba | 488 | * Function under test is called with invalid parameters. |
marcozecchini | 0:9fca2b23d0ba | 489 | * |
marcozecchini | 0:9fca2b23d0ba | 490 | * Given MemoryPool object has been successfully created. |
marcozecchini | 0:9fca2b23d0ba | 491 | * When free operation is performed on NULL address. |
marcozecchini | 0:9fca2b23d0ba | 492 | * Then deallocation fails with osErrorParameter error. |
marcozecchini | 0:9fca2b23d0ba | 493 | * |
marcozecchini | 0:9fca2b23d0ba | 494 | */ |
marcozecchini | 0:9fca2b23d0ba | 495 | void free_block_invalid_parameter_null() |
marcozecchini | 0:9fca2b23d0ba | 496 | { |
marcozecchini | 0:9fca2b23d0ba | 497 | MemoryPool<int, 1> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 498 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 499 | |
marcozecchini | 0:9fca2b23d0ba | 500 | /* Try to free block passing invalid parameter (NULL). */ |
marcozecchini | 0:9fca2b23d0ba | 501 | status = mem_pool.free(NULL); |
marcozecchini | 0:9fca2b23d0ba | 502 | |
marcozecchini | 0:9fca2b23d0ba | 503 | /* Check operation status. */ |
marcozecchini | 0:9fca2b23d0ba | 504 | TEST_ASSERT_EQUAL(osErrorParameter, status); |
marcozecchini | 0:9fca2b23d0ba | 505 | } |
marcozecchini | 0:9fca2b23d0ba | 506 | |
marcozecchini | 0:9fca2b23d0ba | 507 | /* Robustness checks for free() function. |
marcozecchini | 0:9fca2b23d0ba | 508 | * Function under test is called with invalid parameters. |
marcozecchini | 0:9fca2b23d0ba | 509 | * |
marcozecchini | 0:9fca2b23d0ba | 510 | * Given MemoryPool object has been successfully created. |
marcozecchini | 0:9fca2b23d0ba | 511 | * When free operation is performed on invalid address. |
marcozecchini | 0:9fca2b23d0ba | 512 | * Then deallocation fails with osErrorParameter error. |
marcozecchini | 0:9fca2b23d0ba | 513 | * |
marcozecchini | 0:9fca2b23d0ba | 514 | */ |
marcozecchini | 0:9fca2b23d0ba | 515 | void free_block_invalid_parameter() |
marcozecchini | 0:9fca2b23d0ba | 516 | { |
marcozecchini | 0:9fca2b23d0ba | 517 | MemoryPool<int, 1> mem_pool; |
marcozecchini | 0:9fca2b23d0ba | 518 | osStatus status; |
marcozecchini | 0:9fca2b23d0ba | 519 | |
marcozecchini | 0:9fca2b23d0ba | 520 | /* Try to free block passing invalid parameter (variable address). */ |
marcozecchini | 0:9fca2b23d0ba | 521 | status = mem_pool.free(reinterpret_cast<int*>(&status)); |
marcozecchini | 0:9fca2b23d0ba | 522 | |
marcozecchini | 0:9fca2b23d0ba | 523 | /* Check operation status. */ |
marcozecchini | 0:9fca2b23d0ba | 524 | TEST_ASSERT_EQUAL(osErrorParameter, status); |
marcozecchini | 0:9fca2b23d0ba | 525 | } |
marcozecchini | 0:9fca2b23d0ba | 526 | |
marcozecchini | 0:9fca2b23d0ba | 527 | /* Use wrapper functions to reduce memory usage. */ |
marcozecchini | 0:9fca2b23d0ba | 528 | |
marcozecchini | 0:9fca2b23d0ba | 529 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 530 | void test_mem_pool_alloc_success_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 531 | { |
marcozecchini | 0:9fca2b23d0ba | 532 | test_mem_pool_alloc_success<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 533 | test_mem_pool_alloc_success<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 534 | } |
marcozecchini | 0:9fca2b23d0ba | 535 | |
marcozecchini | 0:9fca2b23d0ba | 536 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 537 | void test_mem_pool_alloc_success_complex_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 538 | { |
marcozecchini | 0:9fca2b23d0ba | 539 | test_mem_pool_alloc_success_complex<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 540 | test_mem_pool_alloc_success_complex<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 541 | } |
marcozecchini | 0:9fca2b23d0ba | 542 | |
marcozecchini | 0:9fca2b23d0ba | 543 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 544 | void test_mem_pool_free_success_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 545 | { |
marcozecchini | 0:9fca2b23d0ba | 546 | test_mem_pool_free_success<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 547 | test_mem_pool_free_success<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 548 | } |
marcozecchini | 0:9fca2b23d0ba | 549 | |
marcozecchini | 0:9fca2b23d0ba | 550 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 551 | void test_mem_pool_free_realloc_last_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 552 | { |
marcozecchini | 0:9fca2b23d0ba | 553 | test_mem_pool_free_realloc_last<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 554 | test_mem_pool_free_realloc_last<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 555 | |
marcozecchini | 0:9fca2b23d0ba | 556 | } |
marcozecchini | 0:9fca2b23d0ba | 557 | |
marcozecchini | 0:9fca2b23d0ba | 558 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 559 | void test_mem_pool_free_realloc_first_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 560 | { |
marcozecchini | 0:9fca2b23d0ba | 561 | test_mem_pool_free_realloc_first<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 562 | test_mem_pool_free_realloc_first<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 563 | } |
marcozecchini | 0:9fca2b23d0ba | 564 | |
marcozecchini | 0:9fca2b23d0ba | 565 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 566 | void test_mem_pool_free_realloc_first_complex_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 567 | { |
marcozecchini | 0:9fca2b23d0ba | 568 | test_mem_pool_free_realloc_first_complex<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 569 | test_mem_pool_free_realloc_first_complex<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 570 | } |
marcozecchini | 0:9fca2b23d0ba | 571 | |
marcozecchini | 0:9fca2b23d0ba | 572 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 573 | void test_mem_pool_free_realloc_last_complex_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 574 | { |
marcozecchini | 0:9fca2b23d0ba | 575 | test_mem_pool_free_realloc_last_complex<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 576 | test_mem_pool_free_realloc_last_complex<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 577 | } |
marcozecchini | 0:9fca2b23d0ba | 578 | |
marcozecchini | 0:9fca2b23d0ba | 579 | template<typename T, const uint32_t numOfEntries> |
marcozecchini | 0:9fca2b23d0ba | 580 | void test_mem_pool_alloc_fail_wrapper() |
marcozecchini | 0:9fca2b23d0ba | 581 | { |
marcozecchini | 0:9fca2b23d0ba | 582 | test_mem_pool_alloc_fail<T, numOfEntries>(ALLOC); |
marcozecchini | 0:9fca2b23d0ba | 583 | test_mem_pool_alloc_fail<T, numOfEntries>(CALLOC); |
marcozecchini | 0:9fca2b23d0ba | 584 | } |
marcozecchini | 0:9fca2b23d0ba | 585 | |
marcozecchini | 0:9fca2b23d0ba | 586 | Case cases[] = { |
marcozecchini | 0:9fca2b23d0ba | 587 | Case("Test: alloc()/calloc() - success, 4 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<int, 1>), |
marcozecchini | 0:9fca2b23d0ba | 588 | Case("Test: alloc()/calloc() - success, 4 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<int, 3>), |
marcozecchini | 0:9fca2b23d0ba | 589 | Case("Test: alloc()/calloc() - success, 1 bytes b_type, q_size equal to 1.", test_mem_pool_alloc_success_wrapper<char, 1>), |
marcozecchini | 0:9fca2b23d0ba | 590 | Case("Test: alloc()/calloc() - success, 1 bytes b_type, q_size equal to 3.", test_mem_pool_alloc_success_wrapper<char, 3>), |
marcozecchini | 0:9fca2b23d0ba | 591 | Case("Test: alloc()/calloc() - success, complex b_type, q_size equal to 1.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 1>), |
marcozecchini | 0:9fca2b23d0ba | 592 | Case("Test: alloc()/calloc() - success, complex b_type, q_size equal to 3.", test_mem_pool_alloc_success_complex_wrapper<COMPLEX_TYPE, 3>), |
marcozecchini | 0:9fca2b23d0ba | 593 | |
marcozecchini | 0:9fca2b23d0ba | 594 | Case("Test: free() - success, 4 bytes b_type, q_size equal to 1.", test_mem_pool_free_success_wrapper<int, 1>), |
marcozecchini | 0:9fca2b23d0ba | 595 | Case("Test: free() - success, 4 bytes b_type, q_size equal to 3.", test_mem_pool_free_success_wrapper<int, 3>), |
marcozecchini | 0:9fca2b23d0ba | 596 | Case("Test: free() - success, complex b_type, q_size equal to 1.", test_mem_pool_free_success_wrapper<COMPLEX_TYPE, 1>), |
marcozecchini | 0:9fca2b23d0ba | 597 | Case("Test: free() - success, complex b_type, q_size equal to 3.", test_mem_pool_free_success_wrapper<COMPLEX_TYPE, 3>), |
marcozecchini | 0:9fca2b23d0ba | 598 | |
marcozecchini | 0:9fca2b23d0ba | 599 | Case("Test: re-allocation of the last block, basic type.", test_mem_pool_free_realloc_last_wrapper<int, 3>), |
marcozecchini | 0:9fca2b23d0ba | 600 | Case("Test: re-allocation of the first block, basic type.", test_mem_pool_free_realloc_first_wrapper<int, 3>), |
marcozecchini | 0:9fca2b23d0ba | 601 | Case("Test: re-allocation of the first block, complex type.", test_mem_pool_free_realloc_first_complex_wrapper<COMPLEX_TYPE, 3>), |
marcozecchini | 0:9fca2b23d0ba | 602 | Case("Test: re-allocation of the last block, complex type.", test_mem_pool_free_realloc_last_complex_wrapper<COMPLEX_TYPE, 3>), |
marcozecchini | 0:9fca2b23d0ba | 603 | |
marcozecchini | 0:9fca2b23d0ba | 604 | Case("Test: fail (out of free blocks).", test_mem_pool_alloc_fail_wrapper<int, 3>), |
marcozecchini | 0:9fca2b23d0ba | 605 | |
marcozecchini | 0:9fca2b23d0ba | 606 | Case("Test: free() - robust (free block twice).", test_mem_pool_free_on_freed_block), |
marcozecchini | 0:9fca2b23d0ba | 607 | Case("Test: free() - robust (free called with invalid param - NULL).", free_block_invalid_parameter_null), |
marcozecchini | 0:9fca2b23d0ba | 608 | Case("Test: free() - robust (free called with invalid param).", free_block_invalid_parameter) |
marcozecchini | 0:9fca2b23d0ba | 609 | }; |
marcozecchini | 0:9fca2b23d0ba | 610 | |
marcozecchini | 0:9fca2b23d0ba | 611 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
marcozecchini | 0:9fca2b23d0ba | 612 | { |
marcozecchini | 0:9fca2b23d0ba | 613 | GREENTEA_SETUP(20, "default_auto"); |
marcozecchini | 0:9fca2b23d0ba | 614 | return greentea_test_setup_handler(number_of_cases); |
marcozecchini | 0:9fca2b23d0ba | 615 | } |
marcozecchini | 0:9fca2b23d0ba | 616 | |
marcozecchini | 0:9fca2b23d0ba | 617 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
marcozecchini | 0:9fca2b23d0ba | 618 | |
marcozecchini | 0:9fca2b23d0ba | 619 | int main() |
marcozecchini | 0:9fca2b23d0ba | 620 | { |
marcozecchini | 0:9fca2b23d0ba | 621 | Harness::run(specification); |
marcozecchini | 0:9fca2b23d0ba | 622 | } |
marcozecchini | 0:9fca2b23d0ba | 623 |