Toolbox snapshot
The Reactive C++ Toolbox
Loading...
Searching...
No Matches
Slot.ut.cpp
Go to the documentation of this file.
1// The Reactive C++ Toolbox.
2// Copyright (C) 2013-2019 Swirly Cloud Limited
3// Copyright (C) 2021 Reactive Markets Limited
4//
5// Licensed under the Apache License, Version 2.0 (the "License");
6// you may not use this file except in compliance with the License.
7// You may obtain a copy of the License at
8//
9// http://www.apache.org/licenses/LICENSE-2.0
10//
11// Unless required by applicable law or agreed to in writing, software
12// distributed under the License is distributed on an "AS IS" BASIS,
13// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14// See the License for the specific language governing permissions and
15// limitations under the License.
16
17#include "Slot.hpp"
18
19#include <boost/test/unit_test.hpp>
20
21using namespace std;
22using namespace toolbox;
23
24namespace {
25void foo(int& x)
26{
27 x <<= 1;
28}
29} // namespace
30
31// Test constexpr.
32static_assert(BasicSlot<int>{} == BasicSlot<int>{});
33static_assert(BasicSlot<int>{}.empty());
34static_assert(!BasicSlot<int>{});
35
37
43
45{
46 int x{2};
47 auto cb = bind<foo>();
48 cb(x);
50 cb(x);
52}
53
55{
56 struct Test {
57 void operator()() { x <<= 1; }
58 int x{2};
59 } t;
60
61 auto cb = bind(&t);
62 cb();
63 BOOST_CHECK_EQUAL(t.x, 4);
64 cb();
65 BOOST_CHECK_EQUAL(t.x, 8);
66}
67
69{
70 struct Test {
71 void operator()(int& x) const { x <<= 1; }
72 } t;
73
74 int x{2};
75 auto cb = bind(&t);
76 cb(x);
78 cb(x);
80}
81
83{
84 int x{2};
85 auto fn = [&x]() { x <<= 1; };
86
87 auto cb = bind(&fn);
88 cb();
90 cb();
92}
93
95{
96 int x{2};
97 auto fn = [](int& x) { x <<= 1; };
98
99 auto cb = bind(&fn);
100 cb(x);
102 cb(x);
104}
105
107{
108 struct Test {
109 void foo() { x <<= 1; }
110 void bar(int n) { x += n; }
111 int x{2};
112 } t;
113
114 auto cbfoo = bind<&Test::foo>(&t);
115 cbfoo();
116 BOOST_CHECK_EQUAL(t.x, 4);
117
118 auto cbbar = bind<&Test::bar>(&t);
119 cbbar(3);
120 BOOST_CHECK_EQUAL(t.x, 7);
121}
122
124{
125 struct Test {
126 void get(int& val) const { val = x; }
127 int x{101};
128 } t;
129
130 auto cb = bind<&Test::get>(&t);
131 int x{};
132 cb(x);
134}
135
137{
138 int x{2};
139 auto fn = [&x](int&& y) { x = x + y; };
140
142 cb(3);
144 int&& m = 6;
145 cb.invoke(std::move(m));
146 BOOST_CHECK_EQUAL(x, 11);
147}
148
BOOST_CHECK_EQUAL(v.size(), 10U)
BOOST_AUTO_TEST_CASE(SlotEmptyCase)
Definition Slot.ut.cpp:38
constexpr bool empty() const noexcept
Definition Slot.hpp:49
STL namespace.
constexpr const auto & get(const detail::Struct< detail::Member< TagsT, ValuesT >... > &s, TagT tag={})
Definition Struct.hpp:110
constexpr auto bind() noexcept
Definition Slot.hpp:92
BOOST_CHECK(isnan(stod(""sv, numeric_limits< double >::quiet_NaN())))