Download src/3rdParty/FastSignals/tests/libfastsignals_unit_tests/bind_weak_tests.cpp from AbdulElahGwaith/FreeCAD: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/AbdulElahGwaith/FreeCAD/resolve/main/src/3rdParty/FastSignals/tests/libfastsignals_unit_tests/bind_weak_tests.cpp
- Command line
-
hf download hf://AbdulElahGwaith/FreeCAD/src/3rdParty/FastSignals/tests/libfastsignals_unit_tests/bind_weak_tests.cpp
-
curl -L -o bind_weak_tests.cpp https://huggingface.co/AbdulElahGwaith/FreeCAD/resolve/main/src/3rdParty/FastSignals/tests/libfastsignals_unit_tests/bind_weak_tests.cpp
3.1 kB
| 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); | |
| } | |