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 
00026 // Control: RepeatAll -------------------------------------------------------------------------------------------------
00027 utest::v1::status_t repeat_all_case_setup(const Case *const source, const size_t index_of_case)
00028 {
00029     static int repeat_counter(0);
00030     TEST_ASSERT_EQUAL(0, index_of_case);
00031     TEST_ASSERT_EQUAL(repeat_counter*3, call_counter++);
00032     repeat_counter++;
00033     return greentea_case_setup_handler(source, index_of_case);
00034 }
00035 control_t repeat_all_case(const size_t call_count)
00036 {
00037     static int repeat_counter(1);
00038     TEST_ASSERT_EQUAL(repeat_counter++, call_count);
00039     TEST_ASSERT_EQUAL((call_count-1)*3 + 1, call_counter++);
00040     return (call_count < 10) ? CaseRepeatAll : CaseNoRepeat;
00041 }
00042 utest::v1::status_t repeat_all_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
00043 {
00044     static int repeat_counter(0);
00045     TEST_ASSERT_EQUAL(repeat_counter*3 + 2, call_counter++);
00046     TEST_ASSERT_EQUAL(repeat_counter+1, passed);
00047     TEST_ASSERT_EQUAL(0, failed);
00048     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00049     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00050     repeat_counter++;
00051     return greentea_case_teardown_handler(source, passed, failed, failure);
00052 }
00053 
00054 // Control: RepeatHandler ---------------------------------------------------------------------------------------------
00055 utest::v1::status_t repeat_handler_case_setup(const Case *const source, const size_t index_of_case)
00056 {
00057     TEST_ASSERT_EQUAL(1, index_of_case);
00058     TEST_ASSERT_EQUAL(30, call_counter++);
00059     return greentea_case_setup_handler(source, index_of_case);
00060 }
00061 control_t repeat_handler_case(const size_t call_count)
00062 {
00063     static int repeat_counter(1);
00064     TEST_ASSERT_EQUAL(repeat_counter++, call_count);
00065     TEST_ASSERT_EQUAL((call_count-1) + 31, call_counter++);
00066     return (call_count < 10) ? CaseRepeatHandler : CaseNoRepeat;
00067 }
00068 utest::v1::status_t repeat_handler_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure)
00069 {
00070     TEST_ASSERT_EQUAL(41, call_counter++);
00071     TEST_ASSERT_EQUAL(10, passed);
00072     TEST_ASSERT_EQUAL(0, failed);
00073     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00074     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00075     return greentea_case_teardown_handler(source, passed, failed, failure);
00076 }
00077 
00078 // Control: NoRepeat --------------------------------------------------------------------------------------------------
00079 control_t no_repeat_handler_case(const size_t call_count)
00080 {
00081     static int repeat_counter(1);
00082     TEST_ASSERT_EQUAL(repeat_counter++, call_count);
00083     TEST_ASSERT_EQUAL(1, call_count);
00084     TEST_ASSERT_EQUAL(42, call_counter++);
00085     return CaseNoRepeat;
00086 }
00087 
00088 // Control: CaseNext --------------------------------------------------------------------------------------------------
00089 control_t next_handler_case(const size_t call_count)
00090 {
00091     static int repeat_counter(1);
00092     TEST_ASSERT_EQUAL(repeat_counter++, call_count);
00093     TEST_ASSERT_EQUAL(1, call_count);
00094     TEST_ASSERT_EQUAL(43, call_counter++);
00095     return CaseNext;
00096 }
00097 
00098 Case cases[] = {
00099     Case("Control: RepeatAll", repeat_all_case_setup, repeat_all_case, repeat_all_case_teardown),
00100     Case("Control: RepeatHandler", repeat_handler_case_setup, repeat_handler_case, repeat_handler_case_teardown),
00101     Case("Control: NoRepeat", no_repeat_handler_case),
00102     Case("Control: CaseNext", next_handler_case)
00103 };
00104 
00105 utest::v1::status_t greentea_setup(const size_t number_of_cases)
00106 {
00107     GREENTEA_SETUP(15, "default_auto");
00108 
00109     return greentea_test_setup_handler(number_of_cases);
00110 }
00111 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure)
00112 {
00113     TEST_ASSERT_EQUAL(44, call_counter);
00114     TEST_ASSERT_EQUAL(4, passed);
00115     TEST_ASSERT_EQUAL(0, failed);
00116     TEST_ASSERT_EQUAL(REASON_NONE, failure.reason);
00117     TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location);
00118     greentea_test_teardown_handler(passed, failed, failure);
00119 }
00120 
00121 
00122 Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers);
00123 
00124 int main()
00125 {
00126     // Run the specification only AFTER setting the custom scheduler(if required).
00127     Harness::run(specification);
00128 }