summaryrefslogtreecommitdiff
path: root/tests/test.h
blob: 676ca33ba3d1de2cb58e70ba3f5f7f28aff42347 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
/* This software was written by Dirk Engling <erdgeist@erdgeist.org>
   It is considered beerware. Prost. Skol. Cheers or whatever.

   $id$ */

#ifndef OT_TEST_H__
#define OT_TEST_H__

#include <stddef.h>
#include <string.h>

/* Minimal scaffolding for opentracker's unit tests. No external dependencies.

   Tests are grouped into modules, one per source file under test. A module is
   a list of small case functions, each pinning one behaviour. A failing CHECK
   reports file, line and the offending values, then returns from the case it
   is in, so a single run reports as many independent problems as it can.
   Because the macros return, they may only be used in functions returning
   void. */

void                 test_module(const char *name);
void                 test_case(const char *name);
void                 test_failf(const char *file, int line, const char *fmt, ...);
void                 test_failmem(const char *file, int line, const char *what, const void *got, const void *want, size_t len);
int                  test_summary(void);

extern unsigned long test_checks;
extern unsigned long test_failures;

#define CHECK(cond)                                                                                                                                                \
  do {                                                                                                                                                             \
    ++test_checks;                                                                                                                                                 \
    if (!(cond)) {                                                                                                                                                 \
      test_failf(__FILE__, __LINE__, "expected %s", #cond);                                                                                                        \
      return;                                                                                                                                                      \
    }                                                                                                                                                              \
  } while (0)

#define CHECK_INT(got, want)                                                                                                                                       \
  do {                                                                                                                                                             \
    long long got_ = (long long)(got), want_ = (long long)(want);                                                                                                   \
    ++test_checks;                                                                                                                                                 \
    if (got_ != want_) {                                                                                                                                           \
      test_failf(__FILE__, __LINE__, "%s: got %lld, want %lld", #got, got_, want_);                                                                                 \
      return;                                                                                                                                                      \
    }                                                                                                                                                              \
  } while (0)

#define CHECK_PTR(got, want)                                                                                                                                       \
  do {                                                                                                                                                             \
    const void *got_ = (const void *)(got), *want_ = (const void *)(want);                                                                                          \
    ++test_checks;                                                                                                                                                 \
    if (got_ != want_) {                                                                                                                                           \
      test_failf(__FILE__, __LINE__, "%s: got %p, want %p", #got, got_, want_);                                                                                     \
      return;                                                                                                                                                      \
    }                                                                                                                                                              \
  } while (0)

#define CHECK_MEM(got, want, len)                                                                                                                                  \
  do {                                                                                                                                                             \
    ++test_checks;                                                                                                                                                 \
    if (memcmp((got), (want), (len))) {                                                                                                                            \
      test_failmem(__FILE__, __LINE__, #got, (got), (want), (len));                                                                                                 \
      return;                                                                                                                                                      \
    }                                                                                                                                                              \
  } while (0)

#define CHECK_STR(got, want)                                                                                                                                       \
  do {                                                                                                                                                             \
    const char *got_ = (got), *want_ = (want);                                                                                                                      \
    ++test_checks;                                                                                                                                                 \
    if (strcmp(got_, want_)) {                                                                                                                                     \
      test_failf(__FILE__, __LINE__, "%s: got \"%s\", want \"%s\"", #got, got_, want_);                                                                             \
      return;                                                                                                                                                      \
    }                                                                                                                                                              \
  } while (0)

/* One entry point per module under test */
void test_scan_urlencoded_query(void);
void test_iovec(void);
void test_vector(void);
void test_random(void);
void test_hex(void);
void test_accesslist(void);
void test_scan(void);
void test_buf(void);

#endif