fixing bug in tests: not accounting for copy elision

This commit is contained in:
Andrzej Krzemienski
2015-01-16 14:18:44 +01:00
parent 67c7e21b4b
commit eed1b6ea33
3 changed files with 79 additions and 82 deletions

View File

@ -1,4 +1,5 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// Copyright (C) 2015 Andrzej Krzemienski.
// //
// Use, modification, and distribution is subject to the Boost Software // Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
@ -8,53 +9,45 @@
// //
// You are welcome to contact the author at: // You are welcome to contact the author at:
// fernando_cacciola@hotmail.com // fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string> #include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional/optional.hpp" #include "boost/optional/optional.hpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/utility/in_place_factory.hpp"
#endif
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A #include "boost/utility/in_place_factory.hpp"
{ #include "boost/utility/typed_in_place_factory.hpp"
A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}
friend bool operator == ( A const& x, A const& y )
{ return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }
double m_a0 ;
std::string m_a1 ;
} ;
int test_main( int, char* [] )
{
int invalid_extra_parameter ;
boost::optional<A> opt2 ( boost::in_place(3.14,"pi",invalid_extra_parameter) ) ;
return 0;
}
#else
int test_main( int, char* [] )
{
int invalid_extra_parameter ;
boost::optional<A> opt2 ( A(3.14,"pi",invalid_extra_parameter) ) ;
return 0;
}
#endif #endif
#include "boost/core/lightweight_test.hpp"
#include "boost/none.hpp"
struct Guard
{
double num;
std::string str;
Guard() : num() {}
Guard(double num_, std::string str_) : num(num_), str(str_) {}
friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; }
friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); }
private:
Guard(const Guard&);
Guard& operator=(const Guard&);
};
int main()
{
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
int excessive_param = 2;
boost::optional<Guard> og1 ( boost::in_place(1.0, "one", excessive_param) );
#else
NOTHING_TO_TEST_SO_JUST_FAIL
#endif
return 0;
}

View File

@ -1,4 +1,5 @@
// Copyright (C) 2003, Fernando Luis Cacciola Carballal. // Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// Copyright (C) 2015 Andrzej Krzemienski.
// //
// Use, modification, and distribution is subject to the Boost Software // Use, modification, and distribution is subject to the Boost Software
// License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
@ -8,55 +9,45 @@
// //
// You are welcome to contact the author at: // You are welcome to contact the author at:
// fernando_cacciola@hotmail.com // fernando_cacciola@hotmail.com
//
#include<iostream>
#include<stdexcept>
#include<string> #include<string>
#define BOOST_ENABLE_ASSERT_HANDLER
#include "boost/optional/optional.hpp" #include "boost/optional/optional.hpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/utility/typed_in_place_factory.hpp"
#endif
#ifdef __BORLANDC__ #ifdef __BORLANDC__
#pragma hdrstop #pragma hdrstop
#endif #endif
#include "boost/test/minimal.hpp"
#include "optional_test_common.cpp"
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT #ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
struct A #include "boost/utility/in_place_factory.hpp"
{ #include "boost/utility/typed_in_place_factory.hpp"
A ( double a0, std::string a1 ) : m_a0(a0), m_a1(a1) {}
friend bool operator == ( A const& x, A const& y )
{ return x.m_a0 == y.m_a0 && x.m_a1 == y.m_a1 ; }
double m_a0 ;
std::string m_a1 ;
} ;
int test_main( int, char* [] )
{
// This must fail to compile.
// The first template argument to in_place<> is the target-type,
// not the first constructor parameter type.
boost::optional<A> opt2 ( boost::in_place<int>(3.14,"pi") ) ;
return 0;
}
#else
int test_main( int, char* [] )
{
boost::optional<A> opt2 ( int(3.14) ) ;
return 0;
}
#endif #endif
#include "boost/core/lightweight_test.hpp"
#include "boost/none.hpp"
struct Guard
{
double num;
std::string str;
Guard() : num() {}
Guard(double num_, std::string str_) : num(num_), str(str_) {}
friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; }
friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); }
private:
Guard(const Guard&);
Guard& operator=(const Guard&);
};
int main()
{
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
typedef int BAD_TARGET_TYPE;
boost::optional<Guard> og1 ( boost::in_place<BAD_TARGET_TYPE>(1.0, "one") );
#else
NOTHING_TO_TEST_SO_JUST_FAIL
#endif
return 0;
}

View File

@ -46,9 +46,22 @@ int counting_oracle::copy_ctor_count = 0;
int counting_oracle::copy_assign_count = 0; int counting_oracle::copy_assign_count = 0;
int counting_oracle::equals_count = 0; int counting_oracle::equals_count = 0;
int count_copy_ctors_on_copy() // checks if we have copy elision
{
counting_oracle::copy_ctor_count = 0;
counting_oracle c(1);
counting_oracle c2(c);
int ans = counting_oracle::copy_ctor_count;
counting_oracle::copy_ctor_count = 0;
counting_oracle::val_ctor_count = 0;
return ans;
}
// Test boost::tie() interoperability. // Test boost::tie() interoperability.
int main() int main()
{ {
int copy_factor = count_copy_ctors_on_copy();
const std::pair<counting_oracle, counting_oracle> pair(1, 2); const std::pair<counting_oracle, counting_oracle> pair(1, 2);
boost::optional<counting_oracle> o1, o2; boost::optional<counting_oracle> o1, o2;
boost::tie(o1, o2) = pair; boost::tie(o1, o2) = pair;
@ -57,7 +70,7 @@ int main()
BOOST_TEST(o2); BOOST_TEST(o2);
BOOST_TEST(*o1 == counting_oracle(1)); BOOST_TEST(*o1 == counting_oracle(1));
BOOST_TEST(*o2 == counting_oracle(2)); BOOST_TEST(*o2 == counting_oracle(2));
BOOST_TEST_EQ(2, counting_oracle::copy_ctor_count); BOOST_TEST_EQ(2 * copy_factor, counting_oracle::copy_ctor_count);
BOOST_TEST_EQ(0, counting_oracle::copy_assign_count); BOOST_TEST_EQ(0, counting_oracle::copy_assign_count);
BOOST_TEST_EQ(0, counting_oracle::default_ctor_count); BOOST_TEST_EQ(0, counting_oracle::default_ctor_count);