mirror of
https://github.com/boostorg/core.git
synced 2025-11-28 21:30:09 +01:00
Implement empty_value
This commit is contained in:
76
test/empty_value_test.cpp
Normal file
76
test/empty_value_test.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
/*
|
||||
Copyright 2018 Glen Joseph Fernandes
|
||||
(glenjofe@gmail.com)
|
||||
|
||||
Distributed under the Boost Software License, Version 1.0.
|
||||
(http://www.boost.org/LICENSE_1_0.txt)
|
||||
*/
|
||||
#include <boost/core/empty_value.hpp>
|
||||
#include <boost/core/lightweight_test.hpp>
|
||||
|
||||
struct empty {
|
||||
operator bool() const {
|
||||
return false;
|
||||
}
|
||||
operator bool() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
class type {
|
||||
public:
|
||||
type()
|
||||
: value_(false) { }
|
||||
explicit type(bool value)
|
||||
: value_(value) { }
|
||||
operator bool() const {
|
||||
return value_;
|
||||
}
|
||||
private:
|
||||
bool value_;
|
||||
};
|
||||
|
||||
void test_bool()
|
||||
{
|
||||
const boost::empty_value<bool> v1(boost::empty_init_t(), true);
|
||||
BOOST_TEST(v1.get());
|
||||
boost::empty_value<bool> v2;
|
||||
BOOST_TEST(!v2.get());
|
||||
v2 = v1;
|
||||
BOOST_TEST(v2.get());
|
||||
v2.get() = false;
|
||||
BOOST_TEST(!v2.get());
|
||||
}
|
||||
|
||||
void test_empty()
|
||||
{
|
||||
empty e;
|
||||
const boost::empty_value<empty> v1(boost::empty_init_t(), e);
|
||||
BOOST_TEST(!v1.get());
|
||||
boost::empty_value<empty> v2;
|
||||
BOOST_TEST(v2.get());
|
||||
v2 = v1;
|
||||
BOOST_TEST(v2.get());
|
||||
v2.get() = empty();
|
||||
BOOST_TEST(v2.get());
|
||||
}
|
||||
|
||||
void test_type()
|
||||
{
|
||||
const boost::empty_value<type> v1(boost::empty_init_t(), true);
|
||||
BOOST_TEST(v1.get());
|
||||
boost::empty_value<type> v2;
|
||||
BOOST_TEST(!v2.get());
|
||||
v2 = v1;
|
||||
BOOST_TEST(v2.get());
|
||||
v2.get() = type();
|
||||
BOOST_TEST(!v2.get());
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_bool();
|
||||
test_empty();
|
||||
test_type();
|
||||
return boost::report_errors();
|
||||
}
|
||||
Reference in New Issue
Block a user