More fine grained optional ref assign tests

This commit is contained in:
Andrzej Krzemienski
2015-01-07 16:35:23 +01:00
parent f649878d7e
commit cf665bc3f7
7 changed files with 370 additions and 222 deletions

View File

@ -20,6 +20,9 @@ import testing ;
[ run optional_test_conversions_from_U.cpp ] [ run optional_test_conversions_from_U.cpp ]
[ run optional_test_tie.cpp ] [ run optional_test_tie.cpp ]
[ run optional_test_ref.cpp ] [ run optional_test_ref.cpp ]
[ run optional_test_ref_assign_portable_minimum.cpp ]
[ run optional_test_ref_assign_mutable_int.cpp ]
[ run optional_test_ref_assign_const_int.cpp ]
[ run optional_test_ref_portable_minimum.cpp ] [ run optional_test_ref_portable_minimum.cpp ]
[ run optional_test_inplace.cpp ] [ run optional_test_inplace.cpp ]
[ run optional_test_io.cpp ] [ run optional_test_io.cpp ]

View File

@ -0,0 +1,166 @@
// 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
#ifndef BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP
#define BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP
#include "boost/optional/optional.hpp"
#ifdef __BORLANDC__
#pragma hdrstop
#endif
#include "boost/core/addressof.hpp"
#include "testable_classes.hpp"
using boost::optional;
using boost::none;
template <typename T>
void test_copy_assignment_for_const()
{
const typename concrete_type_of<T>::type v(2);
optional<const T&> o;
o = optional<const 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);
}
template <typename T>
void test_copy_assignment_for_noconst_const()
{
typename concrete_type_of<T>::type v(2);
optional<const T&> o;
o = optional<const 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_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 9);
BOOST_TEST_EQ(val(v), 9);
}
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_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 9);
BOOST_TEST_EQ(val(v), 9);
val(*o) = 7;
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 7);
BOOST_TEST_EQ(val(v), 7);
}
template <typename T>
void test_rebinding_assignment_semantics_const()
{
const typename concrete_type_of<T>::type v(2), w(7);
optional<const T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<const T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
}
template <typename T>
void test_rebinding_assignment_semantics_noconst_const()
{
typename concrete_type_of<T>::type v(2), w(7);
optional<const T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<const T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
}
template <typename T>
void test_rebinding_assignment_semantics()
{
typename concrete_type_of<T>::type v(2), w(7);
optional<T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
val(*o) = 8;
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 8);
BOOST_TEST_EQ(val(w), 8);
BOOST_TEST_EQ(val(v), 2);
}
#endif //BOOST_OPTIONAL_TEST_OPTIONAL_REF_ASSIGN_TEST_DEFS_AK_07JAN2015_HPP

View File

@ -0,0 +1,34 @@
// 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"
#include "testable_classes.hpp"
#include "optional_ref_assign_test_defs.hpp"
using boost::optional;
using boost::none;
int main()
{
test_copy_assignment_for_const<int>();
test_copy_assignment_for_noconst_const<int>();
test_rebinding_assignment_semantics_const<int>();
test_rebinding_assignment_semantics_noconst_const<int>();
return boost::report_errors();
}

View File

@ -0,0 +1,31 @@
// 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"
#include "testable_classes.hpp"
#include "optional_ref_assign_test_defs.hpp"
using boost::optional;
using boost::none;
int main()
{
test_copy_assignment_for<int>();
test_rebinding_assignment_semantics<int>();
return boost::report_errors();
}

View File

@ -0,0 +1,44 @@
// 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"
#include "testable_classes.hpp"
#include "optional_ref_assign_test_defs.hpp"
using boost::optional;
using boost::none;
template <typename T>
void test_optional_ref_assignment()
{
test_copy_assignment_for<T>();
test_rebinding_assignment_semantics<T>();
test_copy_assignment_for_const<T>();
test_copy_assignment_for_noconst_const<T>();
test_rebinding_assignment_semantics_const<T>();
test_rebinding_assignment_semantics_noconst_const<T>();
}
int main()
{
test_optional_ref_assignment<ScopeGuard>();
//test_optional_ref_assignment<Abstract>();
return boost::report_errors();
}

View File

