diff options
author | TheJackiMonster <thejackimonster@gmail.com> | 2021-10-05 16:30:13 +0200 |
---|---|---|
committer | TheJackiMonster <thejackimonster@gmail.com> | 2021-10-05 16:30:13 +0200 |
commit | 5e3ed6ef9f9d447a4ece18f79a4993c0630047c3 (patch) | |
tree | 8bb891cfda7f787483450a16e853a5de0bdcd89c | |
parent | c18988ad59fa8a6653e73abd4b420bd21e9bb2fb (diff) | |
download | libgnunetchat-5e3ed6ef9f9d447a4ece18f79a4993c0630047c3.tar.gz libgnunetchat-5e3ed6ef9f9d447a4ece18f79a4993c0630047c3.zip |
Added header with macros to create efficient tests
Signed-off-by: TheJackiMonster <thejackimonster@gmail.com>
-rw-r--r-- | Makefile | 3 | ||||
-rw-r--r-- | tests/test_gnunet_chat.h | 99 | ||||
-rw-r--r-- | tests/test_gnunet_chat_handle.c | 44 |
3 files changed, 111 insertions, 35 deletions
@@ -81,4 +81,5 @@ uninstall: | |||
81 | 81 | ||
82 | clean: | 82 | clean: |
83 | $(RM) -f $(LIBRARY) | 83 | $(RM) -f $(LIBRARY) |
84 | $(RM) -f $(OBJECT_FILES) \ No newline at end of file | 84 | $(RM) -f $(OBJECT_FILES) |
85 | $(RM) -f $(TEST_CASES) \ No newline at end of file | ||
diff --git a/tests/test_gnunet_chat.h b/tests/test_gnunet_chat.h new file mode 100644 index 0000000..ba3c37e --- /dev/null +++ b/tests/test_gnunet_chat.h | |||
@@ -0,0 +1,99 @@ | |||
1 | /* | ||
2 | This file is part of GNUnet. | ||
3 | Copyright (C) 2021 GNUnet e.V. | ||
4 | |||
5 | GNUnet is free software: you can redistribute it and/or modify it | ||
6 | under the terms of the GNU Affero General Public License as published | ||
7 | by the Free Software Foundation, either version 3 of the License, | ||
8 | or (at your option) any later version. | ||
9 | |||
10 | GNUnet is distributed in the hope that it will be useful, but | ||
11 | WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Affero General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Affero General Public License | ||
16 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
17 | |||
18 | SPDX-License-Identifier: AGPL3.0-or-later | ||
19 | */ | ||
20 | /* | ||
21 | * @author Tobias Frisch | ||
22 | * @file test_gnunet_chat.h | ||
23 | */ | ||
24 | |||
25 | #ifndef TEST_GNUNET_CHAT_H_ | ||
26 | #define TEST_GNUNET_CHAT_H_ | ||
27 | |||
28 | #include <stdlib.h> | ||
29 | #include <check.h> | ||
30 | |||
31 | #include <gnunet/gnunet_chat_lib.h> | ||
32 | |||
33 | #define CREATE_GNUNET_TEST(test_name, test_call) \ | ||
34 | void \ | ||
35 | task_##test_call (__attribute__ ((unused)) void *cls, \ | ||
36 | __attribute__ ((unused)) char *const *args, \ | ||
37 | __attribute__ ((unused)) const char *cfgfile, \ | ||
38 | const struct GNUNET_CONFIGURATION_Handle *cfg) \ | ||
39 | { \ | ||
40 | test_call (cfg); \ | ||
41 | } \ | ||
42 | \ | ||
43 | START_TEST(test_name) \ | ||
44 | { \ | ||
45 | enum GNUNET_GenericReturnValue result; \ | ||
46 | struct GNUNET_GETOPT_CommandLineOption options[] = { \ | ||
47 | GNUNET_GETOPT_OPTION_END \ | ||
48 | }; \ | ||
49 | \ | ||
50 | char *binary = #test_call; \ | ||
51 | char *const args [] = { binary }; \ | ||
52 | \ | ||
53 | result = GNUNET_PROGRAM_run( \ | ||
54 | 1, \ | ||
55 | args, \ | ||
56 | binary, \ | ||
57 | "", \ | ||
58 | options, \ | ||
59 | task_##test_call, \ | ||
60 | NULL \ | ||
61 | ); \ | ||
62 | \ | ||
63 | ck_assert(result == GNUNET_OK); \ | ||
64 | } \ | ||
65 | END_TEST | ||
66 | |||
67 | #define START_SUITE(suite_name, suite_title) \ | ||
68 | Suite* suite_name (void) \ | ||
69 | { \ | ||
70 | Suite *suite; \ | ||
71 | TCase *tcase; \ | ||
72 | \ | ||
73 | suite = suite_create(suite_title); | ||
74 | |||
75 | #define ADD_TEST_TO_SUITE(test_name, test_title) \ | ||
76 | tcase = tcase_create(test_title); \ | ||
77 | tcase_add_test(tcase, test_name); \ | ||
78 | suite_add_tcase(suite, tcase); | ||
79 | |||
80 | #define END_SUITE \ | ||
81 | return suite; \ | ||
82 | } | ||
83 | |||
84 | #define MAIN_SUITE(suite_name, suite_check) \ | ||
85 | int main (void) \ | ||
86 | { \ | ||
87 | int tests_failed; \ | ||
88 | SRunner *runner; \ | ||
89 | \ | ||
90 | runner = srunner_create(suite_name ()); \ | ||
91 | srunner_run_all(runner, suite_check); \ | ||
92 | \ | ||
93 | tests_failed = srunner_ntests_failed(runner); \ | ||
94 | srunner_free(runner); \ | ||
95 | \ | ||
96 | return (tests_failed == 0? EXIT_SUCCESS : EXIT_FAILURE); \ | ||
97 | } | ||
98 | |||
99 | #endif /* TEST_GNUNET_CHAT_H_ */ | ||
diff --git a/tests/test_gnunet_chat_handle.c b/tests/test_gnunet_chat_handle.c index 2492590..ab0e44e 100644 --- a/tests/test_gnunet_chat_handle.c +++ b/tests/test_gnunet_chat_handle.c | |||
@@ -22,47 +22,23 @@ | |||
22 | * @file test_gnunet_chat_handle.c | 22 | * @file test_gnunet_chat_handle.c |
23 | */ | 23 | */ |
24 | 24 | ||
25 | #include <check.h> | 25 | #include "test_gnunet_chat.h" |
26 | #include <gnunet/gnunet_chat_lib.h> | ||
27 | 26 | ||
28 | START_TEST(test_gnunet_chat_handle) | 27 | void |
28 | call_gnunet_chat_handle(const struct GNUNET_CONFIGURATION_Handle *cfg) | ||
29 | { | 29 | { |
30 | struct GNUNET_CHAT_Handle *handle; | 30 | struct GNUNET_CHAT_Handle *handle; |
31 | 31 | ||
32 | handle = GNUNET_CHAT_start(NULL, "", "Test", NULL, NULL, NULL, NULL); | 32 | handle = GNUNET_CHAT_start(cfg, "", "Test", NULL, NULL, NULL, NULL); |
33 | ck_assert_ptr_eq(handle, NULL); | 33 | ck_assert_ptr_ne(handle, NULL); |
34 | 34 | ||
35 | GNUNET_CHAT_stop(handle); | 35 | GNUNET_CHAT_stop(handle); |
36 | } | 36 | } |
37 | END_TEST | ||
38 | 37 | ||
39 | Suite* handle_suite(void) | 38 | CREATE_GNUNET_TEST(test_gnunet_chat_handle, call_gnunet_chat_handle) |
40 | { | ||
41 | Suite *suite; | ||
42 | TCase *test_case; | ||
43 | |||
44 | suite = suite_create("Handle"); | ||
45 | |||
46 | test_case = tcase_create("Start/Stop"); | ||
47 | tcase_add_test(test_case, test_gnunet_chat_handle); | ||
48 | suite_add_tcase(suite, test_case); | ||
49 | |||
50 | return suite; | ||
51 | } | ||
52 | 39 | ||
53 | int main(void) | 40 | START_SUITE(handle_suite, "Handle") |
54 | { | 41 | ADD_TEST_TO_SUITE(test_gnunet_chat_handle, "Start/Stop") |
55 | int tests_failed; | 42 | END_SUITE |
56 | |||
57 | Suite *suite; | ||
58 | SRunner *suite_runner; | ||
59 | |||
60 | suite = handle_suite(); | ||
61 | suite_runner = srunner_create(suite); | ||
62 | 43 | ||
63 | srunner_run_all(suite_runner, CK_NORMAL); | 44 | MAIN_SUITE(handle_suite, CK_NORMAL) |
64 | tests_failed = srunner_ntests_failed(suite_runner); | ||
65 | srunner_free(suite_runner); | ||
66 | |||
67 | return (tests_failed == 0? EXIT_SUCCESS : EXIT_FAILURE); | ||
68 | } | ||