mirror of
https://github.com/boostorg/utility.git
synced 2026-01-29 08:02:17 +00:00
https://svn.boost.org/svn/boost/trunk ........ r41613 | hkaiser | 2007-12-02 16:34:52 -0800 (Sun, 02 Dec 2007) | 1 line Wave: One more fix to enable standalone header compilation. ........ r41621 | hkaiser | 2007-12-02 17:16:28 -0800 (Sun, 02 Dec 2007) | 1 line Wave: Updated documentation. ........ r41625 | noel_belcourt | 2007-12-02 18:04:30 -0800 (Sun, 02 Dec 2007) | 4 lines Change macro logic to get <stdarg.h> included on SunOS. ........ r41626 | grafik | 2007-12-02 18:57:49 -0800 (Sun, 02 Dec 2007) | 1 line Work around some Windows CMD.EXE programs that will fail executing a totally empty batch file. ........ r41627 | grafik | 2007-12-02 19:06:22 -0800 (Sun, 02 Dec 2007) | 1 line Work around some Windows CMD.EXE programs that will fail executing a totally empty batch file. ........ r41629 | grafik | 2007-12-02 20:05:39 -0800 (Sun, 02 Dec 2007) | 1 line Bump bjam to 3.1.17 after 3.1.16 release. ........ r41636 | nesotto | 2007-12-03 01:00:23 -0800 (Mon, 03 Dec 2007) | 1 line missing include ........ r41638 | nesotto | 2007-12-03 01:08:02 -0800 (Mon, 03 Dec 2007) | 1 line Ticket #1477 ........ r41639 | vladimir_prus | 2007-12-03 02:39:46 -0800 (Mon, 03 Dec 2007) | 2 lines Fix 64-bit windows msvc detection, again. ........ r41642 | t_schwinger | 2007-12-03 05:25:26 -0800 (Mon, 03 Dec 2007) | 3 lines Strips top-level cv-qualifiers off non-reference types, now. ........ r41644 | nesotto | 2007-12-03 07:16:16 -0800 (Mon, 03 Dec 2007) | 1 line Ticket #1488 ........ r41645 | nesotto | 2007-12-03 07:19:37 -0800 (Mon, 03 Dec 2007) | 1 line Ticket #1467 ........ r41646 | grafik | 2007-12-03 07:48:40 -0800 (Mon, 03 Dec 2007) | 1 line Switch testing to 3.1.16 bjam release. ........ r41647 | niels_dekker | 2007-12-03 10:14:37 -0800 (Mon, 03 Dec 2007) | 1 line Added value_init test for C style array of bytes ........ r41648 | niels_dekker | 2007-12-03 10:20:19 -0800 (Mon, 03 Dec 2007) | 1 line Added missing #include to value_init_test.cpp. (My mistake!) ........ r41649 | jhunold | 2007-12-03 10:47:17 -0800 (Mon, 03 Dec 2007) | 2 lines Silence unused paramter warning in release mode. ........ r41650 | jhunold | 2007-12-03 10:51:26 -0800 (Mon, 03 Dec 2007) | 2 lines Add cosmetic virtual detructors to silence compile warnings. ........ r41651 | t_schwinger | 2007-12-03 11:00:09 -0800 (Mon, 03 Dec 2007) | 3 lines adds explicit failures markup for function_types ........ r41653 | lbourdev | 2007-12-03 11:13:15 -0800 (Mon, 03 Dec 2007) | 3 lines GIL: Typo in documentation. ........ r41663 | noel_belcourt | 2007-12-03 12:50:58 -0800 (Mon, 03 Dec 2007) | 5 lines Changes to support pgi-7.0 on Linux. * pthread requires rt * force IEEE 754 math for slow, but correct, numerics ........ r41667 | niels_dekker | 2007-12-03 13:41:59 -0800 (Mon, 03 Dec 2007) | 2 lines Added value_init test for an value_initialized<T> object allocated on the heap. ........ r41668 | anthonyw | 2007-12-03 14:00:26 -0800 (Mon, 03 Dec 2007) | 1 line check predicate before returning if we time out on a predicated version of timed_wait ........ [SVN r41678]
252 lines
7.7 KiB
C++
252 lines
7.7 KiB
C++
// Copyright 2002, Fernando Luis Cacciola Carballal.
|
|
//
|
|
// Distributed under 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)
|
|
//
|
|
// Test program for "boost/utility/value_init.hpp"
|
|
//
|
|
// Initial: 21 Agu 2002
|
|
|
|
#include <cstring> // For memcmp.
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include "boost/utility/value_init.hpp"
|
|
#include <boost/shared_ptr.hpp>
|
|
|
|
#ifdef __BORLANDC__
|
|
#pragma hdrstop
|
|
#endif
|
|
|
|
#include "boost/test/minimal.hpp"
|
|
|
|
//
|
|
// Sample POD type
|
|
//
|
|
struct POD
|
|
{
|
|
POD () : c(0), i(0), f(0) {}
|
|
|
|
POD ( char c_, int i_, float f_ ) : c(c_), i(i_), f(f_) {}
|
|
|
|
friend std::ostream& operator << ( std::ostream& os, POD const& pod )
|
|
{ return os << '(' << pod.c << ',' << pod.i << ',' << pod.f << ')' ; }
|
|
|
|
friend bool operator == ( POD const& lhs, POD const& rhs )
|
|
{ return lhs.f == rhs.f && lhs.c == rhs.c && lhs.i == rhs.i ; }
|
|
|
|
float f;
|
|
char c;
|
|
int i;
|
|
} ;
|
|
|
|
//
|
|
// Sample non POD type
|
|
//
|
|
struct NonPODBase
|
|
{
|
|
virtual ~NonPODBase() {}
|
|
} ;
|
|
struct NonPOD : NonPODBase
|
|
{
|
|
NonPOD () : id() {}
|
|
NonPOD ( std::string const& id_) : id(id_) {}
|
|
|
|
friend std::ostream& operator << ( std::ostream& os, NonPOD const& npod )
|
|
{ return os << '(' << npod.id << ')' ; }
|
|
|
|
friend bool operator == ( NonPOD const& lhs, NonPOD const& rhs )
|
|
{ return lhs.id == rhs.id ; }
|
|
|
|
std::string id ;
|
|
} ;
|
|
|
|
//
|
|
// Sample aggregate POD struct type
|
|
// Some compilers do not correctly value-initialize such a struct, for example:
|
|
// Borland C++ Report #51854, "Value-initialization: POD struct should be zero-initialized "
|
|
// http://qc.codegear.com/wc/qcmain.aspx?d=51854
|
|
//
|
|
struct AggregatePODStruct
|
|
{
|
|
float f;
|
|
char c;
|
|
int i;
|
|
};
|
|
|
|
bool operator == ( AggregatePODStruct const& lhs, AggregatePODStruct const& rhs )
|
|
{ return lhs.f == rhs.f && lhs.c == rhs.c && lhs.i == rhs.i ; }
|
|
|
|
//
|
|
// An aggregate struct that contains an std::string and an int.
|
|
// Pavel Kuznetsov (MetaCommunications Engineering) used a struct like
|
|
// this to reproduce the Microsoft Visual C++ compiler bug, reported as
|
|
// Feedback ID 100744, "Value-initialization in new-expression"
|
|
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
|
//
|
|
struct StringAndInt
|
|
{
|
|
std::string s;
|
|
int i;
|
|
};
|
|
|
|
bool operator == ( StringAndInt const& lhs, StringAndInt const& rhs )
|
|
{ return lhs.s == rhs.s && lhs.i == rhs.i ; }
|
|
|
|
|
|
//
|
|
// A struct that has an explicit (user defined) destructor.
|
|
// Some compilers do not correctly value-initialize such a struct, for example:
|
|
// Microsoft Visual C++, Feedback ID 100744, "Value-initialization in new-expression"
|
|
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
|
//
|
|
struct StructWithDestructor
|
|
{
|
|
int i;
|
|
~StructWithDestructor() {}
|
|
};
|
|
|
|
bool operator == ( StructWithDestructor const& lhs, StructWithDestructor const& rhs )
|
|
{ return lhs.i == rhs.i ; }
|
|
|
|
|
|
//
|
|
// A struct that has a virtual function.
|
|
// Some compilers do not correctly value-initialize such a struct either, for example:
|
|
// Microsoft Visual C++, Feedback ID 100744, "Value-initialization in new-expression"
|
|
// https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=100744
|
|
//
|
|
struct StructWithVirtualFunction
|
|
{
|
|
int i;
|
|
virtual void VirtualFunction();
|
|
};
|
|
|
|
void StructWithVirtualFunction::VirtualFunction()
|
|
{
|
|
}
|
|
|
|
bool operator == ( StructWithVirtualFunction const& lhs, StructWithVirtualFunction const& rhs )
|
|
{ return lhs.i == rhs.i ; }
|
|
|
|
|
|
//
|
|
// A struct that is derived from an aggregate POD struct.
|
|
// Some compilers do not correctly value-initialize such a struct, for example:
|
|
// GCC Bugzilla Bug 30111, "Value-initialization of POD base class doesn't initialize members",
|
|
// reported by Jonathan Wakely, http://gcc.gnu.org/bugzilla/show_bug.cgi?id=30111
|
|
//
|
|
struct DerivedFromAggregatePODStruct : AggregatePODStruct
|
|
{
|
|
DerivedFromAggregatePODStruct() : AggregatePODStruct() {}
|
|
};
|
|
|
|
//
|
|
// A struct that wraps an aggregate POD struct as data member.
|
|
//
|
|
struct AggregatePODStructWrapper
|
|
{
|
|
AggregatePODStructWrapper() : dataMember() {}
|
|
AggregatePODStruct dataMember;
|
|
};
|
|
|
|
bool operator == ( AggregatePODStructWrapper const& lhs, AggregatePODStructWrapper const& rhs )
|
|
{ return lhs.dataMember == rhs.dataMember ; }
|
|
|
|
typedef unsigned char ArrayOfBytes[256];
|
|
|
|
//
|
|
// This test function tests boost::value_initialized<T> for a specific type T.
|
|
// The first argument (y) is assumed have the value of a value-initialized object.
|
|
// Returns true on success.
|
|
//
|
|
template<class T>
|
|
bool test ( T const& y, T const& z )
|
|
{
|
|
const boost::unit_test::counter_t counter_before_test = boost::minimal_test::errors_counter();
|
|
boost::value_initialized<T> x ;
|
|
BOOST_CHECK ( y == x ) ;
|
|
BOOST_CHECK ( y == boost::get(x) ) ;
|
|
static_cast<T&>(x) = z ;
|
|
boost::get(x) = z ;
|
|
BOOST_CHECK ( x == z ) ;
|
|
|
|
boost::value_initialized<T> const x_c ;
|
|
BOOST_CHECK ( y == x_c ) ;
|
|
BOOST_CHECK ( y == boost::get(x_c) ) ;
|
|
T& x_c_ref = x_c ;
|
|
x_c_ref = z ;
|
|
BOOST_CHECK ( x_c == z ) ;
|
|
|
|
boost::shared_ptr<boost::value_initialized<T> > ptr( new boost::value_initialized<T> );
|
|
BOOST_CHECK ( y == *ptr ) ;
|
|
|
|
#if !BOOST_WORKAROUND(BOOST_MSVC, < 1300)
|
|
boost::value_initialized<T const> cx ;
|
|
BOOST_CHECK ( y == cx ) ;
|
|
BOOST_CHECK ( y == boost::get(cx) ) ;
|
|
|
|
boost::value_initialized<T const> const cx_c ;
|
|
BOOST_CHECK ( y == cx_c ) ;
|
|
BOOST_CHECK ( y == boost::get(cx_c) ) ;
|
|
#endif
|
|
|
|
return boost::minimal_test::errors_counter() == counter_before_test ;
|
|
}
|
|
|
|
int test_main(int, char **)
|
|
{
|
|
BOOST_CHECK ( test( 0,1234 ) ) ;
|
|
BOOST_CHECK ( test( 0.0,12.34 ) ) ;
|
|
BOOST_CHECK ( test( POD(0,0,0.0), POD('a',1234,56.78) ) ) ;
|
|
BOOST_CHECK ( test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ) ;
|
|
|
|
NonPOD NonPOD_object( std::string("NonPOD_object") );
|
|
BOOST_CHECK ( test<NonPOD *>( 0, &NonPOD_object ) ) ;
|
|
|
|
AggregatePODStruct zeroInitializedAggregatePODStruct = { 0.0f, '\0', 0 };
|
|
AggregatePODStruct nonZeroInitializedAggregatePODStruct = { 1.25f, 'a', -1 };
|
|
BOOST_CHECK ( test(zeroInitializedAggregatePODStruct, nonZeroInitializedAggregatePODStruct) );
|
|
|
|
StringAndInt stringAndInt0;
|
|
StringAndInt stringAndInt1;
|
|
stringAndInt0.i = 0;
|
|
stringAndInt1.i = 1;
|
|
stringAndInt1.s = std::string("1");
|
|
BOOST_CHECK ( test(stringAndInt0, stringAndInt1) );
|
|
|
|
StructWithDestructor structWithDestructor0;
|
|
StructWithDestructor structWithDestructor1;
|
|
structWithDestructor0.i = 0;
|
|
structWithDestructor1.i = 1;
|
|
BOOST_CHECK ( test(structWithDestructor0, structWithDestructor1) );
|
|
|
|
StructWithVirtualFunction structWithVirtualFunction0;
|
|
StructWithVirtualFunction structWithVirtualFunction1;
|
|
structWithVirtualFunction0.i = 0;
|
|
structWithVirtualFunction1.i = 1;
|
|
BOOST_CHECK ( test(structWithVirtualFunction0, structWithVirtualFunction1) );
|
|
|
|
DerivedFromAggregatePODStruct derivedFromAggregatePODStruct0;
|
|
DerivedFromAggregatePODStruct derivedFromAggregatePODStruct1;
|
|
static_cast<AggregatePODStruct &>(derivedFromAggregatePODStruct0) = zeroInitializedAggregatePODStruct;
|
|
static_cast<AggregatePODStruct &>(derivedFromAggregatePODStruct1) = nonZeroInitializedAggregatePODStruct;
|
|
BOOST_CHECK ( test(derivedFromAggregatePODStruct0, derivedFromAggregatePODStruct1) );
|
|
|
|
AggregatePODStructWrapper aggregatePODStructWrapper0;
|
|
AggregatePODStructWrapper aggregatePODStructWrapper1;
|
|
aggregatePODStructWrapper0.dataMember = zeroInitializedAggregatePODStruct;
|
|
aggregatePODStructWrapper1.dataMember = nonZeroInitializedAggregatePODStruct;
|
|
BOOST_CHECK ( test(aggregatePODStructWrapper0, aggregatePODStructWrapper1) );
|
|
|
|
ArrayOfBytes zeroInitializedArrayOfBytes = { 0 };
|
|
boost::value_initialized<ArrayOfBytes> valueInitializedArrayOfBytes;
|
|
BOOST_CHECK (std::memcmp(get(valueInitializedArrayOfBytes), zeroInitializedArrayOfBytes, sizeof(ArrayOfBytes)) == 0);
|
|
|
|
return 0;
|
|
}
|
|
|
|
|
|
unsigned int expected_failures = 0;
|