Macros for unit tests. More...
#include <sofia-sip/su_types.h>
Go to the source code of this file.
Defines | |
#define | BEGIN() |
Begin a test function. | |
#define | END() |
End a test function. | |
#define | TEST0(suite) |
Test that suite returns a nonzero value. | |
#define | TEST_1(suite) |
Test that suite returns a nonzero value. | |
#define | TEST_VOID(suite) |
Test a void suite. | |
#define | TEST(suite, expected) |
Test that suite is equal to expected. | |
#define | TEST_P(suite, expected) |
Test that suite is same pointer as expected. | |
#define | TEST64(suite, expected) |
Test that 64-bit suite is equal to expect. | |
#define | TEST_D(suite, expected) |
Test that suite is same double as expected. | |
#define | TEST_S(suite, expected) |
Test that suite is same string as expected. | |
#define | TEST_M(suite, expected, len) |
Test that suite is results as identical memory as expected. | |
#define | TEST_SIZE(suite, expected) |
Test that suite has same size as expected. | |
#define | TEST_LOG(x) |
Print in torture test with -l option. | |
Enumerations | |
enum | { tst_verbatim, tst_abort, tst_log } |
Macros for unit tests.
The macros defined here can be used by unit test programs. When a test fails, the TEST macros print the offending file name and line number. They use format that is accepted by Emacs and other fine editors so you can directly go to the source code of the failed test with next-error.
You should define the macro TSTFLAGS to a int variable possibly containing a flag tst_verbatim. As a convention, the int variable should be set when your test program is run with -v
or --verbatim
command line option. If the (TSTFLAGS & tst_verbatim) is true, the test macros log the test before executing it and the result of successful tests, too.
You should typedef longlong to integer type at least 64 bit wide before including <sofia-sip/tstdef.h>, too.
As an example, we provide a test program making sure that inet_ntop() and inet_pton() behave as expected and that we can create UDP/IPv4 sockets with su library:
#include "config.h" #include <stdio.h> #include <limits.h> #include <sofia-sip/su.h> #define TSTFLAGS tstflags #include <stdlib.h> #include <sofia-sip/tstdef.h> static int tstflags = 0; void usage(void) { fprintf(stderr, "usage: %s [-v|--verbatim]\n", name); exit(2); } static int socket_test(void); int main(int argc, char *argv[]) { int retval = 0, i; for (i = 1; argv[i]; i++) { if (strcmp(argv[i], "-v") == 0 || strcmp(argv[i], "--verbatim") == 0) tstflags |= tst_verbatim; else usage(); } retval |= socket_test(); fflush(stdout); return retval; } double max_bandwidth() int socket_test(void) { su_socket_t s; char buf[64]; unsigned long localhost = htonl(0x7f000001); unsigned long addr; BEGIN(); // Check inet_ntop() return value (Tests equality of integral values) TEST(inet_ntop(AF_INET, &localhost, buf, sizeof buf), buf); // Check inet_ntop() result (Tests equality of strings) TEST_S(buf, "127.0.0.1"); // Check inet_pton() argument validation (Tests equality of ints) TEST(inet_pton(0, buf, &addr), -1); // Check inet_pton() return value (Tests for true value (non-zero)) TEST_1(inet_pton(AF_INET, buf, &addr) > 0); // Check inet_pton() result (Tests equality of memory areas) TEST_M(&addr, &localhost, sizeof(addr)); // Test to create UDP socket (Test for true value) TEST_1((s = su_socket(AF_INET, SOCK_DGRAM, 0)) != INVALID_SOCKET); // Check max bandwidth TEST_D(max_bandwidth(), DBL_MAX); END(); }
#define TEST0 | ( | suite | ) |
Test that suite returns a nonzero value.