diff --git a/TESTS/mbed_drivers/c_strings/main.cpp b/TESTS/mbed_drivers/c_strings/main.cpp new file mode 100644 index 0000000000..efb73999ef --- /dev/null +++ b/TESTS/mbed_drivers/c_strings/main.cpp @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest/utest.h" + +namespace { +static char buffer[256] = {0}; +} + +#define CLEAN_BUFFER memset(::buffer, 0x00, sizeof(::buffer)) +#define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,-1,-4231,-999,-4123,-32760,-99999 +#define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 +#define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910 + +using namespace utest::v1; + + +void test_case_c_string_i_d() { + CLEAN_BUFFER; + sprintf(buffer, "%i %d %i %d %i %d %i %d %i %d %i %i", NEGATIVE_INTEGERS); + TEST_ASSERT_EQUAL_STRING("-32768 -3214 -999 -100 -1 0 -1 -4231 -999 -4123 -32760 -99999", buffer); +} + +void test_case_c_string_u_d() { + CLEAN_BUFFER; + sprintf(buffer, "%u %d %u %d %u %d %u %d %u %d %u %d", POSITIVE_INTEGERS); + TEST_ASSERT_EQUAL_STRING("32768 3214 999 100 1 0 1 4231 999 4123 32760 99999", buffer); +} + +void test_case_c_string_x_E() { + CLEAN_BUFFER; + sprintf(buffer, "%x %X %x %X %x %X %x %X %x %X %x %X", POSITIVE_INTEGERS); + TEST_ASSERT_EQUAL_STRING("8000 C8E 3e7 64 1 0 1 1087 3e7 101B 7ff8 1869F", buffer); +} + +void test_case_c_string_f_f() { + CLEAN_BUFFER; + sprintf(buffer, "%f %f %f %f %f %f %f %f %f %f", FLOATS); + TEST_ASSERT_EQUAL_STRING("0.002000 0.924300 15.913200 791.773680 6208.200000 25719.495200 426815.982588 6429271.046000 42468024.930000 212006462.910000", buffer); +} + +void test_case_c_string_g_g() { + CLEAN_BUFFER; + sprintf(buffer, "%g %g %g %g %g %g %g %g %g %g", FLOATS); + TEST_ASSERT_EQUAL_STRING("0.002 0.9243 15.9132 791.774 6208.2 25719.5 426816 6.42927e+006 4.2468e+007 2.12006e+008", buffer); + TEST_ASSERT_EQUAL_STRING("0.002 0.9243 15.9132 791.774 6208.2 25719.5 426816 6.42927e+06 4.2468e+07 2.12006e+08", buffer); +} + +void test_case_c_string_e_E() { + CLEAN_BUFFER; + sprintf(buffer, "%e %E %e %E %e %E %e %E %e %E", FLOATS); + TEST_ASSERT_EQUAL_STRING("2.000000e-003 9.243000E-001 1.591320e+001 7.917737E+002 6.208200e+003 2.571950E+004 4.268160e+005 6.429271E+006 4.246802e+007 2.120065E+008", buffer); + TEST_ASSERT_EQUAL_STRING("2.000000e-03 9.243000E-01 1.591320e+01 7.917737E+02 6.208200e+03 2.571950E+04 4.268160e+05 6.429271E+06 4.246802e+07 2.120065E+08", buffer); +} + +void test_case_c_string_strtok() { + CLEAN_BUFFER; + char str[] ="- This, a sample string."; + char * pch = strtok (str," ,.-"); + while (pch != NULL) { + strcat(buffer, pch); + pch = strtok (NULL, " ,.-"); + } + TEST_ASSERT_EQUAL_STRING("Thisasamplestring", buffer); +} + +void test_case_c_string_strpbrk() { + CLEAN_BUFFER; + char str[] = "This is a sample string"; + char key[] = "aeiou"; + char *pch = strpbrk(str, key); + while (pch != NULL) + { + char buf[2] = {*pch, '\0'}; + strcat(buffer, buf); + pch = strpbrk(pch + 1,key); + } + TEST_ASSERT_EQUAL_STRING("iiaaei", buffer); +} + +utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { + greentea_case_failure_abort_handler(source, reason); + return STATUS_CONTINUE; +} + +Case cases[] = { + Case("C strings: strtok", test_case_c_string_strtok, greentea_failure_handler), + Case("C strings: strpbrk", test_case_c_string_strpbrk, greentea_failure_handler), + Case("C strings: %i %d integer formatting", test_case_c_string_i_d, greentea_failure_handler), + Case("C strings: %u %d integer formatting", test_case_c_string_u_d, greentea_failure_handler), + Case("C strings: %x %E integer formatting", test_case_c_string_x_E, greentea_failure_handler), + Case("C strings: %f %f float formatting", test_case_c_string_f_f, greentea_failure_handler), + Case("C strings: %e %E float formatting", test_case_c_string_e_E, greentea_failure_handler), + Case("C strings: %g %g float formatting", test_case_c_string_g_g, greentea_failure_handler), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(5, "default_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/dev_null/main.cpp b/TESTS/mbed_drivers/dev_null/main.cpp new file mode 100644 index 0000000000..22940d026f --- /dev/null +++ b/TESTS/mbed_drivers/dev_null/main.cpp @@ -0,0 +1,48 @@ +/* mbed Microcontroller Library + * Copyright (c) 2013-2014 ARM Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "mbed.h" +#include "greentea-client/test_env.h" + +class DevNull : public Stream { +public: + DevNull(const char *name = NULL) : Stream(name) {} + +protected: + virtual int _getc() { + return 0; + } + virtual int _putc(int c) { + return c; + } +}; + +DevNull null("null"); + +int main() { + GREENTEA_SETUP(2, "dev_null_auto"); + + printf("MBED: before re-routing stdout to /null\n"); // This shouldn't appear + greentea_send_kv("to_stdout", "re-routing stdout to /null"); + + if (freopen("/null", "w", stdout)) { + // This shouldn't appear on serial + // We should use pure printf here to send KV + printf("{{to_null;printf redirected to /null}}\n"); + printf("MBED: this printf is already redirected to /null\n"); + } + GREENTEA_TESTSUITE_RESULT(false); +} diff --git a/TESTS/mbed_drivers/echo/main.cpp b/TESTS/mbed_drivers/echo/main.cpp new file mode 100644 index 0000000000..f531a79f7d --- /dev/null +++ b/TESTS/mbed_drivers/echo/main.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest/utest.h" + +using namespace utest::v1; + +// Echo server (echo payload to host) +template +void test_case_echo_server_x() { + char _key[10] = {}; + char _value[128] = {}; + const int echo_count = N; + + // Handshake with host + greentea_send_kv("echo_count", echo_count); + greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); + TEST_ASSERT_EQUAL_INT(echo_count, atoi(_value)); + + for (int i=0; i < echo_count; ++i) { + greentea_parse_kv(_key, _value, sizeof(_key), sizeof(_value)); + greentea_send_kv(_key, _value); + } +} + +utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { + greentea_case_failure_abort_handler(source, reason); + return STATUS_CONTINUE; +} + +Case cases[] = { + Case("Echo server: x16", test_case_echo_server_x<16>, greentea_failure_handler), + Case("Echo server: x32", test_case_echo_server_x<32>, greentea_failure_handler), + Case("Echo server: x64", test_case_echo_server_x<64>, greentea_failure_handler), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(30, "echo"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/generic_tests/main.cpp b/TESTS/mbed_drivers/generic_tests/main.cpp new file mode 100644 index 0000000000..dc177e3f4d --- /dev/null +++ b/TESTS/mbed_drivers/generic_tests/main.cpp @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include // std::pair +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest/utest.h" + +using namespace utest::v1; + +#define PATTERN_CHECK_VALUE 0xF0F0ADAD + +class CppTestCaseHelperClass { +private: + const char* name; + const unsigned pattern; + +public: + CppTestCaseHelperClass(const char* _name) : name(_name), pattern(PATTERN_CHECK_VALUE) { + print("init"); + } + + void print(const char *message) { + printf("%s::%s\n", name, message); + } + + bool check_init(void) { + bool result = (pattern == PATTERN_CHECK_VALUE); + print(result ? "check_init: OK" : "check_init: ERROR"); + return result; + } + + void stack_test(void) { + print("stack_test"); + CppTestCaseHelperClass t("Stack"); + t.hello(); + } + + void hello(void) { + print("hello"); + } + + ~CppTestCaseHelperClass() { + print("destroy"); + } +}; + + +void test_case_basic() { + TEST_ASSERT_TRUE(true); + TEST_ASSERT_FALSE(false); + TEST_ASSERT_EQUAL_STRING("The quick brown fox jumps over the lazy dog", + "The quick brown fox jumps over the lazy dog"); +} + +void test_case_blinky() { + static DigitalOut myled(LED1); + const int cnt_max = 1024; + for (int cnt = 0; cnt < cnt_max; ++cnt) { + myled = !myled; + } +} + +void test_case_cpp_stack() { + // Check C++ start-up initialisation + CppTestCaseHelperClass s("Static"); + + // Global stack object simple test + s.stack_test(); + TEST_ASSERT_TRUE_MESSAGE(s.check_init(), "s.check_init() failed"); +} + +void test_case_cpp_heap() { + // Heap test object simple test + CppTestCaseHelperClass *m = new CppTestCaseHelperClass("Heap"); + m->hello(); + TEST_ASSERT_TRUE_MESSAGE(m->check_init(), "m->check_init() failed"); + delete m; +} + +utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { + greentea_case_failure_abort_handler(source, reason); + return STATUS_CONTINUE; +} + +// Generic test cases +Case cases[] = { + Case("Basic", test_case_basic, greentea_failure_handler), + Case("Blinky", test_case_blinky, greentea_failure_handler), + Case("C++ stack", test_case_cpp_stack, greentea_failure_handler), + Case("C++ heap", test_case_cpp_heap, greentea_failure_handler) +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "default_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/rtc/main.cpp b/TESTS/mbed_drivers/rtc/main.cpp new file mode 100644 index 0000000000..fae4d06e20 --- /dev/null +++ b/TESTS/mbed_drivers/rtc/main.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest/utest.h" + +using namespace utest::v1; + +#define CUSTOM_TIME 1256729737 + +void test_case_rtc_strftime() { + greentea_send_kv("timestamp", CUSTOM_TIME); + + char buffer[32] = {0}; + char kv_buff[64] = {0}; + set_time(CUSTOM_TIME); // Set RTC time to Wed, 28 Oct 2009 11:35:37 + + for (int i=0; i<10; ++i) { + time_t seconds = time(NULL); + sprintf(kv_buff, "[%ld] ", seconds); + strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S %p", localtime(&seconds)); + strcat(kv_buff, buffer); + greentea_send_kv("rtc", kv_buff); + wait(1); + } +} + +Case cases[] = { + Case("RTC strftime", test_case_rtc_strftime), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "rtc_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/stl_features/main.cpp b/TESTS/mbed_drivers/stl_features/main.cpp new file mode 100644 index 0000000000..ec99282d45 --- /dev/null +++ b/TESTS/mbed_drivers/stl_features/main.cpp @@ -0,0 +1,139 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include +#include +#include +#include +#include +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "unity/unity.h" +#include "utest/utest.h" + +using namespace utest::v1; + +#define TABLE_SIZE(TAB) (sizeof(TAB) / sizeof(TAB[0])) + +#define NEGATIVE_INTEGERS -32768,-3214,-999,-100,-1,0,1,4231,999,4123,32760,99999 +#define POSITIVE_INTEGERS 32768,3214,999,100,1,0,1,4231,999,4123,32760,99999 +#define FLOATS 0.002,0.92430,15.91320,791.77368,6208.2,25719.4952,426815.982588,6429271.046,42468024.93,212006462.910 +#define FLOATS_STR "0.002","0.92430","15.91320","791.77368","6208.2","25719.4952","426815.982588","6429271.046","42468024.93","212006462.910" + + +namespace { +int p_integers[] = {POSITIVE_INTEGERS}; +int n_integers[] = {NEGATIVE_INTEGERS}; +float floats[] = {FLOATS}; + +template +void BubbleSort(T& _array, size_t array_size, F functor) { + bool flag = true; + size_t numLength = array_size; + for(size_t i = 1; (i <= numLength) && flag; i++) { + flag = false; + for (size_t j = 0; j < (numLength - 1); j++) { + if (functor(_array[j+1], _array[j])) { + int temp = _array[j]; + _array[j] = _array[j + 1]; + _array[j+1] = temp; + flag = true; + } + } + } +} + +struct printInt { + void operator()(int i) { printf("%d ", i); } +}; + +struct printFloat { + void operator()(float f) { printf("%f ", f); } +}; + +struct printString { + void operator()(const char* s) { printf("%s ", s); } +}; + +struct greaterAbs { + bool operator()(int a, int b) { return abs(a) > abs(b); } +}; + +} // namespace + +void test_case_stl_equal() { + std::vector v_pints(p_integers, p_integers + TABLE_SIZE(p_integers)); + TEST_ASSERT_TRUE(std::equal(v_pints.begin(), v_pints.end(), p_integers)); +} + +void test_case_stl_transform() { + const char* floats_str[] = {FLOATS_STR}; + float floats_transform[TABLE_SIZE(floats_str)] = {0.0}; + std::transform(floats_str, floats_str + TABLE_SIZE(floats_str), floats_transform, atof); + //printf("stl_transform::floats_str: "); + //std::for_each(floats_str, floats_str + TABLE_SIZE(floats_str), printString()); + //printf("stl_transform::floats_transform: "); + //std::for_each(floats_transform, floats_transform + TABLE_SIZE(floats_transform), printFloat()); + //printf("\n"); + + TEST_ASSERT_TRUE(std::equal(floats_transform, floats_transform + TABLE_SIZE(floats_transform), floats)); +} + +void test_case_stl_sort_greater() { + std::vector v_nints_1(n_integers, n_integers + TABLE_SIZE(n_integers)); + std::vector v_nints_2(n_integers, n_integers + TABLE_SIZE(n_integers)); + + BubbleSort(v_nints_1, v_nints_1.size(), std::greater()); + std::sort(v_nints_2.begin(), v_nints_2.end(), std::greater()); + + TEST_ASSERT_TRUE(std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin())); +} + +void test_case_stl_sort_abs() { + std::vector v_nints_1(n_integers, n_integers + TABLE_SIZE(n_integers)); + std::vector v_nints_2(n_integers, n_integers + TABLE_SIZE(n_integers)); + + BubbleSort(v_nints_1, v_nints_1.size(), greaterAbs()); + std::sort(v_nints_2.begin(), v_nints_2.end(), greaterAbs()); + + TEST_ASSERT_TRUE(std::equal(v_nints_1.begin(), v_nints_1.end(), v_nints_2.begin())); +} + +utest::v1::status_t greentea_failure_handler(const Case *const source, const failure_t reason) { + greentea_case_failure_abort_handler(source, reason); + return STATUS_CONTINUE; +} + +Case cases[] = { + Case("STL std::equal", test_case_stl_equal, greentea_failure_handler), + Case("STL std::transform", test_case_stl_transform, greentea_failure_handler), + Case("STL std::sort greater", test_case_stl_sort_greater, greentea_failure_handler), + Case("STL std::sort abs", test_case_stl_sort_abs, greentea_failure_handler) +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(5, "default_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/ticker/main.cpp b/TESTS/mbed_drivers/ticker/main.cpp new file mode 100644 index 0000000000..0b6cdeac1c --- /dev/null +++ b/TESTS/mbed_drivers/ticker/main.cpp @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "utest/utest.h" + +using namespace utest::v1; + +void send_kv_tick() { + static int count = 0; + if (count < 10) { + greentea_send_kv("tick", count); + } else if (count == 10) { + Harness::validate_callback(); + } + count++; +} + +Ticker flipper_1; +DigitalOut led1(LED1); + +void flip_1() { + static int led1_state = 0; + if (led1_state) { + led1 = 0; led1_state = 0; + } else { + led1 = 1; led1_state = 1; + } + send_kv_tick(); +} + +Ticker flipper_2; +DigitalOut led2(LED2); + +void flip_2() { + static int led2_state = 0; + if (led2_state) { + led2 = 0; led2_state = 0; + } else { + led2 = 1; led2_state = 1; + } +} + +control_t test_case_ticker() { + led1 = 0; + led2 = 0; + flipper_1.attach(&flip_1, 1.0); + flipper_2.attach(&flip_2, 2.0); + return CaseTimeout(15 * 1000); +} + +// Test cases +Case cases[] = { + Case("Timers: 2 x tickers", test_case_ticker), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "wait_us_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/ticker_2/main.cpp b/TESTS/mbed_drivers/ticker_2/main.cpp new file mode 100644 index 0000000000..7fcc526d8a --- /dev/null +++ b/TESTS/mbed_drivers/ticker_2/main.cpp @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "utest/utest.h" + +using namespace utest::v1; + +Ticker tick; +DigitalOut led(LED1); + +namespace { + const int MS_INTERVALS = 1000; +} + +void send_kv_tick() { + static int count = 0; + if (count < 10) { + greentea_send_kv("tick", count); + } else if (count == 10) { + Harness::validate_callback(); + } + count++; +} + +void togglePin(void) { + static int ticker_count = 0; + if (ticker_count >= MS_INTERVALS) { + send_kv_tick(); + ticker_count = 0; + led = !led; + } + ticker_count++; +} + +utest::v1::control_t test_case_ticker() { + tick.attach_us(togglePin, 1000); + return CaseTimeout(15 * 1000); +} + +// Test cases +Case cases[] = { + Case("Timers: 1x ticker", test_case_ticker), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "wait_us_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/ticker_3/main.cpp b/TESTS/mbed_drivers/ticker_3/main.cpp new file mode 100644 index 0000000000..dcb95d8127 --- /dev/null +++ b/TESTS/mbed_drivers/ticker_3/main.cpp @@ -0,0 +1,73 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "utest/utest.h" + +using namespace utest::v1; + +void ticker_callback_1(void); +void ticker_callback_2(void); + +DigitalOut led0(LED1); +DigitalOut led1(LED2); +Ticker ticker; + +void send_kv_tick() { + static int count = 0; + if (count < 10) { + greentea_send_kv("tick", count); + } else if (count == 10) { + Harness::validate_callback(); + } + count++; +} + +void ticker_callback_2(void) { + ticker.detach(); + ticker.attach(ticker_callback_1, 1.0); + led1 = !led1; + send_kv_tick(); +} + +void ticker_callback_1(void) { + ticker.detach(); + ticker.attach(ticker_callback_2, 1.0); + led0 = !led0; + send_kv_tick(); +} + +utest::v1::control_t test_case_ticker() { + ticker.attach(ticker_callback_1, 1.0); + return CaseTimeout(15 * 1000); +} + +// Test cases +Case cases[] = { + Case("Timers: 2x callbacks", test_case_ticker), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "wait_us_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} diff --git a/TESTS/mbed_drivers/timeout/main.cpp b/TESTS/mbed_drivers/timeout/main.cpp new file mode 100644 index 0000000000..1317475db4 --- /dev/null +++ b/TESTS/mbed_drivers/timeout/main.cpp @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "utest/utest.h" + +using namespace utest::v1; + +Timeout timer; +DigitalOut led(LED1); + +namespace { + const int MS_INTERVALS = 1000; +} + +void send_kv_tick() { + static int count = 0; + if (count < 10) { + greentea_send_kv("tick", count); + } else if (count == 10) { + Harness::validate_callback(); + } + count++; +} + +void toggleOff(void); + +void toggleOn(void) { + static int toggle_counter = 0; + if (toggle_counter == MS_INTERVALS) { + led = !led; + send_kv_tick(); + toggle_counter = 0; + } + toggle_counter++; + timer.attach_us(toggleOff, 500); +} + +void toggleOff(void) { + timer.attach_us(toggleOn, 500); +} + +control_t test_case_ticker() { + toggleOn(); + return CaseTimeout(15 * 1000); +} + +// Test cases +Case cases[] = { + Case("Timers: toggle on/off", test_case_ticker), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "wait_us_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +} + diff --git a/TESTS/mbed_drivers/wait_us/main.cpp b/TESTS/mbed_drivers/wait_us/main.cpp new file mode 100644 index 0000000000..429f2409cb --- /dev/null +++ b/TESTS/mbed_drivers/wait_us/main.cpp @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2013-2016, ARM Limited, All Rights Reserved + * SPDX-License-Identifier: Apache-2.0 + * + * Licensed under the Apache License, Version 2.0 (the "License"); you may + * not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "mbed.h" +#include "greentea-client/test_env.h" +#include "utest/utest.h" + +using namespace utest::v1; + +DigitalOut led(LED1); + +void test_case_ticker() { + for (int i=0; i < 10; ++i) { + // 10 secs... + for (int j = 0; j < 1000; ++j) { + // 1000 * 1000us = 1 sec + wait_us(1000); + } + led = !led; // Blink + greentea_send_kv("tick", i); + } +} + +// Test cases +Case cases[] = { + Case("Timers: wait_us", test_case_ticker), +}; + +utest::v1::status_t greentea_test_setup(const size_t number_of_cases) { + GREENTEA_SETUP(20, "wait_us_auto"); + return greentea_test_setup_handler(number_of_cases); +} + +Specification specification(greentea_test_setup, cases, greentea_test_teardown_handler); + +int main() { + Harness::run(specification); +}