mirror of
https://github.com/boostorg/optional.git
synced 2025-07-15 13:26:37 +02:00
unit test improvements
I have split tests for conversions from uptional<U> and from U to optional<T>. I have split the optional refs tests that are expected to pass on all compilers. I started using lightweight_test instead of Boost.Test (now only in some files).
This commit is contained in:
@ -17,8 +17,10 @@ import testing ;
|
||||
{
|
||||
test-suite optional :
|
||||
[ run optional_test.cpp ]
|
||||
[ run optional_test_conversions_from_U.cpp ]
|
||||
[ run optional_test_tie.cpp ]
|
||||
[ run optional_test_ref.cpp ]
|
||||
[ run optional_test_ref_portable_minimum.cpp ]
|
||||
[ run optional_test_inplace.cpp ]
|
||||
[ run optional_test_io.cpp ]
|
||||
[ run optional_test_move.cpp ]
|
||||
|
@ -906,41 +906,6 @@ void test_no_implicit_conversions()
|
||||
test_no_implicit_conversions_impl(p);
|
||||
}
|
||||
|
||||
struct A {} ;
|
||||
void test_conversions1()
|
||||
{
|
||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||
|
||||
#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
|
||||
char c = 20 ;
|
||||
optional<char> opt0(c);
|
||||
optional<int> opt1(opt0);
|
||||
BOOST_CHECK(*opt1 == static_cast<int>(c));
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
|
||||
float f = 21.22f ;
|
||||
double d = f ;
|
||||
optional<float> opt2(f) ;
|
||||
optional<double> opt3 ;
|
||||
opt3 = opt2 ;
|
||||
BOOST_CHECK(*opt3 == d);
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_conversions2()
|
||||
{
|
||||
TRACE( std::endl << BOOST_CURRENT_FUNCTION );
|
||||
|
||||
char c = 20 ;
|
||||
optional<int> opt(c);
|
||||
BOOST_CHECK( get(opt) == static_cast<int>(c));
|
||||
|
||||
float f = 21.22f ;
|
||||
optional<double> opt1;
|
||||
opt1 = f ;
|
||||
BOOST_CHECK(*get(&opt1) == static_cast<double>(f));
|
||||
}
|
||||
|
||||
|
||||
namespace optional_swap_test
|
||||
@ -1307,8 +1272,6 @@ int test_main( int, char* [] )
|
||||
test_with_class_type();
|
||||
test_with_builtin_types();
|
||||
test_no_implicit_conversions();
|
||||
test_conversions1();
|
||||
test_conversions2();
|
||||
test_swap_tweaking();
|
||||
test_custom_addressof_operator();
|
||||
}
|
||||
|
111
test/optional_test_conversions_from_U.cpp
Normal file
111
test/optional_test_conversions_from_U.cpp
Normal file
@ -0,0 +1,111 @@
|
||||
// Copyright (C) 2014 Andrzej Krzemienski.
|
||||
//
|
||||
// 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 __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#include "boost/core/lightweight_test.hpp"
|
||||
|
||||
using boost::optional;
|
||||
|
||||
|
||||
// testing types:
|
||||
// X is convertible to Y
|
||||
// ADeriv is convertible to ABase
|
||||
struct X
|
||||
{
|
||||
int val;
|
||||
explicit X(int v) : val(v) {}
|
||||
};
|
||||
|
||||
struct Y
|
||||
{
|
||||
int yval;
|
||||
Y(X const& x) : yval(x.val) {}
|
||||
friend bool operator==(Y const& l, Y const& r) { return l.yval == r.yval; }
|
||||
};
|
||||
|
||||
struct ABase
|
||||
{
|
||||
int val;
|
||||
explicit ABase(int v) : val(v) {}
|
||||
friend bool operator==(ABase const& l, ABase const& r) { return l.val == r.val; }
|
||||
};
|
||||
|
||||
struct ADeriv : ABase
|
||||
{
|
||||
explicit ADeriv(int v) : ABase(v) {}
|
||||
};
|
||||
|
||||
|
||||
template <typename T, typename U>
|
||||
void test_convert_optional_U_to_optional_T_for()
|
||||
{
|
||||
#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
|
||||
{
|
||||
optional<U> ou(U(8));
|
||||
optional<T> ot1(ou);
|
||||
BOOST_TEST(ot1);
|
||||
BOOST_TEST(*ot1 == T(*ou));
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
|
||||
{
|
||||
optional<U> ou(U(8));
|
||||
optional<T> ot2;
|
||||
ot2 = ou;
|
||||
BOOST_TEST(ot2);
|
||||
BOOST_TEST(*ot2 == T(*ou));
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_convert_optional_U_to_optional_T()
|
||||
{
|
||||
test_convert_optional_U_to_optional_T_for<Y, X>();
|
||||
test_convert_optional_U_to_optional_T_for<ABase, ADeriv>();
|
||||
test_convert_optional_U_to_optional_T_for<long, short>();
|
||||
test_convert_optional_U_to_optional_T_for<double, float>();
|
||||
}
|
||||
|
||||
template <typename T, typename U>
|
||||
void test_convert_U_to_optional_T_for()
|
||||
{
|
||||
U u(8);
|
||||
optional<T> ot1(u);
|
||||
BOOST_TEST(ot1);
|
||||
BOOST_TEST(*ot1 == T(u));
|
||||
|
||||
optional<T> ot2;
|
||||
ot2 = u;
|
||||
BOOST_TEST(ot2);
|
||||
BOOST_TEST(*ot2 == T(u));
|
||||
}
|
||||
|
||||
void test_convert_U_to_optional_T()
|
||||
{
|
||||
test_convert_U_to_optional_T_for<Y, X>();
|
||||
test_convert_U_to_optional_T_for<ABase, ADeriv>();
|
||||
test_convert_U_to_optional_T_for<long, short>();
|
||||
test_convert_U_to_optional_T_for<double, float>();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_convert_optional_U_to_optional_T();
|
||||
test_convert_U_to_optional_T();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
@ -8,18 +8,6 @@
|
||||
//
|
||||
// You are welcome to contact the author at:
|
||||
// akrzemi1@gmail.com
|
||||
//
|
||||
// Revisions:
|
||||
//
|
||||
#include<iostream>
|
||||
#include<stdexcept>
|
||||
#include<string>
|
||||
|
||||
#define BOOST_ENABLE_ASSERT_HANDLER
|
||||
|
||||
#include "boost/bind/apply.hpp" // Included just to test proper interaction with boost::apply<> as reported by Daniel Wallin
|
||||
#include "boost/mpl/bool.hpp"
|
||||
#include "boost/mpl/bool_fwd.hpp" // For mpl::true_ and mpl::false_
|
||||
|
||||
#include "boost/optional/optional.hpp"
|
||||
|
||||
@ -27,11 +15,9 @@
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#include "boost/core/lightweight_test.hpp"
|
||||
#include "boost/none.hpp"
|
||||
|
||||
#include "boost/test/minimal.hpp"
|
||||
|
||||
#include "optional_test_common.cpp"
|
||||
|
||||
struct SemiRegular // no operator==
|
||||
{
|
||||
@ -41,28 +27,17 @@ private: void operator!=(SemiRegular const&) const {}
|
||||
|
||||
void test_equal_to_none_of_noncomparable_T()
|
||||
{
|
||||
optional<SemiRegular> i = SemiRegular();
|
||||
optional<SemiRegular> o;
|
||||
boost::optional<SemiRegular> i = SemiRegular();
|
||||
boost::optional<SemiRegular> o;
|
||||
|
||||
BOOST_CHECK(i != boost::none);
|
||||
BOOST_CHECK(boost::none != i);
|
||||
BOOST_CHECK(o == boost::none);
|
||||
BOOST_CHECK(boost::none == o);
|
||||
BOOST_TEST(i != boost::none);
|
||||
BOOST_TEST(boost::none != i);
|
||||
BOOST_TEST(o == boost::none);
|
||||
BOOST_TEST(boost::none == o);
|
||||
}
|
||||
|
||||
int test_main( int, char* [] )
|
||||
int main()
|
||||
{
|
||||
try
|
||||
{
|
||||
test_equal_to_none_of_noncomparable_T();
|
||||
|
||||
}
|
||||
catch ( ... )
|
||||
{
|
||||
BOOST_ERROR("Unexpected Exception caught!");
|
||||
}
|
||||
|
||||
return 0;
|
||||
test_equal_to_none_of_noncomparable_T();
|
||||
return boost::report_errors();
|
||||
}
|
||||
|
||||
|
||||
|
147
test/optional_test_ref_portable_minimum.cpp
Normal file
147
test/optional_test_ref_portable_minimum.cpp
Normal file
@ -0,0 +1,147 @@
|
||||
// Copyright (C) 2014 Andrzej Krzemienski.
|
||||
//
|
||||
// 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 __BORLANDC__
|
||||
#pragma hdrstop
|
||||
#endif
|
||||
|
||||
#include "boost/core/addressof.hpp"
|
||||
#include "boost/core/lightweight_test.hpp"
|
||||
#include "boost/none.hpp"
|
||||
|
||||
using boost::optional;
|
||||
using boost::none;
|
||||
|
||||
// testable classes
|
||||
struct ScopeGuard // no copy/move ctor/assign
|
||||
{
|
||||
int val;
|
||||
explicit ScopeGuard(int v) : val(v) {}
|
||||
|
||||
private:
|
||||
ScopeGuard(ScopeGuard const&);
|
||||
void operator=(ScopeGuard const&);
|
||||
};
|
||||
|
||||
struct Abstract
|
||||
{
|
||||
virtual int& val() = 0;
|
||||
virtual ~Abstract() {}
|
||||
Abstract(){}
|
||||
private:
|
||||
Abstract(Abstract const&);
|
||||
void operator=(Abstract const&);
|
||||
};
|
||||
|
||||
struct Impl : Abstract
|
||||
{
|
||||
int val_;
|
||||
Impl(int v) : val_(v) {}
|
||||
int& val() { return val_; }
|
||||
};
|
||||
|
||||
template <typename T>
|
||||
struct concrete_type_of
|
||||
{
|
||||
typedef T type;
|
||||
};
|
||||
|
||||
template <>
|
||||
struct concrete_type_of<Abstract>
|
||||
{
|
||||
typedef Impl type;
|
||||
};
|
||||
|
||||
int& val(int& i) { return i; }
|
||||
int& val(Abstract& a) { return a.val(); }
|
||||
int& val(ScopeGuard& g) { return g.val; }
|
||||
|
||||
template <typename T>
|
||||
void test_not_containing_value_for()
|
||||
{
|
||||
optional<T&> o1;
|
||||
optional<T&> o2 = none;
|
||||
optional<T&> o3 = o1;
|
||||
|
||||
BOOST_TEST(!o1);
|
||||
BOOST_TEST(!o2);
|
||||
BOOST_TEST(!o3);
|
||||
|
||||
BOOST_TEST(o1 == none);
|
||||
BOOST_TEST(o2 == none);
|
||||
BOOST_TEST(o3 == none);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void test_direct_init_for()
|
||||
{
|
||||
typename concrete_type_of<T>::type v(2);
|
||||
optional<T&> o(v);
|
||||
|
||||
BOOST_TEST(o);
|
||||
BOOST_TEST(o != none);
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
|
||||
val(v) = 9;
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
BOOST_TEST(val(*o) == 9);
|
||||
|
||||
val(*o) = 7;
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
BOOST_TEST(val(*o) == 7);
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void test_copy_assignment_for()
|
||||
{
|
||||
typename concrete_type_of<T>::type v(2);
|
||||
optional<T&> o;
|
||||
o = optional<T&>(v);
|
||||
|
||||
BOOST_TEST(o);
|
||||
BOOST_TEST(o != none);
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
BOOST_TEST(val(*o) == 2);
|
||||
|
||||
val(v) = 9;
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
BOOST_TEST(val(*o) == 9);
|
||||
|
||||
val(*o) = 7;
|
||||
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
|
||||
BOOST_TEST(val(*o) == val(v));
|
||||
BOOST_TEST(val(*o) == 7);
|
||||
}
|
||||
|
||||
|
||||
template <typename T>
|
||||
void test_optional_ref()
|
||||
{
|
||||
test_not_containing_value_for<T>();
|
||||
test_direct_init_for<T>();
|
||||
test_copy_assignment_for<T>();
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
test_optional_ref<int>();
|
||||
test_optional_ref<ScopeGuard>();
|
||||
test_optional_ref<Abstract>();
|
||||
|
||||
return boost::report_errors();
|
||||
}
|
Reference in New Issue
Block a user