test.h 7.45 KB
Newer Older
1
2
3
/* Copyright (c) 2001-2003, Roger Dingledine.
 * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
 * Copyright (c) 2007, The Tor Project, Inc. */
Nick Mathewson's avatar
Nick Mathewson committed
4
5
6
7
8
/* See LICENSE for licensing information */
/* $Id$ */

#ifndef __TEST_H
#define __TEST_H
9
#define TEST_H_ID "$Id$"
Nick Mathewson's avatar
Nick Mathewson committed
10

11
12
/**
 * \file test.h
13
 * \brief Macros used by unit tests.
14
15
 */

16
#include "compat.h"
17

18
19
20
21
22
23
#ifdef __GNUC__
#define PRETTY_FUNCTION __PRETTY_FUNCTION__
#else
#define PRETTY_FUNCTION ""
#endif

24
25
extern int have_failed;

Nick Mathewson's avatar
Nick Mathewson committed
26
#define test_fail()                                             \
27
  STMT_BEGIN                                                    \
28
    have_failed = 1;                                            \
Nick Mathewson's avatar
Nick Mathewson committed
29
    printf("\nFile %s: line %d (%s): assertion failed.",        \
30
      _SHORT_FILE_,                                             \
Nick Mathewson's avatar
Nick Mathewson committed
31
      __LINE__,                                                 \
32
      PRETTY_FUNCTION);                                         \
Nick Mathewson's avatar
Nick Mathewson committed
33
    return;                                                     \
34
  STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
35
36

#define test_assert(expr)                                       \
37
  STMT_BEGIN                                                    \
38
  if (expr) { printf("."); fflush(stdout); } else {             \
39
    have_failed = 1;                                            \
Nick Mathewson's avatar
Nick Mathewson committed
40
    printf("\nFile %s: line %d (%s): assertion failed: (%s)\n", \
41
      _SHORT_FILE_,                                             \
Nick Mathewson's avatar
Nick Mathewson committed
42
      __LINE__,                                                 \
43
      PRETTY_FUNCTION,                                          \
Nick Mathewson's avatar
Nick Mathewson committed
44
45
      #expr);                                                   \
    return;                                                     \
46
  } STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
47

48
49
#define test_eq_type(tp, fmt, expr1, expr2) \
  STMT_BEGIN                                                            \
50
51
52
  tp _test_v1=(tp)(expr1);                                              \
  tp _test_v2=(tp)(expr2);                                              \
  if (_test_v1==_test_v2) { printf("."); fflush(stdout); } else {       \
53
54
55
56
57
58
59
    have_failed = 1;                                                    \
    printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"      \
           "      "fmt "!="fmt"\n",                                     \
             _SHORT_FILE_,                                              \
             __LINE__,                                                  \
             PRETTY_FUNCTION,                                           \
             #expr1, #expr2,                                            \
60
           _test_v1, _test_v2);                                         \
61
    return;                                                             \
62
  } STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
63

64
65
66
67
68
69
#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)

70
71
#define test_neq_type(tp, fmt, expr1, expr2)                            \
  STMT_BEGIN                                                            \
72
73
74
  tp _test_v1=(tp)(expr1);                                              \
  tp _test_v2=(tp)(expr2);                                              \
  if (_test_v1!=_test_v2) { printf("."); fflush(stdout); } else {       \
75
76
77
78
79
80
81
    have_failed = 1;                                                    \
    printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"      \
           "      ("fmt" == "fmt")\n",                                  \
           _SHORT_FILE_,                                                \
           __LINE__,                                                    \
           PRETTY_FUNCTION,                                             \
           #expr1, #expr2,                                              \
82
           _test_v1, _test_v2);                                         \
83
    return;                                                             \
84
  } STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
85

86
87
88
89
90
91
#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)

Nick Mathewson's avatar
Nick Mathewson committed
92
#define test_streq(expr1, expr2)                                \
93
  STMT_BEGIN                                                    \
94
95
    const char *_test_v1=(expr1), *_test_v2=(expr2);                        \
    if (!strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else { \
96
    have_failed = 1;                                            \
Nick Mathewson's avatar
Nick Mathewson committed
97
    printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n"\
98
           "      (\"%s\" != \"%s\")\n",                        \
99
      _SHORT_FILE_,                                             \
Nick Mathewson's avatar
Nick Mathewson committed
100
      __LINE__,                                                 \
101
      PRETTY_FUNCTION,                                          \
Nick Mathewson's avatar
Nick Mathewson committed
102
      #expr1, #expr2,                                           \
103
      _test_v1, _test_v2);                                      \
Nick Mathewson's avatar
Nick Mathewson committed
104
    return;                                                     \
105
  } STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
106
107

#define test_strneq(expr1, expr2)                               \
108
  STMT_BEGIN                                                    \
109
110
    const char *_test_v1=(expr1), *_test_v2=(expr2);                        \
    if (strcmp(_test_v1,_test_v2)) { printf("."); fflush(stdout); } else {  \
111
    have_failed = 1;                                            \
Nick Mathewson's avatar
Nick Mathewson committed
112
    printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n"\
113
           "      (\"%s\" == \"%s\")\n",                        \
114
      _SHORT_FILE_,                                             \
Nick Mathewson's avatar
Nick Mathewson committed
115
      __LINE__,                                                 \
116
      PRETTY_FUNCTION,                                          \
Nick Mathewson's avatar
Nick Mathewson committed
117
      #expr1, #expr2,                                           \
118
      _test_v1, _test_v2);                                      \
Nick Mathewson's avatar
Nick Mathewson committed
119
    return;                                                     \
120
121
122
  } STMT_END

#define test_memeq(expr1, expr2, len)                           \
123
  STMT_BEGIN                                                    \
124
125
126
    const void *_test_v1=(expr1), *_test_v2=(expr2);            \
    if (!memcmp(_test_v1,_test_v2,(len))) {                     \
      printf("."); fflush(stdout); } else {                     \
127
    have_failed = 1;                                            \
128
    printf("\nFile %s: line %d (%s): Assertion failed: (%s==%s)\n", \
129
      _SHORT_FILE_,                                             \
130
      __LINE__,                                                 \
131
      PRETTY_FUNCTION,                                          \
132
133
134
      #expr1, #expr2);                                          \
    return;                                                     \
  } STMT_END
Nick Mathewson's avatar
Nick Mathewson committed
135

136
#define test_memneq(expr1, expr2, len)                          \
137
  STMT_BEGIN                                                    \
138
139
140
141
   void *_test_v1=(expr1), *_test_v2=(expr2);                   \
   if (memcmp(_test_v1,_test_v2,(len))) {                       \
     printf("."); fflush(stdout);                               \
   } else {                                                     \
142
    have_failed = 1;                                            \
143
    printf("\nFile %s: line %d (%s): Assertion failed: (%s!=%s)\n", \
144
      _SHORT_FILE_,                                             \
145
      __LINE__,                                                 \
146
      PRETTY_FUNCTION,                                          \
147
148
149
150
      #expr1, #expr2);                                          \
    return;                                                     \
  } STMT_END

Nick Mathewson's avatar
Nick Mathewson committed
151
#endif
152