|
| 1 | +/* mbed Microcontroller Library |
| 2 | + * Copyright (c) 2017 ARM Limited |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +#include "mbed.h" |
| 17 | +#include "greentea-client/test_env.h" |
| 18 | +#include "unity/unity.h" |
| 19 | +#include "utest/utest.h" |
| 20 | + |
| 21 | + |
| 22 | +using utest::v1::Case; |
| 23 | + |
| 24 | + |
| 25 | +volatile bool callback_called; |
| 26 | + |
| 27 | +void tiemout_callback(void) |
| 28 | +{ |
| 29 | + callback_called = true; |
| 30 | +} |
| 31 | + |
| 32 | +template<int N> |
| 33 | +void critical_section_raii_recursive(Timeout &timeout) |
| 34 | +{ |
| 35 | + static uint32_t depth = 0; |
| 36 | + CriticalSectionLock cs; |
| 37 | + |
| 38 | + depth++; |
| 39 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 40 | + |
| 41 | + if(depth < N) { |
| 42 | + critical_section_raii_recursive<N>(timeout); |
| 43 | + } else { |
| 44 | + // max depth reached - do the test |
| 45 | + const us_timestamp_t timeout_time_us = 1; |
| 46 | + const int wait_time_us = timeout_time_us * 100; |
| 47 | + |
| 48 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 49 | + wait_us(wait_time_us); |
| 50 | + } |
| 51 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 52 | + TEST_ASSERT_FALSE(callback_called); |
| 53 | +} |
| 54 | + |
| 55 | + |
| 56 | +/** Template for tests |
| 57 | +
|
| 58 | + Test C API of critical section |
| 59 | + Given a Timeout with callback attached |
| 60 | + When before critical section |
| 61 | + Then interrupts are enabled and timeout callback is fired |
| 62 | + When inside critical section |
| 63 | + Then interrupts are disabled and timeout callback is blocked |
| 64 | + When after critical section |
| 65 | + Then interrupts are enabled and timeout callback is fired |
| 66 | +
|
| 67 | + Test C API of critical section - nested lock |
| 68 | + Given a Timeout with callback attached |
| 69 | + When before critical section |
| 70 | + Then interrupts are enabled and timeout callback is fired |
| 71 | + When inside nested critical section |
| 72 | + Then interrupts are disabled and timeout callback is blocked |
| 73 | + When after critical section |
| 74 | + Then interrupts are enabled and timeout callback is fired |
| 75 | + */ |
| 76 | +template<int N> |
| 77 | +void test_C_API(void) |
| 78 | +{ |
| 79 | + Timeout timeout; |
| 80 | + const us_timestamp_t timeout_time_us = 1; |
| 81 | + const int wait_time_us = timeout_time_us * 100; |
| 82 | + |
| 83 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 84 | + |
| 85 | + callback_called = false; |
| 86 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 87 | + wait_us(wait_time_us); |
| 88 | + TEST_ASSERT_TRUE(callback_called); |
| 89 | + |
| 90 | + for(int i = 0; i < N; i++) { |
| 91 | + core_util_critical_section_enter(); |
| 92 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 93 | + } |
| 94 | + |
| 95 | + callback_called = false; |
| 96 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 97 | + wait_us(wait_time_us); |
| 98 | + TEST_ASSERT_FALSE(callback_called); |
| 99 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 100 | + |
| 101 | + for(int i = 0; i < N - 1; i++) { |
| 102 | + core_util_critical_section_exit(); |
| 103 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 104 | + TEST_ASSERT_FALSE(callback_called); |
| 105 | + } |
| 106 | + |
| 107 | + core_util_critical_section_exit(); |
| 108 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 109 | + TEST_ASSERT_TRUE(callback_called); |
| 110 | +} |
| 111 | + |
| 112 | +/** Template for tests |
| 113 | +
|
| 114 | + Test C++ API of critical section constructor/destructor |
| 115 | + Given a Timeout with callback attached |
| 116 | + When before critical section |
| 117 | + Then interrupts are enabled and timeout callback is fired |
| 118 | + When inside critical section |
| 119 | + Then interrupts are disabled and timeout callback is blocked |
| 120 | + When after critical section |
| 121 | + Then interrupts are enabled and timeout callback is fired |
| 122 | +
|
| 123 | + Test C++ API of critical section constructor/destructor - nested lock |
| 124 | + Given a Timeout with callback attached |
| 125 | + When before critical section |
| 126 | + Then interrupts are enabled and timeout callback is fired |
| 127 | + When inside nested critical section |
| 128 | + Then interrupts are disabled and timeout callback is blocked |
| 129 | + When after critical section |
| 130 | + Then interrupts are enabled and timeout callback is fired |
| 131 | + */ |
| 132 | +template<int N> |
| 133 | +void test_CPP_API_constructor_destructor(void) |
| 134 | +{ |
| 135 | + Timeout timeout; |
| 136 | + const us_timestamp_t timeout_time_us = 1; |
| 137 | + const int wait_time_us = timeout_time_us * 100; |
| 138 | + |
| 139 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 140 | + |
| 141 | + callback_called = false; |
| 142 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 143 | + wait_us(wait_time_us); |
| 144 | + TEST_ASSERT_TRUE(callback_called); |
| 145 | + |
| 146 | + callback_called = false; |
| 147 | + critical_section_raii_recursive<N>(timeout); |
| 148 | + |
| 149 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 150 | + TEST_ASSERT_TRUE(callback_called); |
| 151 | +} |
| 152 | + |
| 153 | +/** Template for tests |
| 154 | +
|
| 155 | + Test C++ API of critical section enable/disable |
| 156 | + Given a Timeout with attached callback |
| 157 | + When before critical section |
| 158 | + Then interrupts are enabled and timeout callback is fired |
| 159 | + When inside critical section |
| 160 | + Then interrupts are disabled and timeout callback is blocked |
| 161 | + When after critical section |
| 162 | + Then interrupts are enabled and timeout callback is fired |
| 163 | +
|
| 164 | + Test C++ API of critical section enable/disable - nested lock |
| 165 | + Given a Timeout with attached callback |
| 166 | + When before critical section |
| 167 | + Then interrupts are enabled and timeout callback is fired |
| 168 | + When inside nested critical section |
| 169 | + Then interrupts are disabled and timeout callback is blocked |
| 170 | + When after critical section |
| 171 | + Then interrupts are enabled and timeout callback is fired |
| 172 | + */ |
| 173 | +template<int N> |
| 174 | +void test_CPP_API_enable_disable(void) |
| 175 | +{ |
| 176 | + Timeout timeout; |
| 177 | + const us_timestamp_t timeout_time_us = 1; |
| 178 | + const int wait_time_us = timeout_time_us * 100; |
| 179 | + |
| 180 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 181 | + |
| 182 | + callback_called = false; |
| 183 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 184 | + wait_us(wait_time_us); |
| 185 | + TEST_ASSERT_TRUE(callback_called); |
| 186 | + |
| 187 | + for(int i = 0; i < N; i++) { |
| 188 | + CriticalSectionLock::enable(); |
| 189 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 190 | + } |
| 191 | + |
| 192 | + callback_called = false; |
| 193 | + timeout.attach_us(callback(tiemout_callback), timeout_time_us); |
| 194 | + wait_us(wait_time_us); |
| 195 | + TEST_ASSERT_FALSE(callback_called); |
| 196 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 197 | + |
| 198 | + for(int i = 0; i < N - 1; i++) { |
| 199 | + CriticalSectionLock::disable(); |
| 200 | + TEST_ASSERT_TRUE(core_util_in_critical_section()); |
| 201 | + TEST_ASSERT_FALSE(callback_called); |
| 202 | + } |
| 203 | + |
| 204 | + CriticalSectionLock::disable(); |
| 205 | + TEST_ASSERT_FALSE(core_util_in_critical_section()); |
| 206 | + TEST_ASSERT_TRUE(callback_called); |
| 207 | +} |
| 208 | + |
| 209 | + |
| 210 | +utest::v1::status_t test_setup(const size_t number_of_cases) |
| 211 | +{ |
| 212 | + GREENTEA_SETUP(10, "default_auto"); |
| 213 | + return utest::v1::verbose_test_setup_handler(number_of_cases); |
| 214 | +} |
| 215 | + |
| 216 | +Case cases[] = { |
| 217 | + Case("Test critical section C API", test_C_API<1>), |
| 218 | + Case("Test critical section C API nested lock", test_C_API<10>), |
| 219 | + Case("Test critical section C++ API constructor/destructor", test_CPP_API_constructor_destructor<1>), |
| 220 | + Case("Test critical section C++ API constructor/destructor nested lock", test_CPP_API_constructor_destructor<10>), |
| 221 | + Case("Test critical section C++ API enable/disable", test_CPP_API_enable_disable<1>), |
| 222 | + Case("Test critical section C++ API enable/disable nested lock", test_CPP_API_enable_disable<10>) |
| 223 | +}; |
| 224 | + |
| 225 | +utest::v1::Specification specification(test_setup, cases); |
| 226 | + |
| 227 | +int main() |
| 228 | +{ |
| 229 | + return !utest::v1::Harness::run(specification); |
| 230 | +} |
0 commit comments