Preliminary main mbed library for nexpaq development

Committer:
nexpaq
Date:
Fri Nov 04 20:54:50 2016 +0000
Revision:
1:d96dbedaebdb
Parent:
0:6c56fb4bc5f0
Removed extra directories for other platforms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
nexpaq 0:6c56fb4bc5f0 1 /*
nexpaq 0:6c56fb4bc5f0 2 * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved
nexpaq 0:6c56fb4bc5f0 3 * SPDX-License-Identifier: Apache-2.0
nexpaq 0:6c56fb4bc5f0 4 *
nexpaq 0:6c56fb4bc5f0 5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
nexpaq 0:6c56fb4bc5f0 6 * not use this file except in compliance with the License.
nexpaq 0:6c56fb4bc5f0 7 * You may obtain a copy of the License at
nexpaq 0:6c56fb4bc5f0 8 *
nexpaq 0:6c56fb4bc5f0 9 * http://www.apache.org/licenses/LICENSE-2.0
nexpaq 0:6c56fb4bc5f0 10 *
nexpaq 0:6c56fb4bc5f0 11 * Unless required by applicable law or agreed to in writing, software
nexpaq 0:6c56fb4bc5f0 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
nexpaq 0:6c56fb4bc5f0 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
nexpaq 0:6c56fb4bc5f0 14 * See the License for the specific language governing permissions and
nexpaq 0:6c56fb4bc5f0 15 * limitations under the License.
nexpaq 0:6c56fb4bc5f0 16 */
nexpaq 0:6c56fb4bc5f0 17 #include "mbed.h"
nexpaq 0:6c56fb4bc5f0 18 #include "greentea-client/test_env.h"
nexpaq 0:6c56fb4bc5f0 19 #include "unity/unity.h"
nexpaq 0:6c56fb4bc5f0 20 #include "utest/utest.h"
nexpaq 0:6c56fb4bc5f0 21 #include "mbed_stats.h"
nexpaq 0:6c56fb4bc5f0 22 #include <stdlib.h>
nexpaq 0:6c56fb4bc5f0 23 #include <stdio.h>
nexpaq 0:6c56fb4bc5f0 24
nexpaq 0:6c56fb4bc5f0 25 #if !defined(MBED_HEAP_STATS_ENABLED) || !MBED_HEAP_STATS_ENABLED || defined(__ICCARM__)
nexpaq 0:6c56fb4bc5f0 26 #error [NOT_SUPPORTED] test not supported
nexpaq 0:6c56fb4bc5f0 27 #endif
nexpaq 0:6c56fb4bc5f0 28
nexpaq 0:6c56fb4bc5f0 29 using namespace utest::v1;
nexpaq 0:6c56fb4bc5f0 30
nexpaq 0:6c56fb4bc5f0 31 #define ALLOCATION_SIZE_DEFAULT 564
nexpaq 0:6c56fb4bc5f0 32 #define ALLOCATION_SIZE_SMALL 124
nexpaq 0:6c56fb4bc5f0 33 #define ALLOCATION_SIZE_LARGE 790
nexpaq 0:6c56fb4bc5f0 34 #define ALLOCATION_SIZE_FAIL (1024 * 1024 *1024)
nexpaq 0:6c56fb4bc5f0 35
nexpaq 0:6c56fb4bc5f0 36 typedef void* (*malloc_cb_t) (uint32_t size);
nexpaq 0:6c56fb4bc5f0 37
nexpaq 0:6c56fb4bc5f0 38 static void* thunk_malloc(uint32_t size);
nexpaq 0:6c56fb4bc5f0 39 static void* thunk_calloc_1(uint32_t size);
nexpaq 0:6c56fb4bc5f0 40 static void* thunk_calloc_4(uint32_t size);
nexpaq 0:6c56fb4bc5f0 41 static void* thunk_realloc(uint32_t size);
nexpaq 0:6c56fb4bc5f0 42
nexpaq 0:6c56fb4bc5f0 43 malloc_cb_t malloc_thunk_array[] = {
nexpaq 0:6c56fb4bc5f0 44 thunk_malloc,
nexpaq 0:6c56fb4bc5f0 45 thunk_calloc_1,
nexpaq 0:6c56fb4bc5f0 46 thunk_calloc_4,
nexpaq 0:6c56fb4bc5f0 47 thunk_realloc,
nexpaq 0:6c56fb4bc5f0 48 };
nexpaq 0:6c56fb4bc5f0 49
nexpaq 0:6c56fb4bc5f0 50 void test_case_malloc_free_size()
nexpaq 0:6c56fb4bc5f0 51 {
nexpaq 0:6c56fb4bc5f0 52 printf("Initial print to setup stdio buffers\n");
nexpaq 0:6c56fb4bc5f0 53 mbed_stats_heap_t stats_start;
nexpaq 0:6c56fb4bc5f0 54 mbed_stats_heap_t stats_current;
nexpaq 0:6c56fb4bc5f0 55 void *data;
nexpaq 0:6c56fb4bc5f0 56
nexpaq 0:6c56fb4bc5f0 57 mbed_stats_heap_get(&stats_start);
nexpaq 0:6c56fb4bc5f0 58
nexpaq 0:6c56fb4bc5f0 59 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
nexpaq 0:6c56fb4bc5f0 60
nexpaq 0:6c56fb4bc5f0 61 // Allocate memory and assert size change
nexpaq 0:6c56fb4bc5f0 62 data = malloc_thunk_array[i](ALLOCATION_SIZE_DEFAULT);
nexpaq 0:6c56fb4bc5f0 63 TEST_ASSERT(data != NULL);
nexpaq 0:6c56fb4bc5f0 64 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 65 uint32_t increase = stats_current.current_size - stats_start.current_size;
nexpaq 0:6c56fb4bc5f0 66 TEST_ASSERT_EQUAL_UINT32(ALLOCATION_SIZE_DEFAULT, increase);
nexpaq 0:6c56fb4bc5f0 67 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size + ALLOCATION_SIZE_DEFAULT * (i + 1), stats_current.total_size);
nexpaq 0:6c56fb4bc5f0 68 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt + 1, stats_current.alloc_cnt);
nexpaq 0:6c56fb4bc5f0 69 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
nexpaq 0:6c56fb4bc5f0 70
nexpaq 0:6c56fb4bc5f0 71 // Free memory and assert back to starting size
nexpaq 0:6c56fb4bc5f0 72 free(data);
nexpaq 0:6c56fb4bc5f0 73 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 74 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
nexpaq 0:6c56fb4bc5f0 75 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
nexpaq 0:6c56fb4bc5f0 76 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
nexpaq 0:6c56fb4bc5f0 77 }
nexpaq 0:6c56fb4bc5f0 78 }
nexpaq 0:6c56fb4bc5f0 79
nexpaq 0:6c56fb4bc5f0 80 void test_case_allocate_zero()
nexpaq 0:6c56fb4bc5f0 81 {
nexpaq 0:6c56fb4bc5f0 82 mbed_stats_heap_t stats_start;
nexpaq 0:6c56fb4bc5f0 83 mbed_stats_heap_t stats_current;
nexpaq 0:6c56fb4bc5f0 84 void *data;
nexpaq 0:6c56fb4bc5f0 85
nexpaq 0:6c56fb4bc5f0 86 mbed_stats_heap_get(&stats_start);
nexpaq 0:6c56fb4bc5f0 87
nexpaq 0:6c56fb4bc5f0 88 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
nexpaq 0:6c56fb4bc5f0 89
nexpaq 0:6c56fb4bc5f0 90 // Allocate memory and assert size change
nexpaq 0:6c56fb4bc5f0 91 data = malloc_thunk_array[i](0);
nexpaq 0:6c56fb4bc5f0 92 // Return can be NULL
nexpaq 0:6c56fb4bc5f0 93 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 94 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
nexpaq 0:6c56fb4bc5f0 95 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
nexpaq 0:6c56fb4bc5f0 96 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
nexpaq 0:6c56fb4bc5f0 97
nexpaq 0:6c56fb4bc5f0 98 // Free memory and assert back to starting size
nexpaq 0:6c56fb4bc5f0 99 free(data);
nexpaq 0:6c56fb4bc5f0 100 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 101 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
nexpaq 0:6c56fb4bc5f0 102 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
nexpaq 0:6c56fb4bc5f0 103 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt, stats_current.alloc_fail_cnt);
nexpaq 0:6c56fb4bc5f0 104 }
nexpaq 0:6c56fb4bc5f0 105 }
nexpaq 0:6c56fb4bc5f0 106
nexpaq 0:6c56fb4bc5f0 107 void test_case_allocate_fail()
nexpaq 0:6c56fb4bc5f0 108 {
nexpaq 0:6c56fb4bc5f0 109 mbed_stats_heap_t stats_start;
nexpaq 0:6c56fb4bc5f0 110 mbed_stats_heap_t stats_current;
nexpaq 0:6c56fb4bc5f0 111 void *data;
nexpaq 0:6c56fb4bc5f0 112
nexpaq 0:6c56fb4bc5f0 113 mbed_stats_heap_get(&stats_start);
nexpaq 0:6c56fb4bc5f0 114
nexpaq 0:6c56fb4bc5f0 115 for (uint32_t i = 0; i < sizeof(malloc_thunk_array) / sizeof(malloc_cb_t); i++) {
nexpaq 0:6c56fb4bc5f0 116
nexpaq 0:6c56fb4bc5f0 117 // Trigger a failure by trying to allocate a buffer that won't fit
nexpaq 0:6c56fb4bc5f0 118 data = malloc_thunk_array[i](ALLOCATION_SIZE_FAIL);
nexpaq 0:6c56fb4bc5f0 119 TEST_ASSERT(data == NULL);
nexpaq 0:6c56fb4bc5f0 120 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 121 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
nexpaq 0:6c56fb4bc5f0 122 TEST_ASSERT_EQUAL_UINT32(stats_start.total_size, stats_current.total_size);
nexpaq 0:6c56fb4bc5f0 123 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_cnt, stats_current.alloc_cnt);
nexpaq 0:6c56fb4bc5f0 124 TEST_ASSERT_EQUAL_UINT32(stats_start.alloc_fail_cnt + i + 1, stats_current.alloc_fail_cnt);
nexpaq 0:6c56fb4bc5f0 125 }
nexpaq 0:6c56fb4bc5f0 126 }
nexpaq 0:6c56fb4bc5f0 127
nexpaq 0:6c56fb4bc5f0 128 static void* thunk_malloc(uint32_t size)
nexpaq 0:6c56fb4bc5f0 129 {
nexpaq 0:6c56fb4bc5f0 130 printf("Malloc thunk\n");
nexpaq 0:6c56fb4bc5f0 131 return malloc(size);
nexpaq 0:6c56fb4bc5f0 132 }
nexpaq 0:6c56fb4bc5f0 133
nexpaq 0:6c56fb4bc5f0 134 static void* thunk_calloc_1(uint32_t size)
nexpaq 0:6c56fb4bc5f0 135 {
nexpaq 0:6c56fb4bc5f0 136 printf("Calloc thunk 1 byte\n");
nexpaq 0:6c56fb4bc5f0 137 return calloc(size / 1, 1);
nexpaq 0:6c56fb4bc5f0 138 }
nexpaq 0:6c56fb4bc5f0 139
nexpaq 0:6c56fb4bc5f0 140 static void* thunk_calloc_4(uint32_t size)
nexpaq 0:6c56fb4bc5f0 141 {
nexpaq 0:6c56fb4bc5f0 142 printf("Calloc thunk 4 bytes\n");
nexpaq 0:6c56fb4bc5f0 143 return calloc(size / 4, 4);
nexpaq 0:6c56fb4bc5f0 144 }
nexpaq 0:6c56fb4bc5f0 145
nexpaq 0:6c56fb4bc5f0 146
nexpaq 0:6c56fb4bc5f0 147 static void* thunk_realloc(uint32_t size)
nexpaq 0:6c56fb4bc5f0 148 {
nexpaq 0:6c56fb4bc5f0 149 printf("Realloc thunk\n");
nexpaq 0:6c56fb4bc5f0 150 return realloc(NULL, size);
nexpaq 0:6c56fb4bc5f0 151 }
nexpaq 0:6c56fb4bc5f0 152
nexpaq 0:6c56fb4bc5f0 153 void test_case_realloc_size()
nexpaq 0:6c56fb4bc5f0 154 {
nexpaq 0:6c56fb4bc5f0 155 mbed_stats_heap_t stats_start;
nexpaq 0:6c56fb4bc5f0 156 mbed_stats_heap_t stats_current;
nexpaq 0:6c56fb4bc5f0 157 uint32_t increase;
nexpaq 0:6c56fb4bc5f0 158 void *data;
nexpaq 0:6c56fb4bc5f0 159
nexpaq 0:6c56fb4bc5f0 160 mbed_stats_heap_get(&stats_start);
nexpaq 0:6c56fb4bc5f0 161
nexpaq 0:6c56fb4bc5f0 162 // Allocate memory and assert size change
nexpaq 0:6c56fb4bc5f0 163 data = realloc(NULL, ALLOCATION_SIZE_DEFAULT);
nexpaq 0:6c56fb4bc5f0 164 TEST_ASSERT(data != NULL);
nexpaq 0:6c56fb4bc5f0 165 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 166 increase = stats_current.current_size - stats_start.current_size;
nexpaq 0:6c56fb4bc5f0 167 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_DEFAULT);
nexpaq 0:6c56fb4bc5f0 168
nexpaq 0:6c56fb4bc5f0 169 // Decrease size and assert size change
nexpaq 0:6c56fb4bc5f0 170 data = realloc(data, ALLOCATION_SIZE_SMALL);
nexpaq 0:6c56fb4bc5f0 171 TEST_ASSERT(data != NULL);
nexpaq 0:6c56fb4bc5f0 172 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 173 increase = stats_current.current_size - stats_start.current_size;
nexpaq 0:6c56fb4bc5f0 174 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_SMALL);
nexpaq 0:6c56fb4bc5f0 175
nexpaq 0:6c56fb4bc5f0 176 // Increase size and assert size change
nexpaq 0:6c56fb4bc5f0 177 data = realloc(data, ALLOCATION_SIZE_LARGE);
nexpaq 0:6c56fb4bc5f0 178 TEST_ASSERT(data != NULL);
nexpaq 0:6c56fb4bc5f0 179 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 180 increase = stats_current.current_size - stats_start.current_size;
nexpaq 0:6c56fb4bc5f0 181 TEST_ASSERT_EQUAL_UINT32(increase, ALLOCATION_SIZE_LARGE);
nexpaq 0:6c56fb4bc5f0 182
nexpaq 0:6c56fb4bc5f0 183 // Free memory and assert back to starting size
nexpaq 0:6c56fb4bc5f0 184 free(data);
nexpaq 0:6c56fb4bc5f0 185 mbed_stats_heap_get(&stats_current);
nexpaq 0:6c56fb4bc5f0 186 TEST_ASSERT_EQUAL_UINT32(stats_start.current_size, stats_current.current_size);
nexpaq 0:6c56fb4bc5f0 187 }
nexpaq 0:6c56fb4bc5f0 188
nexpaq 0:6c56fb4bc5f0 189 Case cases[] = {
nexpaq 0:6c56fb4bc5f0 190 Case("malloc and free size", test_case_malloc_free_size),
nexpaq 0:6c56fb4bc5f0 191 Case("allocate size zero", test_case_allocate_zero),
nexpaq 0:6c56fb4bc5f0 192 Case("allocation failure", test_case_allocate_fail),
nexpaq 0:6c56fb4bc5f0 193 Case("realloc size", test_case_realloc_size),
nexpaq 0:6c56fb4bc5f0 194 };
nexpaq 0:6c56fb4bc5f0 195
nexpaq 0:6c56fb4bc5f0 196 utest::v1::status_t greentea_test_setup(const size_t number_of_cases)
nexpaq 0:6c56fb4bc5f0 197 {
nexpaq 0:6c56fb4bc5f0 198 GREENTEA_SETUP(20, "default_auto");
nexpaq 0:6c56fb4bc5f0 199 return greentea_test_setup_handler(number_of_cases);
nexpaq 0:6c56fb4bc5f0 200 }
nexpaq 0:6c56fb4bc5f0 201
nexpaq 0:6c56fb4bc5f0 202 Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler);
nexpaq 0:6c56fb4bc5f0 203
nexpaq 0:6c56fb4bc5f0 204 int main()
nexpaq 0:6c56fb4bc5f0 205 {
nexpaq 0:6c56fb4bc5f0 206 Harness::run(specification);
nexpaq 0:6c56fb4bc5f0 207 }