Norimasa Okamoto / Mbed 2 deprecated STM32_IAP_test

Dependencies:   STM32_IAP mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers mytest.h Source File

mytest.h

00001 // mytest.h 2016/4/30
00002 #pragma once
00003 #include "mbed_debug.h"
00004 
00005 class BaseTest {
00006 public:
00007     virtual void _run() = 0;
00008     char* m_a;
00009     char* m_b;
00010     int m_line;
00011     char* m_file;
00012     BaseTest* next;
00013 };
00014 
00015 class reg {
00016     BaseTest* head;
00017     BaseTest* tail;
00018     reg() {
00019         head = NULL;
00020         tail = NULL;
00021     }
00022 public:
00023     static reg* inst() {
00024         static reg regtest;
00025         return &regtest;
00026     }
00027     void add(BaseTest* test) {
00028         test->next = NULL;
00029         if (head == NULL) {
00030             head = test;
00031             tail = test;
00032         } else {
00033             tail->next = test;
00034             tail = test;
00035         }
00036     }
00037     int run_all_tests(char* a = "") {
00038         debug("TEST BUILD at %f\n", MBED_BUILD_TIMESTAMP); 
00039         BaseTest* test = head;
00040         int pass = 0;
00041         int count = 0;
00042         char* file = "";
00043         while(test) {
00044             if (strcmp(a, "") == 0 || strcmp(a, test->m_a) == 0) {
00045                 if (strcmp(file, test->m_file) != 0) {
00046                     file = test->m_file;
00047                     debug("%s\n", file);
00048                 }
00049                 debug("TEST(%s,%s)@%d ... ",test->m_a, test->m_b, test->m_line);
00050                 Timer t; t.start();
00051                 test->_run();
00052                 debug("OK (%d ms)\n", t.read_ms());
00053                 pass++;
00054             }    
00055             test = test->next;
00056             count++;
00057         }
00058         debug("%d/%d TESTS PASSED!!!\n", pass, count);
00059         return 0;
00060     }
00061 };
00062 
00063 #define TEST(A,B) \
00064 class class_##A##_##B : public BaseTest { \
00065 public: \
00066     class_##A##_##B(char* a, char* b, char* file, int line) { \
00067         m_a = a; m_b = b; \
00068         m_file = file; m_line = line; \
00069         reg::inst()->add(this); \
00070     } \
00071     virtual void _run(); \
00072 }; \
00073 class_##A##_##B instance_##A##_##B(#A,#B,__FILE__,__LINE__); \
00074 void class_##A##_##B::_run()
00075 
00076 #define RUN_TEST(A,B) instance_##A##_##B._run()
00077 #define RUN_ALL_TESTS(A) reg::inst()->run_all_tests(#A)
00078 #define ASSERT_TRUE(A) if(A){}else{debug("\n\n%s@%d %s ASSERT!\n\n",__PRETTY_FUNCTION__,__LINE__,#A);exit(1);};
00079 
00080 #define DBG(FMT, ...) do{debug("[%s:%d]"FMT"\r\n", __PRETTY_FUNCTION__, __LINE__, ##__VA_ARGS__);}while(0);
00081 #define TEST_PRINT(FMT, ...) do{debug("[TEST: %d]"FMT"\r\n", __LINE__, ##__VA_ARGS__);}while(0);