diff --git a/components/utilities/utest/utest.c b/components/utilities/utest/utest.c index 21dc93100b..3c91fa8166 100644 --- a/components/utilities/utest/utest.c +++ b/components/utilities/utest/utest.c @@ -51,6 +51,18 @@ static struct utest local_utest = {UTEST_PASSED, 0, 0}; #if defined(__ICCARM__) || defined(__ICCRX__) /* for IAR compiler */ #pragma section="UtestTcTab" +#elif defined(_MSC_VER) +#pragma section("UtestTcTab$a", read) +__declspec(allocate("UtestTcTab$a")) const struct utest_tc_export __tc_export_begin = +{ + "__start", +}; + +#pragma section("UtestTcTab$z", read) +__declspec(allocate("UtestTcTab$z")) const struct utest_tc_export __tc_export_end = +{ + "__end", +}; #endif #define TC_FAIL_LIST_SIZE (RT_ALIGN(tc_num, 8) / 8) @@ -81,6 +93,28 @@ int utest_init(void) extern const int __rt_utest_tc_tab_end; tc_table = (utest_tc_export_t)&__rt_utest_tc_tab_start; tc_num = (utest_tc_export_t) &__rt_utest_tc_tab_end - tc_table; +#elif defined(_MSC_VER) + unsigned int* ptr_begin, * ptr_end; + + ptr_begin = (unsigned int*)&__tc_export_begin; + ptr_begin += (sizeof(struct utest_tc_export) / sizeof(unsigned int)); + while (*ptr_begin == 0) ptr_begin++; + + ptr_end = (unsigned int*)&__tc_export_end; + ptr_end--; + while (*ptr_end == 0) ptr_end--; + /* copy tc_table from rodata section to ram */ + for (unsigned int *ptr = ptr_begin; ptr < ptr_end;) + { + if (!tc_table) + tc_table = (utest_tc_export_t)rt_malloc(sizeof(struct utest_tc_export)); + else + tc_table = (utest_tc_export_t)rt_realloc(tc_table, (tc_num + 1)* sizeof(struct utest_tc_export)); + RT_ASSERT(tc_table); + tc_table[tc_num++] = *((utest_tc_export_t)ptr); + ptr += (sizeof(struct utest_tc_export) / sizeof(unsigned int)); + while (*ptr == 0) ptr++; + } #endif LOG_I("utest is initialize success."); diff --git a/components/utilities/utest/utest.h b/components/utilities/utest/utest.h index 41859612e2..56a6e0d57e 100644 --- a/components/utilities/utest/utest.h +++ b/components/utilities/utest/utest.h @@ -136,9 +136,23 @@ utest_t utest_handle_get(void); * @return None * */ +#ifdef _MSC_VER +#pragma section("UtestTcTab$f",read) +#define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ + __declspec(allocate("UtestTcTab$f")) \ + static const struct utest_tc_export _utest_testcase = \ + { \ + name, \ + timeout, \ + init, \ + testcase, \ + cleanup \ + } +#pragma comment(linker, "/merge:UtestTcTab=tctext") +#else #define UTEST_TC_EXPORT(testcase, name, init, cleanup, timeout) \ rt_used static const struct utest_tc_export _utest_testcase \ - rt_section("UtestTcTab") = \ + rt_section("UtestTcTab") = \ { \ name, \ timeout, \ @@ -146,6 +160,7 @@ utest_t utest_handle_get(void); testcase, \ cleanup \ } +#endif /* _MSC_VER */ /** * UTEST_UNIT_RUN