Files

76 lines
1.5 KiB
C++
Raw Permalink Normal View History

2015-01-15 22:46:34 +01:00
// Copyright (C) 2014-2015 Andrzej Krzemienski.
2014-06-06 23:24:43 +02:00
//
// Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
#include "boost/optional/optional.hpp"
#ifdef BOOST_BORLANDC
2014-06-06 23:24:43 +02:00
#pragma hdrstop
#endif
#include <string>
2015-01-15 22:46:34 +01:00
#include "boost/core/lightweight_test.hpp"
2014-06-06 23:24:43 +02:00
#include "boost/none.hpp"
class NonConstructible
{
private:
NonConstructible();
NonConstructible(NonConstructible const&);
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
NonConstructible(NonConstructible &&);
#endif
};
void test_non_constructible()
{
2015-01-15 22:46:34 +01:00
boost::optional<NonConstructible> o;
BOOST_TEST(!o);
BOOST_TEST(o == boost::none);
BOOST_TEST_THROWS(o.value(), boost::bad_optional_access);
2014-06-06 23:24:43 +02:00
}
class Guard
{
public:
explicit Guard(int) {}
private:
Guard();
Guard(Guard const&);
#if (!defined BOOST_NO_CXX11_RVALUE_REFERENCES)
Guard(Guard &&);
#endif
};
void test_guard()
{
2015-01-15 22:46:34 +01:00
boost::optional<Guard> o;
2014-06-06 23:24:43 +02:00
o.emplace(1);
2015-01-15 22:46:34 +01:00
BOOST_TEST(o);
BOOST_TEST(o != boost::none);
2014-06-06 23:24:43 +02:00
}
void test_non_assignable()
{
2015-01-15 22:46:34 +01:00
boost::optional<const std::string> o;
2014-06-06 23:24:43 +02:00
o.emplace("cat");
2015-01-15 22:46:34 +01:00
BOOST_TEST(o);
BOOST_TEST(o != boost::none);
BOOST_TEST_EQ(*o, std::string("cat"));
2014-06-06 23:24:43 +02:00
}
2015-01-15 22:46:34 +01:00
int main()
2014-06-06 23:24:43 +02:00
{
2015-01-15 22:46:34 +01:00
test_non_constructible();
test_guard();
test_non_assignable();
2014-06-06 23:24:43 +02:00
2015-01-15 22:46:34 +01:00
return boost::report_errors();
}