STM32_IAP test

Dependencies:   STM32_IAP mbed

Committer:
va009039
Date:
Sun May 01 01:17:14 2016 +0000
Revision:
0:51e4fa01745a
first commit

Who changed what in which revision?

UserRevisionLine numberNew contents of line
va009039 0:51e4fa01745a 1 // mytest.h 2016/4/30
va009039 0:51e4fa01745a 2 #pragma once
va009039 0:51e4fa01745a 3 #include "mbed_debug.h"
va009039 0:51e4fa01745a 4
va009039 0:51e4fa01745a 5 class BaseTest {
va009039 0:51e4fa01745a 6 public:
va009039 0:51e4fa01745a 7 virtual void _run() = 0;
va009039 0:51e4fa01745a 8 char* m_a;
va009039 0:51e4fa01745a 9 char* m_b;
va009039 0:51e4fa01745a 10 int m_line;
va009039 0:51e4fa01745a 11 char* m_file;
va009039 0:51e4fa01745a 12 BaseTest* next;
va009039 0:51e4fa01745a 13 };
va009039 0:51e4fa01745a 14
va009039 0:51e4fa01745a 15 class reg {
va009039 0:51e4fa01745a 16 BaseTest* head;
va009039 0:51e4fa01745a 17 BaseTest* tail;
va009039 0:51e4fa01745a 18 reg() {
va009039 0:51e4fa01745a 19 head = NULL;
va009039 0:51e4fa01745a 20 tail = NULL;
va009039 0:51e4fa01745a 21 }
va009039 0:51e4fa01745a 22 public:
va009039 0:51e4fa01745a 23 static reg* inst() {
va009039 0:51e4fa01745a 24 static reg regtest;
va009039 0:51e4fa01745a 25 return &regtest;
va009039 0:51e4fa01745a 26 }
va009039 0:51e4fa01745a 27 void add(BaseTest* test) {
va009039 0:51e4fa01745a 28 test->next = NULL;
va009039 0:51e4fa01745a 29 if (head == NULL) {
va009039 0:51e4fa01745a 30 head = test;
va009039 0:51e4fa01745a 31 tail = test;
va009039 0:51e4fa01745a 32 } else {
va009039 0:51e4fa01745a 33 tail->next = test;
va009039 0:51e4fa01745a 34 tail = test;
va009039 0:51e4fa01745a 35 }
va009039 0:51e4fa01745a 36 }
va009039 0:51e4fa01745a 37 int run_all_tests(char* a = "") {
va009039 0:51e4fa01745a 38 debug("TEST BUILD at %f\n", MBED_BUILD_TIMESTAMP);
va009039 0:51e4fa01745a 39 BaseTest* test = head;
va009039 0:51e4fa01745a 40 int pass = 0;
va009039 0:51e4fa01745a 41 int count = 0;
va009039 0:51e4fa01745a 42 char* file = "";
va009039 0:51e4fa01745a 43 while(test) {
va009039 0:51e4fa01745a 44 if (strcmp(a, "") == 0 || strcmp(a, test->m_a) == 0) {
va009039 0:51e4fa01745a 45 if (strcmp(file, test->m_file) != 0) {
va009039 0:51e4fa01745a 46 file = test->m_file;
va009039 0:51e4fa01745a 47 debug("%s\n", file);
va009039 0:51e4fa01745a 48 }
va009039 0:51e4fa01745a 49 debug("TEST(%s,%s)@%d ... ",test->m_a, test->m_b, test->m_line);
va009039 0:51e4fa01745a 50 Timer t; t.start();
va009039 0:51e4fa01745a 51 test->_run();
va009039 0:51e4fa01745a 52 debug("OK (%d ms)\n", t.read_ms());
va009039 0:51e4fa01745a 53 pass++;
va009039 0:51e4fa01745a 54 }
va009039 0:51e4fa01745a 55 test = test->next;
va009039 0:51e4fa01745a 56 count++;
va009039 0:51e4fa01745a 57 }
va009039 0:51e4fa01745a 58 debug("%d/%d TESTS PASSED!!!\n", pass, count);
va009039 0:51e4fa01745a 59 return 0;
va009039 0:51e4fa01745a 60 }
va009039 0:51e4fa01745a 61 };
va009039 0:51e4fa01745a 62
va009039 0:51e4fa01745a 63 #define TEST(A,B) \
va009039 0:51e4fa01745a 64 class class_##A##_##B : public BaseTest { \
va009039 0:51e4fa01745a 65 public: \
va009039 0:51e4fa01745a 66 class_##A##_##B(char* a, char* b, char* file, int line) { \
va009039 0:51e4fa01745a 67 m_a = a; m_b = b; \
va009039 0:51e4fa01745a 68 m_file = file; m_line = line; \
va009039 0:51e4fa01745a 69 reg::inst()->add(this); \
va009039 0:51e4fa01745a 70 } \
va009039 0:51e4fa01745a 71 virtual void _run(); \
va009039 0:51e4fa01745a 72 }; \
va009039 0:51e4fa01745a 73 class_##A##_##B instance_##A##_##B(#A,#B,__FILE__,__LINE__); \
va009039 0:51e4fa01745a 74 void class_##A##_##B::_run()
va009039 0:51e4fa01745a 75
va009039 0:51e4fa01745a 76 #define RUN_TEST(A,B) instance_##A##_##B._run()
va009039 0:51e4fa01745a 77 #define RUN_ALL_TESTS(A) reg::inst()->run_all_tests(#A)
va009039 0:51e4fa01745a 78 #define ASSERT_TRUE(A) if(A){}else{debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
va009039 0:51e4fa01745a 79
va009039 0:51e4fa01745a 80 #define DBG(FMT, ...) do{debug("[%s:%d]"FMT"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0);
va009039 0:51e4fa01745a 81 #define TEST_PRINT(FMT, ...) do{debug("[TEST: %d]"FMT"\r\n", __LINE__, ##__VA_ARGS__);}while(0);