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