2007-12-12 22:09:01 +01:00
|
|
|
/* Copyright (c) 2001-2003, Roger Dingledine.
|
|
|
|
* Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
|
2008-02-07 06:31:47 +01:00
|
|
|
* Copyright (c) 2007-2008, The Tor Project, Inc. */
|
2003-04-07 15:25:44 +02:00
|
|
|
/* See LICENSE for licensing information */
|
|
|
|
/* $Id$ */
|
|
|
|
|
|
|
|
#ifndef __TEST_H
|
|
|
|
#define __TEST_H
|
2004-11-29 23:25:31 +01:00
|
|
|
#define TEST_H_ID "$Id$"
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2004-05-10 09:54:13 +02:00
|
|
|
/**
|
|
|
|
* \file test.h
|
2005-06-11 07:31:17 +02:00
|
|
|
* \brief Macros used by unit tests.
|
2004-05-10 09:54:13 +02:00
|
|
|
*/
|
|
|
|
|
2004-12-22 03:32:26 +01:00
|
|
|
#include "compat.h"
|
2003-04-15 21:10:18 +02:00
|
|
|
|
2003-08-12 05:08:41 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#define PRETTY_FUNCTION __PRETTY_FUNCTION__
|
|
|
|
#else
|
|
|
|
#define PRETTY_FUNCTION ""
|
|
|
|
#endif
|
|
|
|
|
2004-03-01 07:45:32 +01:00
|
|
|
extern int have_failed;
|
|
|
|
|
2008-02-08 22:09:35 +01:00
|
|
|
#define test_fail_msg(msg) \
|
2003-04-15 21:10:18 +02:00
|
|
|
STMT_BEGIN \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2008-02-08 22:09:35 +01:00
|
|
|
printf("\nFile %s: line %d (%s): %s", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-07 15:25:44 +02:00
|
|
|
__LINE__, \
|
2008-02-08 22:09:35 +01:00
|
|
|
PRETTY_FUNCTION, \
|
|
|
|
msg); \
|
2003-04-07 15:25:44 +02:00
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2008-02-08 22:09:35 +01:00
|
|
|
#define test_fail() test_fail_msg("Assertion failed.")
|
|
|
|
|
2003-04-07 15:25:44 +02:00
|
|
|
#define test_assert(expr) \
|
2003-04-17 03:55:13 +02:00
|
|
|
STMT_BEGIN \
|
2004-11-28 10:05:49 +01:00
|
|
|
if (expr) { printf("."); fflush(stdout); } else { \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2003-04-07 15:25:44 +02:00
|
|
|
printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-07 15:25:44 +02:00
|
|
|
__LINE__, \
|
2004-04-27 00:12:13 +02:00
|
|
|
PRETTY_FUNCTION, \
|
2003-04-07 15:25:44 +02:00
|
|
|
#expr); \
|
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
} STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2005-08-12 19:24:53 +02:00
|
|
|
#define test_eq_type(tp, fmt, expr1, expr2) \
|
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
tp _test_v1=(tp)(expr1); \
|
|
|
|
tp _test_v2=(tp)(expr2); \
|
|
|
|
if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else { \
|
2005-08-12 19:24:53 +02:00
|
|
|
have_failed = 1; \
|
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
|
|
|
|
" "fmt "!="fmt"\n", \
|
|
|
|
_SHORT_FILE_, \
|
|
|
|
__LINE__, \
|
|
|
|
PRETTY_FUNCTION, \
|
|
|
|
#expr1, #expr2, \
|
2007-06-13 20:15:56 +02:00
|
|
|
_test_v1, _test_v2); \
|
2005-08-12 19:24:53 +02:00
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
} STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2005-08-12 19:24:53 +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)
|
|
|
|
|
2007-03-06 21:25:44 +01:00
|
|
|
#define test_neq_type(tp, fmt, expr1, expr2) \
|
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
tp _test_v1=(tp)(expr1); \
|
|
|
|
tp _test_v2=(tp)(expr2); \
|
|
|
|
if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else { \
|
2007-03-06 21:25:44 +01:00
|
|
|
have_failed = 1; \
|
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n" \
|
|
|
|
" ("fmt" == "fmt")\n", \
|
|
|
|
_SHORT_FILE_, \
|
|
|
|
__LINE__, \
|
|
|
|
PRETTY_FUNCTION, \
|
|
|
|
#expr1, #expr2, \
|
2007-06-13 20:15:56 +02:00
|
|
|
_test_v1, _test_v2); \
|
2007-03-06 21:25:44 +01:00
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
} STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2007-03-06 21:25:44 +01: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) \
|
2003-04-17 03:55:13 +02:00
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
const char *_test_v1=(expr1), *_test_v2=(expr2); \
|
|
|
|
if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2003-04-07 15:25:44 +02:00
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
|
2003-05-06 19:38:16 +02:00
|
|
|
" (\"%s\" != \"%s\")\n", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-07 15:25:44 +02:00
|
|
|
__LINE__, \
|
2004-04-27 00:12:13 +02:00
|
|
|
PRETTY_FUNCTION, \
|
2003-04-07 15:25:44 +02:00
|
|
|
#expr1, #expr2, \
|
2007-06-13 20:15:56 +02:00
|
|
|
_test_v1, _test_v2); \
|
2003-04-07 15:25:44 +02:00
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
} STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
|
|
|
#define test_strneq(expr1, expr2) \
|
2003-04-17 03:55:13 +02:00
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
const char *_test_v1=(expr1), *_test_v2=(expr2); \
|
|
|
|
if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2003-04-07 15:25:44 +02:00
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
|
2003-05-06 19:38:16 +02:00
|
|
|
" (\"%s\" == \"%s\")\n", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-07 15:25:44 +02:00
|
|
|
__LINE__, \
|
2004-04-27 00:12:13 +02:00
|
|
|
PRETTY_FUNCTION, \
|
2003-04-07 15:25:44 +02:00
|
|
|
#expr1, #expr2, \
|
2007-06-13 20:15:56 +02:00
|
|
|
_test_v1, _test_v2); \
|
2003-04-07 15:25:44 +02:00
|
|
|
return; \
|
2003-04-15 21:10:18 +02:00
|
|
|
} STMT_END
|
|
|
|
|
|
|
|
#define test_memeq(expr1, expr2, len) \
|
2003-04-17 03:55:13 +02:00
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
const void *_test_v1=(expr1), *_test_v2=(expr2); \
|
2008-02-27 09:10:28 +01:00
|
|
|
char *mem1, *mem2; \
|
2007-06-13 20:15:56 +02:00
|
|
|
if (!memcmp(_test_v1,_test_v2,(len))) { \
|
|
|
|
printf("."); fflush(stdout); } else { \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2008-02-27 09:10:28 +01:00
|
|
|
mem1 = tor_malloc(len*2+1); \
|
|
|
|
mem2 = tor_malloc(len*2+1); \
|
|
|
|
base16_encode(mem1, len*2+1, _test_v1, len); \
|
|
|
|
base16_encode(mem2, len*2+1, _test_v2, len); \
|
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n" \
|
|
|
|
" %s != %s\n", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-15 21:10:18 +02:00
|
|
|
__LINE__, \
|
2004-04-27 00:12:13 +02:00
|
|
|
PRETTY_FUNCTION, \
|
2008-02-27 09:10:28 +01:00
|
|
|
#expr1, #expr2, mem1, mem2); \
|
2003-04-15 21:10:18 +02:00
|
|
|
return; \
|
|
|
|
} STMT_END
|
2003-04-07 15:25:44 +02:00
|
|
|
|
2004-04-27 00:12:13 +02:00
|
|
|
#define test_memneq(expr1, expr2, len) \
|
2003-04-17 03:55:13 +02:00
|
|
|
STMT_BEGIN \
|
2007-06-13 20:15:56 +02:00
|
|
|
void *_test_v1=(expr1), *_test_v2=(expr2); \
|
|
|
|
if (memcmp(_test_v1,_test_v2,(len))) { \
|
|
|
|
printf("."); fflush(stdout); \
|
|
|
|
} else { \
|
2004-03-01 07:45:32 +01:00
|
|
|
have_failed = 1; \
|
2003-04-16 17:24:09 +02:00
|
|
|
printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
|
2004-12-22 03:32:26 +01:00
|
|
|
_SHORT_FILE_, \
|
2003-04-16 17:24:09 +02:00
|
|
|
__LINE__, \
|
2004-04-27 00:12:13 +02:00
|
|
|
PRETTY_FUNCTION, \
|
2003-04-16 17:24:09 +02:00
|
|
|
#expr1, #expr2); \
|
|
|
|
return; \
|
|
|
|
} STMT_END
|
|
|
|
|
2003-04-07 15:25:44 +02:00
|
|
|
#endif
|
2005-06-09 21:03:31 +02:00
|
|
|
|