summaryrefslogtreecommitdiffstats
path: root/mfbt/tests/TestFunctionRef.cpp
blob: 0ae1d4f1939d35dcf689e92ce5c9b4ee97630abc (plain)
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
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "mozilla/Assertions.h"
#include "mozilla/FunctionRef.h"
#include "mozilla/UniquePtr.h"

using mozilla::FunctionRef;

#define CHECK(c)                                       \
  do {                                                 \
    bool cond = !!(c);                                 \
    MOZ_RELEASE_ASSERT(cond, "Failed assertion: " #c); \
  } while (false)

int addConstRefs(const int& arg1, const int& arg2) { return arg1 + arg2; }

void incrementPointer(int* arg) { (*arg)++; }

int increment(int arg) { return arg + 1; }

int incrementUnique(mozilla::UniquePtr<int> ptr) { return *ptr + 1; }

static bool helloWorldCalled = false;

void helloWorld() { helloWorldCalled = true; }

struct S {
  static int increment(int arg) { return arg + 1; }
};

struct Incrementor {
  int operator()(int arg) { return arg + 1; }
};

template <typename Fn>
struct Caller;

template <typename Fn, typename... Params>
std::invoke_result_t<Fn, Params...> CallFunctionRef(FunctionRef<Fn> aRef,
                                                    Params... aParams) {
  return aRef(std::forward<Params>(aParams)...);
}

static void TestNonmemberFunction() {
  CHECK(CallFunctionRef<int(int)>(increment, 42) == 43);
}

static void TestStaticMemberFunction() {
  CHECK(CallFunctionRef<int(int)>(&S::increment, 42) == 43);
}

static void TestFunctionObject() {
  auto incrementor = Incrementor();
  CHECK(CallFunctionRef<int(int)>(incrementor, 42) == 43);
}

static void TestFunctionObjectTemporary() {
  CHECK(CallFunctionRef<int(int)>(Incrementor(), 42) == 43);
}

static void TestLambda() {
  // Test non-capturing lambda
  auto lambda1 = [](int arg) { return arg + 1; };
  CHECK(CallFunctionRef<int(int)>(lambda1, 42) == 43);

  // Test capturing lambda
  int one = 1;
  auto lambda2 = [one](int arg) { return arg + one; };
  CHECK(CallFunctionRef<int(int)>(lambda2, 42) == 43);

  CHECK(CallFunctionRef<int(int)>([](int arg) { return arg + 1; }, 42) == 43);
}

static void TestOperatorBool() {
  auto ToBool = [](FunctionRef<int(int)> aRef) {
    return static_cast<bool>(aRef);
  };
  CHECK(!ToBool({}));
  CHECK(ToBool(increment));
  CHECK(!ToBool(nullptr));
}

static void TestReferenceParameters() {
  int x = 1;
  int y = 2;
  CHECK(CallFunctionRef<int(const int&, const int&)>(addConstRefs, x, y) == 3);
}

static void TestVoidNoParameters() {
  CHECK(!helloWorldCalled);
  CallFunctionRef<void()>(helloWorld);
  CHECK(helloWorldCalled);
}

static void TestPointerParameters() {
  int x = 1;
  CallFunctionRef<void(int*)>(incrementPointer, &x);
  CHECK(x == 2);
}

static void TestImplicitFunctorTypeConversion() {
  auto incrementor = Incrementor();
  short x = 1;
  CHECK(CallFunctionRef<long(short)>(incrementor, x) == 2);
}

static void TestImplicitLambdaTypeConversion() {
  short x = 1;
  CHECK(CallFunctionRef<long(short)>([](short arg) { return arg + 1; }, x) ==
        2);
}

static void TestImplicitFunctionPointerTypeConversion() {
  short x = 1;
  CHECK(CallFunctionRef<long(short)>(&increment, x) == 2);
}

static void TestMoveOnlyArguments() {
  CHECK(CallFunctionRef<int(mozilla::UniquePtr<int>)>(
            &incrementUnique, mozilla::MakeUnique<int>(5)) == 6);
}

int main() {
  TestNonmemberFunction();
  TestStaticMemberFunction();
  TestFunctionObject();
  TestFunctionObjectTemporary();
  TestLambda();
  TestOperatorBool();
  TestReferenceParameters();
  TestPointerParameters();
  TestVoidNoParameters();
  TestImplicitFunctorTypeConversion();
  TestImplicitLambdaTypeConversion();
  TestImplicitFunctionPointerTypeConversion();
  TestMoveOnlyArguments();

  printf("TestFunctionRef OK!\n");
  return 0;
}