Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of OmniWheels by
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 #include "utest/utest_stack_trace.h" 00022 00023 using namespace utest::v1; 00024 00025 00026 static int call_counter(0); 00027 static Timeout utest_to; 00028 00029 class Utest_func_bind { 00030 00031 public: 00032 inline Utest_func_bind( void (*f)(int), int v) : _callback_fn(f), _callback_value(v) 00033 {} 00034 00035 inline void callback() { 00036 (*_callback_fn)(_callback_value); 00037 } 00038 00039 private: 00040 void (*_callback_fn)(int); 00041 int _callback_value; 00042 }; 00043 00044 00045 00046 void await_case_validate(int expected_call_count) 00047 { 00048 UTEST_LOG_FUNCTION(); 00049 TEST_ASSERT_EQUAL(expected_call_count, call_counter++); 00050 Harness::validate_callback(); 00051 } 00052 00053 static Utest_func_bind validate1(await_case_validate, 7); 00054 static Utest_func_bind validate2(await_case_validate, 37); 00055 static Utest_func_bind validate3(await_case_validate, 50); 00056 00057 00058 00059 // Control: Timeout (Failure) ----------------------------------------------------------------------------------------- 00060 control_t timeout_failure_case(const size_t call_count) 00061 { 00062 UTEST_LOG_FUNCTION(); 00063 TEST_ASSERT_EQUAL(1, call_count); 00064 TEST_ASSERT_EQUAL(0, call_counter++); 00065 return CaseTimeout(100); 00066 } 00067 utest::v1::status_t timeout_failure_case_failure_handler(const Case *const source, const failure_t failure) 00068 { 00069 UTEST_LOG_FUNCTION(); 00070 TEST_ASSERT_EQUAL(1, call_counter++); 00071 TEST_ASSERT_EQUAL(REASON_TIMEOUT, failure.reason); 00072 TEST_ASSERT_EQUAL(LOCATION_CASE_HANDLER, failure.location); 00073 verbose_case_failure_handler(source, failure); 00074 return STATUS_CONTINUE; 00075 } 00076 utest::v1::status_t timeout_failure_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) 00077 { 00078 UTEST_LOG_FUNCTION(); 00079 TEST_ASSERT_EQUAL(2, call_counter++); 00080 TEST_ASSERT_EQUAL(0, passed); 00081 TEST_ASSERT_EQUAL(1, failed); 00082 TEST_ASSERT_EQUAL(REASON_CASES, failure.reason); 00083 TEST_ASSERT_EQUAL(LOCATION_UNKNOWN, failure.location); 00084 return greentea_case_teardown_handler(source, 1, 0, REASON_NONE); 00085 } 00086 00087 // Control: Timeout (Success) ----------------------------------------------------------------------------------------- 00088 void timeout_success_case_validate() { 00089 UTEST_LOG_FUNCTION(); 00090 TEST_ASSERT_EQUAL(4, call_counter++); 00091 Harness::validate_callback(); 00092 } 00093 00094 control_t timeout_success_case(const size_t call_count) 00095 { 00096 UTEST_LOG_FUNCTION(); 00097 TEST_ASSERT_EQUAL(1, call_count); 00098 TEST_ASSERT_EQUAL(3, call_counter++); 00099 utest_to.attach_us(timeout_success_case_validate, 100000); // Fire after 100 ms 00100 00101 return CaseTimeout(200); 00102 } 00103 utest::v1::status_t timeout_success_case_failure_handler(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) 00104 { 00105 UTEST_LOG_FUNCTION(); 00106 TEST_ASSERT_EQUAL(5, call_counter++); 00107 TEST_ASSERT_EQUAL(1, passed); 00108 TEST_ASSERT_EQUAL(0, failed); 00109 TEST_ASSERT_EQUAL(REASON_NONE, failure.reason); 00110 TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location); 00111 return greentea_case_teardown_handler(source, passed, failed, failure); 00112 } 00113 00114 // Control: Await ----------------------------------------------------------------------------------------------------- 00115 control_t await_case(const size_t call_count) 00116 { 00117 UTEST_LOG_FUNCTION(); 00118 TEST_ASSERT_EQUAL(1, call_count); 00119 TEST_ASSERT_EQUAL(6, call_counter++); 00120 00121 utest_to.attach_us(&validate1, &Utest_func_bind::callback, (1372*1000)); // Fire after 1372 ms 00122 00123 return CaseAwait; 00124 } 00125 00126 // Control: RepeatAllOnTimeout ---------------------------------------------------------------------------------------- 00127 bool repeat_all_start_flag = true; 00128 utest::v1::status_t repeat_all_on_timeout_case_setup(const Case *const source, const size_t index_of_case) 00129 { 00130 if (repeat_all_start_flag){ 00131 UTEST_TRACE_START 00132 repeat_all_start_flag = false; 00133 } 00134 UTEST_LOG_FUNCTION(); 00135 static int repeat_counter(0); 00136 TEST_ASSERT_EQUAL(3, index_of_case); 00137 TEST_ASSERT_EQUAL(repeat_counter*3 + 8, call_counter++); 00138 repeat_counter++; 00139 return greentea_case_setup_handler(source, index_of_case); 00140 } 00141 control_t repeat_all_on_timeout_case(const size_t call_count) 00142 { 00143 UTEST_LOG_FUNCTION(); 00144 static int repeat_counter(1); 00145 TEST_ASSERT_EQUAL(repeat_counter++, call_count); 00146 TEST_ASSERT(call_count <= 10); 00147 TEST_ASSERT_EQUAL((call_count-1)*3 + 9, call_counter++); 00148 if (call_count == 10) { 00149 utest_to.attach_us(&validate2, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms 00150 } 00151 return CaseRepeatAllOnTimeout(100); 00152 } 00153 utest::v1::status_t repeat_all_on_timeout_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) 00154 { 00155 UTEST_LOG_FUNCTION(); 00156 static int repeat_counter(0); 00157 00158 TEST_ASSERT_EQUAL((call_counter == 38) ? 1 : 0, passed); 00159 TEST_ASSERT_EQUAL(0, failed); 00160 TEST_ASSERT_EQUAL(REASON_NONE, failure.reason); 00161 TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location); 00162 TEST_ASSERT_EQUAL(repeat_counter*3 + ((repeat_counter == 9) ? 11 : 10), call_counter++); 00163 repeat_counter++; 00164 return greentea_case_teardown_handler(source, passed, failed, failure); 00165 } 00166 00167 // Control: RepeatAllOnTimeout ---------------------------------------------------------------------------------------- 00168 utest::v1::status_t repeat_handler_on_timeout_case_setup(const Case *const source, const size_t index_of_case) 00169 { 00170 UTEST_LOG_FUNCTION(); 00171 TEST_ASSERT_EQUAL(4, index_of_case); 00172 TEST_ASSERT_EQUAL(39, call_counter++); 00173 return greentea_case_setup_handler(source, index_of_case); 00174 } 00175 00176 control_t repeat_handler_on_timeout_case(const size_t call_count) 00177 { 00178 UTEST_LOG_FUNCTION(); 00179 static int repeat_counter(1); 00180 TEST_ASSERT_EQUAL(repeat_counter++, call_count); 00181 TEST_ASSERT(call_count <= 10); 00182 TEST_ASSERT_EQUAL(call_count-1 + 40, call_counter++); 00183 if (call_count == 10) { 00184 utest_to.attach_us(&validate3, &Utest_func_bind::callback, (50*1000)); // Fire after 50ms 00185 } 00186 return CaseRepeatHandlerOnTimeout(100); 00187 } 00188 utest::v1::status_t repeat_handler_on_timeout_case_teardown(const Case *const source, const size_t passed, const size_t failed, const failure_t failure) 00189 { 00190 UTEST_LOG_FUNCTION(); 00191 TEST_ASSERT_EQUAL(1, passed); 00192 TEST_ASSERT_EQUAL(0, failed); 00193 TEST_ASSERT_EQUAL(REASON_NONE, failure.reason); 00194 TEST_ASSERT_EQUAL(LOCATION_NONE, failure.location); 00195 TEST_ASSERT_EQUAL(51, call_counter++); 00196 return greentea_case_teardown_handler(source, passed, failed, failure); 00197 } 00198 00199 // Control: NoTimeout ------------------------------------------------------------------------------------------------- 00200 control_t no_timeout_case(const size_t call_count) 00201 { 00202 UTEST_LOG_FUNCTION(); 00203 TEST_ASSERT_EQUAL(1, call_count); 00204 TEST_ASSERT_EQUAL(52, call_counter++); 00205 return CaseNoTimeout; 00206 } 00207 00208 // Control: NoTimeout ------------------------------------------------------------------------------------------------- 00209 control_t next_case(const size_t call_count) 00210 { 00211 UTEST_LOG_FUNCTION(); 00212 TEST_ASSERT_EQUAL(1, call_count); 00213 TEST_ASSERT_EQUAL(53, call_counter++); 00214 return CaseNoTimeout; 00215 } 00216 00217 // Cases -------------------------------------------------------------------------------------------------------------- 00218 Case cases[] = { 00219 Case("Control: Timeout (Failure)", timeout_failure_case, timeout_failure_case_teardown, timeout_failure_case_failure_handler), 00220 Case("Control: Timeout (Success)", timeout_success_case, timeout_success_case_failure_handler), 00221 Case("Control: Await", await_case), 00222 Case("Control: RepeatAllOnTimeout", repeat_all_on_timeout_case_setup, repeat_all_on_timeout_case, repeat_all_on_timeout_case_teardown), 00223 Case("Control: RepeatHandlerOnTimeout", repeat_handler_on_timeout_case_setup, repeat_handler_on_timeout_case, repeat_handler_on_timeout_case_teardown), 00224 Case("Control: NoTimeout", no_timeout_case), 00225 Case("Control: CaseNext", next_case) 00226 }; 00227 00228 // Specification: Setup & Teardown ------------------------------------------------------------------------------------ 00229 utest::v1::status_t greentea_setup(const size_t number_of_cases) 00230 { 00231 GREENTEA_SETUP(15, "default_auto"); 00232 00233 return verbose_test_setup_handler(number_of_cases); 00234 } 00235 void greentea_teardown(const size_t passed, const size_t failed, const failure_t failure) 00236 { 00237 TEST_ASSERT_EQUAL(54, call_counter++); 00238 TEST_ASSERT_EQUAL(6, passed); 00239 TEST_ASSERT_EQUAL(1, failed); 00240 TEST_ASSERT_EQUAL(REASON_CASES, failure.reason); 00241 TEST_ASSERT_EQUAL(LOCATION_UNKNOWN, failure.location); 00242 greentea_test_teardown_handler(7, 0, REASON_NONE); 00243 } 00244 00245 Specification specification(greentea_setup, cases, greentea_teardown, selftest_handlers); 00246 00247 int main() 00248 { 00249 // Run the specification only AFTER setting the custom scheduler(if required). 00250 Harness::run(specification); 00251 }
Generated on Fri Jul 22 2022 04:53:54 by
1.7.2
