From aa74eb709f619c56c106262fe2ffabfa9e90f89f Mon Sep 17 00:00:00 2001 From: Simon Brand Date: Wed, 11 Jul 2018 14:32:25 +0100 Subject: [PATCH] Add emplace test --- CMakeLists.txt | 1 + tests/emplace.cpp | 13 +++++++++++++ tl/optional.hpp | 2 ++ 3 files changed, 16 insertions(+) create mode 100644 tests/emplace.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 970a76c..9e28de6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -23,6 +23,7 @@ if(OPTIONAL_ENABLE_TESTS) ${CMAKE_CURRENT_SOURCE_DIR}/tests/relops.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/observers.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/extensions.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/tests/emplace.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/constexpr.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/constructors.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tests/assignment.cpp diff --git a/tests/emplace.cpp b/tests/emplace.cpp new file mode 100644 index 0000000..0a87983 --- /dev/null +++ b/tests/emplace.cpp @@ -0,0 +1,13 @@ +#include "catch.hpp" +#include "optional.hpp" +#include +#include + +TEST_CASE("Emplace", "[emplace]") { + tl::optional, std::pair>> i; + i.emplace(std::piecewise_construct, std::make_tuple(0,2), std::make_tuple(3,4)); + REQUIRE(i->first.first == 0); + REQUIRE(i->first.second == 2); + REQUIRE(i->second.first == 3); + REQUIRE(i->second.second == 4); +} diff --git a/tl/optional.hpp b/tl/optional.hpp index 7c12ad5..0615901 100644 --- a/tl/optional.hpp +++ b/tl/optional.hpp @@ -1258,6 +1258,7 @@ public: *this = nullopt; this->construct(std::forward(args)...); + return value(); } /// \group emplace @@ -1269,6 +1270,7 @@ public: emplace(std::initializer_list il, Args &&... args) { *this = nullopt; this->construct(il, std::forward(args)...); + return value(); } /// Swaps this optional with the other.