Skip to content

Commit

Permalink
Update to the latest version of tinytest
Browse files Browse the repository at this point in the history
This brings us up to tinytest 709a36ba63ff16d8
  • Loading branch information
nmathewson committed Mar 6, 2014
1 parent 239d834 commit 7a80476
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 2 deletions.
33 changes: 33 additions & 0 deletions test/tinytest.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
#include <string.h>
#include <assert.h>

#ifndef NO_FORKING

#ifdef _WIN32
#include <windows.h>
#else
Expand All @@ -48,6 +50,8 @@
#endif
#endif

#endif /* !NO_FORKING */

#ifndef __GNUC__
#define __attribute__(x)
#endif
Expand Down Expand Up @@ -111,6 +115,8 @@ testcase_run_bare_(const struct testcase_t *testcase)

#define MAGIC_EXITCODE 42

#ifndef NO_FORKING

static enum outcome
testcase_run_forked_(const struct testgroup_t *group,
const struct testcase_t *testcase)
Expand Down Expand Up @@ -211,6 +217,8 @@ testcase_run_forked_(const struct testgroup_t *group,
#endif
}

#endif /* !NO_FORKING */

int
testcase_run_one(const struct testgroup_t *group,
const struct testcase_t *testcase)
Expand All @@ -234,9 +242,13 @@ testcase_run_one(const struct testgroup_t *group,
cur_test_name = testcase->name;
}

#ifndef NO_FORKING
if ((testcase->flags & TT_FORK) && !(opt_forked||opt_nofork)) {
outcome = testcase_run_forked_(group, testcase);
} else {
#else
{
#endif
outcome = testcase_run_bare_(testcase);
}

Expand Down Expand Up @@ -411,7 +423,9 @@ tinytest_main(int c, const char **v, struct testgroup_t *groups)
if (!n)
tinytest_set_flag_(groups, "..", 1, TT_ENABLED_);

#ifdef _IONBF
setvbuf(stdout, NULL, _IONBF, 0);
#endif

++in_tinytest_main;
for (i=0; groups[i].prefix; ++i)
Expand Down Expand Up @@ -458,3 +472,22 @@ tinytest_set_test_skipped_(void)
cur_test_outcome = SKIP;
}

char *
tinytest_format_hex_(const void *val_, unsigned long len)
{
const unsigned char *val = val_;
char *result, *cp;
size_t i;

if (!val)
return strdup("null");
if (!(result = malloc(len*2+1)))
return strdup("<allocation failure>");
cp = result;
for (i=0;i<len;++i) {
*cp++ = "0123456789ABCDEF"[val[i] >> 4];
*cp++ = "0123456789ABCDEF"[val[i] & 0x0f];
}
*cp = 0;
return result;
}
2 changes: 2 additions & 0 deletions test/tinytest.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ int tinytest_get_verbosity_(void);
/** Implementation: Set a flag on tests matching a name; returns number
* of tests that matched. */
int tinytest_set_flag_(struct testgroup_t *, const char *, int set, unsigned long);
/** Implementation: Put a chunk of memory into hex. */
char *tinytest_format_hex_(const void *, unsigned long);

/** Set all tests in 'groups' matching the name 'named' to be skipped. */
#define tinytest_skip(groups, named) \
Expand Down
3 changes: 3 additions & 0 deletions test/tinytest_demo.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,9 @@ test_memcpy(void *ptr)
memcpy(db->buffer2, db->buffer1, sizeof(db->buffer1));
tt_str_op(db->buffer1, ==, db->buffer2);

/* This one works if there's an internal NUL. */
tt_mem_op(db->buffer1, <, db->buffer2, sizeof(db->buffer1));

/* Now we've allocated memory that's referenced by a local variable.
The end block of the function will clean it up. */
mem = strdup("Hello world.");
Expand Down
19 changes: 17 additions & 2 deletions test/tinytest_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@
tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
{print_=value_;},{},die_on_fail)

#define tt_assert_test_type_opt(a,b,str_test,type,test,fmt,die_on_fail) \
tt_assert_test_fmt_type(a,b,str_test,type,test,type,fmt, \
{print_=value_?value_:"<NULL>";},{},die_on_fail)

/* Helper: assert that a op b, when cast to type. Format the values with
* printf format fmt on failure. */
#define tt_assert_op_type(a,op,b,type,fmt) \
Expand All @@ -163,8 +167,19 @@
(val1_ op val2_),"%p",TT_EXIT_TEST_FUNCTION)

#define tt_str_op(a,op,b) \
tt_assert_test_type(a,b,#a" "#op" "#b,const char *, \
(strcmp(val1_,val2_) op 0),"<%s>",TT_EXIT_TEST_FUNCTION)
tt_assert_test_type_opt(a,b,#a" "#op" "#b,const char *, \
(val1_ && val2_ && strcmp(val1_,val2_) op 0),"<%s>", \
TT_EXIT_TEST_FUNCTION)

#define tt_mem_op(expr1, op, expr2, len) \
tt_assert_test_fmt_type(expr1,expr2,#expr1" "#op" "#expr2, \
const char *, \
(val1_ && val2_ && memcmp(val1_, val2_, len) op 0), \
char *, "%s", \
{ print_ = tinytest_format_hex_(value_, (len)); }, \
{ if (print_) free(print_); }, \
TT_EXIT_TEST_FUNCTION \
);

#define tt_want_int_op(a,op,b) \
tt_assert_test_type(a,b,#a" "#op" "#b,long,(val1_ op val2_),"%ld",(void)0)
Expand Down

0 comments on commit 7a80476

Please sign in to comment.