forked from TartanLlama/expected
Merge swapping
This commit is contained in:
@ -23,6 +23,7 @@ if(EXPECTED_ENABLE_TESTS)
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/issues.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/bases.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/observers.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/swap.cpp
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp)
|
||||
|
||||
add_executable(tests ${TEST_SOURCES})
|
||||
|
@ -26,9 +26,9 @@ conan_basic_setup()
|
||||
def build(self):
|
||||
cmake = self.configure_cmake()
|
||||
cmake.build()
|
||||
|
||||
|
||||
if not tools.cross_building(self.settings):
|
||||
self.run('%s/bin/tests' % self.build_folder)
|
||||
|
||||
def package(self):
|
||||
self.copy('*.hpp', dst='include/tl', src='tl')
|
||||
self.copy('*.hpp', dst='include/tl', src='tl')
|
@ -11,4 +11,4 @@ target_link_libraries(example ${CONAN_LIBS})
|
||||
# enable_testing()
|
||||
# add_test(NAME example
|
||||
# WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/bin
|
||||
# COMMAND example)
|
||||
# COMMAND example)
|
@ -22,4 +22,4 @@ class ExpectedTestConan(ConanFile):
|
||||
def test(self):
|
||||
if not tools.cross_building(self.settings):
|
||||
os.chdir("bin")
|
||||
self.run(".%sexample" % os.sep)
|
||||
self.run(".%sexample" % os.sep)
|
@ -3,7 +3,8 @@
|
||||
tl::expected<int, const char*> maybe_do_something(int i) {
|
||||
if (i < 5) {
|
||||
return 0;
|
||||
} else {
|
||||
}
|
||||
else {
|
||||
return tl::make_unexpected("Uh oh");
|
||||
}
|
||||
}
|
||||
@ -12,4 +13,4 @@ int main(int argc, char** argv) {
|
||||
(void)argv;
|
||||
|
||||
return maybe_do_something(0).value_or(-1);
|
||||
}
|
||||
}
|
@ -110,20 +110,20 @@ TEST_CASE("Issue 43", "[issues.43]") {
|
||||
#if !(__GNUC__ <= 5)
|
||||
#include <memory>
|
||||
|
||||
using MaybeDataPtr = tl::expected<int, std::unique_ptr<int>>;
|
||||
using MaybeDataPtr = tl::expected<int, std::unique_ptr<int>>;
|
||||
|
||||
MaybeDataPtr test(int i) noexcept
|
||||
{
|
||||
return std::move(i);
|
||||
}
|
||||
MaybeDataPtr test(int i) noexcept
|
||||
{
|
||||
return std::move(i);
|
||||
}
|
||||
|
||||
MaybeDataPtr test2(int i) noexcept
|
||||
{
|
||||
return std::move(i);
|
||||
}
|
||||
MaybeDataPtr test2(int i) noexcept
|
||||
{
|
||||
return std::move(i);
|
||||
}
|
||||
|
||||
TEST_CASE("Issue 49", "[issues.49]") {
|
||||
auto m = test(10)
|
||||
.and_then(test2);
|
||||
}
|
||||
#endif
|
||||
#endif
|
101
tests/swap.cpp
Normal file
101
tests/swap.cpp
Normal file
@ -0,0 +1,101 @@
|
||||
#include "catch.hpp"
|
||||
#include "expected.hpp"
|
||||
|
||||
struct no_throw {
|
||||
no_throw(std::string i) : i(i) {}
|
||||
std::string i;
|
||||
};
|
||||
struct canthrow_move {
|
||||
canthrow_move(std::string i) : i(i) {}
|
||||
canthrow_move(canthrow_move const &) = default;
|
||||
canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {}
|
||||
canthrow_move &operator=(canthrow_move &&) = default;
|
||||
std::string i;
|
||||
};
|
||||
|
||||
bool should_throw = false;
|
||||
struct willthrow_move {
|
||||
willthrow_move(std::string i) : i(i) {}
|
||||
willthrow_move(willthrow_move const &) = default;
|
||||
willthrow_move(willthrow_move &&other) : i(other.i) {
|
||||
if (should_throw)
|
||||
throw 0;
|
||||
}
|
||||
willthrow_move &operator=(willthrow_move &&) = default;
|
||||
std::string i;
|
||||
};
|
||||
static_assert(tl::detail::is_swappable<no_throw>::value, "");
|
||||
|
||||
template <class T1, class T2> void swap_test() {
|
||||
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
|
||||
std::string s2 = "zyxwvutsrqponmlkjihgfedcba";
|
||||
|
||||
tl::expected<T1, T2> a{s1};
|
||||
tl::expected<T1, T2> b{s2};
|
||||
swap(a, b);
|
||||
REQUIRE(a->i == s2);
|
||||
REQUIRE(b->i == s1);
|
||||
|
||||
a = s1;
|
||||
b = tl::unexpected<T2>(s2);
|
||||
swap(a, b);
|
||||
REQUIRE(a.error().i == s2);
|
||||
REQUIRE(b->i == s1);
|
||||
|
||||
a = tl::unexpected<T2>(s1);
|
||||
b = s2;
|
||||
swap(a, b);
|
||||
REQUIRE(a->i == s2);
|
||||
REQUIRE(b.error().i == s1);
|
||||
|
||||
a = tl::unexpected<T2>(s1);
|
||||
b = tl::unexpected<T2>(s2);
|
||||
swap(a, b);
|
||||
REQUIRE(a.error().i == s2);
|
||||
REQUIRE(b.error().i == s1);
|
||||
|
||||
a = s1;
|
||||
b = s2;
|
||||
a.swap(b);
|
||||
REQUIRE(a->i == s2);
|
||||
REQUIRE(b->i == s1);
|
||||
|
||||
a = s1;
|
||||
b = tl::unexpected<T2>(s2);
|
||||
a.swap(b);
|
||||
REQUIRE(a.error().i == s2);
|
||||
REQUIRE(b->i == s1);
|
||||
|
||||
a = tl::unexpected<T2>(s1);
|
||||
b = s2;
|
||||
a.swap(b);
|
||||
REQUIRE(a->i == s2);
|
||||
REQUIRE(b.error().i == s1);
|
||||
|
||||
a = tl::unexpected<T2>(s1);
|
||||
b = tl::unexpected<T2>(s2);
|
||||
a.swap(b);
|
||||
REQUIRE(a.error().i == s2);
|
||||
REQUIRE(b.error().i == s1);
|
||||
}
|
||||
|
||||
TEST_CASE("swap") {
|
||||
|
||||
swap_test<no_throw, no_throw>();
|
||||
swap_test<no_throw, canthrow_move>();
|
||||
swap_test<canthrow_move, no_throw>();
|
||||
|
||||
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
|
||||
std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx";
|
||||
tl::expected<no_throw, willthrow_move> a{s1};
|
||||
tl::expected<no_throw, willthrow_move> b{tl::unexpect, s2};
|
||||
should_throw = 1;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
//this seems to break catch on GCC and Clang
|
||||
REQUIRE_THROWS(swap(a, b));
|
||||
#endif
|
||||
|
||||
REQUIRE(a->i == s1);
|
||||
REQUIRE(b.error().i == s2);
|
||||
}
|
32
tests/test.cpp
Normal file
32
tests/test.cpp
Normal file
@ -0,0 +1,32 @@
|
||||
struct no_throw {
|
||||
no_throw(std::string i) : i(i) {}
|
||||
std::string i;
|
||||
};
|
||||
struct canthrow_move {
|
||||
canthrow_move(std::string i) : i(i) {}
|
||||
canthrow_move(canthrow_move const &) = default;
|
||||
canthrow_move(canthrow_move &&other) noexcept(false) : i(other.i) {}
|
||||
canthrow_move &operator=(canthrow_move &&) = default;
|
||||
std::string i;
|
||||
};
|
||||
|
||||
bool should_throw = false;
|
||||
struct willthrow_move {
|
||||
willthrow_move(std::string i) : i(i) {}
|
||||
willthrow_move(willthrow_move const &) = default;
|
||||
willthrow_move(willthrow_move &&other) : i(other.i) {
|
||||
if (should_throw)
|
||||
throw 0;
|
||||
}
|
||||
willthrow_move &operator=(willthrow_move &&) = default;
|
||||
std::string i;
|
||||
};
|
||||
|
||||
int main() {
|
||||
std::string s1 = "abcdefghijklmnopqrstuvwxyz";
|
||||
std::string s2 = "zyxwvutsrqponmlkjihgfedcbaxxx";
|
||||
tl::expected<no_throw, willthrow_move> a{s1};
|
||||
tl::expected<no_throw, willthrow_move> b{tl::unexpect, s2};
|
||||
should_throw = 1;
|
||||
swap(a, b);
|
||||
}
|
4711
tl/expected.hpp
4711
tl/expected.hpp
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user