Changeset 9b20126 in mainline for uspace/lib/pcut/include


Ignore:
Timestamp:
2014-09-19T08:23:01Z (11 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
c85a57f
Parents:
15d0046
Message:

Update PCUT to newest version

Location:
uspace/lib/pcut/include/pcut
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/pcut/include/pcut/asserts.h

    r15d0046 r9b20126  
    3939#include <errno.h>
    4040
     41/** @def PCUT_CURRENT_FILENAME
     42 * Overwrite contents of __FILE__ when printing assertion errors.
     43 */
     44#ifndef PCUT_CURRENT_FILENAME
     45#define PCUT_CURRENT_FILENAME __FILE__
     46#endif
     47
    4148/** @cond devel */
    4249
     
    4653 * (if registered).
    4754 *
     55 * @param filename File where the assertion occurred.
     56 * @param line Line where the assertion occurred.
    4857 * @param fmt Printf-like format string.
    4958 * @param ... Extra arguments.
    5059 */
    51 void pcut_failed_assertion_fmt(const char *fmt, ...);
     60void pcut_failed_assertion_fmt(const char *filename, int line, const char *fmt, ...);
    5261
    5362/** OS-agnostic string comparison.
     
    7079void pcut_str_error(int error, char *buffer, int size);
    7180
     81/** Raise assertion error (internal version).
     82 *
     83 * We expect to be always called from PCUT_ASSERTION_FAILED() where
     84 * the last argument is empty string to conform to strict ISO C99
     85 * ("ISO C99 requires rest arguments to be used").
     86 *
     87 * @param fmt Printf-like format string.
     88 * @param ... Extra arguments.
     89 */
     90#define PCUT_ASSERTION_FAILED_INTERNAL(fmt, ...) \
     91        pcut_failed_assertion_fmt(PCUT_CURRENT_FILENAME, __LINE__, fmt, __VA_ARGS__)
     92
     93
    7294/** @endcond */
    7395
     
    7799 * abort (tear-down function of the test suite is run when available).
    78100 *
    79  * @param fmt Printf-like format string.
    80  * @param ... Extra arguments.
    81  */
    82 #define PCUT_ASSERTION_FAILED(fmt, ...) \
    83         pcut_failed_assertion_fmt(__FILE__ ":%d: " fmt, __LINE__, ##__VA_ARGS__)
     101 * @param ... Printf-like arguments.
     102 */
     103#define PCUT_ASSERTION_FAILED(...) \
     104        PCUT_ASSERTION_FAILED_INTERNAL(__VA_ARGS__, "")
    84105
    85106
     
    115136 */
    116137#define PCUT_ASSERT_EQUALS(expected, actual) \
    117                 do {\
    118                         if (!((expected) == (actual))) { \
    119                                 PCUT_ASSERTION_FAILED("Expected <"#expected "> but got <" #actual ">"); \
    120                         } \
    121                 } while (0)
     138        do {\
     139                if (!((expected) == (actual))) { \
     140                        PCUT_ASSERTION_FAILED("Expected <"#expected "> but got <" #actual ">"); \
     141                } \
     142        } while (0)
    122143
    123144/** Asserts that given pointer is NULL.
  • uspace/lib/pcut/include/pcut/datadef.h

    r15d0046 r9b20126  
    3939/** @cond devel */
    4040
     41#if defined(__GNUC__) || defined(__clang__)
     42#define PCUT_CC_UNUSED_VARIABLE(name, initializer) \
     43        name __attribute__((unused)) = initializer
     44#else
     45#define PCUT_CC_UNUSED_VARIABLE(name, initializer) \
     46        name = initializer
     47#endif
     48
     49
    4150enum {
    4251        PCUT_KIND_SKIP,
     
    5463};
    5564
     65enum {
     66        PCUT_MAIN_EXTRA_PREINIT_HOOK,
     67        PCUT_MAIN_EXTRA_INIT_HOOK,
     68        PCUT_MAIN_EXTRA_REPORT_XML,
     69        PCUT_MAIN_EXTRA_LAST
     70};
     71
    5672/** Generic wrapper for test cases, test suites etc. */
    5773typedef struct pcut_item pcut_item_t;
     
    5975/** Extra information about a test. */
    6076typedef struct pcut_extra pcut_extra_t;
     77
     78/** Extra information for the main() function. */
     79typedef struct pcut_main_extra pcut_main_extra_t;
    6180
    6281/** Test method type. */
     
    7392         */
    7493        int type;
    75         union {
    76                 /** Test-specific time-out in seconds. */
    77                 int timeout;
    78         };
     94        /** Test-specific time-out in seconds. */
     95        int timeout;
     96};
     97
     98/** @copydoc pcut_main_extra_t */
     99struct pcut_main_extra {
     100        /** Discriminator for the union.
     101         *
     102         * Use PCUT_MAIN_EXTRA_* to determine which field of the union is used.
     103         */
     104        int type;
     105        /** Callback once PCUT initializes itself. */
     106        void (*init_hook)(void);
     107        /** Callback even before command-line arguments are processed. */
     108        void (*preinit_hook)(int *, char ***);
    79109};
    80110
     
    89119        int id;
    90120
    91         /** Discriminator for the union.
    92          *
    93          * Use PCUT_KIND_* to determine which field of the union is used.
    94          */
     121        /** Discriminator for this item. */
    95122        int kind;
    96         union {
    97                 struct {
    98                         const char *name;
    99                         pcut_setup_func_t setup;
    100                         pcut_setup_func_t teardown;
    101                 } suite;
    102                 struct {
    103                         const char *name;
    104                         pcut_test_func_t func;
    105                         pcut_extra_t *extras;
    106                 } test;
    107                 /* setup is used for both set-up and tear-down */
    108                 struct {
    109                         pcut_setup_func_t func;
    110                 } setup;
    111                 struct {
    112                         pcut_item_t *last;
    113                 } nested;
    114                 struct {
    115                         int dummy;
    116                 } meta;
    117         };
     123
     124        /** Name of this item. */
     125        const char *name;
     126
     127        /** Test-case function. */
     128        pcut_test_func_t test_func;
     129
     130        /** Set-up function of a suite. */
     131        pcut_setup_func_t setup_func;
     132        /** Tear-down function of a suite. */
     133        pcut_setup_func_t teardown_func;
     134
     135        /** Extra attributes. */
     136        pcut_extra_t *extras;
     137
     138        /** Extra attributes for main() function. */
     139        pcut_main_extra_t *main_extras;
     140
     141        /** Nested lists. */
     142        pcut_item_t *nested;
    118143};
    119 
    120 #ifdef PCUT_DEBUG_BUILD
    121 #define PCUT_DEBUG(msg, ...) \
    122         printf("[PCUT]: Debug: " msg "\n", ##__VA_ARGS__)
    123 #else
    124 
    125 /** Debug printing.
    126  *
    127  * By default, this macro does nothing. Define PCUT_DEBUG_BUILD to
    128  * actually print the messages to the console.
    129  *
    130  * @param msg Printf-like formatting message.
    131  * @param ... Extra arguments for printf.
    132  */
    133 #define PCUT_DEBUG(msg, ...) (void)0
    134 #endif
    135144
    136145/** @endcond */
  • uspace/lib/pcut/include/pcut/tests.h

    r15d0046 r9b20126  
    3737#define PCUT_TESTS_H_GUARD
    3838
     39#include <pcut/helper.h>
    3940#include <pcut/datadef.h>
    4041
     
    8182/** @cond devel */
    8283
    83 /** Join the two arguments on preprocessor level (inner call). */
    84 #define PCUT_JOIN_IMPL(a, b) a##b
    85 
    86 /** Join the two arguments on preprocessor level. */
    87 #define PCUT_JOIN(a, b) PCUT_JOIN_IMPL(a, b)
    88 
    8984/** Produce identifier name for an item with given number.
    9085 *
     
    132127 */
    133128#define PCUT_ADD_ITEM(number, itemkind, ...) \
    134                 static pcut_item_t PCUT_ITEM_NAME(number) = { \
    135                                 .previous = &PCUT_ITEM_NAME_PREV(number), \
    136                                 .next = NULL, \
    137                                 .id = -1, \
    138                                 .kind = itemkind, \
    139                                 __VA_ARGS__ \
    140                 };
     129        static pcut_item_t PCUT_ITEM_NAME(number) = { \
     130                &PCUT_ITEM_NAME_PREV(number), \
     131                NULL, \
     132                -1, \
     133                itemkind, \
     134                __VA_ARGS__ \
     135        }
    141136
    142137/** @endcond */
     
    156151 */
    157152#define PCUT_TEST_SET_TIMEOUT(time_out) \
    158         { .type = PCUT_EXTRA_TIMEOUT, .timeout = (time_out) }
     153        { PCUT_EXTRA_TIMEOUT, (time_out) }
    159154
    160155/** Skip current test.
     
    163158 */
    164159#define PCUT_TEST_SKIP \
    165         { .type = PCUT_EXTRA_SKIP }
     160        { PCUT_EXTRA_SKIP, 0 }
    166161
    167162
     
    169164
    170165/** Terminate list of extra test options. */
    171 #define PCUT_TEST_EXTRA_LAST { .type = PCUT_EXTRA_LAST }
     166#define PCUT_TEST_EXTRA_LAST { PCUT_EXTRA_LAST, 0 }
    172167
    173168/** Define a new test with given name and given item number.
     
    177172 * @param ... Extra test properties.
    178173 */
    179 #define PCUT_TEST_WITH_NUMBER(testname, number, ...) \
    180                 PCUT_ITEM_COUNTER_INCREMENT \
    181                 static pcut_extra_t PCUT_ITEM_EXTRAS_NAME(number)[] = { \
    182                                 __VA_ARGS__ \
    183                 }; \
    184                 static void PCUT_JOIN(test_, testname)(void); \
    185                 PCUT_ADD_ITEM(number, PCUT_KIND_TEST, \
    186                                 .test = { \
    187                                         .name = #testname, \
    188                                         .func = PCUT_JOIN(test_, testname), \
    189                                         .extras = PCUT_ITEM_EXTRAS_NAME(number), \
    190                                 } \
    191                 ) \
    192                 void PCUT_JOIN(test_, testname)(void)
     174#define PCUT_TEST_WITH_NUMBER(number, testname, ...) \
     175        PCUT_ITEM_COUNTER_INCREMENT \
     176        static pcut_extra_t PCUT_ITEM_EXTRAS_NAME(number)[] = { \
     177                __VA_ARGS__ \
     178        }; \
     179        static int PCUT_CC_UNUSED_VARIABLE(PCUT_JOIN(testname, 0_test_name_missing_or_duplicated), 0); \
     180        static void PCUT_JOIN(test_, testname)(void); \
     181        PCUT_ADD_ITEM(number, PCUT_KIND_TEST, \
     182                PCUT_QUOTE(testname), \
     183                PCUT_JOIN(test_, testname), \
     184                NULL, NULL, \
     185                PCUT_ITEM_EXTRAS_NAME(number), \
     186                NULL, NULL \
     187        ); \
     188        void PCUT_JOIN(test_, testname)(void)
    193189
    194190/** @endcond */
     
    196192/** Define a new test with given name.
    197193 *
    198  * @param name A valid C identifier name (not quoted).
    199  * @param ... Extra test properties.
    200  */
    201 #define PCUT_TEST(name, ...) \
    202         PCUT_TEST_WITH_NUMBER(name, PCUT_ITEM_COUNTER, ##__VA_ARGS__, PCUT_TEST_EXTRA_LAST)
     194 * @param ... Test name (C identifier) followed by extra test properties.
     195 */
     196#define PCUT_TEST(...) \
     197        PCUT_TEST_WITH_NUMBER(PCUT_ITEM_COUNTER, \
     198                PCUT_VARG_GET_FIRST(__VA_ARGS__, this_arg_is_ignored), \
     199                PCUT_VARG_SKIP_FIRST(__VA_ARGS__, PCUT_TEST_EXTRA_LAST) \
     200        )
    203201
    204202
     
    221219 */
    222220#define PCUT_TEST_SUITE_WITH_NUMBER(suitename, number) \
    223                 PCUT_ITEM_COUNTER_INCREMENT \
    224                 PCUT_ADD_ITEM(number, PCUT_KIND_TESTSUITE, \
    225                                 .suite = { \
    226                                         .name = #suitename, \
    227                                         .setup = NULL, \
    228                                         .teardown = NULL \
    229                                 } \
    230                 )
     221        PCUT_ITEM_COUNTER_INCREMENT \
     222        PCUT_ADD_ITEM(number, PCUT_KIND_TESTSUITE, \
     223                #suitename, \
     224                NULL, \
     225                NULL, NULL, \
     226                NULL, NULL, \
     227                NULL \
     228        )
    231229
    232230/** Define a set-up function for a test suite.
     
    237235 */
    238236#define PCUT_TEST_BEFORE_WITH_NUMBER(number) \
    239                 PCUT_ITEM_COUNTER_INCREMENT \
    240                 static void PCUT_ITEM_SETUP_NAME(number)(void); \
    241                 PCUT_ADD_ITEM(number, PCUT_KIND_SETUP, \
    242                                 .setup.func = PCUT_ITEM_SETUP_NAME(number) \
    243                 ) \
    244                 void PCUT_ITEM_SETUP_NAME(number)(void)
     237        PCUT_ITEM_COUNTER_INCREMENT \
     238        static void PCUT_ITEM_SETUP_NAME(number)(void); \
     239        PCUT_ADD_ITEM(number, PCUT_KIND_SETUP, \
     240                "setup", NULL, \
     241                PCUT_ITEM_SETUP_NAME(number), \
     242                NULL, NULL, NULL, NULL \
     243        ); \
     244        void PCUT_ITEM_SETUP_NAME(number)(void)
    245245
    246246/** Define a tear-down function for a test suite.
     
    251251 */
    252252#define PCUT_TEST_AFTER_WITH_NUMBER(number) \
    253                 PCUT_ITEM_COUNTER_INCREMENT \
    254                 static void PCUT_ITEM_SETUP_NAME(number)(void); \
    255                 PCUT_ADD_ITEM(number, PCUT_KIND_TEARDOWN, \
    256                                 .setup.func = PCUT_ITEM_SETUP_NAME(number) \
    257                 ) \
    258                 void PCUT_ITEM_SETUP_NAME(number)(void)
     253        PCUT_ITEM_COUNTER_INCREMENT \
     254        static void PCUT_ITEM_SETUP_NAME(number)(void); \
     255        PCUT_ADD_ITEM(number, PCUT_KIND_TEARDOWN, \
     256                "teardown", NULL, NULL, \
     257                PCUT_ITEM_SETUP_NAME(number), \
     258                NULL, NULL, NULL \
     259        ); \
     260        void PCUT_ITEM_SETUP_NAME(number)(void)
    259261
    260262/** @endcond */
     
    328330        PCUT_ITEM_COUNTER_INCREMENT \
    329331        pcut_item_t pcut_exported_##identifier = { \
    330                 .previous = &PCUT_ITEM_NAME_PREV(number), \
    331                 .next = NULL, \
    332                 .kind = PCUT_KIND_SKIP \
     332                &PCUT_ITEM_NAME_PREV(number), \
     333                NULL, \
     334                -1, \
     335                PCUT_KIND_SKIP, \
     336                "exported_" #identifier, NULL, NULL, NULL, NULL, NULL, NULL \
    333337        }
    334338
     
    344348        extern pcut_item_t pcut_exported_##identifier; \
    345349        PCUT_ADD_ITEM(number, PCUT_KIND_NESTED, \
    346                 .nested.last = &pcut_exported_##identifier \
     350                "import_" #identifier, NULL, NULL, NULL, NULL, NULL, \
     351                &pcut_exported_##identifier \
    347352        )
    348353
     
    382387        PCUT_ITEM_COUNTER_INCREMENT \
    383388        static pcut_item_t PCUT_ITEM_NAME(first_number) = { \
    384                 .previous = NULL, \
    385                 .next = NULL, \
    386                 .id = -1, \
    387                 .kind = PCUT_KIND_SKIP \
     389                NULL, \
     390                NULL, \
     391                -1, \
     392                PCUT_KIND_SKIP, \
     393                "init", NULL, NULL, NULL, NULL, NULL, NULL \
    388394        }; \
    389395        PCUT_TEST_SUITE(Default);
     
    395401 * @param number Item number.
    396402 */
    397 #define PCUT_MAIN_WITH_NUMBER(number) \
    398         PCUT_ITEM_COUNTER_INCREMENT \
     403#define PCUT_MAIN_WITH_NUMBER(number, ...) \
     404        PCUT_ITEM_COUNTER_INCREMENT \
     405        static pcut_main_extra_t pcut_main_extras[] = { \
     406                __VA_ARGS__ \
     407        }; \
    399408        static pcut_item_t pcut_item_last = { \
    400                 .previous = &PCUT_ITEM_NAME_PREV(number), \
    401                 .kind = PCUT_KIND_SKIP \
     409                &PCUT_ITEM_NAME_PREV(number), \
     410                NULL, \
     411                -1, \
     412                PCUT_KIND_SKIP, \
     413                "main", NULL, NULL, NULL, \
     414                NULL, \
     415                pcut_main_extras, \
     416                NULL \
    402417        }; \
    403418        int main(int argc, char *argv[]) { \
     
    405420        }
    406421
     422/** Terminate list of extra options for main. */
     423#define PCUT_MAIN_EXTRA_SET_LAST \
     424        { PCUT_MAIN_EXTRA_LAST, NULL, NULL }
     425
    407426/** @endcond */
    408427
     
    413432/** Insert code to run all the tests. */
    414433#define PCUT_MAIN() \
    415         PCUT_MAIN_WITH_NUMBER(PCUT_ITEM_COUNTER)
    416 
     434        PCUT_MAIN_WITH_NUMBER(PCUT_ITEM_COUNTER, PCUT_MAIN_EXTRA_SET_LAST)
     435
     436
     437/** Set callback for PCUT initialization.
     438 *
     439 * Use from within PCUT_CUSTOM_MAIN().
     440 *
     441 * @warning The callback is called for each test and also for the wrapping
     442 * invocation.
     443 */
     444#define PCUT_MAIN_SET_INIT_HOOK(callback) \
     445        { PCUT_MAIN_EXTRA_INIT_HOOK, callback, NULL }
     446
     447/** Set callback for PCUT pre-initialization.
     448 *
     449 * Use from within PCUT_CUSTOM_MAIN().
     450 * This callback is useful only if you want to manipulate command-line
     451 * arguments.
     452 * You probably will not need this.
     453 *
     454 * @warning The callback is called for each test and also for the wrapping
     455 * invocation.
     456 */
     457#define PCUT_MAIN_SET_PREINIT_HOOK(callback) \
     458        { PCUT_MAIN_EXTRA_PREINIT_HOOK, NULL, callback }
     459
     460
     461/** Set XML report as default.
     462 *
     463 * Use from within PCUT_CUSTOM_MAIN().
     464 *
     465 */
     466#define PCUT_MAIN_SET_XML_REPORT \
     467        { PCUT_MAIN_EXTRA_REPORT_XML, NULL, NULL }
     468
     469
     470/** Insert code to run all tests. */
     471#define PCUT_CUSTOM_MAIN(...) \
     472        PCUT_MAIN_WITH_NUMBER(PCUT_ITEM_COUNTER, \
     473                PCUT_VARG_GET_FIRST(__VA_ARGS__, PCUT_MAIN_EXTRA_SET_LAST), \
     474                PCUT_VARG_SKIP_FIRST(__VA_ARGS__, PCUT_MAIN_EXTRA_SET_LAST) \
     475        )
    417476
    418477/**
Note: See TracChangeset for help on using the changeset viewer.