Skip to content

Commit f6712b6

Browse files
authored
[libc++] Reformat optional constructor tests (#169231)
- Mass-reformat tests in `std/utilities/optional/optional.object/optional.object.ctor` and rearrange header `#include`s - No functional changes - Prelude for #169203
1 parent 6696e0c commit f6712b6

17 files changed

+982
-1116
lines changed

libcxx/test/std/utilities/optional/optional.object/optional.object.ctor/U.pass.cpp

Lines changed: 91 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -13,147 +13,145 @@
1313
// template <class U>
1414
// constexpr EXPLICIT optional(U&& u);
1515

16+
#include <cassert>
1617
#include <optional>
1718
#include <type_traits>
18-
#include <cassert>
1919

2020
#include "test_macros.h"
2121
#include "archetypes.h"
2222
#include "test_convertible.h"
2323

24-
2524
using std::optional;
2625

27-
struct ImplicitThrow
28-
{
29-
constexpr ImplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
26+
struct ImplicitThrow {
27+
constexpr ImplicitThrow(int x) {
28+
if (x != -1)
29+
TEST_THROW(6);
30+
}
3031
};
3132

32-
struct ExplicitThrow
33-
{
34-
constexpr explicit ExplicitThrow(int x) { if (x != -1) TEST_THROW(6);}
33+
struct ExplicitThrow {
34+
constexpr explicit ExplicitThrow(int x) {
35+
if (x != -1)
36+
TEST_THROW(6);
37+
}
3538
};
3639

3740
struct ImplicitAny {
3841
template <class U>
3942
constexpr ImplicitAny(U&&) {}
4043
};
4144

42-
4345
template <class To, class From>
44-
constexpr bool implicit_conversion(optional<To>&& opt, const From& v)
45-
{
46-
using O = optional<To>;
47-
static_assert(test_convertible<O, From>(), "");
48-
static_assert(!test_convertible<O, void*>(), "");
49-
static_assert(!test_convertible<O, From, int>(), "");
50-
return opt && *opt == static_cast<To>(v);
46+
constexpr bool implicit_conversion(optional<To>&& opt, const From& v) {
47+
using O = optional<To>;
48+
static_assert(test_convertible<O, From>(), "");
49+
static_assert(!test_convertible<O, void*>(), "");
50+
static_assert(!test_convertible<O, From, int>(), "");
51+
return opt && *opt == static_cast<To>(v);
5152
}
5253

5354
template <class To, class Input, class Expect>
54-
constexpr bool explicit_conversion(Input&& in, const Expect& v)
55-
{
56-
using O = optional<To>;
57-
static_assert(std::is_constructible<O, Input>::value, "");
58-
static_assert(!std::is_convertible<Input, O>::value, "");
59-
static_assert(!std::is_constructible<O, void*>::value, "");
60-
static_assert(!std::is_constructible<O, Input, int>::value, "");
61-
optional<To> opt(std::forward<Input>(in));
62-
optional<To> opt2{std::forward<Input>(in)};
63-
return opt && *opt == static_cast<To>(v) && (opt2 && *opt2 == static_cast<To>(v));
55+
constexpr bool explicit_conversion(Input&& in, const Expect& v) {
56+
using O = optional<To>;
57+
static_assert(std::is_constructible<O, Input>::value, "");
58+
static_assert(!std::is_convertible<Input, O>::value, "");
59+
static_assert(!std::is_constructible<O, void*>::value, "");
60+
static_assert(!std::is_constructible<O, Input, int>::value, "");
61+
optional<To> opt(std::forward<Input>(in));
62+
optional<To> opt2{std::forward<Input>(in)};
63+
return opt && *opt == static_cast<To>(v) && (opt2 && *opt2 == static_cast<To>(v));
6464
}
6565

66-
void test_implicit()
67-
{
68-
{
69-
static_assert(implicit_conversion<long long>(42, 42), "");
70-
}
71-
{
72-
static_assert(implicit_conversion<long double>(3.14, 3.14), "");
73-
}
74-
{
75-
int x = 42;
76-
optional<void* const> o(&x);
77-
assert(*o == &x);
78-
}
79-
{
80-
using T = TrivialTestTypes::TestType;
81-
static_assert(implicit_conversion<T>(42, 42), "");
82-
}
83-
{
84-
using T = TestTypes::TestType;
85-
assert(implicit_conversion<T>(3, T(3)));
86-
}
87-
{
88-
using T = TestTypes::TestType;
89-
optional<T> opt({3});
90-
assert(opt && *opt == static_cast<T>(3));
91-
}
66+
void test_implicit() {
67+
{
68+
static_assert(implicit_conversion<long long>(42, 42), "");
69+
}
70+
{
71+
static_assert(implicit_conversion<long double>(3.14, 3.14), "");
72+
}
73+
{
74+
int x = 42;
75+
optional<void* const> o(&x);
76+
assert(*o == &x);
77+
}
78+
{
79+
using T = TrivialTestTypes::TestType;
80+
static_assert(implicit_conversion<T>(42, 42), "");
81+
}
82+
{
83+
using T = TestTypes::TestType;
84+
assert(implicit_conversion<T>(3, T(3)));
85+
}
86+
{
87+
using T = TestTypes::TestType;
88+
optional<T> opt({3});
89+
assert(opt && *opt == static_cast<T>(3));
90+
}
9291
{
9392
using O = optional<ImplicitAny>;
9493
static_assert(!test_convertible<O, std::in_place_t>(), "");
9594
static_assert(!test_convertible<O, std::in_place_t&>(), "");
9695
static_assert(!test_convertible<O, const std::in_place_t&>(), "");
9796
static_assert(!test_convertible<O, std::in_place_t&&>(), "");
9897
static_assert(!test_convertible<O, const std::in_place_t&&>(), "");
99-
10098
}
10199
#ifndef TEST_HAS_NO_EXCEPTIONS
102-
{
103-
try {
104-
using T = ImplicitThrow;
105-
optional<T> t = 42;
106-
assert(false);
107-
((void)t);
108-
} catch (int) {
109-
}
100+
{
101+
try {
102+
using T = ImplicitThrow;
103+
optional<T> t = 42;
104+
assert(false);
105+
((void)t);
106+
} catch (int) {
110107
}
108+
}
111109
#endif
112110
}
113111

114112
void test_explicit() {
113+
{
114+
using T = ExplicitTrivialTestTypes::TestType;
115+
static_assert(explicit_conversion<T>(42, 42), "");
116+
}
117+
{
118+
using T = ExplicitConstexprTestTypes::TestType;
119+
static_assert(explicit_conversion<T>(42, 42), "");
120+
static_assert(!std::is_convertible<int, T>::value, "");
121+
}
122+
{
123+
using T = ExplicitTestTypes::TestType;
124+
T::reset();
115125
{
116-
using T = ExplicitTrivialTestTypes::TestType;
117-
static_assert(explicit_conversion<T>(42, 42), "");
118-
}
119-
{
120-
using T = ExplicitConstexprTestTypes::TestType;
121-
static_assert(explicit_conversion<T>(42, 42), "");
122-
static_assert(!std::is_convertible<int, T>::value, "");
126+
assert(explicit_conversion<T>(42, 42));
127+
assert(T::alive == 0);
123128
}
129+
T::reset();
124130
{
125-
using T = ExplicitTestTypes::TestType;
126-
T::reset();
127-
{
128-
assert(explicit_conversion<T>(42, 42));
129-
assert(T::alive == 0);
130-
}
131-
T::reset();
132-
{
133-
optional<T> t(42);
134-
assert(T::alive == 1);
135-
assert(T::value_constructed == 1);
136-
assert(T::move_constructed == 0);
137-
assert(T::copy_constructed == 0);
138-
assert(t.value().value == 42);
139-
}
140-
assert(T::alive == 0);
131+
optional<T> t(42);
132+
assert(T::alive == 1);
133+
assert(T::value_constructed == 1);
134+
assert(T::move_constructed == 0);
135+
assert(T::copy_constructed == 0);
136+
assert(t.value().value == 42);
141137
}
138+
assert(T::alive == 0);
139+
}
142140
#ifndef TEST_HAS_NO_EXCEPTIONS
143-
{
144-
try {
145-
using T = ExplicitThrow;
146-
optional<T> t(42);
147-
assert(false);
148-
} catch (int) {
149-
}
141+
{
142+
try {
143+
using T = ExplicitThrow;
144+
optional<T> t(42);
145+
assert(false);
146+
} catch (int) {
150147
}
148+
}
151149
#endif
152150
}
153151

154152
int main(int, char**) {
155-
test_implicit();
156-
test_explicit();
153+
test_implicit();
154+
test_explicit();
157155

158156
return 0;
159157
}

0 commit comments

Comments
 (0)