diff --git a/libraries/tests/mbed/dev_null/main.cpp b/libraries/tests/mbed/dev_null/main.cpp index eeaf5ecba2..ee43645da3 100644 --- a/libraries/tests/mbed/dev_null/main.cpp +++ b/libraries/tests/mbed/dev_null/main.cpp @@ -1,9 +1,10 @@ #include "mbed.h" +#include "test_env.h" class DevNull : public Stream { public: - DevNull(const char *name=NULL) : Stream(name) {} + DevNull(const char *name = NULL) : Stream(name) {} protected: virtual int _getc() {return 0;} @@ -13,14 +14,10 @@ protected: DevNull null("null"); int main() { - printf("re-routing stdout to /null\n"); - + printf("MBED: re-routing stdout to /null\n"); freopen("/null", "w", stdout); - printf("printf redirected to /null\n"); - - DigitalOut led(LED1); - while (true) { - led = !led; - wait(1); - } + printf("MBED: printf redirected to /null\n"); // This shouldn't appear + // If failure message can be seen test should fail :) + notify_completion(false); // This is 'false' on purpose + return 0; } diff --git a/libraries/tests/mbed/div/main.cpp b/libraries/tests/mbed/div/main.cpp index 5ad7e5e20a..263809a522 100644 --- a/libraries/tests/mbed/div/main.cpp +++ b/libraries/tests/mbed/div/main.cpp @@ -1,4 +1,6 @@ +#include // std::pair #include "mbed.h" +#include "test_env.h" uint32_t test_64(uint64_t ticks) { ticks >>= 3; // divide by 8 @@ -10,18 +12,30 @@ uint32_t test_64(uint64_t ticks) { return (uint32_t)(0xFFFFFFFF & ticks); } -int main() { - // 0xFFFFFFFF * 8 = 0x7fffffff8 - printf("expected : 55555555\n"); - printf("64: 0x7fffffff8: %x\n", test_64(0x7fffffff8)); - - // 0xFFFFFFFF * 24 = 0x17ffffffe8 - printf("expected : ffffffff\n"); - printf("64: 0x17ffffffe8: %x\n", test_64(0x17FFFFFFE8)); - - DigitalOut led(LED1); - while (1) { - led = !led; - wait(0.5); - } +const char *result_str(bool result) { + return result ? "[OK]" : "[FAIL]"; +} + +int main() { + DigitalOut led(LED1); + bool result = true; + + { // 0xFFFFFFFF * 8 = 0x7fffffff8 + std::pair values = std::make_pair(0x55555555, 0x7FFFFFFF8); + uint32_t test_ret = test_64(values.second); + bool test_res = values.first == test_ret; + result = result && test_res; + printf("64bit: 0x7FFFFFFF8: expected 0x%X got 0x%X ... %s\r\n", values.first, test_ret, result_str(test_res)); + } + + { // 0xFFFFFFFF * 24 = 0x17ffffffe8 + std::pair values = std::make_pair(0xFFFFFFFF, 0x17FFFFFFE8); + uint32_t test_ret = test_64(values.second); + bool test_res = values.first == test_ret; + result = result && test_res; + printf("64bit: 0x17FFFFFFE8: expected 0x%X got 0x%X ... %s\r\n", values.first, test_ret, result_str(test_res)); + } + + notify_completion(result); + return 0; } diff --git a/libraries/tests/mbed/hello/main.cpp b/libraries/tests/mbed/hello/main.cpp index f354b4a871..578eafbab0 100644 --- a/libraries/tests/mbed/hello/main.cpp +++ b/libraries/tests/mbed/hello/main.cpp @@ -4,7 +4,6 @@ DigitalOut myled(LED1); int main() { printf("Hello World\n"); - while (true) { wait(0.5); myled = !myled; diff --git a/libraries/tests/mbed/semihost/main.cpp b/libraries/tests/mbed/semihost/main.cpp index ca4344b7a6..438df017a9 100644 --- a/libraries/tests/mbed/semihost/main.cpp +++ b/libraries/tests/mbed/semihost/main.cpp @@ -1,17 +1,31 @@ #include "test_env.h" +#define MAC_VENDOR_ARM_0 0x00 +#define MAC_VENDOR_ARM_1 0x02 +#define MAC_VENDOR_ARM_2 0xF7 + int main() { - char uid[DEVICE_ID_LENGTH + 1]; - - if (mbed_interface_uid(uid) == 0) { - printf("UID: %s\n", uid); - } else{ - printf("Error\n"); + char uid[DEVICE_ID_LENGTH + 1] = {0}; + bool result = true; + + const int ret = mbed_interface_uid(uid); + if (ret == 0) { + printf("UID: %s\r\n", uid); } - - char mac[16]; + else { + result = false; + } + + char mac[6] = {0}; // @param mac A 6-byte array to write the MAC address mbed_mac_address(mac); - printf("mac address: %02x,%02x,%02x,%02x,%02x,%02x\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); - - notify_completion(true); + printf("MAC Address: %02X:%02X:%02X:%02X:%02X:%02X\r\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]); + + if (mac[0] == MAC_VENDOR_ARM_0 && + mac[1] == MAC_VENDOR_ARM_1 && + mac[2] == MAC_VENDOR_ARM_2) { + printf("MAC Address Prefix: 00:02:F7, Vendor: ARM\r\n"); + } + + notify_completion(result); + return 0; } diff --git a/libraries/tests/net/helloworld/tcpclient/main.cpp b/libraries/tests/net/helloworld/tcpclient/main.cpp index 5c5f7c79e3..8be303513b 100644 --- a/libraries/tests/net/helloworld/tcpclient/main.cpp +++ b/libraries/tests/net/helloworld/tcpclient/main.cpp @@ -1,31 +1,61 @@ +#include #include "mbed.h" #include "EthernetInterface.h" +#include "test_env.h" + +namespace { + const char *HTTP_SERVER_NAME = "mbed.org"; + const int HTTP_SERVER_PORT = 80; + const int RECV_BUFFER_SIZE = 512; + + // Test related data + const char *HTTP_OK_STR = "200 OK"; + const char *HTTP_HELLO_STR = "Hello world!"; +} + +bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) { + const char *f = std::search(first, last, s_first, s_last); + return (f != last); +} int main() { EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); - printf("IP Address is %s\n", eth.getIPAddress()); - + printf("TCP client IP Address is %s\n", eth.getIPAddress()); + TCPSocketConnection sock; - sock.connect("mbed.org", 80); - + sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT); + char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n"; sock.send_all(http_cmd, sizeof(http_cmd)); - - char buffer[300]; - int ret; + + char buffer[RECV_BUFFER_SIZE] = {0}; + bool result = true; while (true) { - ret = sock.receive(buffer, sizeof(buffer)-1); + const int ret = sock.receive(buffer, sizeof(buffer) - 1); if (ret <= 0) break; buffer[ret] = '\0'; - printf("Received %d chars from server:\n%s\n", ret, buffer); + + // Find 200 OK HTTP status in reply + bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR)); + result = result && found_200_ok; + + // Find Hello World! in reply + bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR)); + result = result && found_hello; + + // Print results + printf("HTTP: Received %d chars from server\r\n", ret); + printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]"); + printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]"); + printf("HTTP: Received massage:\r\n\r\n"); + printf("%s", buffer); } - + sock.close(); - eth.disconnect(); - - while(1) {} + notify_completion(result); + return 0; } diff --git a/libraries/tests/net/helloworld/udpclient/main.cpp b/libraries/tests/net/helloworld/udpclient/main.cpp index a6d3c822ad..0cc446d31e 100644 --- a/libraries/tests/net/helloworld/udpclient/main.cpp +++ b/libraries/tests/net/helloworld/udpclient/main.cpp @@ -1,28 +1,48 @@ #include "mbed.h" #include "EthernetInterface.h" - +#include "test_env.h" + +namespace { + const char *HTTP_SERVER_NAME = "utcnist.colorado.edu"; + const int HTTP_SERVER_PORT = 37; +} + int main() { + bool result = false; EthernetInterface eth; eth.init(); //Use DHCP eth.connect(); - + printf("UDP client IP Address is %s\n", eth.getIPAddress()); + UDPSocket sock; sock.init(); - + Endpoint nist; - nist.set_address("utcnist.colorado.edu", 37); - + nist.set_address(HTTP_SERVER_NAME, HTTP_SERVER_PORT); + char out_buffer[] = "plop"; // Does not matter sock.sendTo(nist, out_buffer, sizeof(out_buffer)); - - char in_buffer[4]; - int n = sock.receiveFrom(nist, in_buffer, sizeof(in_buffer)); - - unsigned int timeRes = ntohl( *((unsigned int*)in_buffer)); - printf("Received %d bytes from server %s on port %d: %u seconds since 1/01/1900 00:00 GMT\n", n, nist.get_address(), nist.get_port(), timeRes); - + + union { + char in_buffer_tab[4]; + unsigned int in_buffer_uint; + }; + + const int n = sock.receiveFrom(nist, in_buffer_tab, sizeof(in_buffer_tab)); + if (n > 0) { + const unsigned int timeRes = ntohl(in_buffer_uint); + const float years = timeRes / 60.0 / 60.0 / 24.0 / 365; + printf("UDP: Received %d bytes from server %s on port %d\r\n", n, nist.get_address(), nist.get_port()); + printf("UDP: %u seconds since 01/01/1900 00:00 GMT ... %s\r\n", timeRes, timeRes > 0 ? "[OK]" : "[FAIL]"); + printf("UDP: %.2f years since 01/01/1900 00:00 GMT ... %s\r\n", years, timeRes > 114.0 ? "[OK]" : "[FAIL]"); + result = true; + + if (years < 114.0) { + result = false; + } + } sock.close(); - eth.disconnect(); - while(1) {} + notify_completion(result); + return 0; } diff --git a/workspace_tools/host_tests/dev_null_auto.py b/workspace_tools/host_tests/dev_null_auto.py new file mode 100644 index 0000000000..64d37b5eae --- /dev/null +++ b/workspace_tools/host_tests/dev_null_auto.py @@ -0,0 +1,37 @@ +""" +mbed SDK +Copyright (c) 2011-2013 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. +""" + +from host_test import Test, DefaultTest +from sys import stdout + +class DevNullTest(DefaultTest): + + def print_result(self, result): + print "\n{%s}\n{end}" % result + + def run(self): + test_result = True + c = self.mbed.serial.read(512) + print "Received %d bytes" % len(c) + if "{failure}" not in c: + self.print_result('success') + else: + self.print_result('failure') + stdout.flush() + +if __name__ == '__main__': + DevNullTest().run() diff --git a/workspace_tools/host_tests/hello_auto.py b/workspace_tools/host_tests/hello_auto.py new file mode 100644 index 0000000000..3381a1b896 --- /dev/null +++ b/workspace_tools/host_tests/hello_auto.py @@ -0,0 +1,37 @@ +""" +mbed SDK +Copyright (c) 2011-2013 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. +""" + +from host_test import Test, DefaultTest +from sys import stdout + +class HelloTest(DefaultTest): + HELLO_WORLD = "Hello World\n" + + def print_result(self, result): + print "\n{%s}\n{end}" % result + + def run(self): + c = self.mbed.serial.read(len(self.HELLO_WORLD)) + stdout.write(c) + if c == self.HELLO_WORLD: # Hello World received + self.print_result('success') + else: + self.print_result('failure') + stdout.flush() + +if __name__ == '__main__': + HelloTest().run() diff --git a/workspace_tools/singletest.py b/workspace_tools/singletest.py index 7a76d1b557..2376142fe9 100644 --- a/workspace_tools/singletest.py +++ b/workspace_tools/singletest.py @@ -93,6 +93,7 @@ from workspace_tools.paths import BUILD_DIR from workspace_tools.paths import HOST_TESTS from workspace_tools.targets import TARGET_MAP from workspace_tools.tests import TEST_MAP +from workspace_tools.tests import TESTS # Be sure that the tools directory is in the search path ROOT = abspath(join(dirname(__file__), "..")) @@ -306,9 +307,9 @@ def reset(mcu_name, serial, verbose=False, sleep_before_reset=0, sleep_after_res sleep(sleep_before_reset) if verbose: verbose_msg = "Reset::cmd(sendBreak)" - + serial.sendBreak() - + if sleep_before_reset > 0: sleep(sleep_after_reset) if verbose: @@ -377,6 +378,73 @@ def get_json_data_from_file(json_spec_filename, verbose=False): return result +def get_result_summary_table(): + # get all unique test ID prefixes + unique_test_id = [] + for test in TESTS: + split = test['id'].split('_')[:-1] + test_id_prefix = '_'.join(split) + if test_id_prefix not in unique_test_id: + unique_test_id.append(test_id_prefix) + unique_test_id.sort() + counter_dict_test_id_types = dict((t, 0) for t in unique_test_id) + counter_dict_test_id_types_all = dict((t, 0) for t in unique_test_id) + + test_properties = ['id', 'automated', 'description', 'peripherals', 'host_test', 'duration'] + pt = PrettyTable(test_properties) + for col in test_properties: + pt.align[col] = "l" # Left align + + counter_all = 0 + counter_automated = 0 + + pt.padding_width = 1 # One space between column edges and contents (default) + for test in TESTS: + row = [] + split = test['id'].split('_')[:-1] + test_id_prefix = '_'.join(split) + + for col in test_properties: + row.append(test[col] if col in test else "") + if 'automated' in test and test['automated'] == True: + counter_dict_test_id_types[test_id_prefix] += 1 + counter_automated += 1 + pt.add_row(row) + # Update counters + counter_all += 1 + counter_dict_test_id_types_all[test_id_prefix] += 1 + print pt + print "Result:" + percent_progress = round(100.0 * counter_automated / float(counter_all), 2) + print "\tAutomated: %d / %d (%s %%)" % (counter_automated, counter_all, percent_progress) + print + test_id_cols = ['id', 'automated', 'all', 'percent [%]', 'progress'] + pt = PrettyTable(test_id_cols) + pt.align['id'] = "l" # Left align + for unique_id in unique_test_id: + # print "\t\t%s: %d / %d" % (unique_id, counter_dict_test_id_types[unique_id], counter_dict_test_id_types_all[unique_id]) + percent_progress = round(100.0 * counter_dict_test_id_types[unique_id] / float(counter_dict_test_id_types_all[unique_id]), 2) + str_progress = progress_bar(percent_progress, 75) + row = [unique_id, + counter_dict_test_id_types[unique_id], + counter_dict_test_id_types_all[unique_id], + percent_progress, + "[" + str_progress + "]"] + pt.add_row(row) + print pt + + +def progress_bar(percent_progress, saturation=0): + """ This function creates progress bar with optional simple saturation mark""" + step = int(percent_progress / 2) # Scale by to (scale: 1 - 50) + str_progress = '#' * step + '.' * int(50 - step) + c = '!' if str_progress[38] == '.' else '|' + if (saturation > 0): + saturation = saturation / 2 + str_progress = str_progress[:saturation] + c + str_progress[saturation:] + return str_progress + + if __name__ == '__main__': # Command line options parser = optparse.OptionParser() @@ -408,6 +476,18 @@ if __name__ == '__main__': action="store_true", help='Suppresses display of wellformatted table with test results') + parser.add_option('-r', '--test-automation-report', + dest='test_automation_report', + default=False, + action="store_true", + help='Prints information about all tests and exits') + + parser.add_option('-P', '--only-peripheral', + dest='test_only_peripheral', + default=False, + action="store_true", + help='Test only peripheral declared for MUT and skip common tests') + parser.add_option('-v', '--verbose', dest='verbose', default=False, @@ -415,10 +495,15 @@ if __name__ == '__main__': help='Verbose mode (pronts some extra information)') parser.description = """This script allows you to run mbed defined test cases for particular MCU(s) and corresponding toolchain(s).""" - parser.epilog = """Example: singletest.py -i test_spec.json [-M muts_all.json]""" + parser.epilog = """Example: singletest.py -i test_spec.json -M muts_all.json""" (opts, args) = parser.parse_args() + # Print summary / information about automation test status + if opts.test_automation_report: + get_result_summary_table() + exit(0) + # Open file with test specification # test_spec_filename tells script which targets and their toolchain(s) # should be covered by the test scenario @@ -458,6 +543,11 @@ if __name__ == '__main__': if test_ids and test_id not in test_ids: continue + if opts.test_only_peripheral and not test.peripherals: + if opts.verbose: + print "TargetTest::%s::NotPeripheralTestSkipped(%s)" % (target, ",".join(test.peripherals)) + continue + if test.automated and test.is_supported(target, toolchain): if not is_peripherals_available(target, test.peripherals): if opts.verbose: diff --git a/workspace_tools/tests.py b/workspace_tools/tests.py index 7a5fc67e95..9f42c58643 100644 --- a/workspace_tools/tests.py +++ b/workspace_tools/tests.py @@ -322,6 +322,8 @@ TESTS = [ "id": "MBED_10", "description": "Hello World", "source_dir": join(TEST_DIR, "mbed", "hello"), "dependencies": [MBED_LIBRARIES], + "automated": True, + "host_test": "hello_auto", }, { "id": "MBED_11", "description": "Ticker Int", @@ -391,6 +393,7 @@ TESTS = [ "id": "MBED_22", "description": "Semihost", "source_dir": join(TEST_DIR, "mbed", "semihost"), "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + "automated": True, }, { "id": "MBED_23", "description": "Ticker Int us", @@ -420,6 +423,7 @@ TESTS = [ "id": "MBED_26", "description": "Integer constant division", "source_dir": join(TEST_DIR, "mbed", "div"), "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + "automated": True, }, { "id": "MBED_27", "description": "SPI ADXL345", @@ -568,12 +572,18 @@ TESTS = [ { "id": "NET_1", "description": "TCP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "tcpclient"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "duration": 15, + "automated": True, + "peripherals": ["ethernet"], }, { "id": "NET_2", "description": "UDP client hello world", "source_dir": join(TEST_DIR, "net", "helloworld", "udpclient"), - "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY, TEST_MBED_LIB], + "duration": 15, + "automated": True, + "peripherals": ["ethernet"], }, { "id": "NET_3", "description": "TCP echo server", @@ -581,7 +591,7 @@ TESTS = [ "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], "automated": True, "host_test" : "tcpecho_server_auto", - "peripherals": ["ethernet"] + "peripherals": ["ethernet"], }, { "id": "NET_4", "description": "TCP echo client", @@ -626,26 +636,31 @@ TESTS = [ "id": "NET_9", "description": "Multicast Send", "source_dir": join(TEST_DIR, "net", "helloworld", "multicast_send"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "peripherals": ["ethernet"], }, { "id": "NET_10", "description": "Multicast Receive", "source_dir": join(TEST_DIR, "net", "helloworld", "multicast_receive"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "peripherals": ["ethernet"], }, { "id": "NET_11", "description": "Broadcast Send", "source_dir": join(TEST_DIR, "net", "helloworld", "broadcast_send"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "peripherals": ["ethernet"], }, { "id": "NET_12", "description": "Broadcast Receive", "source_dir": join(TEST_DIR, "net", "helloworld", "broadcast_receive"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "peripherals": ["ethernet"], }, { "id": "NET_13", "description": "TCP client echo loop", "source_dir": join(TEST_DIR, "net", "echo", "tcp_client_loop"), "dependencies": [MBED_LIBRARIES, RTOS_LIBRARIES, ETH_LIBRARY], + "peripherals": ["ethernet"], }, # u-blox tests @@ -752,7 +767,9 @@ TESTS = [ { "id": "EXAMPLE_1", "description": "/dev/null", "source_dir": join(TEST_DIR, "mbed", "dev_null"), - "dependencies": [MBED_LIBRARIES], + "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], + "automated": True, + "host_test" : "dev_null_auto", }, { "id": "EXAMPLE_2", "description": "FS + RTOS",