[LoongArch] Supports FP_TO_SINT operation for fp16 (#118303)
[llvm-project.git] / libcxx / test / std / utilities / expected / expected.void / ctor / ctor.unexpect.pass.cpp
blob4e7657cb878f73ae975992493aeb3ca901c3efa6
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
11 // template<class... Args>
12 // constexpr explicit expected(unexpect_t, Args&&... args);
14 // Constraints: is_constructible_v<E, Args...> is true.
16 // Effects: Direct-non-list-initializes unex with std::forward<Args>(args)....
18 // Postconditions: has_value() is false.
20 // Throws: Any exception thrown by the initialization of unex.
22 #include <cassert>
23 #include <expected>
24 #include <tuple>
25 #include <type_traits>
26 #include <utility>
28 #include "MoveOnly.h"
29 #include "test_macros.h"
30 #include "../../types.h"
32 // Test Constraints:
33 static_assert(std::is_constructible_v<std::expected<void, int>, std::unexpect_t>);
34 static_assert(std::is_constructible_v<std::expected<void, int>, std::unexpect_t, int>);
36 // !is_constructible_v<T, Args...>
37 struct foo {};
38 static_assert(!std::is_constructible_v<std::expected<void, foo>, std::unexpect_t, int>);
40 // test explicit
41 template <class T>
42 void conversion_test(T);
44 template <class T, class... Args>
45 concept ImplicitlyConstructible = requires(Args&&... args) { conversion_test<T>({std::forward<Args>(args)...}); };
46 static_assert(ImplicitlyConstructible<int, int>);
48 static_assert(!ImplicitlyConstructible<std::expected<void, int>, std::unexpect_t>);
49 static_assert(!ImplicitlyConstructible<std::expected<void, int>, std::unexpect_t, int>);
51 struct CopyOnly {
52 int i;
53 constexpr CopyOnly(int ii) : i(ii) {}
54 CopyOnly(const CopyOnly&) = default;
55 CopyOnly(CopyOnly&&) = delete;
56 friend constexpr bool operator==(const CopyOnly& mi, int ii) { return mi.i == ii; }
59 template <class T>
60 constexpr void testInt() {
61 std::expected<void, T> e(std::unexpect, 5);
62 assert(!e.has_value());
63 assert(e.error() == 5);
66 template <class T>
67 constexpr void testLValue() {
68 T t(5);
69 std::expected<void, T> e(std::unexpect, t);
70 assert(!e.has_value());
71 assert(e.error() == 5);
74 template <class T>
75 constexpr void testRValue() {
76 std::expected<void, T> e(std::unexpect, T(5));
77 assert(!e.has_value());
78 assert(e.error() == 5);
81 constexpr bool test() {
82 testInt<int>();
83 testInt<CopyOnly>();
84 testInt<MoveOnly>();
85 testInt<TailClobberer<1>>();
86 testLValue<int>();
87 testLValue<CopyOnly>();
88 testLValue<TailClobberer<1>>();
89 testRValue<int>();
90 testRValue<MoveOnly>();
91 testRValue<TailClobberer<1>>();
93 // no arg
95 std::expected<void, int> e(std::unexpect);
96 assert(!e.has_value());
97 assert(e.error() == 0);
100 // one arg
102 std::expected<void, int> e(std::unexpect, 5);
103 assert(!e.has_value());
104 assert(e.error() == 5);
107 // multi args
109 std::expected<void, std::tuple<int, short, MoveOnly>> e(std::unexpect, 1, short{2}, MoveOnly(3));
110 assert(!e.has_value());
111 assert((e.error() == std::tuple<int, short, MoveOnly>(1, short{2}, MoveOnly(3))));
114 return true;
117 void testException() {
118 #ifndef TEST_HAS_NO_EXCEPTIONS
119 struct Throwing {
120 Throwing(int) { throw Except{}; };
123 try {
124 std::expected<void, Throwing> u(std::unexpect, 5);
125 assert(false);
126 } catch (Except) {
128 #endif // TEST_HAS_NO_EXCEPTIONS
131 int main(int, char**) {
132 test();
133 static_assert(test());
134 testException();
135 return 0;