init

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* mbed Microcontroller Library
00002  * Copyright (c) 2017 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 #include "mbed.h"
00017 #include "greentea-client/test_env.h"
00018 #include "unity/unity.h"
00019 #include "utest/utest.h"
00020 
00021 
00022 using utest::v1::Case;
00023 
00024 
00025 volatile bool callback_called;
00026 
00027 void tiemout_callback(void)
00028 {
00029     callback_called = true;
00030 }
00031 
00032 template<int N>
00033 void critical_section_raii_recursive(Timeout &timeout)
00034 {
00035     static uint32_t depth = 0;
00036     CriticalSectionLock cs;
00037 
00038     depth++;
00039     TEST_ASSERT_TRUE(core_util_in_critical_section());
00040 
00041     if(depth < N) {
00042         critical_section_raii_recursive<N>(timeout);
00043     } else {
00044         // max depth reached - do the test
00045         const us_timestamp_t timeout_time_us = 1;
00046         const int wait_time_us = timeout_time_us * 100;
00047 
00048         timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00049         wait_us(wait_time_us);
00050     }
00051     TEST_ASSERT_TRUE(core_util_in_critical_section());
00052     TEST_ASSERT_FALSE(callback_called);
00053 }
00054 
00055 
00056 /** Template for tests
00057 
00058     Test C API of critical section
00059     Given a Timeout with callback attached
00060     When before critical section
00061     Then interrupts are enabled and timeout callback is fired
00062     When inside critical section
00063     Then interrupts are disabled and timeout callback is blocked
00064     When after critical section
00065     Then interrupts are enabled and timeout callback is fired
00066 
00067     Test C API of critical section - nested lock
00068     Given a Timeout with callback attached
00069     When before critical section
00070     Then interrupts are enabled and timeout callback is fired
00071     When inside nested critical section
00072     Then interrupts are disabled and timeout callback is blocked
00073     When after critical section
00074     Then interrupts are enabled and timeout callback is fired
00075  */
00076 template<int N>
00077 void test_C_API(void)
00078 {
00079     Timeout timeout;
00080     const us_timestamp_t timeout_time_us = 1;
00081     const int wait_time_us = timeout_time_us * 100;
00082 
00083     TEST_ASSERT_FALSE(core_util_in_critical_section());
00084 
00085     callback_called = false;
00086     timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00087     wait_us(wait_time_us);
00088     TEST_ASSERT_TRUE(callback_called);
00089 
00090     for(int i = 0; i < N; i++) {
00091         core_util_critical_section_enter();
00092         TEST_ASSERT_TRUE(core_util_in_critical_section());
00093     }
00094 
00095     callback_called = false;
00096     timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00097     wait_us(wait_time_us);
00098     TEST_ASSERT_FALSE(callback_called);
00099     TEST_ASSERT_TRUE(core_util_in_critical_section());
00100 
00101     for(int i = 0; i < N - 1; i++) {
00102         core_util_critical_section_exit();
00103         TEST_ASSERT_TRUE(core_util_in_critical_section());
00104         TEST_ASSERT_FALSE(callback_called);
00105     }
00106 
00107     core_util_critical_section_exit();
00108     TEST_ASSERT_FALSE(core_util_in_critical_section());
00109     TEST_ASSERT_TRUE(callback_called);
00110 }
00111 
00112 /** Template for tests
00113 
00114     Test C++ API of critical section constructor/destructor
00115     Given a Timeout with callback attached
00116     When before critical section
00117     Then interrupts are enabled and timeout callback is fired
00118     When inside critical section
00119     Then interrupts are disabled and timeout callback is blocked
00120     When after critical section
00121     Then interrupts are enabled and timeout callback is fired
00122 
00123     Test C++ API of critical section constructor/destructor - nested lock
00124     Given a Timeout with callback attached
00125     When before critical section
00126     Then interrupts are enabled and timeout callback is fired
00127     When inside nested critical section
00128     Then interrupts are disabled and timeout callback is blocked
00129     When after critical section
00130     Then interrupts are enabled and timeout callback is fired
00131  */
00132 template<int N>
00133 void test_CPP_API_constructor_destructor(void)
00134 {
00135     Timeout timeout;
00136     const us_timestamp_t timeout_time_us = 1;
00137     const int wait_time_us = timeout_time_us * 100;
00138 
00139     TEST_ASSERT_FALSE(core_util_in_critical_section());
00140 
00141     callback_called = false;
00142     timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00143     wait_us(wait_time_us);
00144     TEST_ASSERT_TRUE(callback_called);
00145 
00146     callback_called = false;
00147     critical_section_raii_recursive<N>(timeout);
00148 
00149     TEST_ASSERT_FALSE(core_util_in_critical_section());
00150     TEST_ASSERT_TRUE(callback_called);
00151 }
00152 
00153 /** Template for tests
00154 
00155     Test C++ API of critical section enable/disable
00156     Given a Timeout with attached callback
00157     When before critical section
00158     Then interrupts are enabled and timeout callback is fired
00159     When inside critical section
00160     Then interrupts are disabled and timeout callback is blocked
00161     When after critical section
00162     Then interrupts are enabled and timeout callback is fired
00163 
00164     Test C++ API of critical section enable/disable - nested lock
00165     Given a Timeout with attached callback
00166     When before critical section
00167     Then interrupts are enabled and timeout callback is fired
00168     When inside nested critical section
00169     Then interrupts are disabled and timeout callback is blocked
00170     When after critical section
00171     Then interrupts are enabled and timeout callback is fired
00172  */
00173 template<int N>
00174 void test_CPP_API_enable_disable(void)
00175 {
00176     Timeout timeout;
00177     const us_timestamp_t timeout_time_us = 1;
00178     const int wait_time_us = timeout_time_us * 100;
00179 
00180     TEST_ASSERT_FALSE(core_util_in_critical_section());
00181 
00182     callback_called = false;
00183     timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00184     wait_us(wait_time_us);
00185     TEST_ASSERT_TRUE(callback_called);
00186 
00187     for(int i = 0; i < N; i++) {
00188         CriticalSectionLock::enable();
00189         TEST_ASSERT_TRUE(core_util_in_critical_section());
00190     }
00191 
00192     callback_called = false;
00193     timeout.attach_us(callback(tiemout_callback), timeout_time_us);
00194     wait_us(wait_time_us);
00195     TEST_ASSERT_FALSE(callback_called);
00196     TEST_ASSERT_TRUE(core_util_in_critical_section());
00197 
00198     for(int i = 0; i < N - 1; i++) {
00199         CriticalSectionLock::disable();
00200         TEST_ASSERT_TRUE(core_util_in_critical_section());
00201         TEST_ASSERT_FALSE(callback_called);
00202     }
00203 
00204     CriticalSectionLock::disable();
00205     TEST_ASSERT_FALSE(core_util_in_critical_section());
00206     TEST_ASSERT_TRUE(callback_called);
00207 }
00208 
00209 
00210 utest::v1::status_t test_setup(const size_t number_of_cases)
00211 {
00212     GREENTEA_SETUP(10, "default_auto");
00213     return utest::v1::verbose_test_setup_handler(number_of_cases);
00214 }
00215 
00216 Case cases[] = {
00217     Case("Test critical section C API", test_C_API<1>),
00218     Case("Test critical section C API nested lock", test_C_API<10>),
00219     Case("Test critical section C++ API constructor/destructor", test_CPP_API_constructor_destructor<1>),
00220     Case("Test critical section C++ API constructor/destructor nested lock", test_CPP_API_constructor_destructor<10>),
00221     Case("Test critical section C++ API enable/disable", test_CPP_API_enable_disable<1>),
00222     Case("Test critical section C++ API enable/disable nested lock", test_CPP_API_enable_disable<10>)
00223 };
00224 
00225 utest::v1::Specification specification(test_setup, cases);
00226 
00227 int main()
00228 {
00229     return !utest::v1::Harness::run(specification);
00230 }