1 /* $NetBSD: tinytest.h,v 1.5 2016/01/08 21:35:41 christos Exp $ */ 2 3 /* tinytest.h -- Copyright 2009-2012 Nick Mathewson 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef TINYTEST_H_INCLUDED_ 29 #define TINYTEST_H_INCLUDED_ 30 31 /** Flag for a test that needs to run in a subprocess. */ 32 #define TT_FORK (1<<0) 33 /** Runtime flag for a test we've decided to skip. */ 34 #define TT_SKIP (1<<1) 35 /** Internal runtime flag for a test we've decided to run. */ 36 #define TT_ENABLED_ (1<<2) 37 /** Flag for a test that's off by default. */ 38 #define TT_OFF_BY_DEFAULT (1<<3) 39 /** If you add your own flags, make them start at this point. */ 40 #define TT_FIRST_USER_FLAG (1<<4) 41 42 typedef void (*testcase_fn)(void *); 43 44 struct testcase_t; 45 46 /** Functions to initialize/teardown a structure for a testcase. */ 47 struct testcase_setup_t { 48 /** Return a new structure for use by a given testcase. */ 49 void *(*setup_fn)(const struct testcase_t *); 50 /** Clean/free a structure from setup_fn. Return 1 if ok, 0 on err. */ 51 int (*cleanup_fn)(const struct testcase_t *, void *); 52 }; 53 54 /** A single test-case that you can run. */ 55 struct testcase_t { 56 const char *name; /**< An identifier for this case. */ 57 testcase_fn fn; /**< The function to run to implement this case. */ 58 unsigned long flags; /**< Bitfield of TT_* flags. */ 59 const struct testcase_setup_t *setup; /**< Optional setup/cleanup fns*/ 60 void *setup_data; /**< Extra data usable by setup function */ 61 }; 62 #define END_OF_TESTCASES { NULL, NULL, 0, NULL, NULL } 63 64 /** A group of tests that are selectable together. */ 65 struct testgroup_t { 66 const char *prefix; /**< Prefix to prepend to testnames. */ 67 struct testcase_t *cases; /** Array, ending with END_OF_TESTCASES */ 68 }; 69 #define END_OF_GROUPS { NULL, NULL} 70 71 struct testlist_alias_t { 72 const char *name; 73 const char **tests; 74 }; 75 #define END_OF_ALIASES { NULL, NULL } 76 77 /** Implementation: called from a test to indicate failure, before logging. */ 78 void tinytest_set_test_failed_(void); 79 /** Implementation: called from a test to indicate that we're skipping. */ 80 void tinytest_set_test_skipped_(void); 81 /** Implementation: return 0 for quiet, 1 for normal, 2 for loud. */ 82 int tinytest_get_verbosity_(void); 83 /** Implementation: Set a flag on tests matching a name; returns number 84 * of tests that matched. */ 85 int tinytest_set_flag_(struct testgroup_t *, const char *, int set, unsigned long); 86 /** Implementation: Put a chunk of memory into hex. */ 87 char *tinytest_format_hex_(const void *, unsigned long); 88 89 /** Set all tests in 'groups' matching the name 'named' to be skipped. */ 90 #define tinytest_skip(groups, named) \ 91 tinytest_set_flag_(groups, named, 1, TT_SKIP) 92 93 /** Run a single testcase in a single group. */ 94 int testcase_run_one(const struct testgroup_t *,const struct testcase_t *); 95 96 void tinytest_set_aliases(const struct testlist_alias_t *aliases); 97 98 /** Run a set of testcases from an END_OF_GROUPS-terminated array of groups, 99 as selected from the command line. */ 100 int tinytest_main(int argc, const char **argv, struct testgroup_t *groups); 101 102 #endif 103