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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
//===----------------------------------------------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++98, c++03, c++11, c++14
// <optional>
// template <class... Args> T& optional<T>::emplace(Args&&... args);
#include <optional>
#include <type_traits>
#include <cassert>
#include <memory>
#include "test_macros.h"
#include "archetypes.hpp"
using std::optional;
class X
{
int i_;
int j_ = 0;
public:
X() : i_(0) {}
X(int i) : i_(i) {}
X(int i, int j) : i_(i), j_(j) {}
friend bool operator==(const X& x, const X& y)
{return x.i_ == y.i_ && x.j_ == y.j_;}
};
class Y
{
public:
static bool dtor_called;
Y() = default;
Y(int) { TEST_THROW(6);}
~Y() {dtor_called = true;}
};
bool Y::dtor_called = false;
template <class T>
void test_one_arg() {
using Opt = std::optional<T>;
{
Opt opt;
auto & v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(0));
assert(&v == &*opt);
}
{
Opt opt;
auto & v = opt.emplace(1);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
assert(&v == &*opt);
}
{
Opt opt(2);
auto & v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(0));
assert(&v == &*opt);
}
{
Opt opt(2);
auto & v = opt.emplace(1);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert(*opt == T(1));
assert(&v == &*opt);
}
}
template <class T>
void test_multi_arg()
{
test_one_arg<T>();
using Opt = std::optional<T>;
{
Opt opt;
auto &v = opt.emplace(101, 41);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(101, 41));
assert(*opt == T(101, 41));
}
{
Opt opt;
auto &v = opt.emplace({1, 2, 3, 4});
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(4)); // T sets its value to the size of the init list
assert(*opt == T(4));
}
{
Opt opt;
auto &v = opt.emplace({1, 2, 3, 4, 5}, 6);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(static_cast<bool>(opt) == true);
assert( v == T(5)); // T sets its value to the size of the init list
assert(*opt == T(5)); // T sets its value to the size of the init list
}
}
template <class T>
void test_on_test_type() {
T::reset();
optional<T> opt;
assert(T::alive == 0);
{
T::reset_constructors();
auto &v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::default_constructed == 1);
assert(T::destroyed == 0);
assert(static_cast<bool>(opt) == true);
assert(*opt == T());
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace();
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::default_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T());
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(101);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(101));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(-10, 99);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(-10, 99));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace(-10, 99);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(-10, 99));
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace({-10, 99, 42, 1});
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(4)); // size of the initializer list
assert(&v == &*opt);
}
{
T::reset_constructors();
auto &v = opt.emplace({-10, 99, 42, 1}, 42);
static_assert( std::is_same_v<T&, decltype(v)>, "" );
assert(T::alive == 1);
assert(T::constructed == 1);
assert(T::value_constructed == 1);
assert(T::destroyed == 1);
assert(static_cast<bool>(opt) == true);
assert(*opt == T(4)); // size of the initializer list
assert(&v == &*opt);
}
}
int main()
{
{
test_on_test_type<TestTypes::TestType>();
test_on_test_type<ExplicitTestTypes::TestType>();
}
{
using T = int;
test_one_arg<T>();
test_one_arg<const T>();
}
{
using T = ConstexprTestTypes::TestType;
test_multi_arg<T>();
}
{
using T = ExplicitConstexprTestTypes::TestType;
test_multi_arg<T>();
}
{
using T = TrivialTestTypes::TestType;
test_multi_arg<T>();
}
{
using T = ExplicitTrivialTestTypes::TestType;
test_multi_arg<T>();
}
{
optional<const int> opt;
auto &v = opt.emplace(42);
static_assert( std::is_same_v<const int&, decltype(v)>, "" );
assert(*opt == 42);
assert( v == 42);
opt.emplace();
assert(*opt == 0);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
Y::dtor_called = false;
{
Y y;
optional<Y> opt(y);
try
{
assert(static_cast<bool>(opt) == true);
assert(Y::dtor_called == false);
auto &v = opt.emplace(1);
static_assert( std::is_same_v<Y&, decltype(v)>, "" );
assert(false);
}
catch (int i)
{
assert(i == 6);
assert(static_cast<bool>(opt) == false);
assert(Y::dtor_called == true);
}
}
#endif
}
|