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