offa пре 7 година
родитељ
комит
eadc953f4f
2 измењених фајлова са 26 додато и 11 уклоњено
  1. +0
    -1
      include/unique_resource.h
  2. +26
    -10
      test/UniqueResourceTest.cpp

+ 0
- 1
include/unique_resource.h Прегледај датотеку

} }
catch( ... ) catch( ... )
{ {

other.get_deleter()(other.m_resource); other.get_deleter()(other.m_resource);
other.release(); other.release();
throw; throw;

+ 26
- 10
test/UniqueResourceTest.cpp Прегледај датотеку

{ {
ThrowOnCopyMock() { } ThrowOnCopyMock() { }


ThrowOnCopyMock(const ThrowOnCopyMock&)
ThrowOnCopyMock(const ThrowOnCopyMock&) noexcept(false)
{ {
throw std::exception{}; throw std::exception{};
} }


ThrowOnCopyMock(ThrowOnCopyMock&&) = delete;

MAKE_CONST_MOCK1(deleter, void(ThrowOnCopyMock)); MAKE_CONST_MOCK1(deleter, void(ThrowOnCopyMock));


ThrowOnCopyMock& operator=(const ThrowOnCopyMock&)
ThrowOnCopyMock& operator=(const ThrowOnCopyMock&) noexcept(false)
{ {
throw std::exception{}; throw std::exception{};
} }

ThrowOnCopyMock& operator=(ThrowOnCopyMock&&) = delete;

}; };


struct NotNothrowMoveMock struct NotNothrowMoveMock
{ {
NotNothrowMoveMock(CallMock* mo) : m_mock(mo) { } NotNothrowMoveMock(CallMock* mo) : m_mock(mo) { }
NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock) { }

NotNothrowMoveMock(const NotNothrowMoveMock& other) : m_mock(other.m_mock)
{
throw std::exception{};
}

NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { } NotNothrowMoveMock(NotNothrowMoveMock&& other) noexcept(false) : m_mock(other.m_mock) { }


void operator()(Handle h) const void operator()(Handle h) const
} }


CallMock* m_mock; CallMock* m_mock;

}; };


struct ConditialThrowOnCopyMock struct ConditialThrowOnCopyMock
bool m_shouldThrow; bool m_shouldThrow;
}; };


struct CopyMock
{
CopyMock() {}
CopyMock(const CopyMock&) { }
};




CallMock m; CallMock m;




TEST_CASE("move-construction with copy", "[UniqueResource]") TEST_CASE("move-construction with copy", "[UniqueResource]")
{ {
CallMock mock;
REQUIRE_CALL(mock, deleter(3));
const NotNothrowMoveMock notNothrow{&mock};
Handle h{3};
sr::unique_resource<Handle, decltype(notNothrow)> movedFrom{h, notNothrow};
REQUIRE_CALL(m, deleter(7));
auto d = [](auto) { deleter(7); };
const CopyMock copyMock;
sr::unique_resource<CopyMock, decltype(d)> movedFrom{copyMock, d};
auto guard = std::move(movedFrom); auto guard = std::move(movedFrom);
CHECK(guard.get() == 3);
static_cast<void>(guard);
} }


TEST_CASE("move assignment calls deleter", "[UniqueResource]") TEST_CASE("move assignment calls deleter", "[UniqueResource]")

Loading…
Откажи
Сачувај