File: value_init_test.cpp

package info (click to toggle)
boost 1.33.1-10
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 100,948 kB
  • ctags: 145,103
  • sloc: cpp: 573,492; xml: 49,055; python: 15,626; ansic: 13,588; sh: 2,099; yacc: 858; makefile: 660; perl: 427; lex: 111; csh: 6
file content (107 lines) | stat: -rw-r--r-- 2,266 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// (C) 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 <iostream>
#include <string>

#include "boost/utility/value_init.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 ;
} ;

template<class T>
void test ( T const& y, T const& z )
{
  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 ) ;

#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
}

int test_main(int, char **)
{
  test( 0,1234 ) ;
  test( 0.0,12.34 ) ;
  test( POD(0,0,0.0), POD('a',1234,56.78) ) ;
  test( NonPOD( std::string() ), NonPOD( std::string("something") ) ) ;

  return 0;
}


unsigned int expected_failures = 0;