add test optional

This commit is contained in:
bolero-MURAKAMI 2012-10-27 15:37:39 +09:00
parent f9d8908f95
commit 5aa4cf8cbe
5 changed files with 338 additions and 13 deletions

View file

@ -0,0 +1,315 @@
#ifndef SPROUT_LIBS_OPTIONAL_TEST_OPTIONAL_CPP
#define SPROUT_LIBS_OPTIONAL_TEST_OPTIONAL_CPP
#include <sstream>
#include <sprout/optional.hpp>
#include <testspr/tools.hpp>
namespace testspr {
static void optional_test() {
using namespace sprout;
{
SPROUT_STATIC_CONSTEXPR auto opt1 = sprout::optional<int>(1234);
SPROUT_STATIC_CONSTEXPR auto opt2 = sprout::optional<int>();
// constructor
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>();
TESTSPR_BOTH_ASSERT(!opt3);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(1234);
TESTSPR_BOTH_ASSERT(!!opt3);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(sprout::nullopt);
TESTSPR_BOTH_ASSERT(!opt3);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(sprout::nullopt);
TESTSPR_BOTH_ASSERT(!opt3);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(false, 1234);
TESTSPR_BOTH_ASSERT(!opt3);
SPROUT_STATIC_CONSTEXPR auto opt4 = sprout::optional<int>(true, 1234);
TESTSPR_BOTH_ASSERT(!!opt4);
TESTSPR_BOTH_ASSERT(opt4.get() == 1234);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(opt1);
TESTSPR_BOTH_ASSERT(!!opt3);
TESTSPR_BOTH_ASSERT(opt3.get() == 1234);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::optional<int>(sprout::optional<unsigned>(1234));
TESTSPR_BOTH_ASSERT(!!opt3);
TESTSPR_BOTH_ASSERT(opt3.get() == 1234);
}
// operator=
{
auto opt3 = sprout::optional<int>(1234);
opt3 = sprout::nullopt;
TESTSPR_ASSERT(!opt3);
}
{
auto opt3 = sprout::optional<int>();
opt3 = 1234;
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
{
auto opt3 = sprout::optional<int>();
opt3 = opt1;
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
{
auto opt3 = sprout::optional<int>();
opt3 = sprout::optional<unsigned>(1234);
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
// assign
{
auto opt3 = sprout::optional<int>(1234);
opt3.assign(sprout::nullopt);
TESTSPR_ASSERT(!opt3);
}
{
auto opt3 = sprout::optional<int>();
opt3.assign(1234);
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
{
auto opt3 = sprout::optional<int>();
opt3.assign(opt1);
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
{
auto opt3 = sprout::optional<int>();
opt3.assign(sprout::optional<unsigned>(1234));
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
// reset
{
auto opt3 = sprout::optional<int>(1234);
opt3.reset();
TESTSPR_ASSERT(!opt3);
}
{
auto opt3 = sprout::optional<int>(1234);
opt3.reset(sprout::nullopt);
TESTSPR_ASSERT(!opt3);
}
{
auto opt3 = sprout::optional<int>();
opt3.reset(1234);
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
// swap
{
auto opt3 = sprout::optional<int>(1234);
auto opt4 = sprout::optional<int>();
opt3.swap(opt4);
TESTSPR_ASSERT(!opt3);
TESTSPR_ASSERT(!!opt4);
TESTSPR_ASSERT(opt4.get() == 1234);
}
{
auto opt3 = sprout::optional<int>(1234);
auto opt4 = sprout::optional<int>();
swap(opt3, opt4);
TESTSPR_ASSERT(!opt3);
TESTSPR_ASSERT(!!opt4);
TESTSPR_ASSERT(opt4.get() == 1234);
}
// operator*
TESTSPR_BOTH_ASSERT(*opt1 == 1234);
{
auto opt3 = sprout::optional<int>(1234);
TESTSPR_ASSERT(*opt3 == 1234);
*opt3 = 12345;
TESTSPR_ASSERT(*opt3 == 12345);
}
// get
TESTSPR_BOTH_ASSERT(opt1.get() == 1234);
{
auto opt3 = sprout::optional<int>(1234);
TESTSPR_ASSERT(opt3.get() == 1234);
opt3.get() = 12345;
TESTSPR_ASSERT(opt3.get() == 12345);
}
// get_value_or
TESTSPR_BOTH_ASSERT(opt1.get_value_or(12345) == 1234);
TESTSPR_BOTH_ASSERT(opt2.get_value_or(12345) == 12345);
{
auto opt3 = sprout::optional<int>(1234);
int v = 12345;
TESTSPR_ASSERT(opt3.get_value_or(v) == 1234);
}
{
auto opt3 = sprout::optional<int>();
int v = 12345;
TESTSPR_ASSERT(opt3.get_value_or(v) == 12345);
}
// operator->
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::make_optional(testspr::is_odd<int>());
TESTSPR_BOTH_ASSERT(opt3->operator()(123));
}
{
auto opt3 = sprout::make_optional(testspr::is_odd<int>());
TESTSPR_ASSERT(opt3->operator()(123));
}
// get_pointer
TESTSPR_BOTH_ASSERT(*opt1.get_pointer() == 1234);
{
auto opt3 = sprout::optional<int>(1234);
TESTSPR_ASSERT(*opt3.get_pointer() == 1234);
opt3.get() = 12345;
TESTSPR_ASSERT(*opt3.get_pointer() == 12345);
}
// get_ptr
TESTSPR_BOTH_ASSERT(*opt1.get_ptr() == 1234);
{
auto opt3 = sprout::optional<int>(1234);
TESTSPR_ASSERT(*opt3.get_ptr() == 1234);
opt3.get() = 12345;
TESTSPR_ASSERT(*opt3.get_ptr() == 12345);
}
// operator bool
TESTSPR_BOTH_ASSERT(static_cast<bool>(opt1));
TESTSPR_BOTH_ASSERT(!static_cast<bool>(opt2));
// operator!
TESTSPR_BOTH_ASSERT(!!opt1);
TESTSPR_BOTH_ASSERT(!opt2);
// is_initialized
TESTSPR_BOTH_ASSERT(opt1.is_initialized());
TESTSPR_BOTH_ASSERT(!opt2.is_initialized());
// make_optional
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::make_optional(1234);
TESTSPR_BOTH_ASSERT(!!opt3);
TESTSPR_BOTH_ASSERT(opt3.get() == 1234);
}
{
SPROUT_STATIC_CONSTEXPR auto opt3 = sprout::make_optional(false, 1234);
TESTSPR_BOTH_ASSERT(!opt3);
SPROUT_STATIC_CONSTEXPR auto opt4 = sprout::make_optional(true, 1234);
TESTSPR_BOTH_ASSERT(!!opt4);
TESTSPR_BOTH_ASSERT(opt4.get() == 1234);
}
// operator==
TESTSPR_BOTH_ASSERT(!(opt1 == opt2));
TESTSPR_BOTH_ASSERT(opt1 == sprout::optional<int>(1234));
// operator!=
TESTSPR_BOTH_ASSERT(opt1 != opt2);
TESTSPR_BOTH_ASSERT(!(opt1 != sprout::optional<int>(1234)));
// operator<
TESTSPR_BOTH_ASSERT(!(opt1 < opt2));
TESTSPR_BOTH_ASSERT(!(opt1 < sprout::optional<int>()));
TESTSPR_BOTH_ASSERT(!(opt1 < sprout::optional<int>(123)));
TESTSPR_BOTH_ASSERT(!(opt1 < sprout::optional<int>(1234)));
TESTSPR_BOTH_ASSERT(opt1 < sprout::optional<int>(12345));
TESTSPR_BOTH_ASSERT(!(opt2 < sprout::optional<int>()));
TESTSPR_BOTH_ASSERT(opt2 < sprout::optional<int>(123));
TESTSPR_BOTH_ASSERT(opt2 < sprout::optional<int>(1234));
TESTSPR_BOTH_ASSERT(opt2 < sprout::optional<int>(12345));
// operator>
TESTSPR_BOTH_ASSERT(opt1 > opt2);
TESTSPR_BOTH_ASSERT(opt1 > sprout::optional<int>());
TESTSPR_BOTH_ASSERT(opt1 > sprout::optional<int>(123));
TESTSPR_BOTH_ASSERT(!(opt1 > sprout::optional<int>(1234)));
TESTSPR_BOTH_ASSERT(!(opt1 > sprout::optional<int>(12345)));
TESTSPR_BOTH_ASSERT(!(opt2 > sprout::optional<int>()));
TESTSPR_BOTH_ASSERT(!(opt2 > sprout::optional<int>(123)));
TESTSPR_BOTH_ASSERT(!(opt2 > sprout::optional<int>(1234)));
TESTSPR_BOTH_ASSERT(!(opt2 > sprout::optional<int>(12345)));
// operator<=
TESTSPR_BOTH_ASSERT(!(opt1 <= opt2));
TESTSPR_BOTH_ASSERT(!(opt1 <= sprout::optional<int>()));
TESTSPR_BOTH_ASSERT(!(opt1 <= sprout::optional<int>(123)));
TESTSPR_BOTH_ASSERT(opt1 <= sprout::optional<int>(1234));
TESTSPR_BOTH_ASSERT(opt1 <= sprout::optional<int>(12345));
TESTSPR_BOTH_ASSERT(opt2 <= sprout::optional<int>());
TESTSPR_BOTH_ASSERT(opt2 <= sprout::optional<int>(123));
TESTSPR_BOTH_ASSERT(opt2 <= sprout::optional<int>(1234));
TESTSPR_BOTH_ASSERT(opt2 <= sprout::optional<int>(12345));
// operator>=
TESTSPR_BOTH_ASSERT(opt1 >= opt2);
TESTSPR_BOTH_ASSERT(opt1 >= sprout::optional<int>());
TESTSPR_BOTH_ASSERT(opt1 >= sprout::optional<int>(123));
TESTSPR_BOTH_ASSERT(opt1 >= sprout::optional<int>(1234));
TESTSPR_BOTH_ASSERT(!(opt1 >= sprout::optional<int>(12345)));
TESTSPR_BOTH_ASSERT(opt2 >= sprout::optional<int>());
TESTSPR_BOTH_ASSERT(!(opt2 >= sprout::optional<int>(123)));
TESTSPR_BOTH_ASSERT(!(opt2 >= sprout::optional<int>(1234)));
TESTSPR_BOTH_ASSERT(!(opt2 >= sprout::optional<int>(12345)));
// operator<<
{
std::ostringstream os;
os << opt1;
TESTSPR_ASSERT(os.str() == " 1234");
}
{
std::ostringstream os;
os << opt2;
TESTSPR_ASSERT(os.str() == "--");
}
// operator>>
{
std::istringstream is(" 1234");
auto opt3 = sprout::optional<int>();
is >> opt3;
TESTSPR_ASSERT(!!opt3);
TESTSPR_ASSERT(opt3.get() == 1234);
}
{
std::istringstream is("--");
auto opt3 = sprout::optional<int>(1234);
is >> opt3;
TESTSPR_ASSERT(!opt3);
}
}
}
} // namespace testspr
#ifndef TESTSPR_CPP_INCLUDE
# define TESTSPR_TEST_FUNCTION testspr::optional_test
# include <testspr/include_main.hpp>
#endif
#endif // #ifndef SPROUT_LIBS_OPTIONAL_TEST_OPTIONAL_CPP

View file

@ -13,7 +13,7 @@ namespace sprout {
// //
template<typename Elem, typename Traits, typename T> template<typename Elem, typename Traits, typename T>
inline std::basic_istream<Elem, Traits>& inline std::basic_istream<Elem, Traits>&
operator<<(std::basic_istream<Elem, Traits>& lhs, sprout::optional<T>& rhs) { operator>>(std::basic_istream<Elem, Traits>& lhs, sprout::optional<T>& rhs) {
if (lhs.good()) { if (lhs.good()) {
int d = lhs.get(); int d = lhs.get();
if (d == ' ') { if (d == ' ') {

View file

@ -55,10 +55,10 @@ namespace sprout {
: init(v.init) : init(v.init)
, val(v.val) , val(v.val)
{} {}
template<class U> template<typename U>
explicit SPROUT_CONSTEXPR optional(optional<U> const& v) explicit SPROUT_CONSTEXPR optional(optional<U> const& v)
: init(v.init) : init(v.is_initialized())
, val(v.val.get()) , val(v.get())
{} {}
optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT { optional& operator=(sprout::nullopt_t v) SPROUT_NOEXCEPT {
@ -73,7 +73,7 @@ namespace sprout {
assign(v); assign(v);
return *this; return *this;
} }
template<class U> template<typename U>
optional& operator=(optional<U> const& v) { optional& operator=(optional<U> const& v) {
assign(v); assign(v);
return *this; return *this;
@ -84,16 +84,16 @@ namespace sprout {
} }
void assign(argument_type v) { void assign(argument_type v) {
optional temp(v); optional temp(v);
temp.swap(v); temp.swap(*this);
} }
void assign(optional const& v) { void assign(optional const& v) {
optional temp(v); optional temp(v);
temp.swap(v); temp.swap(*this);
} }
template<class U> template<typename U>
void assign(optional<U> const& v) { void assign(optional<U> const& v) {
optional temp(v); optional temp(v);
temp.swap(v); temp.swap(*this);
} }
void reset() SPROUT_NOEXCEPT { void reset() SPROUT_NOEXCEPT {
@ -113,14 +113,20 @@ namespace sprout {
sprout::swap(val, other.val); sprout::swap(val, other.val);
} }
SPROUT_CONSTEXPR reference_const_type operator*() const {
return get();
}
reference_type operator*() {
return get();
}
SPROUT_CONSTEXPR reference_const_type get() const { SPROUT_CONSTEXPR reference_const_type get() const {
return is_initialized() ? val.get() return is_initialized() ? val.get()
: throw std::domain_error("optional: value not initialized") : (throw std::domain_error("optional: value not initialized"), val.get())
; ;
} }
reference_type get() { reference_type get() {
return is_initialized() ? val.get() return is_initialized() ? val.get()
: throw std::domain_error("optional: value not initialized") : (throw std::domain_error("optional: value not initialized"), val.get())
; ;
} }
SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const { SPROUT_CONSTEXPR reference_const_type get_value_or(reference_const_type& v) const {

View file

@ -138,8 +138,10 @@ namespace sprout {
private: private:
holder_type holder_; holder_type holder_;
public: public:
value_holder() = default; SPROUT_CONSTEXPR value_holder()
value_holder(value_holder const&) = default; : holder_()
{}
SPROUT_CONSTEXPR value_holder(value_holder const&) = default;
explicit SPROUT_CONSTEXPR value_holder(param_type p) explicit SPROUT_CONSTEXPR value_holder(param_type p)
: holder_(helper_type::hold(p)) : holder_(helper_type::hold(p))
{} {}

View file

@ -9,6 +9,7 @@
#include "../libs/array/test/array.cpp" #include "../libs/array/test/array.cpp"
#include "../libs/string/test/string.cpp" #include "../libs/string/test/string.cpp"
#include "../libs/bitset/test/bitset.cpp" #include "../libs/bitset/test/bitset.cpp"
#include "../libs/optional/test/optional.cpp"
#include "../libs/algorithm/test/algorithm.cpp" #include "../libs/algorithm/test/algorithm.cpp"
#include "../libs/random/test/random.cpp" #include "../libs/random/test/random.cpp"
@ -21,6 +22,7 @@ namespace testspr {
testspr::array_test(); testspr::array_test();
testspr::string_test(); testspr::string_test();
testspr::bitset_test(); testspr::bitset_test();
testspr::optional_test();
testspr::algorithm_test(); testspr::algorithm_test();
testspr::random_test(); testspr::random_test();
} }