Loading...
Searching...
No Matches
tstdef.h File Reference

Macros for unit tests. More...

`#include <sofia-sip/su_types.h>`
Include dependency graph for tstdef.h:

Go to the source code of this file.

## Macros

#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 }

## Detailed Description

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.

Note
  There is no protection agains multiple inclusion.

Author
  Pekka Pessi Pekka.nosp@m..Pes.nosp@m.si@no.nosp@m.kia..nosp@m.com

Date
  Created: Wed Aug 22 13:53:24 2001 ppessi

Example Program

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();
}
su.h
Socket and network address interface.
su_socket_t
int su_socket_t
Socket descriptor type.
Definition su.h:128
su_socket
su_socket_t su_socket(int af, int sock, int proto)
Create an endpoint for communication.
Definition su.c:84
tstdef.h
Macros for unit tests.
tst_verbatim
@ tst_verbatim
If (TSTFLAGS & tst_verbatim) is non-zero, be verbatim.
Definition tstdef.h:145
TEST_M
#define TEST_M(suite, expected, len)
Test that suite is results as identical memory as expected.
Definition tstdef.h:179
END
#define END()
End a test function.
Definition tstdef.h:159
TEST_D
#define TEST_D(suite, expected)
Test that suite is same double as expected.
Definition tstdef.h:175
TEST_1
#define TEST_1(suite)
Test that suite returns a nonzero value.
Definition tstdef.h:165
TEST_S
#define TEST_S(suite, expected)
Test that suite is same string as expected.
Definition tstdef.h:177
TEST
#define TEST(suite, expected)
Test that suite is equal to expected.
Definition tstdef.h:169
BEGIN
#define BEGIN()
Begin a test function.
Definition tstdef.h:157

## Macro Definition Documentation

## TEST0

#define TEST0 | ( |  | suite | ) |

Test that suite returns a nonzero value.

Deprecated
  Use TEST_1()

## Enumeration Type Documentation

## anonymous enum

anonymous enum

Enumerator
tst_verbatim | If (TSTFLAGS & tst_verbatim) is non-zero, be verbatim.
tst_abort | If (TSTFLAGS & tst_abort) is non-zero, abort() when failed.
tst_log | If (TSTFLAGS & tst_log) is non-zero, log intermediate results.

Sofia-SIP 1.12.11devel - Copyright (C) 2006 Nokia Corporation. All rights reserved. Licensed under the terms of the GNU Lesser General Public License.