-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathInlineVariable.cpp
More file actions
159 lines (139 loc) · 3.29 KB
/
InlineVariable.cpp
File metadata and controls
159 lines (139 loc) · 3.29 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "Require.h"
#include <stdexcept>
#include <string>
// Replaces all references to a variable with its value.
namespace InlineVariableNamespace
{
int Function1() { return 1; }
int Function2() { return 2; }
void TestInteger()
{
// #TEST#: IV1 Inline Variable i
int i = 4;
int j = i * 4 + 6;
require_equal(22, j);
j += 10;
require_equal(32, j);
j /= 15;
require_equal(2, j);
j &= 7 + i;
require_equal(2, j);
}
void TestFunction()
{
// #TEST#: IV2 Inline Variable fn
int (*fn)() = Function1;
int j = (*fn)();
require_equal(1, j);
fn = Function2;
j += (*fn)();
require_equal(3, j);
}
void TestFunction2()
{
int (*fn)() = Function1;
// #TEST#: IV3 Inline Variable fn
int j = (*fn)();
require_equal(1, j);
int (*fn2)() = Function2;
// #TEST#: IV4 Inline Variable fn2
j += (*fn2)();
require_equal(3, j);
}
void TestString()
{
std::string temp = "This is a temp string.";
// #TEST#: IV5 Inline Variable temp
std::string foo = temp;
require_equal(foo, std::string{"This is a temp string."});
foo += " This is more text.";
foo += temp;
require_equal(foo,
std::string{"This is a temp string."
" This is more text."
"This is a temp string."});
}
class Foo
{
public:
int Operation1() { return 1; }
int Operation2() { return 2; }
int Method1() const { return 1; }
int Method2() const { return 2; }
};
void TestMemberPointer()
{
// #TEST#: IV6 Inline Variable member
int (Foo::*member)() = &Foo::Operation1;
Foo f;
int j = (f.*member)();
require_equal(1, j);
member = &Foo::Operation2;
j += (f.*member)();
require_equal(3, j);
}
void TestConstMemberPointer()
{
// #TEST#: IV7 Inline Variable member
int (Foo::*member)() const = &Foo::Method1;
Foo f;
int j = (f.*member)();
require_equal(1, j);
member = &Foo::Method2;
j += (f.*member)();
require_equal(3, j);
}
void TestMemberPointer2()
{
int (Foo::*member)() = &Foo::Operation1;
Foo f;
// #TEST#: IV8 Inline Variable member
int j = (f.*member)();
require_equal(1, j);
int (Foo::*member2)() = &Foo::Operation2;
// #TEST#: IV9 Inline Variable member2
j += (f.*member2)();
require_equal(3, j);
}
void TestConstMemberPointer2()
{
int (Foo::*member)() const = &Foo::Method1;
Foo f;
// #TEST#: IV10 Inline Variable member
int j = (f.*member)();
require_equal(1, j);
int (Foo::*member2)() const = &Foo::Method2;
// #TEST#: IV11 Inline Variable member2
j += (f.*member2)();
require_equal(3, j);
}
int boolParam(bool ¶m)
{
static int i = 0;
param = (++i % 2) != 0;
return i;
}
bool globalBool = true;
void TestReference()
{
globalBool = false;
bool &r = globalBool;
// #TEST#: IV12 Inline Variable r
int value = boolParam(r);
require_equal(1, value);
require_equal(true, globalBool);
}
} // namespace InlineVariableNamespace
using namespace InlineVariableNamespace;
void TestInlineVariable()
{
TestInteger();
TestFunction();
TestFunction2();
TestString();
TestMemberPointer();
TestConstMemberPointer();
TestMemberPointer2();
TestConstMemberPointer2();
TestReference();
}