| 1 | // (C) 2002, Fernando Luis Cacciola Carballal. |
|---|
| 2 | // |
|---|
| 3 | // Distributed under the Boost Software License, Version 1.0. (See |
|---|
| 4 | // accompanying file LICENSE_1_0.txt or copy at |
|---|
| 5 | // http://www.boost.org/LICENSE_1_0.txt) |
|---|
| 6 | // |
|---|
| 7 | // Test program for "boost/utility/value_init.hpp" |
|---|
| 8 | // |
|---|
| 9 | // Initial: 21 Agu 2002 |
|---|
| 10 | |
|---|
| 11 | #include <iostream> |
|---|
| 12 | #include <string> |
|---|
| 13 | |
|---|
| 14 | #include "boost/utility/value_init.hpp" |
|---|
| 15 | |
|---|
| 16 | #ifdef __BORLANDC__ |
|---|
| 17 | #pragma hdrstop |
|---|
| 18 | #endif |
|---|
| 19 | |
|---|
| 20 | #include "boost/test/minimal.hpp" |
|---|
| 21 | |
|---|
| 22 | // |
|---|
| 23 | // Sample POD type |
|---|
| 24 | // |
|---|
| 25 | struct POD |
|---|
| 26 | { |
|---|
| 27 | POD () : c(0), i(0), f(0) {} |
|---|
| 28 | |
|---|
| 29 | POD ( char c_, int i_, float f_ ) : c(c_), i(i_), f(f_) {} |
|---|
| 30 | |
|---|
| 31 | friend std::ostream& operator << ( std::ostream& os, POD const& pod ) |
|---|
| 32 | { return os << '(' << pod.c << ',' << pod.i << ',' << pod.f << ')' ; } |
|---|
| 33 | |
|---|
| 34 | friend bool operator == ( POD const& lhs, POD const& rhs ) |
|---|
| 35 | { return lhs.f == rhs.f && lhs.c == rhs.c && lhs.i == rhs.i ; } |
|---|
| 36 | |
|---|
| 37 | float f; |
|---|
| 38 | char c; |
|---|
| 39 | int i; |
|---|
| 40 | } ; |
|---|
| 41 | |
|---|
| 42 | // |
|---|
| 43 | // Sample non POD type |
|---|
| 44 | // |
|---|
| 45 | struct NonPODBase |
|---|
| 46 | { |
|---|
| 47 | virtual ~NonPODBase() {} |
|---|
| 48 | } ; |
|---|
| 49 | struct NonPOD : NonPODBase |
|---|
| 50 | { |
|---|
| 51 | NonPOD () : id() {} |
|---|
| 52 | NonPOD ( std::string const& id_) : id(id_) {} |
|---|
| 53 | |
|---|
| 54 | friend std::ostream& operator << ( std::ostream& os, NonPOD const& npod ) |
|---|
| 55 | { return os << '(' << npod.id << ')' ; } |
|---|
| 56 | |
|---|
| 57 | friend bool operator == ( NonPOD const& lhs, NonPOD const& rhs ) |
|---|
| 58 | { return lhs.id == rhs.id ; } |
|---|
| 59 | |
|---|
| 60 | std::string id ; |
|---|
| 61 | } ; |
|---|
| 62 | |
|---|
| 63 | template<class T> |
|---|
| 64 | void test ( T const& y, T const& z ) |
|---|
| 65 | { |
|---|
| 66 | boost::value_initialized<T> x ; |
|---|
| 67 | BOOST_CHECK ( y == x ) ; |
|---|
| 68 | BOOST_CHECK ( y == boost::get(x) ) ; |
|---|
| 69 | static_cast<T&>(x) = z ; |
|---|
| 70 | boost::get(x) = z ; |
|---|
| 71 | BOOST_CHECK ( x == z ) ; |
|---|
| 72 | |
|---|
| 73 | boost::value_initialized<T> const x_c ; |
|---|
| 74 | BOOST_CHECK ( y == x_c ) ; |
|---|
| 75 | BOOST_CHECK ( y == boost::get(x_c) ) ; |
|---|
| 76 | T& x_c_ref = x_c ; |
|---|
| 77 | x_c_ref = z ; |
|---|
| 78 | BOOST_CHECK ( x_c == z ) ; |
|---|
| 79 | |
|---|
| 80 | #if !BOOST_WORKAROUND(BOOST_MSVC, < 1300) |
|---|
| 81 | boost::value_initialized<T const> cx ; |
|---|
| 82 | BOOST_CHECK ( y == cx ) ; |
|---|
| 83 | BOOST_CHECK ( y == boost::get(cx) ) ; |
|---|
| 84 | |
|---|
| 85 | boost::value_initialized<T const> const cx_c ; |
|---|
| 86 | BOOST_CHECK ( y == cx_c ) ; |
|---|
| 87 | BOOST_CHECK ( y == boost::get(cx_c) ) ; |
|---|
| 88 | #endif |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | int test_main(int, char **) |
|---|
| 92 | { |
|---|
| 93 | test( 0,1234 ) ; |
|---|
| 94 | test( 0.0,12.34 ) ; |
|---|
| 95 | test( POD(0,0,0.0), POD('a',1234,56.78) ) ; |
|---|
| 96 | test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ; |
|---|
| 97 | |
|---|
| 98 | return 0; |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | |
|---|
| 102 | unsigned int expected_failures = 0; |
|---|
| 103 | |
|---|
| 104 | |
|---|
| 105 | |
|---|
| 106 | |
|---|
| 107 | |
|---|