File: optional_test_make_optional.cpp

package info (click to toggle)
boost1.83 1.83.0-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 545,632 kB
  • sloc: cpp: 3,857,086; xml: 125,552; ansic: 34,414; python: 25,887; asm: 5,276; sh: 4,799; ada: 1,681; makefile: 1,629; perl: 1,212; pascal: 1,139; sql: 810; yacc: 478; ruby: 102; lisp: 24; csh: 6
file content (121 lines) | stat: -rw-r--r-- 3,256 bytes parent folder | download | duplicates (5)
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
112
113
114
115
116
117
118
119
120
121
// Copyright (C) 2017 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 BOOST_BORLANDC
#pragma hdrstop
#endif

#include "boost/core/ignore_unused.hpp"
#include "boost/core/is_same.hpp"
#include "boost/core/lightweight_test.hpp"
#include "boost/core/lightweight_test_trait.hpp"


using boost::optional;
using boost::make_optional;
using boost::core::is_same;

template <typename Expected, typename Deduced>
void verify_type(Deduced)
{
  BOOST_TEST_TRAIT_TRUE(( is_same<Expected, Deduced> ));
}
  
#if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
struct MoveOnly
{
  int value;
  explicit MoveOnly(int i) : value(i) {}
  MoveOnly(MoveOnly && r) : value(r.value) { r.value = 0; }
  MoveOnly& operator=(MoveOnly && r) { value = r.value; r.value = 0; return *this; }
  
private:
  MoveOnly(MoveOnly const&);
  void operator=(MoveOnly const&);
};

MoveOnly makeMoveOnly(int i)
{
  return MoveOnly(i);
}

void test_make_optional_for_move_only_type()
{
  verify_type< optional<MoveOnly> >(make_optional(makeMoveOnly(2)));
  verify_type< optional<MoveOnly> >(make_optional(true, makeMoveOnly(2)));
  
  optional<MoveOnly> o1 = make_optional(makeMoveOnly(1));
  BOOST_TEST (o1);
  BOOST_TEST_EQ (1, o1->value);
  
  optional<MoveOnly> o2 = make_optional(true, makeMoveOnly(2));
  BOOST_TEST (o2);
  BOOST_TEST_EQ (2, o2->value);
  
  optional<MoveOnly> oN = make_optional(false, makeMoveOnly(2));
  BOOST_TEST (!oN);
}

#endif // !defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES

void test_make_optional_for_optional()
{
  optional<int> oi;
  verify_type< optional< optional<int> > >(make_optional(oi));
  verify_type< optional< optional<int> > >(make_optional(true, oi));
  
  optional< optional<int> > ooi = make_optional(oi);
  BOOST_TEST (ooi);
  BOOST_TEST (!*ooi);
  
  optional< optional<int> > ooT = make_optional(true, oi);
  BOOST_TEST (ooT);
  BOOST_TEST (!*ooT);
  
  optional< optional<int> > ooF = make_optional(false, oi);
  BOOST_TEST (!ooF);
}

void test_nested_make_optional()
{
  verify_type< optional< optional<int> > >(make_optional(make_optional(1)));
  verify_type< optional< optional<int> > >(make_optional(true, make_optional(true, 2)));
  
  optional< optional<int> > oo1 = make_optional(make_optional(1));
  BOOST_TEST (oo1);
  BOOST_TEST (*oo1);
  BOOST_TEST_EQ (1, **oo1);
  
  optional< optional<int> > oo2 = make_optional(true, make_optional(true, 2));
  BOOST_TEST (oo2);
  BOOST_TEST (*oo2);
  BOOST_TEST_EQ (2, **oo2);
  
  optional< optional<int> > oo3 = make_optional(true, make_optional(false, 3));
  BOOST_TEST (oo3);
  BOOST_TEST (!*oo3);
  
  optional< optional<int> > oo4 = make_optional(false, make_optional(true, 4));
  BOOST_TEST (!oo4);
}

int main()
{
#if (!defined BOOST_OPTIONAL_DETAIL_NO_RVALUE_REFERENCES)
  test_make_optional_for_move_only_type();
#endif
  test_make_optional_for_optional();
  test_nested_make_optional();

  return boost::report_errors();
}