is_bool, is_int, is_double, is_string, is_hex

(Check values for a TAP test)

SYNOPSIS

#include <tap/basic.h>

int is_bool(int wanted, int seen, const char *format, ...);

int is_int(long wanted, long seen, const char *format, ...);

int is_string(const char *wanted, const char *seen, const char *format, ...);

int is_hex(unsigned long wanted, unsigned long seen, const char *format, ...);

#include <tap/float.h>

int is_double(double wanted, double seen, double epsilon, const char *format, ...);

DESCRIPTION

These functions compare two values and report success to a TAP harness if the values match and failure otherwise. In each case, format may be NULL; if not NULL, format should be a printf-style format string with possible optional arguments giving the name or intention of this test. format should be specified where possible to make it easier to track failures down to specific tests.

is_bool() compares two boolean values, is_int() compares two longs, is_double() compares two doubles, is_string() compares two strings, and is_hex() compares two unsigned longs. The advantage of these functions over ok() is that, if the values don't match, the expected and seen values are reported as diagnostics to make it easier to understand the test failure. Note that, although is_bool() takes parameters of type int, both values are evaluated for their truth value not for their numeric value. (In other words, is_bool(1, 4) is a passing test.)

is_double() takes an epsilon value and considers the two values equal if their difference is less than epsilon. This avoids problems with rounding errors in representation of floating point numbers. wanted and seen may be positive or negative infinity or NaN as well as a regular number. If the expected value is NaN (not a number), any seen NaN value is considered equal to it, contrary to the normal NaN behavior. Callers of is_double() need to also include tap/float.h and link the test program with -lm.

is_string() correctly handles NULL values for both wanted and seen.

is_hex() reports its values as hex strings and is useful for testing flags or similar values.

RETURN VALUE

All functions return true if the test passed and false if it failed.

CAVEATS

plan() or plan_lazy() should always be called before calling any of these functions.

AUTHOR

Russ Allbery <eagle@eyrie.org>

COPYRIGHT AND LICENSE

Copyright 2010, 2012, 2013, 2014 Russ Allbery <eagle@eyrie.org>

Copying and distribution of this file, with or without modification, are permitted in any medium without royalty provided the copyright notice and this notice are preserved. This file is offered as-is, without any warranty.

SEE ALSO

ok(3), plan(3)

The current version of the C TAP Harness library is available from its web page at <https://www.eyrie.org/~eagle/software/c-tap-harness/>.

Last spun 2016-11-05 from POD modified 2016-10-26