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