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