Refactored few more tests to support autodetection: basic, call_before_main, dev_null, rtc, stdio

pull/900/head
Przemek Wirkus 2015-01-27 12:51:53 +00:00
parent 6903b54b9e
commit a8506caa1d
12 changed files with 88 additions and 96 deletions

View File

@ -1,4 +1,9 @@
#include "test_env.h" #include "test_env.h"
int main() { int main() {
notify_completion(true); TEST_TIMEOUT(20);
TEST_HOSTTEST(default_auto);
TEST_DESCRIPTION(Basic);
TEST_START("MBED_A1");
TEST_RESULT(true);
} }

View File

@ -10,6 +10,12 @@ extern "C" void mbed_main() {
} }
int main() { int main() {
TEST_TIMEOUT(20);
TEST_HOSTTEST(default_auto);
TEST_DESCRIPTION(Call function mbed_main before main);
TEST_START("MBED_A21");
printf("MBED: main() starts now!\r\n"); printf("MBED: main() starts now!\r\n");
notify_completion(mbed_main_called);
TEST_RESULT(mbed_main_called);
} }

View File

@ -1,8 +1,7 @@
#include "mbed.h" #include "mbed.h"
#include "test_env.h" #include "test_env.h"
class DevNull : public Stream class DevNull : public Stream {
{
public: public:
DevNull(const char *name = NULL) : Stream(name) {} DevNull(const char *name = NULL) : Stream(name) {}
@ -17,12 +16,15 @@ protected:
DevNull null("null"); DevNull null("null");
int main() int main() {
{ TEST_TIMEOUT(20);
TEST_HOSTTEST(dev_null_auto);
TEST_DESCRIPTION(stdout redirected to dev null);
TEST_START("EXAMPLE_1");
printf("MBED: re-routing stdout to /null\r\n"); printf("MBED: re-routing stdout to /null\r\n");
freopen("/null", "w", stdout); freopen("/null", "w", stdout);
printf("MBED: printf redirected to /null\r\n"); // This shouldn't appear printf("MBED: printf redirected to /null\r\n"); // This shouldn't appear
// If failure message can be seen test should fail :) // If failure message can be seen test should fail :)
notify_completion(false); // This is 'false' on purpose TEST_RESULT(false); // This is 'false' on purpose
return 0;
} }

View File

@ -6,8 +6,7 @@ Serial pc(USBTX, USBRX);
#define FILENAME "/local/out.txt" #define FILENAME "/local/out.txt"
#define TEST_STRING "Hello World!" #define TEST_STRING "Hello World!"
FILE *test_open(const char *mode) FILE *test_open(const char *mode) {
{
FILE *f = fopen(FILENAME, mode); FILE *f = fopen(FILENAME, mode);
if (f == NULL) { if (f == NULL) {
printf("Error opening file"NL); printf("Error opening file"NL);
@ -16,8 +15,7 @@ FILE *test_open(const char *mode)
return f; return f;
} }
void test_write(FILE *f, char *str, int str_len) void test_write(FILE *f, char *str, int str_len) {
{
int n = fprintf(f, str); int n = fprintf(f, str);
if (n != str_len) { if (n != str_len) {
@ -26,8 +24,7 @@ void test_write(FILE *f, char *str, int str_len)
} }
} }
void test_read(FILE *f, char *str, int str_len) void test_read(FILE *f, char *str, int str_len) {
{
int n = fread(str, sizeof(unsigned char), str_len, f); int n = fread(str, sizeof(unsigned char), str_len, f);
if (n != str_len) { if (n != str_len) {
@ -36,8 +33,7 @@ void test_read(FILE *f, char *str, int str_len)
} }
} }
void test_close(FILE *f) void test_close(FILE *f) {
{
int rc = fclose(f); int rc = fclose(f);
if (rc != 0) { if (rc != 0) {
@ -46,8 +42,12 @@ void test_close(FILE *f)
} }
} }
int main() int main() {
{ TEST_TIMEOUT(20);
TEST_HOSTTEST(default_auto);
TEST_DESCRIPTION(Semihost file system);
TEST_START("MBED_A2");
pc.printf("Test the Stream class\n"); pc.printf("Test the Stream class\n");
printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No")); printf("connected: %s\n", (semihost_connected()) ? ("Yes") : ("No"));
@ -74,5 +74,5 @@ int main()
test_close(f); test_close(f);
// Check the two strings are equal // Check the two strings are equal
notify_completion((strncmp(buffer, str, str_len) == 0)); TEST_RESULT((strncmp(buffer, str, str_len) == 0));
} }

View File

@ -1,8 +1,14 @@
#include "mbed.h" #include "mbed.h"
#include "test_env.h"
#define CUSTOM_TIME 1256729737 #define CUSTOM_TIME 1256729737
int main() { int main() {
TEST_TIMEOUT(20);
TEST_HOSTTEST(rtc_auto);
TEST_DESCRIPTION(RTC);
TEST_START("MBED_16");
char buffer[32] = {0}; char buffer[32] = {0};
set_time(CUSTOM_TIME); // Set RTC time to Wed, 28 Oct 2009 11:35:37 set_time(CUSTOM_TIME); // Set RTC time to Wed, 28 Oct 2009 11:35:37
while(1) { while(1) {

View File

@ -7,19 +7,23 @@
*/ */
int main() { int main() {
TEST_TIMEOUT(20);
TEST_HOSTTEST(stdio_auto);
TEST_DESCRIPTION(stdio);
TEST_START("MBED_2");
DigitalOut led1(LED1); DigitalOut led1(LED1);
DigitalOut led2(LED2); DigitalOut led2(LED2);
union { union {
int value_int; int value_int;
}; };
notify_start(); notify_start(); // Just to sync with host test supervisor
const char* PRINT_PATTERN = "MBED: Your value was: %d\r\n"; const char* PRINT_PATTERN = "MBED: Your value was: %d\r\n";
while (true) while (true) {
{
// SCANF PRINTF family // SCANF PRINTF family
value_int = 0; value_int = 0;
led1 = 1; led1 = 1;

View File

@ -19,6 +19,9 @@ from host_registry import HostRegistry
from default_auto import DefaultAuto from default_auto import DefaultAuto
from hello_auto import HelloTest from hello_auto import HelloTest
from wait_us_auto import WaitusTest from wait_us_auto import WaitusTest
from stdio_auto import StdioTest
from dev_null_auto import DevNullTest
from rtc_auto import RTCTest
HOSTREGISTRY = HostRegistry() HOSTREGISTRY = HostRegistry()
@ -26,6 +29,9 @@ HOSTREGISTRY.register_host_test("default", DefaultAuto())
HOSTREGISTRY.register_host_test("default_auto", DefaultAuto()) HOSTREGISTRY.register_host_test("default_auto", DefaultAuto())
HOSTREGISTRY.register_host_test("hello_auto", HelloTest()) HOSTREGISTRY.register_host_test("hello_auto", HelloTest())
HOSTREGISTRY.register_host_test("wait_us_auto", WaitusTest()) HOSTREGISTRY.register_host_test("wait_us_auto", WaitusTest())
HOSTREGISTRY.register_host_test("stdio_auto", StdioTest())
HOSTREGISTRY.register_host_test("dev_null_auto", DevNullTest())
HOSTREGISTRY.register_host_test("rtc_auto", RTCTest())
############################################################################### ###############################################################################
# Functional interface for test supervisor registry # Functional interface for test supervisor registry

View File

@ -15,25 +15,22 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
""" """
from host_test import DefaultTest class DevNullTest():
def check_readline(self, selftest, text):
class DevNullTest(DefaultTest):
def check_readline(self, text):
""" Reads line from serial port and checks if text was part of read string """ Reads line from serial port and checks if text was part of read string
""" """
result = False result = False
c = self.mbed.serial_readline() c = selftest.mbed.serial_readline()
if c and text in c: if c and text in c:
result = True result = True
return result return result
def test(self): def test(self, selftest):
result = True result = True
# Test should print some text and later stop printing # Test should print some text and later stop printing
# 'MBED: re-routing stdout to /null' # 'MBED: re-routing stdout to /null'
res = self.check_readline("re-routing stdout to /null") res = self.check_readline(selftest, "re-routing stdout to /null")
if not res: if not res:
# We haven't read preamble line # We haven't read preamble line
result = False result = False
@ -41,17 +38,13 @@ class DevNullTest(DefaultTest):
# Check if there are printed characters # Check if there are printed characters
str = '' str = ''
for i in range(3): for i in range(3):
c = self.mbed.serial_read(32) c = selftest.mbed.serial_read(32)
if c is None: if c is None:
return self.RESULT_IO_SERIAL return selftest.RESULT_IO_SERIAL
else: else:
str += c str += c
if len(str) > 0: if len(str) > 0:
result = False result = False
break break
self.notify("Received %d bytes: %s"% (len(str), str)) selftest.notify("Received %d bytes: %s"% (len(str), str))
return self.RESULT_SUCCESS if result else self.RESULT_FAILURE return selftest.RESULT_SUCCESS if result else selftest.RESULT_FAILURE
if __name__ == '__main__':
DevNullTest().run()

View File

@ -254,6 +254,7 @@ class HostTestResults:
self.RESULT_NO_IMAGE = 'no_image' self.RESULT_NO_IMAGE = 'no_image'
self.RESULT_IOERR_COPY = "ioerr_copy" self.RESULT_IOERR_COPY = "ioerr_copy"
self.RESULT_PASSIVE = "passive" self.RESULT_PASSIVE = "passive"
self.RESULT_NOT_DETECTED = "not_detected"
import workspace_tools.host_tests as host_tests import workspace_tools.host_tests as host_tests
@ -348,31 +349,12 @@ class Test(HostTestResults):
self.notify("\n{{%s}}\n{{end}}" % result) self.notify("\n{{%s}}\n{{end}}" % result)
class DefaultTest(Test): class DefaultTestSelector(Test):
""" Test class with serial port initialization """ Test class with serial port initialization
""" """
def __init__(self): def __init__(self):
HostTestResults.__init__(self) HostTestResults.__init__(self)
Test.__init__(self) Test.__init__(self)
class Simple(DefaultTest):
""" Simple, basic host test's test runner waiting for serial port
output from MUT, no supervision over test running in MUT is executed.
"""
def test(self):
result = self.RESULT_SUCCESS
try:
while True:
c = self.mbed.serial_read(512)
if c is None:
return self.RESULT_IO_SERIAL
stdout.write(c)
stdout.flush()
except KeyboardInterrupt, _:
self.notify("\r\n[CTRL+C] exit")
result = self.RESULT_ERROR
return result
if __name__ == '__main__': if __name__ == '__main__':
Simple().run() DefaultTestSelector().run()

View File

@ -16,24 +16,22 @@ limitations under the License.
""" """
import re import re
from host_test import DefaultTest
from time import time, strftime, gmtime from time import time, strftime, gmtime
class RTCTest():
class RTCTest(DefaultTest):
PATTERN_RTC_VALUE = "\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]" PATTERN_RTC_VALUE = "\[(\d+)\] \[(\d+-\d+-\d+ \d+:\d+:\d+ [AaPpMm]{2})\]"
re_detect_rtc_value = re.compile(PATTERN_RTC_VALUE) re_detect_rtc_value = re.compile(PATTERN_RTC_VALUE)
def test(self): def test(self, selftest):
test_result = True test_result = True
start = time() start = time()
sec_prev = 0 sec_prev = 0
for i in range(0, 5): for i in range(0, 5):
# Timeout changed from default: we need to wait longer for some boards to start-up # Timeout changed from default: we need to wait longer for some boards to start-up
c = self.mbed.serial_readline(timeout=10) c = selftest.mbed.serial_readline(timeout=10)
if c is None: if c is None:
return self.RESULT_IO_SERIAL return selftest.RESULT_IO_SERIAL
self.notify(c.strip()) selftest.notify(c.strip())
delta = time() - start delta = time() - start
m = self.re_detect_rtc_value.search(c) m = self.re_detect_rtc_value.search(c)
if m and len(m.groups()): if m and len(m.groups()):
@ -42,14 +40,10 @@ class RTCTest(DefaultTest):
correct_time_str = strftime("%Y-%m-%d %H:%M:%S %p", gmtime(float(sec))) correct_time_str = strftime("%Y-%m-%d %H:%M:%S %p", gmtime(float(sec)))
test_result = test_result and (time_str == correct_time_str) test_result = test_result and (time_str == correct_time_str)
result_msg = "OK" if (time_str == correct_time_str and sec > 0 and sec > sec_prev) else "FAIL" result_msg = "OK" if (time_str == correct_time_str and sec > 0 and sec > sec_prev) else "FAIL"
self.notify("HOST: [%s] [%s] received time %+d sec after %.2f sec... %s"% (sec, time_str, sec - sec_prev, delta, result_msg)) selftest.notify("HOST: [%s] [%s] received time %+d sec after %.2f sec... %s"% (sec, time_str, sec - sec_prev, delta, result_msg))
sec_prev = sec sec_prev = sec
else: else:
test_result = False test_result = False
break break
start = time() start = time()
return self.RESULT_SUCCESS if test_result else self.RESULT_FAILURE return selftest.RESULT_SUCCESS if test_result else selftest.RESULT_FAILURE
if __name__ == '__main__':
RTCTest().run()

View File

@ -18,32 +18,30 @@ limitations under the License.
import re import re
import random import random
from time import time from time import time
from host_test import DefaultTest
class StdioTest():
class StdioTest(DefaultTest):
PATTERN_INT_VALUE = "Your value was: (-?\d+)" PATTERN_INT_VALUE = "Your value was: (-?\d+)"
re_detect_int_value = re.compile(PATTERN_INT_VALUE) re_detect_int_value = re.compile(PATTERN_INT_VALUE)
def test(self): def test(self, selftest):
test_result = True test_result = True
c = self.mbed.serial_readline() # {{start}} preamble c = selftest.mbed.serial_readline() # {{start}} preamble
if c is None: if c is None:
return self.RESULT_IO_SERIAL return selftest.RESULT_IO_SERIAL
self.notify(c) selftest.notify(c)
for i in range(0, 10): for i in range(0, 10):
random_integer = random.randint(-99999, 99999) random_integer = random.randint(-99999, 99999)
self.notify("HOST: Generated number: " + str(random_integer)) selftest.notify("HOST: Generated number: " + str(random_integer))
start = time() start = time()
self.mbed.serial_write(str(random_integer) + "\n") selftest.mbed.serial_write(str(random_integer) + "\n")
serial_stdio_msg = self.mbed.serial_readline() serial_stdio_msg = selftest.mbed.serial_readline()
if serial_stdio_msg is None: if serial_stdio_msg is None:
return self.RESULT_IO_SERIAL return selftest.RESULT_IO_SERIAL
delay_time = time() - start delay_time = time() - start
self.notify(serial_stdio_msg.strip()) selftest.notify(serial_stdio_msg.strip())
# Searching for reply with scanned values # Searching for reply with scanned values
m = self.re_detect_int_value.search(serial_stdio_msg) m = self.re_detect_int_value.search(serial_stdio_msg)
@ -51,12 +49,8 @@ class StdioTest(DefaultTest):
int_value = m.groups()[0] int_value = m.groups()[0]
int_value_cmp = random_integer == int(int_value) int_value_cmp = random_integer == int(int_value)
test_result = test_result and int_value_cmp test_result = test_result and int_value_cmp
self.notify("HOST: Number %s read after %.3f sec ... [%s]"% (int_value, delay_time, "OK" if int_value_cmp else "FAIL")) selftest.notify("HOST: Number %s read after %.3f sec ... [%s]"% (int_value, delay_time, "OK" if int_value_cmp else "FAIL"))
else: else:
test_result = False test_result = False
break break
return self.RESULT_SUCCESS if test_result else self.RESULT_FAILURE return selftest.RESULT_SUCCESS if test_result else selftest.RESULT_FAILURE
if __name__ == '__main__':
StdioTest().run()

View File

@ -329,7 +329,7 @@ TESTS = [
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
"duration": 20, "duration": 20,
"automated": True, "automated": True,
"host_test": "stdio_auto" #"host_test": "stdio_auto"
}, },
{ {
"id": "MBED_3", "description": "PortOut", "id": "MBED_3", "description": "PortOut",
@ -411,9 +411,9 @@ TESTS = [
{ {
"id": "MBED_16", "description": "RTC", "id": "MBED_16", "description": "RTC",
"source_dir": join(TEST_DIR, "mbed", "rtc"), "source_dir": join(TEST_DIR, "mbed", "rtc"),
"dependencies": [MBED_LIBRARIES], "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
"automated": True, "automated": True,
"host_test": "rtc_auto", #"host_test": "rtc_auto",
"duration": 15 "duration": 15
}, },
{ {
@ -862,7 +862,7 @@ TESTS = [
"source_dir": join(TEST_DIR, "mbed", "dev_null"), "source_dir": join(TEST_DIR, "mbed", "dev_null"),
"dependencies": [MBED_LIBRARIES, TEST_MBED_LIB], "dependencies": [MBED_LIBRARIES, TEST_MBED_LIB],
"automated": True, "automated": True,
"host_test" : "dev_null_auto", #"host_test" : "dev_null_auto",
}, },
{ {
"id": "EXAMPLE_2", "description": "FS + RTOS", "id": "EXAMPLE_2", "description": "FS + RTOS",