Bug 1642744 [wpt PR 23920] - [ScrollTimeline] Update compositor timeline from blink...
[gecko.git] / mfbt / tests / TestFunctionRef.cpp
blob714c6e83401e37f2674c191d16173c793e4b178b
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"
11 #define CHECK(c) \
12 do { \
13 bool cond = !!(c); \
14 MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
15 } while (false)
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; }
29 struct S {
30 static int increment(int arg) { return arg + 1; }
33 struct Incrementor {
34 int operator()(int arg) { return arg + 1; }
37 static void TestNonmemberFunction() {
38 mozilla::FunctionRef<int(int)> f(&increment);
39 CHECK(f(42) == 43);
42 static void TestStaticMemberFunction() {
43 mozilla::FunctionRef<int(int)> f(&S::increment);
44 CHECK(f(42) == 43);
47 static void TestFunctionObject() {
48 auto incrementor = Incrementor();
49 mozilla::FunctionRef<int(int)> f(incrementor);
50 CHECK(f(42) == 43);
53 static void TestLambda() {
54 // Test non-capturing lambda
55 auto lambda1 = [](int arg) { return arg + 1; };
56 mozilla::FunctionRef<int(int)> f(lambda1);
57 CHECK(f(42) == 43);
59 // Test capturing lambda
60 int one = 1;
61 auto lambda2 = [one](int arg) { return arg + one; };
62 mozilla::FunctionRef<int(int)> g(lambda2);
63 CHECK(g(42) == 43);
65 mozilla::FunctionRef<int(int)> h([](int arg) { return arg + 1; });
66 CHECK(h(42) == 43);
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;
82 int x = 1;
83 int y = 2;
84 CHECK(f(x, y) == 3);
87 static void TestVoidNoParameters() {
88 mozilla::FunctionRef<void()> f = &helloWorld;
89 CHECK(!helloWorldCalled);
90 f();
91 CHECK(helloWorldCalled);
94 static void TestPointerParameters() {
95 mozilla::FunctionRef<void(int*)> f = &incrementPointer;
96 int x = 1;
97 f(&x);
98 CHECK(x == 2);
101 static void TestImplicitFunctorTypeConversion() {
102 auto incrementor = Incrementor();
103 mozilla::FunctionRef<long(short)> f = incrementor;
104 short x = 1;
105 CHECK(f(x) == 2);
108 static void TestImplicitLambdaTypeConversion() {
109 mozilla::FunctionRef<long(short)> f = [](short arg) { return arg + 1; };
110 short x = 1;
111 CHECK(f(x) == 2);
114 static void TestImplicitFunctionPointerTypeConversion() {
115 mozilla::FunctionRef<long(short)> f = &increment;
116 short x = 1;
117 CHECK(f(x) == 2);
120 static void TestMoveOnlyArguments() {
121 mozilla::FunctionRef<int(mozilla::UniquePtr<int>)> f(&incrementUnique);
123 CHECK(f(mozilla::MakeUnique<int>(5)) == 6);
126 int main() {
127 TestNonmemberFunction();
128 TestStaticMemberFunction();
129 TestFunctionObject();
130 TestLambda();
131 TestOperatorBool();
132 TestReferenceParameters();
133 TestPointerParameters();
134 TestVoidNoParameters();
135 TestImplicitFunctorTypeConversion();
136 TestImplicitLambdaTypeConversion();
137 TestImplicitFunctionPointerTypeConversion();
138 TestMoveOnlyArguments();
140 printf("TestFunctionRef OK!\n");
141 return 0;