Development mbed library for MAX32630FTHR
Dependents: blinky_max32630fthr
TESTS/mbed_drivers/race_test/main.cpp@0:5c4d7b2438d3, 2016-11-11 (annotated)
- Committer:
- switches
- Date:
- Fri Nov 11 20:59:50 2016 +0000
- Revision:
- 0:5c4d7b2438d3
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
switches | 0:5c4d7b2438d3 | 1 | /* |
switches | 0:5c4d7b2438d3 | 2 | * Copyright (c) 2016-2016, ARM Limited, All Rights Reserved |
switches | 0:5c4d7b2438d3 | 3 | * SPDX-License-Identifier: Apache-2.0 |
switches | 0:5c4d7b2438d3 | 4 | * |
switches | 0:5c4d7b2438d3 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
switches | 0:5c4d7b2438d3 | 6 | * not use this file except in compliance with the License. |
switches | 0:5c4d7b2438d3 | 7 | * You may obtain a copy of the License at |
switches | 0:5c4d7b2438d3 | 8 | * |
switches | 0:5c4d7b2438d3 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
switches | 0:5c4d7b2438d3 | 10 | * |
switches | 0:5c4d7b2438d3 | 11 | * Unless required by applicable law or agreed to in writing, software |
switches | 0:5c4d7b2438d3 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
switches | 0:5c4d7b2438d3 | 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
switches | 0:5c4d7b2438d3 | 14 | * See the License for the specific language governing permissions and |
switches | 0:5c4d7b2438d3 | 15 | * limitations under the License. |
switches | 0:5c4d7b2438d3 | 16 | */ |
switches | 0:5c4d7b2438d3 | 17 | #include "mbed.h" |
switches | 0:5c4d7b2438d3 | 18 | #include "rtos.h" |
switches | 0:5c4d7b2438d3 | 19 | #include "greentea-client/test_env.h" |
switches | 0:5c4d7b2438d3 | 20 | #include "unity/unity.h" |
switches | 0:5c4d7b2438d3 | 21 | #include "utest/utest.h" |
switches | 0:5c4d7b2438d3 | 22 | #include "SingletonPtr.h" |
switches | 0:5c4d7b2438d3 | 23 | #include <stdio.h> |
switches | 0:5c4d7b2438d3 | 24 | |
switches | 0:5c4d7b2438d3 | 25 | using namespace utest::v1; |
switches | 0:5c4d7b2438d3 | 26 | |
switches | 0:5c4d7b2438d3 | 27 | #define TEST_STACK_SIZE 1024 |
switches | 0:5c4d7b2438d3 | 28 | static uint32_t instance_count = 0; |
switches | 0:5c4d7b2438d3 | 29 | |
switches | 0:5c4d7b2438d3 | 30 | class TestClass { |
switches | 0:5c4d7b2438d3 | 31 | public: |
switches | 0:5c4d7b2438d3 | 32 | TestClass() { |
switches | 0:5c4d7b2438d3 | 33 | printf("TestClass ctor start\r\n"); |
switches | 0:5c4d7b2438d3 | 34 | Thread::wait(500); |
switches | 0:5c4d7b2438d3 | 35 | instance_count++; |
switches | 0:5c4d7b2438d3 | 36 | printf("TestClass ctor end\r\n"); |
switches | 0:5c4d7b2438d3 | 37 | } |
switches | 0:5c4d7b2438d3 | 38 | |
switches | 0:5c4d7b2438d3 | 39 | void do_something() { |
switches | 0:5c4d7b2438d3 | 40 | printf("Do something called\r\n"); |
switches | 0:5c4d7b2438d3 | 41 | } |
switches | 0:5c4d7b2438d3 | 42 | |
switches | 0:5c4d7b2438d3 | 43 | ~TestClass() { |
switches | 0:5c4d7b2438d3 | 44 | instance_count--; |
switches | 0:5c4d7b2438d3 | 45 | } |
switches | 0:5c4d7b2438d3 | 46 | }; |
switches | 0:5c4d7b2438d3 | 47 | |
switches | 0:5c4d7b2438d3 | 48 | static TestClass* get_test_class() |
switches | 0:5c4d7b2438d3 | 49 | { |
switches | 0:5c4d7b2438d3 | 50 | static TestClass tc; |
switches | 0:5c4d7b2438d3 | 51 | return &tc; |
switches | 0:5c4d7b2438d3 | 52 | } |
switches | 0:5c4d7b2438d3 | 53 | |
switches | 0:5c4d7b2438d3 | 54 | static SingletonPtr<TestClass> test_class; |
switches | 0:5c4d7b2438d3 | 55 | |
switches | 0:5c4d7b2438d3 | 56 | static void main_func_race() |
switches | 0:5c4d7b2438d3 | 57 | { |
switches | 0:5c4d7b2438d3 | 58 | get_test_class(); |
switches | 0:5c4d7b2438d3 | 59 | } |
switches | 0:5c4d7b2438d3 | 60 | |
switches | 0:5c4d7b2438d3 | 61 | static void main_class_race() |
switches | 0:5c4d7b2438d3 | 62 | { |
switches | 0:5c4d7b2438d3 | 63 | test_class->do_something(); |
switches | 0:5c4d7b2438d3 | 64 | } |
switches | 0:5c4d7b2438d3 | 65 | |
switches | 0:5c4d7b2438d3 | 66 | void test_case_func_race() |
switches | 0:5c4d7b2438d3 | 67 | { |
switches | 0:5c4d7b2438d3 | 68 | printf("Running function race test\r\n"); |
switches | 0:5c4d7b2438d3 | 69 | Callback<void()> cb(main_func_race); |
switches | 0:5c4d7b2438d3 | 70 | Thread *t1 = new Thread(osPriorityNormal, TEST_STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 71 | Thread *t2 = new Thread(osPriorityNormal, TEST_STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 72 | |
switches | 0:5c4d7b2438d3 | 73 | // Start start first thread |
switches | 0:5c4d7b2438d3 | 74 | t1->start(cb); |
switches | 0:5c4d7b2438d3 | 75 | // Start second thread while the first is inside the constructor |
switches | 0:5c4d7b2438d3 | 76 | Thread::wait(250); |
switches | 0:5c4d7b2438d3 | 77 | t2->start(cb); |
switches | 0:5c4d7b2438d3 | 78 | |
switches | 0:5c4d7b2438d3 | 79 | // Wait for the threads to finish |
switches | 0:5c4d7b2438d3 | 80 | t1->join(); |
switches | 0:5c4d7b2438d3 | 81 | t2->join(); |
switches | 0:5c4d7b2438d3 | 82 | |
switches | 0:5c4d7b2438d3 | 83 | delete t1; |
switches | 0:5c4d7b2438d3 | 84 | delete t2; |
switches | 0:5c4d7b2438d3 | 85 | |
switches | 0:5c4d7b2438d3 | 86 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
switches | 0:5c4d7b2438d3 | 87 | |
switches | 0:5c4d7b2438d3 | 88 | // Reset instance count |
switches | 0:5c4d7b2438d3 | 89 | instance_count = 0; |
switches | 0:5c4d7b2438d3 | 90 | } |
switches | 0:5c4d7b2438d3 | 91 | |
switches | 0:5c4d7b2438d3 | 92 | void test_case_class_race() |
switches | 0:5c4d7b2438d3 | 93 | { |
switches | 0:5c4d7b2438d3 | 94 | printf("Running class race test\r\n"); |
switches | 0:5c4d7b2438d3 | 95 | Callback<void()> cb(main_class_race); |
switches | 0:5c4d7b2438d3 | 96 | Thread *t1 = new Thread(osPriorityNormal, TEST_STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 97 | Thread *t2 = new Thread(osPriorityNormal, TEST_STACK_SIZE); |
switches | 0:5c4d7b2438d3 | 98 | |
switches | 0:5c4d7b2438d3 | 99 | // Start start first thread |
switches | 0:5c4d7b2438d3 | 100 | t1->start(cb); |
switches | 0:5c4d7b2438d3 | 101 | // Start second thread while the first is inside the constructor |
switches | 0:5c4d7b2438d3 | 102 | Thread::wait(250); |
switches | 0:5c4d7b2438d3 | 103 | t2->start(cb); |
switches | 0:5c4d7b2438d3 | 104 | |
switches | 0:5c4d7b2438d3 | 105 | // Wait for the threads to finish |
switches | 0:5c4d7b2438d3 | 106 | t1->join(); |
switches | 0:5c4d7b2438d3 | 107 | t2->join(); |
switches | 0:5c4d7b2438d3 | 108 | |
switches | 0:5c4d7b2438d3 | 109 | delete t1; |
switches | 0:5c4d7b2438d3 | 110 | delete t2; |
switches | 0:5c4d7b2438d3 | 111 | |
switches | 0:5c4d7b2438d3 | 112 | TEST_ASSERT_EQUAL_UINT32(1, instance_count); |
switches | 0:5c4d7b2438d3 | 113 | |
switches | 0:5c4d7b2438d3 | 114 | // Reset instance count |
switches | 0:5c4d7b2438d3 | 115 | instance_count = 0; |
switches | 0:5c4d7b2438d3 | 116 | } |
switches | 0:5c4d7b2438d3 | 117 | |
switches | 0:5c4d7b2438d3 | 118 | Case cases[] = { |
switches | 0:5c4d7b2438d3 | 119 | Case("function init race", test_case_func_race), |
switches | 0:5c4d7b2438d3 | 120 | Case("class init race", test_case_class_race), |
switches | 0:5c4d7b2438d3 | 121 | }; |
switches | 0:5c4d7b2438d3 | 122 | |
switches | 0:5c4d7b2438d3 | 123 | utest::v1::status_t greentea_test_setup(const size_t number_of_cases) |
switches | 0:5c4d7b2438d3 | 124 | { |
switches | 0:5c4d7b2438d3 | 125 | GREENTEA_SETUP(20, "default_auto"); |
switches | 0:5c4d7b2438d3 | 126 | return greentea_test_setup_handler(number_of_cases); |
switches | 0:5c4d7b2438d3 | 127 | } |
switches | 0:5c4d7b2438d3 | 128 | |
switches | 0:5c4d7b2438d3 | 129 | Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); |
switches | 0:5c4d7b2438d3 | 130 | |
switches | 0:5c4d7b2438d3 | 131 | int main() |
switches | 0:5c4d7b2438d3 | 132 | { |
switches | 0:5c4d7b2438d3 | 133 | Harness::run(specification); |
switches | 0:5c4d7b2438d3 | 134 | } |