File size: 3,099 Bytes
985c397 | 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 | #include "catch2/catch.hpp"
#include <fastsignals/bind_weak.h>
using namespace fastsignals;
namespace
{
class Testbed
{
public:
Testbed(unsigned& counter)
: m_pCounter(&counter)
{
}
void IncrementNonConst()
{
++(*m_pCounter);
}
void IncrementsConst() const
{
++(*m_pCounter);
}
int ReflectInt(int value) const
{
return value;
}
private:
unsigned* m_pCounter = nullptr;
};
} // namespace
TEST_CASE("can bind const methods", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto boundFn = bind_weak(&Testbed::IncrementNonConst, pSharedBed);
REQUIRE(counter == 0u);
boundFn();
REQUIRE(counter == 1u);
boundFn();
REQUIRE(counter == 2u);
pSharedBed = nullptr;
boundFn();
REQUIRE(counter == 2u);
boundFn();
REQUIRE(counter == 2u);
}
TEST_CASE("can bind non const methods", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto boundFn = bind_weak(&Testbed::IncrementsConst, pSharedBed);
REQUIRE(counter == 0u);
boundFn();
REQUIRE(counter == 1u);
boundFn();
REQUIRE(counter == 2u);
pSharedBed = nullptr;
boundFn();
REQUIRE(counter == 2u);
boundFn();
REQUIRE(counter == 2u);
}
TEST_CASE("can bind method with argument value", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto boundFn = bind_weak(&Testbed::ReflectInt, pSharedBed, 42);
REQUIRE(boundFn() == 42);
REQUIRE(boundFn() == 42);
pSharedBed = nullptr;
REQUIRE(boundFn() == 0);
REQUIRE(boundFn() == 0);
}
TEST_CASE("copies value when bind method with argument const reference value", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto makeBoundFn = [&]() {
int value = 15;
const int& valueRef = value;
auto result = bind_weak(&Testbed::ReflectInt, pSharedBed, valueRef);
value = 25;
return result;
};
auto boundFn = makeBoundFn();
REQUIRE(boundFn(42) == 15);
REQUIRE(boundFn(42) == 15);
pSharedBed = nullptr;
REQUIRE(boundFn(42) == 0);
REQUIRE(boundFn(42) == 0);
}
TEST_CASE("copies value when bind method with argument reference value", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto makeBoundFn = [&]() {
int value = 15;
int& valueRef = value;
auto result = bind_weak(&Testbed::ReflectInt, pSharedBed, valueRef);
valueRef = 25;
return result;
};
auto boundFn = makeBoundFn();
REQUIRE(boundFn(42) == 15);
REQUIRE(boundFn(42) == 15);
pSharedBed = nullptr;
REQUIRE(boundFn(42) == 0);
REQUIRE(boundFn(42) == 0);
}
TEST_CASE("can bind method with placeholder", "[bind_weak]")
{
unsigned counter = 0;
auto pSharedBed = std::make_shared<Testbed>(counter);
auto boundFn = bind_weak(&Testbed::ReflectInt, pSharedBed, std::placeholders::_1);
REQUIRE(boundFn(42) == 42);
REQUIRE(boundFn(42) == 42);
pSharedBed = nullptr;
REQUIRE(boundFn(42) == 0);
REQUIRE(boundFn(42) == 0);
}
|