Lee Kai Xuan / mbed-os

Fork of mbed-os by erkin yucel

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "greentea-client/test_env.h"
00003 #include "unity.h"
00004 #include "utest.h"
00005 #include "rtos.h"
00006 #include "SynchronizedIntegral.h"
00007 #include "LockGuard.h"
00008 
00009 #if defined(MBED_RTOS_SINGLE_THREAD)
00010   #error [NOT_SUPPORTED] test not supported
00011 #endif
00012 
00013 /*
00014  * The stack size is defined in cmsis_os.h mainly dependent on the underlying toolchain and
00015  * the C standard library. For GCC, ARM_STD and IAR it is defined with a size of 2048 bytes
00016  * and for ARM_MICRO 512. Because of reduce RAM size some targets need a reduced stacksize.
00017  */
00018 #if defined(TARGET_MCU_NRF51822) || defined(TARGET_MCU_NRF52832)
00019     #define STACK_SIZE 512
00020 #elif defined(TARGET_STM32F070RB) || defined(TARGET_STM32F072RB) || defined(TARGET_STM32F103RB) || defined(TARGET_STM32F091RC)
00021     #define STACK_SIZE 512
00022 #elif defined(TARGET_STM32F410RB)
00023     #define STACK_SIZE 512
00024 #elif defined(TARGET_STM32L073RZ)
00025     #define STACK_SIZE 512
00026 #elif defined(TARGET_XDOT_L151CC)
00027     #define STACK_SIZE 1024
00028 #else
00029     #define STACK_SIZE DEFAULT_STACK_SIZE
00030 #endif
00031 
00032 using namespace utest::v1;
00033 
00034 // The counter type used accross all the tests
00035 // It is internall ysynchronized so read
00036 typedef SynchronizedIntegral<int> counter_t;
00037 
00038 // Tasks with different functions to test on threads
00039 void increment(counter_t* counter) {
00040     (*counter)++;
00041 }
00042 
00043 void increment_with_yield(counter_t* counter) {
00044     Thread::yield();
00045     (*counter)++;
00046 }
00047 
00048 void increment_with_wait(counter_t* counter) {
00049     Thread::wait(100);
00050     (*counter)++;
00051 }
00052 
00053 void increment_with_child(counter_t* counter) {
00054     Thread child(counter, increment, osPriorityNormal, STACK_SIZE);
00055     child.join();
00056 }
00057 
00058 void increment_with_murder(counter_t* counter) {
00059     {
00060         // take ownership of the counter mutex so it prevent the child to
00061         // modify counter.
00062         LockGuard lock(counter->internal_mutex());
00063         Thread child(counter, increment, osPriorityNormal, STACK_SIZE);
00064         child.terminate();
00065     }
00066 
00067     (*counter)++;
00068 }
00069 
00070 void self_terminate(Thread *self) {
00071     self->terminate();
00072     // Code should not get here
00073     TEST_ASSERT(0);
00074 }
00075 
00076 // Tests that spawn tasks in different configurations
00077 template <void (*F)(counter_t *)>
00078 void test_single_thread() {
00079     counter_t counter(0);
00080     Thread thread(&counter, F, osPriorityNormal, STACK_SIZE);
00081     thread.join();
00082     TEST_ASSERT_EQUAL(counter, 1);
00083 }
00084 
00085 template <int N, void (*F)(counter_t *)>
00086 void test_parallel_threads() {
00087     counter_t counter(0);
00088     Thread *threads[N];
00089 
00090     for (int i = 0; i < N; i++) {
00091         threads[i] = new Thread(&counter, F, osPriorityNormal, STACK_SIZE);
00092     }
00093 
00094     for (int i = 0; i < N; i++) {
00095         threads[i]->join();
00096         delete threads[i];
00097     }
00098 
00099     TEST_ASSERT_EQUAL(counter, N);
00100 }
00101 
00102 template <int N, void (*F)(counter_t *)>
00103 void test_serial_threads() {
00104     counter_t counter(0);
00105 
00106     for (int i = 0; i < N; i++) {
00107         Thread thread(&counter, F, osPriorityNormal, STACK_SIZE);
00108         thread.join();
00109     }
00110 
00111     TEST_ASSERT_EQUAL(counter, N);
00112 }
00113 
00114 void test_self_terminate() {
00115     Thread *thread = new Thread(osPriorityNormal, STACK_SIZE);
00116     thread->start(thread, self_terminate);
00117     thread->join();
00118     delete thread;
00119 }
00120 
00121 utest::v1::status_t  test_setup(const size_t number_of_cases) {
00122     GREENTEA_SETUP(40, "default_auto");
00123     return verbose_test_setup_handler(number_of_cases);
00124 }
00125 
00126 // Test cases
00127 Case cases[] = {
00128     Case("Testing single thread", test_single_thread<increment>),
00129     Case("Testing parallel threads", test_parallel_threads<3, increment>),
00130     Case("Testing serial threads", test_serial_threads<10, increment>),
00131 
00132     Case("Testing single thread with yield", test_single_thread<increment_with_yield>),
00133     Case("Testing parallel threads with yield", test_parallel_threads<3, increment_with_yield>),
00134     Case("Testing serial threads with yield", test_serial_threads<10, increment_with_yield>),
00135 
00136     Case("Testing single thread with wait", test_single_thread<increment_with_wait>),
00137     Case("Testing parallel threads with wait", test_parallel_threads<3, increment_with_wait>),
00138     Case("Testing serial threads with wait", test_serial_threads<10, increment_with_wait>),
00139 
00140     Case("Testing single thread with child", test_single_thread<increment_with_child>),
00141     Case("Testing parallel threads with child", test_parallel_threads<2, increment_with_child>),
00142     Case("Testing serial threads with child", test_serial_threads<10, increment_with_child>),
00143 
00144     Case("Testing single thread with murder", test_single_thread<increment_with_murder>),
00145     Case("Testing parallel threads with murder", test_parallel_threads<3, increment_with_murder>),
00146     Case("Testing serial threads with murder", test_serial_threads<10, increment_with_murder>),
00147 
00148     Case("Testing thread self terminate", test_self_terminate),
00149 };
00150 
00151 Specification specification(test_setup, cases);
00152 
00153 int main() {
00154     return !Harness::run(specification);
00155 }