utest.c 6.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
/*
 * Copyright (c) 2006-2018, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2018-11-19     MurphyZhao   the first version
 */

#include "utest.h"
#include <rtthread.h>
#include <finsh.h>

#undef DBG_SECTION_NAME
#undef DBG_LEVEL
#undef DBG_COLOR
#undef DBG_ENABLE

#define DBG_ENABLE
#define DBG_SECTION_NAME          "utest"
#ifdef UTEST_DEBUG
#define DBG_LEVEL                 DBG_LOG
#else
#define DBG_LEVEL                 DBG_INFO
#endif
#define DBG_COLOR
#include <rtdbg.h>

#if RT_CONSOLEBUF_SIZE < 256
#error "RT_CONSOLEBUF_SIZE is less than 256!"
#endif

static utest_tc_export_t tc_table = RT_NULL;
static rt_size_t tc_num;
static struct utest local_utest = {UTEST_PASSED, 0, 0};

#if defined(__ICCARM__) || defined(__ICCRX__)         /* for IAR compiler */
#pragma section="UtestTcTab"
#endif

int utest_init(void)
{
    /* initialize the utest commands table.*/
#if defined(__CC_ARM)                                 /* ARM C Compiler */
    extern const int UtestTcTab$$Base;
    extern const int UtestTcTab$$Limit;
    tc_table = (utest_tc_export_t)&UtestTcTab$$Base;
    tc_num = (utest_tc_export_t)&UtestTcTab$$Limit - tc_table;
#elif defined (__ICCARM__) || defined(__ICCRX__)      /* for IAR Compiler */
    tc_table = (utest_tc_export_t)__section_begin("UtestTcTab");
    tc_num = (utest_tc_export_t)__section_end("UtestTcTab") - tc_table;
#elif defined (__GNUC__)                              /* for GCC Compiler */
M
MurphyZhao 已提交
54 55 56 57
    extern const int __rt_utest_tc_tab_start;
    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;
58 59
#endif /* defined(__CC_ARM) */

60 61
    LOG_I("utest is initialize success.");
    LOG_I("total utest testcase num: (%d)", tc_num);
62 63 64 65 66 67 68 69
    return tc_num;
}
INIT_COMPONENT_EXPORT(utest_init);

static void utest_tc_list(void)
{
    rt_size_t i = 0;

70
    LOG_I("Commands list : ");
71 72 73

    for (i = 0; i < tc_num; i++)
    {
74
        LOG_I("[testcase name]:%s; [run timeout]:%d", tc_table[i].name, tc_table[i].run_timeout);
75 76
    }
}
77
MSH_CMD_EXPORT_ALIAS(utest_tc_list, utest_list, output all utest testcase);
78

79
static const char *file_basename(const char *file)
80
{
81
    char *end_ptr = RT_NULL;
82
    char *rst = RT_NULL;
83 84 85 86

    if (!((end_ptr = strrchr(file, '\\')) != RT_NULL || \
        (end_ptr = strrchr(file, '/')) != RT_NULL) || \
        (rt_strlen(file) < 2))
87
    {
88
        rst = (char *)file;
89 90 91
    }
    else
    {
92
        rst = (char *)(end_ptr + 1);
93
    }
94
    return (const char *)rst;
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
}

