forked from timescale/timescaledb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_utils.h
104 lines (91 loc) · 6.31 KB
/
test_utils.h
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/*
* This file and its contents are licensed under the Apache License 2.0.
* Please see the included NOTICE for copyright information and
* LICENSE-APACHE for a copy of the license.
*/
#ifndef TIMESCALEDB_TEST_UTILS_H
#define TIMESCALEDB_TEST_UTILS_H
#include <postgres.h>
#include <access/xact.h>
#include <fmgr.h>
#include "export.h"
static inline const char *
strip_path(const char *filename)
{
int i = 0, slash = 0;
while (filename[i] != '\0')
{
if (filename[i] == '/' || filename[i] == '\\')
slash = i;
i++;
}
return &filename[slash + 1];
}
#define TestFailure(fmt, ...) \
do \
{ \
elog(ERROR, \
"TestFailure @ %s %s:%d | " fmt "", \
strip_path(__FILE__), \
__func__, \
__LINE__, \
##__VA_ARGS__); \
pg_unreachable(); \
} while (0)
#define TestAssertInt64Eq(a, b) \
do \
{ \
int64 a_i = (a); \
int64 b_i = (b); \
if (a_i != b_i) \
TestFailure("(%s == %s) [" INT64_FORMAT " == " INT64_FORMAT "]", #a, #b, a_i, b_i); \
} while (0)
#define TestAssertPtrEq(a, b) \
do \
{ \
void *a_i = (a); \
void *b_i = (b); \
if (a_i != b_i) \
TestFailure("(%s == %s)", #a, #b); \
} while (0)
#define TestAssertDoubleEq(a, b) \
do \
{ \
double a_i = (a); \
double b_i = (b); \
if (a_i != b_i) \
TestFailure("(%s == %s) [%f == %f]", #a, #b, a_i, b_i); \
} while (0)
#define TestEnsureError(a) \
do \
{ \
volatile bool this_has_panicked = false; \
MemoryContext oldctx = CurrentMemoryContext; \
BeginInternalSubTransaction("error expected"); \
PG_TRY(); \
{ \
(a); \
} \
PG_CATCH(); \
{ \
this_has_panicked = true; \
RollbackAndReleaseCurrentSubTransaction(); \
FlushErrorState(); \
} \
PG_END_TRY(); \
MemoryContextSwitchTo(oldctx); \
if (!this_has_panicked) \
{ \
elog(ERROR, "failed to panic @ line %d", __LINE__); \
} \
} while (0)
#define TestAssertTrue(cond) \
do \
{ \
if (!(cond)) \
TestFailure("(%s)", #cond); \
} while (0)
#define TS_TEST_FN(name) \
TS_FUNCTION_INFO_V1(name); \
Datum name(PG_FUNCTION_ARGS)
#endif /* TIMESCALEDB_TEST_UTILS_H */