LCOV - code coverage report
Current view: top level - subsys/testsuite/include - tc_util.h (source / functions) Hit Total Coverage
Test: coverage.info Lines: 12 18 66.7 %
Date: 2022-08-18 11:36:24 Functions: 3 3 100.0 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 2 6 33.3 %

           Branch data     Line data    Source code
       1                 :            : /* tc_utilities.h - testcase utilities header file */
       2                 :            : 
       3                 :            : /*
       4                 :            :  * Copyright (c) 2012-2015 Wind River Systems, Inc.
       5                 :            :  *
       6                 :            :  * SPDX-License-Identifier: Apache-2.0
       7                 :            :  */
       8                 :            : 
       9                 :            : #ifndef ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
      10                 :            : #define ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_
      11                 :            : 
      12                 :            : #include <zephyr.h>
      13                 :            : 
      14                 :            : #include <string.h>
      15                 :            : #ifdef CONFIG_SHELL
      16                 :            : #include <shell/shell.h>
      17                 :            : #endif
      18                 :            : #include <sys/printk.h>
      19                 :            : 
      20                 :            : #if defined CONFIG_ZTEST_TC_UTIL_USER_OVERRIDE
      21                 :            : #include <tc_util_user_override.h>
      22                 :            : #endif
      23                 :            : 
      24                 :            : #ifndef PRINT_DATA
      25                 :            : #define PRINT_DATA(fmt, ...) printk(fmt, ##__VA_ARGS__)
      26                 :            : #endif
      27                 :            : 
      28                 :            : #if defined CONFIG_ARCH_POSIX
      29                 :            : #include "posix_board_if.h"
      30                 :            : #endif
      31                 :            : 
      32                 :            : /**
      33                 :            :  * @def TC_PRINT_RUNID
      34                 :            :  * @brief Report a Run ID
      35                 :            :  *
      36                 :            :  * When the CPP symbol \c TC_RUNID is defined (for example, from the
      37                 :            :  * compile environment), print the defined string ``RunID:
      38                 :            :  * <TC_RUNID>`` when called (TC_END_REPORT() will also call it).
      39                 :            :  *
      40                 :            :  * This is used mainly when automating the execution and running of
      41                 :            :  * multiple test cases, to verify that the expected image is being
      42                 :            :  * executed (as sometimes the targets fail to flash or reset
      43                 :            :  * properly).
      44                 :            :  *
      45                 :            :  * TC_RUNID is any string, that will be converted to a string literal.
      46                 :            :  */
      47                 :            : #define TC_STR_HELPER(x) #x
      48                 :            : #define TC_STR(x) TC_STR_HELPER(x)
      49                 :            : #ifdef TC_RUNID
      50                 :            : #define TC_PRINT_RUNID PRINT_DATA("RunID: " TC_STR(TC_RUNID) "\n")
      51                 :            : #else
      52                 :            : #define TC_PRINT_RUNID do {} while (0)
      53                 :            : #endif
      54                 :            : 
      55                 :            : #ifndef PRINT_LINE
      56                 :            : #define PRINT_LINE                          \
      57                 :            :         PRINT_DATA(                                                        \
      58                 :            :                 "============================================================" \
      59                 :            :                 "=======\n")
      60                 :            : #endif
      61                 :            : 
      62                 :            : /* stack size and priority for test suite task */
      63                 :            : #define TASK_STACK_SIZE (1024 * 2)
      64                 :            : 
      65                 :            : #define FMT_ERROR "%s - %s@%d. "
      66                 :            : 
      67                 :            : #define TC_PASS 0
      68                 :            : #define TC_FAIL 1
      69                 :            : #define TC_SKIP 2
      70                 :            : 
      71                 :            : #ifndef TC_PASS_STR
      72                 :            : #define TC_PASS_STR "PASS"
      73                 :            : #endif
      74                 :            : #ifndef TC_FAIL_STR
      75                 :            : #define TC_FAIL_STR "FAIL"
      76                 :            : #endif
      77                 :            : #ifndef TC_SKIP_STR
      78                 :            : #define TC_SKIP_STR "SKIP"
      79                 :            : #endif
      80                 :            : 
      81                 :          1 : static inline const char *TC_RESULT_TO_STR(int result)
      82                 :            : {
      83   [ +  -  -  - ]:          1 :         switch (result) {
      84                 :          1 :         case TC_PASS:
      85                 :          1 :                 return TC_PASS_STR;
      86                 :          0 :         case TC_FAIL:
      87                 :          0 :                 return TC_FAIL_STR;
      88                 :          0 :         case TC_SKIP:
      89                 :          0 :                 return TC_SKIP_STR;
      90                 :          0 :         default:
      91                 :          0 :                 return "?";
      92                 :            :         }
      93                 :            : }
      94                 :            : 
      95                 :            : static uint32_t tc_start_time;
      96                 :            : static uint32_t tc_spend_time;
      97                 :            : 
      98                 :          1 : static inline void get_start_time_cyc(void)
      99                 :            : {
     100                 :            :         /* Besides the ztest framework, some testcase will also call
     101                 :            :          * TC_START() in their code. But the caller thread cannot be
     102                 :            :          * in userspace.
     103                 :            :          */
     104         [ +  - ]:          1 :         if (!k_is_user_context()) {
     105                 :          1 :                 tc_start_time = k_cycle_get_32();
     106                 :            :         }
     107                 :          1 : }
     108                 :            : 
     109                 :          1 : static inline void test_time_ms(void)
     110                 :            : {
     111                 :          1 :         uint32_t spend_cycle = k_cycle_get_32() - tc_start_time;
     112                 :            : 
     113                 :          1 :         tc_spend_time = k_cyc_to_ms_ceil32(spend_cycle);
     114                 :          1 : }
     115                 :            : 
     116                 :            : #ifndef TC_ERROR
     117                 :            : #define TC_ERROR(fmt, ...)                               \
     118                 :            :         do {                                                 \
     119                 :            :                 PRINT_DATA(FMT_ERROR, "FAIL", __func__, __LINE__); \
     120                 :            :                 PRINT_DATA(fmt, ##__VA_ARGS__);                  \
     121                 :            :         } while (0)
     122                 :            : #endif
     123                 :            : 
     124                 :            : #ifndef TC_PRINT
     125                 :            : #define TC_PRINT(fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
     126                 :            : #endif
     127                 :            : 
     128                 :            : #ifndef TC_START
     129                 :            : #define TC_START(name)                                                  \
     130                 :            :         do {                                                            \
     131                 :            :                 PRINT_DATA("START - %s\n", name);                     \
     132                 :            :                 get_start_time_cyc();                                   \
     133                 :            :         } while (0)
     134                 :            : #endif
     135                 :            : 
     136                 :            : #ifndef TC_END
     137                 :            : #define TC_END(result, fmt, ...) PRINT_DATA(fmt, ##__VA_ARGS__)
     138                 :            : #endif
     139                 :            : 
     140                 :            : #ifndef Z_TC_END_RESULT
     141                 :            : /* prints result and the function name */
     142                 :            : #define Z_TC_END_RESULT(result, func)                                           \
     143                 :            :         do {                                                                    \
     144                 :            :                 test_time_ms();                                                 \
     145                 :            :                 TC_END(result, " %s - %s in %u.%u seconds\n",                 \
     146                 :            :                         TC_RESULT_TO_STR(result), func, tc_spend_time/1000,     \
     147                 :            :                         tc_spend_time%1000);                                    \
     148                 :            :                 PRINT_LINE;                                                     \
     149                 :            :         } while (0)
     150                 :            : #endif
     151                 :            : 
     152                 :            : #ifndef TC_END_RESULT
     153                 :            : #define TC_END_RESULT(result)                           \
     154                 :            :         Z_TC_END_RESULT((result), __func__)
     155                 :            : #endif
     156                 :            : 
     157                 :            : #ifndef TC_SUITE_START
     158                 :            : #define TC_SUITE_START(name)                                    \
     159                 :            :         do {                                                    \
     160                 :            :                 TC_PRINT("Running test suite %s\n", name);    \
     161                 :            :                 PRINT_LINE;                                     \
     162                 :            :         } while (0)
     163                 :            : #endif
     164                 :            : 
     165                 :            : #ifndef TC_SUITE_END
     166                 :            : #define TC_SUITE_END(name, result)                              \
     167                 :            :         do {                                                            \
     168                 :            :                 if (result == TC_PASS) {                                        \
     169                 :            :                         TC_PRINT("Test suite %s succeeded\n", name);  \
     170                 :            :                 } else {                                                \
     171                 :            :                         TC_PRINT("Test suite %s failed.\n", name);    \
     172                 :            :                 }                                                       \
     173                 :            :         } while (0)
     174                 :            : #endif
     175                 :            : 
     176                 :            : #if defined(CONFIG_ARCH_POSIX)
     177                 :            : #include <logging/log_ctrl.h>
     178                 :            : #define TC_END_POST(result) do { \
     179                 :            :         LOG_PANIC(); \
     180                 :            :         posix_exit(result); \
     181                 :            : } while (0)
     182                 :            : #else
     183                 :            : #define TC_END_POST(result)
     184                 :            : #endif /* CONFIG_ARCH_POSIX */
     185                 :            : 
     186                 :            : #ifndef TC_END_REPORT
     187                 :            : #define TC_END_REPORT(result)                               \
     188                 :            :         do {                                                    \
     189                 :            :                 PRINT_LINE;                                         \
     190                 :            :                 TC_PRINT_RUNID;                                         \
     191                 :            :                 TC_END(result,                                      \
     192                 :            :                        "PROJECT EXECUTION %s\n",               \
     193                 :            :                        (result) == TC_PASS ? "SUCCESSFUL" : "FAILED");      \
     194                 :            :                 TC_END_POST(result);                                    \
     195                 :            :         } while (0)
     196                 :            : #endif
     197                 :            : 
     198                 :            : #if defined(CONFIG_SHELL)
     199                 :            : #define TC_CMD_DEFINE(name)                                             \
     200                 :            :         static int cmd_##name(const struct shell *shell, size_t argc,   \
     201                 :            :                               char **argv) \
     202                 :            :         {                                                               \
     203                 :            :                 TC_START(__func__);                                     \
     204                 :            :                 name();                                                 \
     205                 :            :                 TC_END_RESULT(TC_PASS);                                 \
     206                 :            :                 return 0;                                               \
     207                 :            :         }
     208                 :            : #define TC_CMD_ITEM(name)       cmd_##name
     209                 :            : #else
     210                 :            : #define TC_CMD_DEFINE(name)                             \
     211                 :            :         int cmd_##name(int argc, char *argv[])          \
     212                 :            :         {                                               \
     213                 :            :                 TC_START(__func__);                     \
     214                 :            :                 name();                                 \
     215                 :            :                 TC_END_RESULT(TC_PASS);                 \
     216                 :            :                 return 0;                               \
     217                 :            :         }
     218                 :            : #define TC_CMD_ITEM(name) {STRINGIFY(name), cmd_##name, "none"}
     219                 :            : #endif
     220                 :            : 
     221                 :            : #endif /* ZEPHYR_TESTSUITE_INCLUDE_TC_UTIL_H_ */

Generated by: LCOV version 1.14