summaryrefslogtreecommitdiff
path: root/libs/optional/test/optional_test_conversions_from_U.cpp
blob: 57d821577b1eb13d86608c594423c7f0f17aaabd (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
105
106
107
108
109
110
111
// Copyright (C) 2014 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: akrzemi1@gmail.com


#include "boost/optional/optional.hpp"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#include "boost/core/lightweight_test.hpp"

using boost::optional;


// testing types:
// X is convertible to Y
// ADeriv is convertible to ABase
struct X
{
  int val;
  explicit X(int v) : val(v) {}
};

struct Y
{
  int yval;
  Y(X const& x) : yval(x.val) {}
  friend bool operator==(Y const& l, Y const& r) { return l.yval == r.yval; }
};

struct ABase
{
  int val;
  explicit ABase(int v) : val(v) {}
  friend bool operator==(ABase const& l, ABase const& r) { return l.val == r.val; }
};

struct ADeriv : ABase
{
    explicit ADeriv(int v) : ABase(v) {}
};


template <typename T, typename U>
void test_convert_optional_U_to_optional_T_for()
{
#ifndef BOOST_OPTIONAL_NO_CONVERTING_COPY_CTOR
  {
    optional<U> ou(U(8));
    optional<T> ot1(ou);
    BOOST_TEST(ot1);
    BOOST_TEST(*ot1 == T(*ou));
  }
#endif

#ifndef BOOST_OPTIONAL_NO_CONVERTING_ASSIGNMENT
  {
    optional<U> ou(U(8));
    optional<T> ot2;
    ot2 = ou;
    BOOST_TEST(ot2);
    BOOST_TEST(*ot2 == T(*ou));
  }
#endif
}

void test_convert_optional_U_to_optional_T()
{
  test_convert_optional_U_to_optional_T_for<Y, X>();
  test_convert_optional_U_to_optional_T_for<ABase, ADeriv>();
  test_convert_optional_U_to_optional_T_for<long, short>();
  test_convert_optional_U_to_optional_T_for<double, float>();
}

template <typename T, typename U>
void test_convert_U_to_optional_T_for()
{
  U u(8);
  optional<T> ot1(u);
  BOOST_TEST(ot1);
  BOOST_TEST(*ot1 == T(u));
  
  optional<T> ot2;
  ot2 = u;
  BOOST_TEST(ot2);
  BOOST_TEST(*ot2 == T(u));
}

void test_convert_U_to_optional_T()
{
  test_convert_U_to_optional_T_for<Y, X>();
  test_convert_U_to_optional_T_for<ABase, ADeriv>();
  test_convert_U_to_optional_T_for<long, short>();
  test_convert_U_to_optional_T_for<double, float>();
}

int main()
{
  test_convert_optional_U_to_optional_T();
  test_convert_U_to_optional_T();
  
  return boost::report_errors();
}