1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/FunctionRef.h"
9 #include "mozilla/UniquePtr.h"
14 MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
17 int addConstRefs(const int& arg1
, const int& arg2
) { return arg1
+ arg2
; }
19 void incrementPointer(int* arg
) { (*arg
)++; }
21 int increment(int arg
) { return arg
+ 1; }
23 int incrementUnique(mozilla::UniquePtr
<int> ptr
) { return *ptr
+ 1; }
25 static bool helloWorldCalled
= false;
27 void helloWorld() { helloWorldCalled
= true; }
30 static int increment(int arg
) { return arg
+ 1; }
34 int operator()(int arg
) { return arg
+ 1; }
37 static void TestNonmemberFunction() {
38 mozilla::FunctionRef
<int(int)> f(&increment
);
42 static void TestStaticMemberFunction() {
43 mozilla::FunctionRef
<int(int)> f(&S::increment
);
47 static void TestFunctionObject() {
48 auto incrementor
= Incrementor();
49 mozilla::FunctionRef
<int(int)> f(incrementor
);
53 static void TestLambda() {
54 // Test non-capturing lambda
55 auto lambda1
= [](int arg
) { return arg
+ 1; };
56 mozilla::FunctionRef
<int(int)> f(lambda1
);
59 // Test capturing lambda
61 auto lambda2
= [one
](int arg
) { return arg
+ one
; };
62 mozilla::FunctionRef
<int(int)> g(lambda2
);
65 mozilla::FunctionRef
<int(int)> h([](int arg
) { return arg
+ 1; });
69 static void TestOperatorBool() {
70 mozilla::FunctionRef
<int(int)> f1
;
71 CHECK(!static_cast<bool>(f1
));
73 mozilla::FunctionRef
<int(int)> f2
= increment
;
74 CHECK(static_cast<bool>(f2
));
76 mozilla::FunctionRef
<int(int)> f3
= nullptr;
77 CHECK(!static_cast<bool>(f3
));
80 static void TestReferenceParameters() {
81 mozilla::FunctionRef
<int(const int&, const int&)> f
= &addConstRefs
;
87 static void TestVoidNoParameters() {
88 mozilla::FunctionRef
<void()> f
= &helloWorld
;
89 CHECK(!helloWorldCalled
);
91 CHECK(helloWorldCalled
);
94 static void TestPointerParameters() {
95 mozilla::FunctionRef
<void(int*)> f
= &incrementPointer
;
101 static void TestImplicitFunctorTypeConversion() {
102 auto incrementor
= Incrementor();
103 mozilla::FunctionRef
<long(short)> f
= incrementor
;
108 static void TestImplicitLambdaTypeConversion() {
109 mozilla::FunctionRef
<long(short)> f
= [](short arg
) { return arg
+ 1; };
114 static void TestImplicitFunctionPointerTypeConversion() {
115 mozilla::FunctionRef
<long(short)> f
= &increment
;
120 static void TestMoveOnlyArguments() {
121 mozilla::FunctionRef
<int(mozilla::UniquePtr
<int>)> f(&incrementUnique
);
123 CHECK(f(mozilla::MakeUnique
<int>(5)) == 6);
127 TestNonmemberFunction();
128 TestStaticMemberFunction();
129 TestFunctionObject();
132 TestReferenceParameters();
133 TestPointerParameters();
134 TestVoidNoParameters();
135 TestImplicitFunctorTypeConversion();
136 TestImplicitLambdaTypeConversion();
137 TestImplicitFunctionPointerTypeConversion();
138 TestMoveOnlyArguments();
140 printf("TestFunctionRef OK!\n");