static void utest_run(const char *utest_name)
{
    rt_size_t i = 0;

    LOG_I("[==========] [ utest    ] started");
    while(i < tc_num)
    {
        if (utest_name && rt_strcmp(utest_name, tc_table[i].name))
        {
            i++;
            continue;
        }

110
        LOG_I("[----------] [ testcase ] (%s) started", tc_table[i].name);
111 112
        if (tc_table[i].init != RT_NULL)
        {
113 114
            if (tc_table[i].init() != RT_EOK)
            {
115
                LOG_E("[  FAILED  ] [ result   ] testcase (%s)", tc_table[i].name);
116 117
                goto __tc_continue;
            }
118 119
        }

120
        if (tc_table[i].tc != RT_NULL)
121
        {
122 123 124 125 126 127 128
            tc_table[i].tc();
            if (local_utest.failed_num == 0)
            {
                LOG_I("[  PASSED  ] [ result   ] testcase (%s)", tc_table[i].name);
            }
            else
            {
129
                LOG_E("[  FAILED  ] [ result   ] testcase (%s)", tc_table[i].name);
130
            }
131 132 133
        }
        else
        {
134
            LOG_E("[  FAILED  ] [ result   ] testcase (%s)", tc_table[i].name);
135 136 137 138
        }

        if (tc_table[i].cleanup != RT_NULL)
        {
139 140
            if (tc_table[i].cleanup() != RT_EOK)
            {
141
                LOG_E("[  FAILED  ] [ result   ] testcase (%s)", tc_table[i].name);
142 143
                goto __tc_continue;
            }
144 145
        }

146 147 148
__tc_continue:
        LOG_I("[----------] [ testcase ] (%s) finished", tc_table[i].name);

149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
        i++;
    }
    LOG_I("[==========] [ utest    ] finished");
}

static void utest_testcase_run(int argc, char** argv)
{
    char utest_name[UTEST_NAME_MAX_LEN];

    if (argc == 1)
    {
        utest_run(RT_NULL);
    }
    else if (argc == 2)
    {
        rt_memset(utest_name, 0x0, sizeof(utest_name));
        rt_strncpy(utest_name, argv[1], sizeof(utest_name) -1);
        utest_run(utest_name);
    }
    else
    {
        LOG_E("[  error   ] at (%s:%d), in param error.", __func__, __LINE__);
    }
}
MSH_CMD_EXPORT_ALIAS(utest_testcase_run, utest_run, utest_run [testcase name]);

utest_t utest_handle_get(void)
{
    return (utest_t)&local_utest;
}

void utest_unit_run(test_unit_func func, const char *unit_func_name)
{
    // LOG_I("[==========] utest unit name: (%s)", unit_func_name);
    local_utest.error = UTEST_PASSED;
    local_utest.passed_num = 0;
    local_utest.failed_num = 0;
186 187 188 189 190

    if (func != RT_NULL)
    {
        func();
    }
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
}

void utest_assert(int value, const char *file, int line, const char *func, const char *msg)
{
    if (!(value))
    {
        local_utest.error = UTEST_FAILED;
        local_utest.failed_num ++;
        LOG_E("[  ASSERT  ] [ unit     ] at (%s); func: (%s:%d); msg: (%s)", file_basename(file), func, line, msg);
    }
    else
    {
        LOG_D("[    OK    ] [ unit     ] (%s:%d) is passed", func, line);
        local_utest.error = UTEST_PASSED;
        local_utest.passed_num ++;
    }
}

void utest_assert_string(const char *a, const char *b, rt_bool_t equal, const char *file, int line, const char *func, const char *msg)
{
    if (a == RT_NULL || b == RT_NULL)
    {
        utest_assert(0, file, line, func, msg);
    }

    if (equal)
    {
        if (rt_strcmp(a, b) == 0)
        {
            utest_assert(1, file, line, func, msg);
        }
        else
        {
            utest_assert(0, file, line, func, msg);
        }
    }
    else
    {
        if (rt_strcmp(a, b) == 0)
        {
            utest_assert(0, file, line, func, msg);
        }
        else
        {
            utest_assert(1, file, line, func, msg);
        }
    }
}
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269

void utest_assert_buf(const char *a, const char *b, rt_size_t sz, rt_bool_t equal, const char *file, int line, const char *func, const char *msg)
{
    if (a == RT_NULL || b == RT_NULL)
    {
        utest_assert(0, file, line, func, msg);
    }

    if (equal)
    {
        if (rt_memcmp(a, b, sz) == 0)
        {
            utest_assert(1, file, line, func, msg);
        }
        else
        {
            utest_assert(0, file, line, func, msg);
        }
    }
    else
    {
        if (rt_memcmp(a, b, sz) == 0)
        {
            utest_assert(0, file, line, func, msg);
        }
        else
        {
            utest_assert(1, file, line, func, msg);
        }
    }
}