LCOV - code coverage report
Current view: top level - core/test - test_main.c Hit Total Coverage
Test: ROOT-Sim develop Documentation Coverage Lines: 6 6 100.0 %
Date: 2021-03-02 11:24:52

          Line data    Source code
       1           1 : /**
       2             :  * @file test/test_main.c
       3             :  *
       4             :  * @brief Main stub for test
       5             :  *
       6             :  * The main function stub for tests which do not declare a main() entry point
       7             :  *
       8             :  * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com>
       9             :  * SPDX-License-Identifier: GPL-3.0-only
      10             :  */
      11             : #include <test.h>
      12             : 
      13             : #include <arch/thread.h>
      14             : 
      15             : /// The arguments passed to test threads
      16           1 : struct stub_arguments {
      17             :         /// The actual entry point defined by the test configuration
      18           1 :         int (*test_fnc)(void);
      19             :         /// The thread identifier which is set upon thread startup
      20           1 :         rid_t tid;
      21             : };
      22             : 
      23             : /**
      24             :  * @brief The entry point of the test threads
      25             :  * @arg the arguments passed by the main stub, a struct stub_arguments
      26             :  * @return THREAD_RET_SUCCESS in case of success, THREAD_RET_FAILURE otherwise
      27             :  */
      28           1 : static thr_ret_t THREAD_CALL_CONV test_run_stub(void *arg)
      29             : {
      30             :         struct stub_arguments *args = arg;
      31             :         rid = args->tid;
      32             :         int ret = args->test_fnc();
      33             :         return ret ? THREAD_RET_FAILURE : THREAD_RET_SUCCESS;
      34             : }
      35             : 
      36           1 : int main(int argc, char **argv)
      37             : {
      38             :         (void)argc; (void)argv;
      39             :         int ret = 0;
      40             :         thr_id_t threads[test_config.threads_count];
      41             :         struct stub_arguments args[test_config.threads_count];
      42             : 
      43             :         if (test_config.test_init_fnc && (ret = test_config.test_init_fnc())) {
      44             :                 printf("Test initialization failed with code %d\n", ret);
      45             :                 return ret;
      46             :         }
      47             : 
      48             :         for (unsigned i = 0; i < test_config.threads_count; ++i) {
      49             :                 args[i].test_fnc = test_config.test_fnc;
      50             :                 args[i].tid = i;
      51             :                 if (thread_start(&threads[i], test_run_stub, &args[i]))
      52             :                         return TEST_BAD_FAIL_EXIT_CODE;
      53             :         }
      54             : 
      55             :         for (unsigned i = 0; i < test_config.threads_count; ++i) {
      56             :                 thr_ret_t thr_ret;
      57             :                 if (thread_wait(threads[i], &thr_ret))
      58             :                         return TEST_BAD_FAIL_EXIT_CODE;
      59             : 
      60             :                 if (thr_ret) {
      61             :                         printf("Thread %u failed the test\n", i);
      62             :                         return -1;
      63             :                 }
      64             :         }
      65             : 
      66             :         if (test_config.test_fini_fnc && (ret = test_config.test_fini_fnc())) {
      67             :                 printf("Test finalization failed with code %d\n", ret);
      68             :                 return ret;
      69             :         }
      70             : 
      71             :         return 0;
      72             : }

Generated by: LCOV version 1.14