36namespace FunctionTestsHelpers
39 static double multiply (
double x,
double a)
noexcept {
return a *
x; }
45 for (
auto i = 0; i < bigDataSize; ++i)
52 for (
auto i = 0; i < bigDataSize; ++i)
58 static const int bigDataSize = 32,
59 bigDataSum = bigDataSize * (bigDataSize + 1) / 2;
60 int content[bigDataSize];
65 FunctionObject() =
default;
67 FunctionObject (
const FunctionObject& other)
69 bigData.reset (
new BigData (*other.bigData));
72 int operator()(
int i)
const {
return bigData->sum() + i; }
74 std::unique_ptr<BigData> bigData {
new BigData() };
76 JUCE_LEAK_DETECTOR (FunctionObject)
81 BigFunctionObject() =
default;
83 BigFunctionObject (
const BigFunctionObject& other)
85 bigData.reset (
new BigData (*other.bigData));
88 int operator()(
int i)
const {
return bigData->sum() + i; }
90 std::unique_ptr<BigData> bigData {
new BigData() };
94 JUCE_LEAK_DETECTOR (BigFunctionObject)
98class FunctionTests :
public UnitTest
103 void runTest()
override
105 FunctionTestsHelpers::BigData
bigData;
108 beginTest (
"Functions");
110 std::function<
void(
int&)>
f1 (FunctionTestsHelpers::incrementArgument);
116 std::function<
double(
double,
double)>
f2 (FunctionTestsHelpers::multiply);
117 expectEquals (6.0,
f2 (2.0, 3.0));
121 beginTest (
"Function objects");
123 std::function<
int(
int)>
f1 = FunctionTestsHelpers::FunctionObject();
124 expectEquals (
f1 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
126 std::function<
int(
int)>
f2 { FunctionTestsHelpers::BigFunctionObject() };
127 expectEquals (
f2 (5), FunctionTestsHelpers::BigData::bigDataSum + 5);
131 beginTest (
"Lambdas");
133 std::function<
int()>
fStack ([] {
return 3; });
134 expectEquals (
fStack(), 3);
137 expectEquals (
fHeap(), FunctionTestsHelpers::BigData::bigDataSum);
141 beginTest (
"Boolean");
143 std::function<
void(
int&)>
f1;
148 std::function<
int()>
f2 ([]() {
return 3; });
156 std::function<
int()>
fStack ([] {
return 3; });
161 beginTest (
"copy constructor");
164 expectEquals (
f1(), 3);
167 expectEquals (
f2(), FunctionTestsHelpers::BigData::bigDataSum);
175 beginTest (
"assignment");
177 std::function<
int()>
f1;
179 expectEquals (
f1(), 3);
181 std::function<
int()>
f2;
183 expectEquals (
f2(), FunctionTestsHelpers::BigData::bigDataSum);
186 expectEquals (
f1(), FunctionTestsHelpers::BigData::bigDataSum);
189 expectEquals (
f2(), 3);
197 beginTest (
"move constructor");
203 expectEquals (
f1(), 3);
205 std::unique_ptr<std::function<
int()>>
fHeapTmp (
new std::function<
int()> (
fHeap));
211 expectEquals (
f2(), FunctionTestsHelpers::BigData::bigDataSum);
213 std::unique_ptr<std::function<
int()>>
fEmptyTmp (
new std::function<
int()>());
221 beginTest (
"move assignment");
228 expectEquals (
f1(), 3);
231 std::unique_ptr<std::function<
int()>>
fHeapTmp (
new std::function<
int()> (
fHeap));
237 expectEquals (
f2(), FunctionTestsHelpers::BigData::bigDataSum);
240 std::unique_ptr<std::function<
int()>>
fEmptyTmp (
new std::function<
int()>());
248 beginTest (
"nullptr");
250 std::function<
int()>
f1 (
nullptr);
254 std::function<
int()>
f2 ([]() {
return 11; });
263 std::function<
int()>
f1;
266 expectEquals (
f1(), 3);
272 expectEquals (
f3(), 3);
273 expectEquals (
f1(), FunctionTestsHelpers::BigData::bigDataSum);
278static FunctionTests functionTests;
Array()=default
Creates an empty array.