-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInlineMacro.cpp
More file actions
81 lines (67 loc) · 1.65 KB
/
InlineMacro.cpp
File metadata and controls
81 lines (67 loc) · 1.65 KB
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
#include "Require.h"
#include <cmath>
#include <sstream>
#include <string>
// clang-format off
#define MACRO1 -20
#define MACRO2 "This is a test." \
" This is more of a test!"
#define MACRO3 -30.0f/std::pow(2.0f, 2.0f)
// #TEST#: IM1 Inline macro MACRO1
#define MACRO4(x_) (MACRO1*x_)
#define MACRO5(x_) \
do \
{ \
if (x_ > 5) \
{ \
x_ *= 2; \
} \
else \
{ \
x_ += 4; \
} \
} while (0)
// clang-format on
namespace
{
template <typename T>
void f1_aux(std::ostream &stream, T value)
{
stream << value << '\n';
}
template <typename T, typename... Types>
void f1_aux(std::ostream &stream, T head, Types... tail)
{
f1_aux(stream, head);
f1_aux(stream, tail...);
}
#define F1_AUX(stream, ...) f1_aux(stream, __VA_ARGS__)
void f1()
{
std::ostringstream result;
// #TEST#: IM2 Inline macro F1_AUX
F1_AUX(result, 10, 20.5, "hello world!");
require_equal(std::string{"10\n20.5\nhello world!\n"}, result.str());
}
#undef F1_AUX
} // namespace
void TestInlineMacro()
{
// #TEST#: IM3 Inline macro MACRO1
int x = MACRO1;
REQUIRE_EQUAL(MACRO1, x);
// #TEST#: IM4 Inline macro MACRO2
std::string s = MACRO2;
REQUIRE_EQUAL(std::string{MACRO2}, s);
// #TEST#: IM5 Inline macro MACRO3
float f = MACRO3;
REQUIRE_EQUAL(MACRO3, f);
// #TEST#: IM6 Inline macro MACRO4
x = MACRO4(2);
REQUIRE_EQUAL(MACRO4(2), x);
// #TEST#: IM7 Inline macro MACRO5
x = 2;
MACRO5(x);
REQUIRE_EQUAL(6, x);
f1();
}