Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp
00001 /* 00002 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved 00003 * SPDX-License-Identifier: Apache-2.0 00004 * 00005 * Licensed under the Apache License, Version 2.0 (the "License"); you may 00006 * not use this file except in compliance with the License. 00007 * You may obtain a copy of the License at 00008 * 00009 * http://www.apache.org/licenses/LICENSE-2.0 00010 * 00011 * Unless required by applicable law or agreed to in writing, software 00012 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 00013 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00014 * See the License for the specific language governing permissions and 00015 * limitations under the License. 00016 */ 00017 #include "mbed.h" 00018 #include "greentea-client/test_env.h" 00019 #include "unity/unity.h" 00020 #include "utest/utest.h" 00021 #include "mbed_stats.h" 00022 #include <stdlib.h> 00023 #include <stdio.h> 00024 00025 #if !defined(MBED_HEAP_STATS_ENABLED) 00026 #error [NOT_SUPPORTED] test not supported 00027 #endif 00028 00029 using namespace utest::v1; 00030 00031 #define ALLOCATION_SIZE_DEFAULT 564 00032 #define ALLOCATION_SIZE_SMALL 124 00033 #define ALLOCATION_SIZE_LARGE 700 00034 #define ALLOCATION_SIZE_FAIL (1024 * 1024 *1024) 00035 00036 typedef void* (*malloc_cb_t) (uint32_t size); 00037 00038 static void* thunk_malloc(uint32_t size); 00039 static void* thunk_calloc_1(uint32_t size); 00040 static void* thunk_calloc_4(uint32_t size); 00041 static void* thunk_realloc(uint32_t size); 00042 00043 malloc_cb_t malloc_thunk_array[] = { 00044 thunk_malloc, 00045 thunk_calloc_1, 00046 thunk_calloc_4, 00047 thunk_realloc, 00048 }; 00049 00050 void test_case_malloc_free_size() 00051 { 00052 mbed_stats_heap_t stats_start; 00053 mbed_stats_heap_t stats_current; 00054 void *data; 00055 00056 mbed_stats_heap_get(&stats_start); 00057 00058 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) { 00059 00060 // Allocate memory and assert size change 00061 data = malloc_thunk_array[i](ALLOCATION_SIZE_DEFAULT); 00062 TEST_ASSERT(data != NULL); 00063 mbed_stats_heap_get(&stats_current); 00064 uint32_t increase = stats_current.current_size - stats_start.current_size; 00065 TEST_ASSERT_EQUAL_UINT32(ALLOCATION_SIZE_DEFAULT, increase); 00066 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size + ALLOCATION_SIZE_DEFAULT * (i + 1), stats_current.total_size); 00067 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt + 1, stats_current.alloc_cnt); 00068 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); 00069 00070 // Free memory and assert back to starting size 00071 free(data); 00072 mbed_stats_heap_get(&stats_current); 00073 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); 00074 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); 00075 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); 00076 } 00077 } 00078 00079 void test_case_allocate_zero() 00080 { 00081 mbed_stats_heap_t stats_start; 00082 mbed_stats_heap_t stats_current; 00083 void *data; 00084 00085 mbed_stats_heap_get(&stats_start); 00086 00087 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) { 00088 00089 // Allocate memory and assert size change 00090 data = malloc_thunk_array[i](0); 00091 // Return can be NULL 00092 mbed_stats_heap_get(&stats_current); 00093 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); 00094 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size); 00095 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); 00096 00097 // Free memory and assert back to starting size 00098 free(data); 00099 mbed_stats_heap_get(&stats_current); 00100 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); 00101 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); 00102 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt); 00103 } 00104 } 00105 00106 void test_case_allocate_fail() 00107 { 00108 mbed_stats_heap_t stats_start; 00109 mbed_stats_heap_t stats_current; 00110 void *data; 00111 00112 mbed_stats_heap_get(&stats_start); 00113 00114 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) { 00115 00116 // Trigger a failure by trying to allocate a buffer that won't fit 00117 data = malloc_thunk_array[i](ALLOCATION_SIZE_FAIL); 00118 TEST_ASSERT(data == NULL); 00119 mbed_stats_heap_get(&stats_current); 00120 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); 00121 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size); 00122 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt); 00123 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt + i + 1, stats_current.alloc_fail_cnt); 00124 } 00125 } 00126 00127 static void* thunk_malloc(uint32_t size) 00128 { 00129 return malloc(size); 00130 } 00131 00132 static void* thunk_calloc_1(uint32_t size) 00133 { 00134 return calloc(size / 1, 1); 00135 } 00136 00137 static void* thunk_calloc_4(uint32_t size) 00138 { 00139 return calloc(size / 4, 4); 00140 } 00141 00142 00143 static void* thunk_realloc(uint32_t size) 00144 { 00145 return realloc(NULL, size); 00146 } 00147 00148 void test_case_realloc_size() 00149 { 00150 mbed_stats_heap_t stats_start; 00151 mbed_stats_heap_t stats_current; 00152 uint32_t increase; 00153 void *data; 00154 00155 mbed_stats_heap_get(&stats_start); 00156 00157 // Allocate memory and assert size change 00158 data = realloc(NULL, ALLOCATION_SIZE_DEFAULT); 00159 TEST_ASSERT(data != NULL); 00160 mbed_stats_heap_get(&stats_current); 00161 increase = stats_current.current_size - stats_start.current_size; 00162 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_DEFAULT); 00163 00164 // Decrease size and assert size change 00165 data = realloc(data, ALLOCATION_SIZE_SMALL); 00166 TEST_ASSERT(data != NULL); 00167 mbed_stats_heap_get(&stats_current); 00168 increase = stats_current.current_size - stats_start.current_size; 00169 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_SMALL); 00170 00171 // Increase size and assert size change 00172 data = realloc(data, ALLOCATION_SIZE_LARGE); 00173 TEST_ASSERT(data != NULL); 00174 mbed_stats_heap_get(&stats_current); 00175 increase = stats_current.current_size - stats_start.current_size; 00176 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_LARGE); 00177 00178 // Free memory and assert back to starting size 00179 free(data); 00180 mbed_stats_heap_get(&stats_current); 00181 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size); 00182 } 00183 00184 Case cases[] = { 00185 Case("malloc and free size", test_case_malloc_free_size), 00186 Case("allocate size zero", test_case_allocate_zero), 00187 Case("allocation failure", test_case_allocate_fail), 00188 Case("realloc size", test_case_realloc_size), 00189 }; 00190 00191 utest::v1::status_t greentea_test_setup(const size_t number_of_cases) 00192 { 00193 GREENTEA_SETUP(20, "default_auto"); 00194 return greentea_test_setup_handler(number_of_cases); 00195 } 00196 00197 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); 00198 00199 int main() 00200 { 00201 Harness::run(specification); 00202 }
Generated on Thu Jul 14 2022 14:36:19 by
