summaryrefslogtreecommitdiff
path: root/libs/optional/test/optional_test_inplace_factory.cpp
blob: 1e852f0a209c3ee0351f1ac71ffdeb6c3888d8bf (plain)
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
// Copyright (C) 2003, Fernando Luis Cacciola Carballal.
// Copyright (C) 2015 Andrzej Krzemienski.
//
// Use, modification, and distribution is subject to 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)
//
// See http://www.boost.org/lib/optional for documentation.
//
// You are welcome to contact the author at:
//  fernando_cacciola@hotmail.com

#include<string>
#include "boost/optional/optional.hpp"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#include "boost/utility/in_place_factory.hpp"
#include "boost/utility/typed_in_place_factory.hpp"
#endif

#include "boost/core/lightweight_test.hpp"
#include "boost/none.hpp"

struct Guard
{
  double num;
  std::string str;
  Guard() : num() {}
  Guard(double num_, std::string str_) : num(num_), str(str_) {}
  
  friend bool operator==(const Guard& lhs, const Guard& rhs) { return lhs.num == rhs.num && lhs.str == rhs.str; }
  friend bool operator!=(const Guard& lhs, const Guard& rhs) { return !(lhs == rhs); }
  
private:
  Guard(const Guard&);
  Guard& operator=(const Guard&);
};

void test_ctor()
{
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
  Guard g0, g1(1.0, "one"), g2(2.0, "two");
  
  boost::optional<Guard> og0 ( boost::in_place() );
  boost::optional<Guard> og1 ( boost::in_place(1.0, "one") );
  boost::optional<Guard> og1_( boost::in_place(1.0, "one") );
  boost::optional<Guard> og2 ( boost::in_place<Guard>(2.0, "two") );
  
  BOOST_TEST(og0);
  BOOST_TEST(og1);
  BOOST_TEST(og1_);
  BOOST_TEST(og2);
  
  BOOST_TEST(*og0  == g0);
  BOOST_TEST(*og1  == g1);
  BOOST_TEST(*og1_ == g1);
  BOOST_TEST(*og2  == g2);
  
  BOOST_TEST(og1_ == og1);
  BOOST_TEST(og1_ != og2);
  BOOST_TEST(og1_ != og0);
#endif 
}

void test_assign()
{
#ifndef BOOST_OPTIONAL_NO_INPLACE_FACTORY_SUPPORT
#ifndef BOOST_OPTIONAL_WEAK_OVERLOAD_RESOLUTION
  Guard g0, g1(1.0, "one"), g2(2.0, "two");
  
  boost::optional<Guard> og0, og1, og1_, og2;
  
  og0  = boost::in_place();
  og1  = boost::in_place(1.0, "one");
  og1_ = boost::in_place(1.0, "one");
  og2  = boost::in_place<Guard>(2.0, "two");
  
  BOOST_TEST(og0);
  BOOST_TEST(og1);
  BOOST_TEST(og1_);
  BOOST_TEST(og2);
  
  BOOST_TEST(*og0  == g0);
  BOOST_TEST(*og1  == g1);
  BOOST_TEST(*og1_ == g1);
  BOOST_TEST(*og2  == g2);
  
  BOOST_TEST(og1_ == og1);
  BOOST_TEST(og1_ != og2);
  BOOST_TEST(og1_ != og0);
#endif
#endif
}

int main()
{
  test_ctor();
  test_assign();
  return boost::report_errors();
}