Greg Steiert / pegasus_dev

Dependents:   blinky_max32630fthr

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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) || !MBED_HEAP_STATS_ENABLED || defined(__ICCARM__)
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   790
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     printf("Initial print to setup stdio buffers\n");
00053     mbed_stats_heap_t stats_start;
00054     mbed_stats_heap_t stats_current;
00055     void *data;
00056 
00057     mbed_stats_heap_get(&stats_start);
00058 
00059     for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
00060 
00061         // Allocate memory and assert size change
00062         data = malloc_thunk_array[i](ALLOCATION_SIZE_DEFAULT);
00063         TEST_ASSERT(data != NULL);
00064         mbed_stats_heap_get(&stats_current);
00065         uint32_t increase = stats_current.current_size - stats_start.current_size;
00066         TEST_ASSERT_EQUAL_UINT32(ALLOCATION_SIZE_DEFAULT, increase);
00067         TEST_ASSERT_EQUAL_UINT32(stats_start.total_size + ALLOCATION_SIZE_DEFAULT * (i + 1), stats_current.total_size);
00068         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt + 1, stats_current.alloc_cnt);
00069         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
00070 
00071         // Free memory and assert back to starting size
00072         free(data);
00073         mbed_stats_heap_get(&stats_current);
00074         TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
00075         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
00076         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
00077     }
00078 }
00079 
00080 void test_case_allocate_zero()
00081 {
00082     mbed_stats_heap_t stats_start;
00083     mbed_stats_heap_t stats_current;
00084     void *data;
00085 
00086     mbed_stats_heap_get(&stats_start);
00087 
00088     for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
00089 
00090         // Allocate memory and assert size change
00091         data = malloc_thunk_array[i](0);
00092         // Return can be NULL
00093         mbed_stats_heap_get(&stats_current);
00094         TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
00095         TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
00096         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
00097 
00098         // Free memory and assert back to starting size
00099         free(data);
00100         mbed_stats_heap_get(&stats_current);
00101         TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
00102         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
00103         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
00104     }
00105 }
00106 
00107 void test_case_allocate_fail()
00108 {
00109     mbed_stats_heap_t stats_start;
00110     mbed_stats_heap_t stats_current;
00111     void *data;
00112 
00113     mbed_stats_heap_get(&stats_start);
00114 
00115     for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
00116 
00117         // Trigger a failure by trying to allocate a buffer that won't fit
00118         data = malloc_thunk_array[i](ALLOCATION_SIZE_FAIL);
00119         TEST_ASSERT(data == NULL);
00120         mbed_stats_heap_get(&stats_current);
00121         TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
00122         TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
00123         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
00124         TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt + i + 1, stats_current.alloc_fail_cnt);
00125     }
00126 }
00127 
00128 static void* thunk_malloc(uint32_t size)
00129 {
00130     printf("Malloc thunk\n");
00131     return malloc(size);
00132 }
00133 
00134 static void* thunk_calloc_1(uint32_t size)
00135 {
00136     printf("Calloc thunk 1 byte\n");
00137     return calloc(size / 1, 1);
00138 }
00139 
00140 static void* thunk_calloc_4(uint32_t size)
00141 {
00142     printf("Calloc thunk 4 bytes\n");
00143     return calloc(size / 4, 4);
00144 }
00145 
00146 
00147 static void* thunk_realloc(uint32_t size)
00148 {
00149     printf("Realloc thunk\n");
00150     return realloc(NULL, size);
00151 }
00152 
00153 void test_case_realloc_size()
00154 {
00155     mbed_stats_heap_t stats_start;
00156     mbed_stats_heap_t stats_current;
00157     uint32_t increase;
00158     void *data;
00159 
00160     mbed_stats_heap_get(&stats_start);
00161 
00162     // Allocate memory and assert size change
00163     data = realloc(NULL, ALLOCATION_SIZE_DEFAULT);
00164     TEST_ASSERT(data != NULL);
00165     mbed_stats_heap_get(&stats_current);
00166     increase = stats_current.current_size - stats_start.current_size;
00167     TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_DEFAULT);
00168 
00169     // Decrease size and assert size change
00170     data = realloc(data, ALLOCATION_SIZE_SMALL);
00171     TEST_ASSERT(data != NULL);
00172     mbed_stats_heap_get(&stats_current);
00173     increase = stats_current.current_size - stats_start.current_size;
00174     TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_SMALL);
00175 
00176     // Increase size and assert size change
00177     data = realloc(data, ALLOCATION_SIZE_LARGE);
00178     TEST_ASSERT(data != NULL);
00179     mbed_stats_heap_get(&stats_current);
00180     increase = stats_current.current_size - stats_start.current_size;
00181     TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_LARGE);
00182 
00183     // Free memory and assert back to starting size
00184     free(data);
00185     mbed_stats_heap_get(&stats_current);
00186     TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
00187 }
00188 
00189 Case cases[] = {
00190     Case("malloc and free size", test_case_malloc_free_size),
00191     Case("allocate size zero", test_case_allocate_zero),
00192     Case("allocation failure", test_case_allocate_fail),
00193     Case("realloc size", test_case_realloc_size),
00194 };
00195 
00196 utest::v1::status_t  greentea_test_setup(const size_t number_of_cases)
00197 {
00198     GREENTEA_SETUP(20, "default_auto");
00199     return greentea_test_setup_handler(number_of_cases);
00200 }
00201 
00202 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
00203 
00204 int main()
00205 {
00206     Harness::run(specification);
00207 }