Rtos API example

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 
00002 /* mbed Microcontroller Library
00003  * Copyright (c) 2013-2015 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 #include "mbed.h"
00018 #include "greentea-client/test_env.h"
00019 #include "utest/utest.h"
00020 #include "unity/unity.h"
00021 
00022 using namespace utest::v1;
00023 
00024 static int call_counter(0);
00025 static bool never_call = false;
00026 
00027 void never_call_case()
00028 {
00029       never_call = true;
00030 }
00031 
00032 utest::v1::status_t abort_case_setup(const Case *const source, const size_t index_of_case)
00033 {
00034     call_counter++;
00035     TEST_ASSERT_EQUAL(0, index_of_case);
00036     greentea_case_setup_handler(source, index_of_case);
00037     return STATUS_ABORT;
00038 }
00039 
00040 utest::v1::status_t abort_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
00041 {
00042     TEST_ASSERT_FALSE_MESSAGE(never_call, "Case handler should never have been called!");
00043     TEST_ASSERT_EQUAL(1, call_counter);
00044     TEST_ASSERT_EQUAL(0, passed);
00045     TEST_ASSERT_EQUAL(1, failed);
00046     TEST_ASSERT_EQUAL(REASON_CASE_SETUP, failure.reason);
00047     TEST_ASSERT_EQUAL(LOCATION_CASE_SETUP, failure.location);
00048     call_counter++;
00049     return greentea_case_teardown_handler(source, 1, 0, failure);
00050 }
00051 
00052 utest::v1::status_t ignore_case_setup(const Case *const source, const size_t index_of_case)
00053 {
00054     TEST_ASSERT_EQUAL(2, call_counter);
00055     TEST_ASSERT_EQUAL(1, index_of_case);
00056     greentea_case_setup_handler(source, index_of_case);
00057     call_counter++;
00058     return STATUS_IGNORE;   // this is the same
00059 }
00060 
00061 utest::v1::status_t ignore_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
00062 {
00063     TEST_ASSERT_EQUAL(3, call_counter);
00064     TEST_ASSERT_EQUAL(0, passed);
00065     TEST_ASSERT_EQUAL(1, failed);
00066     TEST_ASSERT_EQUAL(REASON_CASE_SETUP, failure.reason);
00067     TEST_ASSERT_EQUAL(LOCATION_CASE_SETUP, failure.location);
00068     call_counter++;
00069     return greentea_case_teardown_handler(source, 1, 0, failure);
00070 }
00071 
00072 utest::v1::status_t continue_case_setup(const Case *const source, const size_t index_of_case)
00073 {
00074     TEST_ASSERT_EQUAL(4, call_counter);
00075     TEST_ASSERT_EQUAL(2, index_of_case);
00076     greentea_case_setup_handler(source, index_of_case);
00077     call_counter++;
00078     return STATUS_CONTINUE;   // this is the same
00079 }
00080 
00081 void continue_case_handler()
00082 {
00083     TEST_ASSERT_EQUAL(5, call_counter);
00084     call_counter++;
00085 }
00086 
00087 utest::v1::status_t continue_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
00088 {
00089     TEST_ASSERT_EQUAL(6, call_counter);
00090     TEST_ASSERT_EQUAL(1, passed);
00091     TEST_ASSERT_EQUAL(0, failed);
00092     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00093     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00094     call_counter++;
00095     return greentea_case_teardown_handler(source, passed, failed, failure);
00096 }
00097 
00098 Case cases[] = {
00099     Case("Setup handler returns ABORT", abort_case_setup, never_call_case, abort_case_teardown),
00100     Case("Setup handler returns IGNORE", ignore_case_setup, never_call_case, ignore_case_teardown),
00101     Case("Setup handler returns CONTINUE", continue_case_setup, continue_case_handler, continue_case_teardown)
00102 };
00103 
00104 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00105 {
00106     GREENTEA_SETUP(15, "default_auto");
00107 
00108     return greentea_test_setup_handler(number_of_cases);
00109 }
00110 
00111 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00112 {
00113     TEST_ASSERT_EQUAL(7, call_counter);
00114     TEST_ASSERT_EQUAL(1, passed);
00115     TEST_ASSERT_EQUAL(2, failed);
00116     TEST_ASSERT_EQUAL(REASON_CASES, failure.reason);
00117     TEST_ASSERT_EQUAL(LOCATION_UNKNOWN, failure.location);
00118 
00119     // pretend to greentea that the test was successful
00120     greentea_test_teardown_handler(3, 0, REASON_NONE);
00121 }
00122 
00123 Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers);
00124 
00125 int main()
00126 {
00127     // Run the specification only AFTER setting the custom scheduler(if required).
00128     Harness::run(specification);
00129 }