3 * Copyright (c) 2012 Samsung Electronics Co., Ltd.
5 * Licensed under the Apache License, Version 2.0 (the License);
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "unit_test_common.h"
19 #include "unit_test_run.h"
20 #include "unit_test_suites.h"
22 #include <sys/types.h>
25 /* introduction to unit test using check by yangsuh
26 * SRunner : runner of all test which belong to added suites
27 * Suite : container of test cases(TCase)
28 * TCase : container of tests
29 * test : container of many asserts
31 * SRunner has fork_status option. I will set this option to CK_FORK which means
32 * not stopping until all test finished.
33 * (Of course, when error like SIGSEGV occurs or test failed, current test will be stopped
34 * and goes to next test)
37 typedef enum fork_status fork_status_t;
38 int unit_test_main(fork_status_t fork_status)
42 /* srunner build up by defined test suites */
43 int suite_count = sizeof(suiteFunctions) / sizeof(SUITE_FUNCTION);
44 fprintf(stderr, "total test suites number = %d\n", suite_count);
46 if (suite_count == 0) {
47 return 0; /* nothing to do */
48 } else { /* suite_count > 0 */
49 SUITE_FUNCTION suite_func = NULL;
52 for (i = 0; i < suite_count; i++) {
53 suite_func = suiteFunctions[i];
54 Suite *s = suite_func();
57 sr = srunner_create(s);
58 } else if (i > 0 && i < suite_count) {
59 srunner_add_suite(sr, s);
62 fprintf(stderr, "%s\n", "invalid suite function");
68 srunner_set_log(sr, "/tmp/test.log"); /* set log file */
69 srunner_set_fork_status(sr, fork_status); /* set fork status of Runner */
71 srunner_run_all(sr, CK_VERBOSE); /* set print mode to verbose */
77 /* TODO : return handling */
78 static inline int unit_test_run_fork_mode()
86 fprintf(stderr, "%s\n", "Error in call to fork");
88 /* child process : run unit_test_main */
89 unit_test_main(CK_FORK);
93 fprintf(stderr, "test process pid = %d", pid);
94 pid_w = waitpid(pid, &status, 0);
97 fprintf(stderr, "%s\n", "test finished successfully");
98 return 1; /* test finished */
100 fprintf(stderr, "%s\n", "test failed");
101 return 0; /* test error */
108 /* running as main function will be need during debugging */
109 /* TODO : return handling */
110 static inline int unit_test_run_function_mode()
112 return unit_test_main(CK_NOFORK);
115 int unit_test_run(run_unit_test_mode_t mode)
117 int success = 1; /* success */
121 success = unit_test_run_fork_mode();
124 success = unit_test_run_function_mode();