Annotation of embedaddon/strongswan/src/libstrongswan/utils/test.h, revision 1.1

1.1     ! misho       1: /*
        !             2:  * Copyright (C) 2013 Tobias Brunner
        !             3:  * HSR Hochschule fuer Technik Rapperswil
        !             4:  *
        !             5:  * This program is free software; you can redistribute it and/or modify it
        !             6:  * under the terms of the GNU General Public License as published by the
        !             7:  * Free Software Foundation; either version 2 of the License, or (at your
        !             8:  * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
        !             9:  *
        !            10:  * This program is distributed in the hope that it will be useful, but
        !            11:  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
        !            12:  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
        !            13:  * for more details.
        !            14:  */
        !            15: 
        !            16: /**
        !            17:  * @defgroup test test
        !            18:  * @{ @ingroup utils
        !            19:  */
        !            20: 
        !            21: #ifndef TEST_H_
        !            22: #define TEST_H_
        !            23: 
        !            24: #include "collections/hashtable.h"
        !            25: 
        !            26: /**
        !            27:  * Register a (possibly static) function so that it can be called from tests.
        !            28:  *
        !            29:  * @param name         name (namespace/function)
        !            30:  * @param fn           function to register (set to NULL to unregister)
        !            31:  */
        !            32: void testable_function_register(char *name, void *fn);
        !            33: 
        !            34: /**
        !            35:  * Find a previously registered testable function.
        !            36:  *
        !            37:  * @param name         name (namespace/function)
        !            38:  * @return                     function, NULL if not found
        !            39:  */
        !            40: void* testable_function_get(char *name);
        !            41: 
        !            42: /**
        !            43:  * Macro to automatically register/unregister a function that can be called
        !            44:  * from tests.
        !            45:  *
        !            46:  * @note The constructor has a priority set so that it runs after the
        !            47:  * constructor that creates the hashtable.  The destructor, on the other hand,
        !            48:  * does not have a priority set, as test coverage would report that function as
        !            49:  * untested otherwise.
        !            50:  *
        !            51:  * @param ns           namespace
        !            52:  * @param fn           function to register
        !            53:  */
        !            54: #define EXPORT_FUNCTION_FOR_TESTS(ns, fn) \
        !            55: static void testable_function_register_##fn() __attribute__ ((constructor)); \
        !            56: static void testable_function_register_##fn() \
        !            57: { \
        !            58:        testable_function_register(#ns "/" #fn, fn); \
        !            59: } \
        !            60: static void testable_function_unregister_##fn() __attribute__ ((destructor)); \
        !            61: static void testable_function_unregister_##fn() \
        !            62: { \
        !            63:        testable_function_register(#ns "/" #fn, NULL); \
        !            64: }
        !            65: 
        !            66: /**
        !            67:  * Import a registered function so that it can be called from tests.
        !            68:  *
        !            69:  * @param ns           namespace of the function
        !            70:  * @param name         name of the function
        !            71:  * @param ret          return type of the function
        !            72:  * @param ...          arguments of the function
        !            73:  */
        !            74: #define IMPORT_FUNCTION_FOR_TESTS(ns, name, ret, ...) \
        !            75: static ret (*TEST_##ns##name)(__VA_ARGS__);
        !            76: 
        !            77: /**
        !            78:  * Call a registered function from tests.
        !            79:  *
        !            80:  * @param ns           namespace of the function
        !            81:  * @param name         name of the function
        !            82:  * @param ...          arguments for the function
        !            83:  */
        !            84: #define TEST_FUNCTION(ns, name, ...) \
        !            85: ({ \
        !            86:        TEST_##ns##name = testable_function_get( #ns "/" #name); \
        !            87:        if (!TEST_##ns##name) \
        !            88:        { \
        !            89:                test_fail_msg(__FILE__, __LINE__, "function " #name " (" #ns ") not found"); \
        !            90:        } \
        !            91:        TEST_##ns##name(__VA_ARGS__); \
        !            92: })
        !            93: 
        !            94: #endif /** TEST_H_ @}*/

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>