Boost logo

Boost-Commit :

Subject: [Boost-commit] svn:boost r63014 - in trunk/libs/utility: . test
From: nielsdekker_at_[hidden]
Date: 2010-06-16 04:45:44


Author: niels_dekker
Date: 2010-06-16 04:45:43 EDT (Wed, 16 Jun 2010)
New Revision: 63014
URL: http://svn.boost.org/trac/boost/changeset/63014

Log:
Added value_init_workaround_test, reviewed by Fernando Cacciola, see #3869
Added:
   trunk/libs/utility/value_init_workaround_test.cpp (contents, props changed)
Text files modified:
   trunk/libs/utility/test/Jamfile.v2 | 1 +
   1 files changed, 1 insertions(+), 0 deletions(-)

Modified: trunk/libs/utility/test/Jamfile.v2
==============================================================================
--- trunk/libs/utility/test/Jamfile.v2 (original)
+++ trunk/libs/utility/test/Jamfile.v2 2010-06-16 04:45:43 EDT (Wed, 16 Jun 2010)
@@ -32,6 +32,7 @@
         [ compile result_of_test.cpp ]
         [ run ../shared_iterator_test.cpp ]
         [ run ../value_init_test.cpp ]
+ [ run ../value_init_workaround_test.cpp ]
         [ run ../initialized_test.cpp ]
         [ compile-fail ../value_init_test_fail1.cpp ]
         [ compile-fail ../value_init_test_fail2.cpp ]

Added: trunk/libs/utility/value_init_workaround_test.cpp
==============================================================================
--- (empty file)
+++ trunk/libs/utility/value_init_workaround_test.cpp 2010-06-16 04:45:43 EDT (Wed, 16 Jun 2010)
@@ -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
+}


Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk