]>
git.ipfire.org Git - thirdparty/strongswan.git/blob - src/libstrongswan/utils/test.h
2 * Copyright (C) 2013 Tobias Brunner
4 * Copyright (C) secunet Security Networks AG
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2 of the License, or (at your
9 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25 #include "collections/hashtable.h"
28 * Register a (possibly static) function so that it can be called from tests.
30 * @param name name (namespace/function)
31 * @param fn function to register (set to NULL to unregister)
33 void testable_function_register(char *name
, void *fn
);
36 * Find a previously registered testable function.
38 * @param name name (namespace/function)
39 * @return function, NULL if not found
41 void* testable_function_get(char *name
);
44 * Macro to automatically register/unregister a function that can be called
47 * @note The constructor has a priority set so that it runs after the
48 * constructor that creates the hashtable. The destructor, on the other hand,
49 * does not have a priority set, as test coverage would report that function as
53 * @param fn function to register
55 #define EXPORT_FUNCTION_FOR_TESTS(ns, fn) \
56 static void testable_function_register_##fn() __attribute__ ((constructor)); \
57 static void testable_function_register_##fn() \
59 testable_function_register(#ns "/" #fn, fn); \
61 static void testable_function_unregister_##fn() __attribute__ ((destructor)); \
62 static void testable_function_unregister_##fn() \
64 testable_function_register(#ns "/" #fn, NULL); \
68 * Import a registered function so that it can be called from tests.
70 * @param ns namespace of the function
71 * @param name name of the function
72 * @param ret return type of the function
73 * @param ... arguments of the function
75 #define IMPORT_FUNCTION_FOR_TESTS(ns, name, ret, ...) \
76 static ret (*TEST_##ns##name)(__VA_ARGS__);
79 * Call a registered function from tests.
81 * @param ns namespace of the function
82 * @param name name of the function
83 * @param ... arguments for the function
85 #define TEST_FUNCTION(ns, name, ...) \
87 TEST_##ns##name = testable_function_get( #ns "/" #name); \
88 if (!TEST_##ns##name) \
90 test_fail_msg(__FILE__, __LINE__, "function " #name " (" #ns ") not found"); \
92 TEST_##ns##name(__VA_ARGS__); \
95 #endif /** TEST_H_ @}*/