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 1 : extern __thread rid_t rid; 16 : 17 : /// The arguments passed to test threads 18 1 : struct stub_arguments { 19 : /// The actual entry point defined by the test configuration 20 1 : int (*test_fnc)(void); 21 : /// The thread identifier which is set upon thread startup 22 1 : rid_t tid; 23 : }; 24 : 25 : /** 26 : * @brief The entry point of the test threads 27 : * @arg the arguments passed by the main stub, a struct stub_arguments 28 : * @return THREAD_RET_SUCCESS in case of success, THREAD_RET_FAILURE otherwise 29 : */ 30 1 : static thr_ret_t THREAD_CALL_CONV test_run_stub(void *arg) 31 : { 32 : struct stub_arguments *args = arg; 33 : rid = args->tid; 34 : int ret = args->test_fnc(); 35 : return ret ? THREAD_RET_FAILURE : THREAD_RET_SUCCESS; 36 : } 37 : 38 1 : int main(int argc, char **argv) 39 : { 40 : (void)argc; (void)argv; 41 : int ret = 0; 42 : thr_id_t threads[test_config.threads_count]; 43 : struct stub_arguments args[test_config.threads_count]; 44 : 45 : if (test_config.test_init_fnc && (ret = test_config.test_init_fnc())) { 46 : printf("Test initialization failed with code %d\n", ret); 47 : return ret; 48 : } 49 : 50 : for (unsigned i = 0; i < test_config.threads_count; ++i) { 51 : args[i].test_fnc = test_config.test_fnc; 52 : args[i].tid = i; 53 : if (thread_start(&threads[i], test_run_stub, &args[i])) 54 : return TEST_BAD_FAIL_EXIT_CODE; 55 : } 56 : 57 : for (unsigned i = 0; i < test_config.threads_count; ++i) { 58 : thr_ret_t thr_ret; 59 : if (thread_wait(threads[i], &thr_ret)) 60 : return TEST_BAD_FAIL_EXIT_CODE; 61 : 62 : if (thr_ret) { 63 : printf("Thread %u failed the test\n", i); 64 : return -1; 65 : } 66 : } 67 : 68 : if (test_config.test_fini_fnc && (ret = test_config.test_fini_fnc())) { 69 : printf("Test finalization failed with code %d\n", ret); 70 : return ret; 71 : } 72 : 73 : return 0; 74 : }