Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / algorithms / algorithms.results / in_found_result.pass.cpp
blobaf23a1b051c89e5281dbd4800904f5d8d4a33a61
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
11 // template <class I>
12 // struct in_found_result;
14 #include <algorithm>
15 #include <cassert>
16 #include <type_traits>
18 #include "MoveOnly.h"
20 struct A {
21 explicit A(int);
23 // no implicit conversion
24 static_assert(!std::is_constructible_v<std::ranges::in_found_result<A>, std::ranges::in_found_result<int>>);
26 struct B {
27 B(const int&);
28 B(int&&);
30 // implicit conversion
31 static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, std::ranges::in_found_result<int>>);
32 static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, std::ranges::in_found_result<int>&>);
33 static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, const std::ranges::in_found_result<int>>);
34 static_assert(std::is_constructible_v<std::ranges::in_found_result<B>, const std::ranges::in_found_result<int>&>);
36 struct C {
37 C(int&);
39 static_assert(!std::is_constructible_v<std::ranges::in_found_result<C>, std::ranges::in_found_result<int>&>);
41 // has to be convertible via const&
42 static_assert(std::is_convertible_v<std::ranges::in_found_result<int>&, std::ranges::in_found_result<long>>);
43 static_assert(std::is_convertible_v<const std::ranges::in_found_result<int>&, std::ranges::in_found_result<long>>);
44 static_assert(std::is_convertible_v<std::ranges::in_found_result<int>&&, std::ranges::in_found_result<long>>);
45 static_assert(std::is_convertible_v<const std::ranges::in_found_result<int>&&, std::ranges::in_found_result<long>>);
47 // should be move constructible
48 static_assert(std::is_move_constructible_v<std::ranges::in_found_result<MoveOnly>>);
50 // should not be copy constructible
51 static_assert(!std::is_copy_constructible_v<std::ranges::in_found_result<MoveOnly>>);
53 struct NotConvertible {};
54 // conversions should not work if there is no conversion
55 static_assert(!std::is_convertible_v<std::ranges::in_found_result<NotConvertible>, std::ranges::in_found_result<int>>);
57 static_assert(std::is_same_v<decltype(std::ranges::in_found_result<int>::in), int>);
58 static_assert(std::is_same_v<decltype(std::ranges::in_found_result<int>::found), bool>);
60 template <class T>
61 struct ConvertibleFrom {
62 constexpr ConvertibleFrom(T c) : content{c} {}
63 T content;
66 constexpr bool test() {
68 std::ranges::in_found_result<double> res{10, true};
69 assert(res.in == 10);
70 assert(res.found == true);
71 std::ranges::in_found_result<ConvertibleFrom<int>> res2 = res;
72 assert(res2.in.content == 10);
73 assert(res2.found == true);
76 std::ranges::in_found_result<MoveOnly> res{MoveOnly{}, false};
77 assert(res.in.get() == 1);
78 assert(!res.found);
79 auto res2 = std::move(res);
80 assert(res2.in.get() == 1);
81 assert(!res2.found);
82 assert(res.in.get() == 0);
83 assert(!res.found);
85 auto [in, found] = std::ranges::in_found_result<int>{2, false};
86 assert(in == 2);
87 assert(!found);
89 return true;
92 int main(int, char**) {
93 test();
94 static_assert(test());
96 return 0;