-
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathTestUtilities.cpp
More file actions
255 lines (205 loc) · 8.2 KB
/
TestUtilities.cpp
File metadata and controls
255 lines (205 loc) · 8.2 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*==============================================================================
Copyright 2018 by Tracktion Corporation.
For more information visit www.tracktion.com
You may also use this code under the terms of the GPL v3 (see
www.gnu.org/licenses).
pluginval IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
DISCLAIMED.
==============================================================================*/
#include <future>
#include "TestUtilities.h"
inline bool logAllocationViolationIfNotAllowed()
{
auto& ai = getAllocatorInterceptor();
if (! ai.isAllowedToAllocate())
{
ai.logAllocationViolation();
return false;
}
return true;
}
inline void violationError (const std::string& text)
{
switch (AllocatorInterceptor::getViolationBehaviour())
{
case AllocatorInterceptor::ViolationBehaviour::logToCerr:
std::cerr << text;
break;
case AllocatorInterceptor::ViolationBehaviour::throwException:
throw;
break;
case AllocatorInterceptor::ViolationBehaviour::none:
default:
break;
}
}
inline bool throwIfRequiredAndReturnShouldLog()
{
switch (AllocatorInterceptor::getViolationBehaviour())
{
case AllocatorInterceptor::ViolationBehaviour::logToCerr:
return true;
case AllocatorInterceptor::ViolationBehaviour::throwException:
throw std::bad_alloc();
case AllocatorInterceptor::ViolationBehaviour::none:
default:
break;
}
return false;
}
//==============================================================================
#if JUCE_CLANG
#define ATTRIBUTE_USED __attribute__((used))
#else
#define ATTRIBUTE_USED
#endif
ATTRIBUTE_USED void* operator new (std::size_t sz)
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal allocation of " << sz << " bytes\n";
return std::malloc (sz);
}
ATTRIBUTE_USED void* operator new[] (std::size_t sz)
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal array allocation of " << sz << " bytes\n";
return std::malloc (sz);
}
ATTRIBUTE_USED void operator delete (void* ptr) noexcept
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal deletion\n";
std::free (ptr);
}
ATTRIBUTE_USED void operator delete[] (void* ptr) noexcept
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal array deletion\n";
std::free (ptr);
}
#if JUCE_CXX14_IS_AVAILABLE
void operator delete (void* ptr, size_t) noexcept
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal deletion\n";
std::free (ptr);
}
void operator delete[] (void* ptr, size_t) noexcept
{
if (! logAllocationViolationIfNotAllowed())
if (throwIfRequiredAndReturnShouldLog())
std::cerr << "!!! WARNING: Illegal array deletion\n";
std::free (ptr);
}
#endif
//==============================================================================
std::atomic<AllocatorInterceptor::ViolationBehaviour> AllocatorInterceptor::violationBehaviour (ViolationBehaviour::logToCerr);
void AllocatorInterceptor::setViolationBehaviour (ViolationBehaviour behaviour) noexcept
{
violationBehaviour.store (behaviour);
}
AllocatorInterceptor::ViolationBehaviour AllocatorInterceptor::getViolationBehaviour() noexcept
{
return violationBehaviour.load();
}
AllocatorInterceptor& getAllocatorInterceptor()
{
thread_local AllocatorInterceptor ai;
return ai;
}
//==============================================================================
ScopedAllocationDisabler::ScopedAllocationDisabler() { getAllocatorInterceptor().disableAllocations(); }
ScopedAllocationDisabler::~ScopedAllocationDisabler() { getAllocatorInterceptor().enableAllocations(); }
//==============================================================================
struct AllocatorInterceptorTests : public juce::UnitTest,
private juce::AsyncUpdater
{
AllocatorInterceptorTests()
: juce::UnitTest ("AllocatorInterceptorTests", "pluginval")
{
}
void runTest() override
{
AllocatorInterceptor::setViolationBehaviour (AllocatorInterceptor::ViolationBehaviour::none);
auto& allocatorInterceptor = getAllocatorInterceptor();
beginTest ("Ensure separate allocators are used for threads");
{
AllocatorInterceptor* ai1 = &getAllocatorInterceptor();
AllocatorInterceptor *ai2 = nullptr;
auto task = std::async (std::launch::async, [&ai2] { ai2 = &getAllocatorInterceptor(); });
task.get();
expect (ai1 != ai2);
}
beginTest ("Ensure all allocations pass");
{
performAllocations();
expect (! allocatorInterceptor.getAndClearAllocationViolation());
expectEquals (allocatorInterceptor.getAndClearNumAllocationViolations(), 0);
}
beginTest ("Ensure all allocations fail on MessageThread");
{
{
ScopedAllocationDisabler sad;
performAllocations();
}
expect (allocatorInterceptor.getAndClearAllocationViolation());
expectGreaterThan (allocatorInterceptor.getAndClearNumAllocationViolations(), 0);
}
beginTest ("Ensure allocation violation status is reset");
{
expect (! allocatorInterceptor.getAndClearAllocationViolation());
expectEquals (allocatorInterceptor.getAndClearNumAllocationViolations(), 0);
}
beginTest ("Ensure all allocations pass on MessageThread, fail on background thread");
{
auto task = std::async (std::launch::async, [this]
{
ScopedAllocationDisabler sad;
performAllocations();
expect (getAllocatorInterceptor().getAndClearAllocationViolation());
expectGreaterThan (getAllocatorInterceptor().getAndClearNumAllocationViolations(), 0);
});
task.get();
expect (! allocatorInterceptor.getAndClearAllocationViolation());
expectEquals (allocatorInterceptor.getAndClearNumAllocationViolations(), 0);
performAllocations();
expect (! allocatorInterceptor.getAndClearAllocationViolation());
expectEquals (allocatorInterceptor.getAndClearNumAllocationViolations(), 0);
}
beginTest ("Ensure array allocations are caught");
{
expect (! allocatorInterceptor.getAndClearAllocationViolation());
expectEquals (getAllocatorInterceptor().getAndClearNumAllocationViolations(), 0);
{
ScopedAllocationDisabler sad;
std::vector<int> ints (42);
ints.resize (100);
}
expect (allocatorInterceptor.getAndClearAllocationViolation());
expectGreaterThan (getAllocatorInterceptor().getAndClearNumAllocationViolations(), 0);
}
beginTest ("Ensure allocations are thrown");
{
AllocatorInterceptor::setViolationBehaviour (AllocatorInterceptor::ViolationBehaviour::throwException);
ScopedAllocationDisabler sad;
expectThrows (std::vector<int> (42));
AllocatorInterceptor::setViolationBehaviour (AllocatorInterceptor::ViolationBehaviour::logToCerr);
}
}
private:
void performAllocations()
{
juce::String s ("Hello world");
std::vector<float> floats (42);
triggerAsyncUpdate();
ignoreUnused (s, floats);
}
void handleAsyncUpdate() override {}
};
static AllocatorInterceptorTests allocatorInterceptorTests;