mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 02:44:10 +00:00
Added value_init_workaround_test, reviewed by Fernando Cacciola, see #3869
[SVN r63014]
This commit is contained in:
parent
a991936c96
commit
9da96d9737
@ -32,6 +32,7 @@ test-suite utility
|
|||||||
[ compile result_of_test.cpp ]
|
[ compile result_of_test.cpp ]
|
||||||
[ run ../shared_iterator_test.cpp ]
|
[ run ../shared_iterator_test.cpp ]
|
||||||
[ run ../value_init_test.cpp ]
|
[ run ../value_init_test.cpp ]
|
||||||
|
[ run ../value_init_workaround_test.cpp ]
|
||||||
[ run ../initialized_test.cpp ]
|
[ run ../initialized_test.cpp ]
|
||||||
[ compile-fail ../value_init_test_fail1.cpp ]
|
[ compile-fail ../value_init_test_fail1.cpp ]
|
||||||
[ compile-fail ../value_init_test_fail2.cpp ]
|
[ compile-fail ../value_init_test_fail2.cpp ]
|
||||||
|
119
value_init_workaround_test.cpp
Normal file
119
value_init_workaround_test.cpp
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
// Copyright 2010, Niels Dekker.
|
||||||
|
//
|
||||||
|
// 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 the boost::value_initialized<T> workaround.
|
||||||
|
//
|
||||||
|
// 30 May 2010 (Created) Niels Dekker
|
||||||
|
|
||||||
|
// Switch the workaround off, before inluding "value_init.hpp".
|
||||||
|
#define BOOST_DETAIL_VALUE_INIT_WORKAROUND 0
|
||||||
|
#include <boost/utility/value_init.hpp>
|
||||||
|
|
||||||
|
#include <iostream> // For cout.
|
||||||
|
#include <cstdlib> // For EXIT_SUCCESS and EXIT_FAILURE.
|
||||||
|
|
||||||
|
namespace
|
||||||
|
{
|
||||||
|
struct empty_struct
|
||||||
|
{
|
||||||
|
};
|
||||||
|
|
||||||
|
// A POD aggregate struct derived from an empty struct.
|
||||||
|
// Similar to struct Foo1 from Microsoft Visual C++ bug report 484295,
|
||||||
|
// "VC++ does not value-initialize members of derived classes without
|
||||||
|
// user-declared constructor", reported in 2009 by Sylvester Hesp:
|
||||||
|
// https://connect.microsoft.com/VisualStudio/feedback/details/484295
|
||||||
|
struct derived_struct: empty_struct
|
||||||
|
{
|
||||||
|
int data;
|
||||||
|
};
|
||||||
|
|
||||||
|
bool is_value_initialized(const derived_struct& arg)
|
||||||
|
{
|
||||||
|
return arg.data == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
class virtual_destructor_holder
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int i;
|
||||||
|
virtual ~virtual_destructor_holder()
|
||||||
|
{
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
bool is_value_initialized(const virtual_destructor_holder& arg)
|
||||||
|
{
|
||||||
|
return arg.i == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool is_value_initialized(const T(& arg)[2])
|
||||||
|
{
|
||||||
|
return
|
||||||
|
is_value_initialized(arg[0]) &&
|
||||||
|
is_value_initialized(arg[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
bool is_value_initialized(const boost::value_initialized<T>& arg)
|
||||||
|
{
|
||||||
|
return is_value_initialized(arg.data());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Returns zero when the specified object is value-initializated, and one otherwise.
|
||||||
|
// Prints a message to standard output if the value-initialization has failed.
|
||||||
|
template <class T>
|
||||||
|
unsigned failed_to_value_initialized(const T& object, const char *const object_name)
|
||||||
|
{
|
||||||
|
if ( is_value_initialized(object) )
|
||||||
|
{
|
||||||
|
return 0u;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
std::cout << "Note: Failed to value-initialize " << object_name << '.' << std::endl;
|
||||||
|
return 1u;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A macro that passed both the name and the value of the specified object to
|
||||||
|
// the function above here.
|
||||||
|
#define FAILED_TO_VALUE_INITIALIZE(value) failed_to_value_initialized(value, #value)
|
||||||
|
|
||||||
|
// Equivalent to the dirty_stack() function from GCC Bug 33916,
|
||||||
|
// "Default constructor fails to initialize array members", reported in 2007 by
|
||||||
|
// Michael Elizabeth Chastain: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=33916
|
||||||
|
void dirty_stack()
|
||||||
|
{
|
||||||
|
unsigned char array_on_stack[4096];
|
||||||
|
for (unsigned i = 0; i < sizeof(array_on_stack); ++i)
|
||||||
|
{
|
||||||
|
array_on_stack[i] = 0x11;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
dirty_stack();
|
||||||
|
|
||||||
|
// TODO More types may be added later.
|
||||||
|
const unsigned num_failures =
|
||||||
|
FAILED_TO_VALUE_INITIALIZE(boost::value_initialized<derived_struct>()) +
|
||||||
|
FAILED_TO_VALUE_INITIALIZE(boost::value_initialized<virtual_destructor_holder[2]>());
|
||||||
|
|
||||||
|
#ifdef BOOST_DETAIL_VALUE_INIT_WORKAROUND_SUGGESTED
|
||||||
|
// One or more failures are expected.
|
||||||
|
return num_failures > 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
#else
|
||||||
|
// No failures are expected.
|
||||||
|
return num_failures == 0 ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||||
|
#endif
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user