Audacity 3.2.0
CallableTest.cpp
Go to the documentation of this file.
1/* SPDX-License-Identifier: GPL-2.0-or-later */
2/*!********************************************************************
3
4 Audacity: A Digital Audio Editor
5
6 CallableTest.cpp
7
8 Paul Licameli
9
10**********************************************************************/
11#include <catch2/catch.hpp>
12#include "Callable.h"
13#include <variant>
14
15// See also VariantTest for more exercise of OverloadSet
16
17using namespace Callable;
18
19namespace {
20struct X {
21 int member{ 0 };
22
23 X() = default;
24 explicit X(int value) : member{ value } {}
25 explicit X(int value, std::shared_ptr<X>) : member{ value } {}
26};
27
29 static int x;
30 int & operator () (std::monostate) const { return x; }
31};
32
33int TestVisitor::x{};
34
35template<auto T> struct TakesNonTypeParameter {};
36}
37
38TEST_CASE("Compilation")
39{
40 {
41 // Test contexpr-ness of OverloadSet constructor, and MemberInvoker too
42 constexpr auto visitor = OverloadSet{ TestVisitor{}, &X::member },
43 // and copy constructor
44 visitor2{ visitor },
45 // and move constructor
46 visitor3{ OverloadSet{ TestVisitor{}, &X::member } };
47
48 // OverloadSet can be default-constructed, when all the callables can be
49 constexpr auto visitor4 = OverloadSet<TestVisitor>{};
50 }
51
52 {
53 // These function objects are of literal types
54 constexpr auto f1 = UniquePtrFactory<X>::Function;
55 constexpr auto f2 = UniquePtrFactory<X, int>::Function;
56 // How to get multiple signatures
57 constexpr auto f3 = OverloadSet{ f1, f2 };
58 constexpr auto f4 =
60
61 {
62 auto p1 = f1();
63 REQUIRE(p1->member == 0);
64 auto p2 = f2(1);
65 REQUIRE(p2->member == 1);
66
67 // Demonstrate move of argument
68 auto p3 = f4(2, move(p2));
69 REQUIRE(p3->member == 2);
70 REQUIRE(!p2);
71 }
72
73 {
74 auto p1 = f3();
75 REQUIRE(p1->member == 0);
76 auto p2 = f3(1);
77 REQUIRE(p2->member == 1);
78 }
79
80 TakesNonTypeParameter<f1> t1{};
81 TakesNonTypeParameter<f2> t2{};
82 // Doesn't work with f3 in C++17
83 }
84
85 {
86 // These function objects are of literal types
87 constexpr auto f1 = SharedPtrFactory<X>::Function;
88 constexpr auto f2 = SharedPtrFactory<X, int>::Function;
89 // How to get multiple signatures
90 constexpr auto f3 = OverloadSet{ f1, f2 };
91 constexpr auto f4 =
93
94 {
95 auto p1 = f1();
96 REQUIRE(p1->member == 0);
97 auto p2 = f2(1);
98 REQUIRE(p2->member == 1);
99
100 // Demonstrate move of argument
101 auto p3 = f4(2, move(p2));
102 REQUIRE(p3->member == 2);
103 REQUIRE(!p2);
104 }
105
106 {
107 auto p1 = f3();
108 REQUIRE(p1->member == 0);
109 auto p2 = f3(1);
110 REQUIRE(p2->member == 1);
111 }
112
113 TakesNonTypeParameter<f1> t1{};
114 TakesNonTypeParameter<f2> t2{};
115 // Doesn't work with f3 in C++17
116 }
117
118 {
119 // These function objects are of literal types
120 constexpr auto f1 = Constantly<0>::Function;
121 constexpr auto f2 = Constantly<0, int>::Function;
122 // How to get multiple signatures
123 constexpr auto f3 = OverloadSet{ f1, f2 };
124
125 REQUIRE(f1() == 0);
126 REQUIRE(f2(1) == 0);
127
128 REQUIRE(f3() == 0);
129 REQUIRE(f3(1) == 0);
130
131 TakesNonTypeParameter<f1> t1{};
132 TakesNonTypeParameter<f2> t2{};
133 // Doesn't work with f3 in C++17
134 }
135
136 {
137 // These function objects are of literal types
138 constexpr auto f1 = UniqueMaker<X>();
139 constexpr auto f2 = UniqueMaker<X, int>();
140 // How to get multiple signatures
141 constexpr auto f3 = OverloadSet{ f1, f2 };
142 constexpr auto f4 =
143 UniqueMaker<X, int, std::shared_ptr<X>>();
144
145 {
146 auto p1 = f1();
147 REQUIRE(p1->member == 0);
148 auto p2 = f2(1);
149 REQUIRE(p2->member == 1);
150
151 // Demonstrate move of argument
152 auto p3 = f4(2, move(p2));
153 REQUIRE(p3->member == 2);
154 REQUIRE(!p2);
155
156 // Demonstrate how {} can work as an argument
157 auto p4 = f2({});
158 // auto p4 = f1({}); sorry
159 auto p5 = f1(0); // But this works, f1 is still variadic
160 }
161
162 {
163 auto p1 = f3();
164 REQUIRE(p1->member == 0);
165 auto p2 = f3(1);
166 REQUIRE(p2->member == 1);
167 }
168
169 // generat3ed lambdas won't work as template arguments in C++17
170 }
171}
Functions and classes that generate callable objects.
TEST_CASE("Compilation")
Generates functions useable as non-type template parameters.
Definition: Callable.h:113
Generates functions useable as non-type template parameters.
Definition: Callable.h:104
Generates functions useable as non-type template parameters.
Definition: Callable.h:95
int & operator()(std::monostate) const
static int x
X(int value, std::shared_ptr< X >)