瀏覽代碼

Methods implemented.

main
offa 7 年之前
父節點
當前提交
bdb03f8cd7
共有 2 個檔案被更改,包括 58 行新增2 行删除
  1. +10
    -1
      include/unique_resource.h
  2. +48
    -1
      test/UniqueResourceTest.cpp

+ 10
- 1
include/unique_resource.h 查看文件

@@ -40,7 +40,7 @@ namespace sr
&& std::is_copy_assignable<T>::value),
T const &,
T &&>>
constexpr U move_assign_if_noexcept(T&& value) noexcept
constexpr U move_assign_if_noexcept(T& value) noexcept
{
return std::move(value);
}
@@ -121,6 +121,15 @@ namespace sr
}
}

template<class RR>
void reset(RR&& r)
{
reset();

m_resource = move_assign_if_noexcept(r);
m_execute_on_destruction = true;
}

void release()
{
m_execute_on_destruction = false;

+ 48
- 1
test/UniqueResourceTest.cpp 查看文件

@@ -78,6 +78,45 @@ namespace

};

struct ConditialThrowOnCopyMock
{
explicit ConditialThrowOnCopyMock(Handle h, bool shouldThrow) : m_handle(h), m_shouldThrow(shouldThrow)
{
}

ConditialThrowOnCopyMock(const ConditialThrowOnCopyMock& other) : m_handle(other.m_handle), m_shouldThrow(other.m_shouldThrow)
{
if( m_shouldThrow == true )
{
throw std::exception{};
}
}

ConditialThrowOnCopyMock(ConditialThrowOnCopyMock&&) = default;

ConditialThrowOnCopyMock& operator=(const ConditialThrowOnCopyMock& other)
{
if( &other != this )
{
m_handle = other.m_handle;
m_shouldThrow = other.m_shouldThrow;

if( m_shouldThrow == true )
{
throw std::exception{};
}
}

return *this;
}

ConditialThrowOnCopyMock& operator=(ConditialThrowOnCopyMock&&) = default;

Handle m_handle;
bool m_shouldThrow;
};


CallMock m;

void deleter(Handle h)
@@ -161,7 +200,13 @@ TEST_CASE("reset does not call deleter if released", "[UniqueResource]")
guard.reset();
}

// TODO: Reset with new value
TEST_CASE("reset sets new value and calls deleter on previous", "[UniqueResource]")
{
REQUIRE_CALL(m, deleter(3));
REQUIRE_CALL(m, deleter(7));
auto guard = sr::make_unique_resource(Handle{3}, deleter);
guard.reset(Handle{7});
}

TEST_CASE("release disables deleter", "[UniqueResource]")
{
@@ -176,3 +221,5 @@ TEST_CASE("get returns resource", "[UniqueResource]")
auto guard = sr::make_unique_resource(Handle{3}, deleter);
CHECK(guard.get() == 3);
}



Loading…
取消
儲存