@ -18,87 +18,11 @@
#include "boost/core/addressof.hpp" #include "boost/core/addressof.hpp"
#include "boost/core/enable_if.hpp" #include "boost/core/enable_if.hpp"
#include "boost/core/lightweight_test.hpp" #include "boost/core/lightweight_test.hpp"
#include "testable_classes.hpp"
using boost::optional; using boost::optional;
using boost::none; using boost::none;
// testable classes
struct ScopeGuard // no copy/move ctor/assign
{
int val_;
explicit ScopeGuard(int v) : val_(v) {}
int& val() { return val_; }
const int& val() const { return val_; }
private:
ScopeGuard(ScopeGuard const&);
void operator=(ScopeGuard const&);
};
struct Abstract
{
virtual int& val() = 0;
virtual const int& val() const = 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_; }
const int& val() const { return val_; }
};
template <typename T>
struct concrete_type_of
{
typedef T type;
};
template <>
struct concrete_type_of<Abstract>
{
typedef Impl type;
};
template <>
struct concrete_type_of<const Abstract>
{
typedef const Impl type;
};
template <typename T>
struct has_arrow
{
static const bool value = true;
};
template <>
struct has_arrow<int>
{
static const bool value = false;
};
int& val(int& i) { return i; }
int& val(Abstract& a) { return a.val(); }
int& val(ScopeGuard& g) { return g.val(); }
const int& val(const int& i) { return i; }
const int& val(const Abstract& a) { return a.val(); }
const int& val(const ScopeGuard& g) { return g.val(); }
bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); }
bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); }
bool operator<(const Abstract& l, const Abstract& r) { return l.val() < r.val(); }
bool operator<(const ScopeGuard& l, const ScopeGuard& r) { return l.val() < r.val(); }
// end testable classes
template <typename T> template <typename T>
typename boost::enable_if< has_arrow<T> >::type typename boost::enable_if< has_arrow<T> >::type
@ -264,145 +188,6 @@ void test_clearing_the_value()
BOOST_TEST_EQ(val(v), 2); BOOST_TEST_EQ(val(v), 2);
} }
template <typename T>
void test_copy_assignment_for_const()
{
const typename concrete_type_of<T>::type v(2);
optional<const T&> o;
o = optional<const 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);
}
template <typename T>
void test_copy_assignment_for_noconst_const()
{
typename concrete_type_of<T>::type v(2);
optional<const T&> o;
o = optional<const 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_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 9);
BOOST_TEST_EQ(val(v), 9);
}
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_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 9);
BOOST_TEST_EQ(val(v), 9);
val(*o) = 7;
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 7);
BOOST_TEST_EQ(val(v), 7);
}
template <typename T>
void test_rebinding_assignment_semantics_const()
{
const typename concrete_type_of<T>::type v(2), w(7);
optional<const T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<const T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
}
template <typename T>
void test_rebinding_assignment_semantics_noconst_const()
{
typename concrete_type_of<T>::type v(2), w(7);
optional<const T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<const T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
}
template <typename T>
void test_rebinding_assignment_semantics()
{
typename concrete_type_of<T>::type v(2), w(7);
optional<T&> o(v);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) == boost::addressof(v));
BOOST_TEST_EQ(val(*o), val(v));
BOOST_TEST_EQ(val(*o), 2);
o = optional<T&>(w);
BOOST_TEST_EQ(val(v), 2);
BOOST_TEST(o);
BOOST_TEST(boost::addressof(*o) != boost::addressof(v));
BOOST_TEST_NE(val(*o), val(v));
BOOST_TEST_NE(val(*o), 2);
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 7);
val(*o) = 8;
BOOST_TEST(boost::addressof(*o) == boost::addressof(w));
BOOST_TEST_EQ(val(*o), val(w));
BOOST_TEST_EQ(val(*o), 8);
BOOST_TEST_EQ(val(w), 8);
BOOST_TEST_EQ(val(v), 2);
}
template <typename T> template <typename T>
void test_equality() void test_equality()
{ {
@ -596,8 +381,6 @@ void test_optional_ref()
{ {
test_not_containing_value_for<T>(); test_not_containing_value_for<T>();
test_direct_init_for<T>(); test_direct_init_for<T>();
test_copy_assignment_for<T>();
test_rebinding_assignment_semantics<T>();
test_clearing_the_value<T>(); test_clearing_the_value<T>();
test_arrow<T>(); test_arrow<T>();
test_equality<T>(); test_equality<T>();
@ -611,10 +394,6 @@ void test_optional_const_ref()
test_not_containing_value_for<const T>(); test_not_containing_value_for<const T>();
test_direct_init_for_const<T>(); test_direct_init_for_const<T>();
test_direct_init_for_noconst_const<T>(); test_direct_init_for_noconst_const<T>();
test_copy_assignment_for_const<T>();
test_copy_assignment_for_noconst_const<T>();
test_rebinding_assignment_semantics_const<T>();
test_rebinding_assignment_semantics_noconst_const<T>();
test_clearing_the_value<const T>(); test_clearing_the_value<const T>();
test_arrow_const<T>(); test_arrow_const<T>();
test_arrow_noconst_const<T>(); test_arrow_noconst_const<T>();

91
test/testable_classes.hpp Normal file
View File

@ -0,0 +1,91 @@
// 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/libs/optional for documentation.
//
// You are welcome to contact the author at:
// akrzemi1@gmail.com
#ifndef BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP
#define BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP
struct ScopeGuard // no copy/move ctor/assign
{
int val_;
explicit ScopeGuard(int v) : val_(v) {}
int& val() { return val_; }
const int& val() const { return val_; }
private:
ScopeGuard(ScopeGuard const&);
void operator=(ScopeGuard const&);
};
struct Abstract
{
virtual int& val() = 0;
virtual const int& val() const = 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_; }
const int& val() const { return val_; }
};
template <typename T>
struct concrete_type_of
{
typedef T type;
};
template <>
struct concrete_type_of<Abstract>
{
typedef Impl type;
};
template <>
struct concrete_type_of<const Abstract>
{
typedef const Impl type;
};
template <typename T>
struct has_arrow
{
static const bool value = true;
};
template <>
struct has_arrow<int>
{
static const bool value = false;
};
int& val(int& i) { return i; }
int& val(Abstract& a) { return a.val(); }
int& val(ScopeGuard& g) { return g.val(); }
const int& val(const int& i) { return i; }
const int& val(const Abstract& a) { return a.val(); }
const int& val(const ScopeGuard& g) { return g.val(); }
bool operator==(const Abstract& l, const Abstract& r) { return l.val() == r.val(); }
bool operator==(const ScopeGuard& l, const ScopeGuard& r) { return l.val() == r.val(); }
bool operator<(const Abstract& l, const Abstract& r) { return l.val() < r.val(); }
bool operator<(const ScopeGuard& l, const ScopeGuard& r) { return l.val() < r.val(); }
#endif //BOOST_OPTIONAL_TEST_TESTABKE_CLASSES_AK_07JAN2015_HPP