tor/src/common/test.h

154 lines
7.4 KiB
C
Raw Normal View History

2005-04-01 22:15:56 +02:00
/* Copyright 2001,2002,2003 Roger Dingledine.
* Copyright 2004-2007 Roger Dingledine, Nick Mathewson */
2003-04-07 15:25:44 +02:00
/* See LICENSE for licensing information */
/* $Id$ */
#ifndef __TEST_H
#define __TEST_H
#define TEST_H_ID "$Id$"
2003-04-07 15:25:44 +02:00
/**
* \file test.h
* \brief Macros used by unit tests.
*/
#include <string.h>
#include <stdio.h>
#include "compat.h"
#ifdef __GNUC__
#define PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define PRETTY_FUNCTION ""
#endif
extern int have_failed;
2003-04-07 15:25:44 +02:00
#define test_fail() \
STMT_BEGIN \
have_failed = 1; \
2003-04-07 15:25:44 +02:00
printf("\nFile %s: line %d (%s): assertion failed.", \
_SHORT_FILE_, \
2003-04-07 15:25:44 +02:00
__LINE__, \
PRETTY_FUNCTION); \
2003-04-07 15:25:44 +02:00
return; \
STMT_END
2003-04-07 15:25:44 +02:00
#define test_assert(expr) \
STMT_BEGIN \
if (expr) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
2003-04-07 15:25:44 +02:00
printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
_SHORT_FILE_, \
2003-04-07 15:25:44 +02:00
__LINE__, \
PRETTY_FUNCTION, \
2003-04-07 15:25:44 +02:00
#expr); \
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#define test_eq_type(tp, fmt, expr1, expr2) \
STMT_BEGIN \
tp _test_v1=(tp)(expr1); \
tp _test_v2=(tp)(expr2); \
if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
" "fmt "!="fmt"\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#define test_eq(expr1, expr2) \
test_eq_type(long, "%ld", expr1, expr2)
#define test_eq_ptr(expr1, expr2) \
test_eq_type(void*, "%p", expr1, expr2)
#define test_neq_type(tp, fmt, expr1, expr2) \
STMT_BEGIN \
tp _test_v1=(tp)(expr1); \
tp _test_v2=(tp)(expr2); \
if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
" ("fmt" == "fmt")\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2, \
_test_v1, _test_v2); \
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#define test_neq(expr1, expr2) \
test_neq_type(long, "%ld", expr1, expr2)
#define test_neq_ptr(expr1, expr2) \
test_neq_type(void *, "%p", expr1, expr2)
2003-04-07 15:25:44 +02:00
#define test_streq(expr1, expr2) \
STMT_BEGIN \
const char *_test_v1=(expr1), *_test_v2=(expr2); \
if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
2003-04-07 15:25:44 +02:00
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
" (\"%s\" != \"%s\")\n", \
_SHORT_FILE_, \
2003-04-07 15:25:44 +02:00
__LINE__, \
PRETTY_FUNCTION, \
2003-04-07 15:25:44 +02:00
#expr1, #expr2, \
_test_v1, _test_v2); \
2003-04-07 15:25:44 +02:00
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#define test_strneq(expr1, expr2) \
STMT_BEGIN \
const char *_test_v1=(expr1), *_test_v2=(expr2); \
if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
have_failed = 1; \
2003-04-07 15:25:44 +02:00
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
" (\"%s\" == \"%s\")\n", \
_SHORT_FILE_, \
2003-04-07 15:25:44 +02:00
__LINE__, \
PRETTY_FUNCTION, \
2003-04-07 15:25:44 +02:00
#expr1, #expr2, \
_test_v1, _test_v2); \
2003-04-07 15:25:44 +02:00
return; \
} STMT_END
#define test_memeq(expr1, expr2, len) \
STMT_BEGIN \
const void *_test_v1=(expr1), *_test_v2=(expr2); \
if (!memcmp(_test_v1,_test_v2,(len))) { \
printf("."); fflush(stdout); } else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2); \
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#define test_memneq(expr1, expr2, len) \
STMT_BEGIN \
void *_test_v1=(expr1), *_test_v2=(expr2); \
if (memcmp(_test_v1,_test_v2,(len))) { \
printf("."); fflush(stdout); \
} else { \
have_failed = 1; \
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
_SHORT_FILE_, \
__LINE__, \
PRETTY_FUNCTION, \
#expr1, #expr2); \
return; \
} STMT_END
2003-04-07 15:25:44 +02:00
#endif