OpenShot Library | libopenshot-audio 0.2.0
juce_StdFunctionCompat.cpp
1/*
2 ==============================================================================
3
4 This file is part of the JUCE library.
5 Copyright (c) 2017 - ROLI Ltd.
6
7 Permission is granted to use this software under the terms of the ISC license
8 http://www.isc.org/downloads/software-support-policy/isc-license/
9
10 Permission to use, copy, modify, and/or distribute this software for any
11 purpose with or without fee is hereby granted, provided that the above
12 copyright notice and this permission notice appear in all copies.
13
14 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
15 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
17 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
18 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20 OF THIS SOFTWARE.
21
22 -----------------------------------------------------------------------------
23
24 To release a closed-source product which uses other parts of JUCE not
25 licensed under the ISC terms, commercial licenses are available: visit
26 www.juce.com for more information.
27
28 ==============================================================================
29*/
30
31namespace juce
32{
33
34#if JUCE_UNIT_TESTS
35
36namespace FunctionTestsHelpers
37{
38 static void incrementArgument (int& x) { x++; }
39 static double multiply (double x, double a) noexcept { return a * x; }
40
41 struct BigData
42 {
43 BigData()
44 {
45 for (auto i = 0; i < bigDataSize; ++i)
46 content[i] = i + 1;
47 }
48
49 int sum() const
50 {
51 int result = 0;
52 for (auto i = 0; i < bigDataSize; ++i)
53 result += content[i];
54
55 return result;
56 }
57
58 static const int bigDataSize = 32,
59 bigDataSum = bigDataSize * (bigDataSize + 1) / 2;
60 int content[bigDataSize];
61 };
62
63 struct FunctionObject
64 {
65 FunctionObject() = default;
66
67 FunctionObject (const FunctionObject& other)
68 {
69 bigData.reset (new BigData (*other.bigData));
70 }
71
72 int operator()(int i) const { return bigData->sum() + i; }
73
74 std::unique_ptr<BigData> bigData { new BigData() };
75
76 JUCE_LEAK_DETECTOR (FunctionObject)
77 };
78
79 struct BigFunctionObject
80 {
81 BigFunctionObject() = default;
82
83 BigFunctionObject (const BigFunctionObject& other)
84 {
85 bigData.reset (new BigData (*other.bigData));
86 }
87
88 int operator()(int i) const { return bigData->sum() + i; }
89
90 std::unique_ptr<BigData> bigData { new BigData() };
91
92 int stackUsage[32];
93
94 JUCE_LEAK_DETECTOR (BigFunctionObject)
95 };
96}
97
98class FunctionTests : public UnitTest
99{
100public:
101 FunctionTests() : UnitTest ("Function", "Function") {}
102
103 void runTest() override
104 {
105 FunctionTestsHelpers::BigData bigData;
106
107 {
108 beginTest ("Functions");
109
110 std::function<void(int&)> f1 (FunctionTestsHelpers::incrementArgument);
111
112 auto x = 0;
113 f1 (x);
114 expectEquals (x, 1);
115
116 std::function<double(double, double)> f2 (FunctionTestsHelpers::multiply);
117 expectEquals (6.0, f2 (2.0, 3.0));
118 }
119
120 {
121 beginTest ("Function objects");
122
123 std::function<int(int)> f1 = FunctionTestsHelpers::FunctionObject();
124 expectEquals (f1 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
125
126 std::function<int(int)> f2 { FunctionTestsHelpers::BigFunctionObject() };
127 expectEquals (f2 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
128 }
129
130 {
131 beginTest ("Lambdas");
132
133 std::function<int()> fStack ([] { return 3; });
134 expectEquals (fStack(), 3);
135
136 std::function<int()> fHeap ([=] { return bigData.sum(); });
137 expectEquals (fHeap(), FunctionTestsHelpers::BigData::bigDataSum);
138 }
139
140 {
141 beginTest ("Boolean");
142
143 std::function<void(int&)> f1;
144
145 if (f1)
146 expect (false);
147
148 std::function<int()> f2 ([]() { return 3; });
149
150 if (! f2)
151 expect (false);
152 }
153
154 std::function<int()> fEmpty;
155
156 std::function<int()> fStack ([] { return 3; });
157
158 std::function<int()> fHeap ([=] { return bigData.sum(); });
159
160 {
161 beginTest ("copy constructor");
162
163 std::function<int()> f1 (fStack);
164 expectEquals (f1(), 3);
165
166 std::function<int()> f2 (fHeap);
167 expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
168
169 std::function<int()> f3 (fEmpty);
170 if (f3)
171 expect (false);
172 }
173
174 {
175 beginTest ("assignment");
176
177 std::function<int()> f1;
178 f1 = fStack;
179 expectEquals (f1(), 3);
180
181 std::function<int()> f2;
182 f2 = fHeap;
183 expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
184
185 f1 = fHeap;
186 expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum);
187
188 f2 = fStack;
189 expectEquals (f2(), 3);
190
191 f1 = fEmpty;
192 if (f1)
193 expect (false);
194 }
195
196 {
197 beginTest ("move constructor");
198
199 std::unique_ptr<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
200 std::function<int()> f1 (std::move (*fStackTmp));
201
202 fStackTmp.reset();
203 expectEquals (f1(), 3);
204
205 std::unique_ptr<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
206 std::function<int()> f2 (std::move (*fHeapTmp));
207 if (*fHeapTmp)
208 expect (false);
209
210 fHeapTmp.reset();
211 expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
212
213 std::unique_ptr<std::function<int()>> fEmptyTmp (new std::function<int()>());
214 std::function<int()> f3 (std::move (*fEmptyTmp));
215 fEmptyTmp.reset();
216 if (f3)
217 expect (false);
218 }
219
220 {
221 beginTest ("move assignment");
222
223 std::function<int()> f1 (fHeap);
224 std::unique_ptr<std::function<int()>> fStackTmp (new std::function<int()> (fStack));
225 f1 = std::move (*fStackTmp);
226
227 fStackTmp.reset();
228 expectEquals (f1(), 3);
229
230 std::function<int()> f2 (fStack);
231 std::unique_ptr<std::function<int()>> fHeapTmp (new std::function<int()> (fHeap));
232 f2 = std::move (*fHeapTmp);
233 if (*fHeapTmp)
234 expect (false);
235
236 fHeapTmp.reset();
237 expectEquals (f2(), FunctionTestsHelpers::BigData::bigDataSum);
238
239 std::function<int()> f3 (fHeap);
240 std::unique_ptr<std::function<int()>> fEmptyTmp (new std::function<int()>());
241 f3 = std::move (*fEmptyTmp);
242 fEmptyTmp.reset();
243 if (f3)
244 expect (false);
245 }
246
247 {
248 beginTest ("nullptr");
249
250 std::function<int()> f1 (nullptr);
251 if (f1)
252 expect (false);
253
254 std::function<int()> f2 ([]() { return 11; });
255 f2 = nullptr;
256 if (f2)
257 expect (false);
258 }
259
260 {
261 beginTest ("Swap");
262
263 std::function<int()> f1;
264 std::function<int()> f2 (fStack);
265 f2.swap (f1);
266 expectEquals (f1(), 3);
267 if (f2)
268 expect (false);
269
270 std::function<int()> f3 (fHeap);
271 f3.swap (f1);
272 expectEquals (f3(), 3);
273 expectEquals (f1(), FunctionTestsHelpers::BigData::bigDataSum);
274 }
275 }
276};
277
278static FunctionTests functionTests;
279
280#endif
281
282} // namespace juce