Debian Codegraph @

libgomp/testsuite/libgomp.c++/target-flex-2002.C

raw
1 /* { dg-additional-options "-std=c++23" } */
2
3 /* expected/optional */
4
5 #include <optional>
6 #include <expected>
7
8 #include "target-flex-common.h"
9
10 std::optional<unsigned> make_optional(bool b, unsigned arg = 0u) noexcept
11 {
12 if (!b)
13 return std::nullopt;
14 return {arg};
15 }
16
17 bool test_optional(unsigned arg)
18 {
19 bool ok;
20 #pragma omp target map(from: ok) map(to: arg)
21 {
22 bool inner_ok = true;
23 {
24 auto null_opt = make_optional(false);
25 VERIFY (!null_opt);
26 VERIFY (!null_opt.has_value());
27 VERIFY (null_opt.value_or(arg * 2u) == arg * 2u);
28 VERIFY (null_opt.or_else([&](){ return std::optional<unsigned>{arg}; })
29 .transform([](int a){ return a * 2u; })
30 .value_or(0) == arg * 2u);
31
32 auto opt = make_optional(true, arg);
33 VERIFY (opt);
34 VERIFY (opt.has_value());
35 VERIFY (opt.value() == arg);
36 VERIFY (*opt == arg);
37 VERIFY (opt.value_or(arg + 42) == arg);
38 VERIFY (opt.or_else([&](){ return std::optional<unsigned>{arg + 42}; })
39 .transform([](int a){ return a * 2u; })
40 .value_or(0) == arg * 2u);
41 }
42 end:
43 ok = inner_ok;
44 }
45 return ok;
46 }
47
48 struct my_error
49 {
50 int _e;
51 };
52
53 std::expected<unsigned, my_error> make_expected(bool b, unsigned arg = 0u) noexcept
54 {
55 if (!b)
56 return std::unexpected{my_error{-1}};
57 return {arg};
58 }
59
60 bool test_expected(unsigned arg)
61 {
62 bool ok;
63 #pragma omp target map(from: ok) map(to: arg)
64 {
65 bool inner_ok = true;
66 {
67 auto unexpected = make_expected(false);
68 VERIFY (!unexpected);
69 VERIFY (!unexpected.has_value());
70 VERIFY (unexpected.error()._e == -1);
71 VERIFY (unexpected.value_or(arg * 2u) == arg * 2u);
72 VERIFY (unexpected.or_else([&](my_error e){ return std::expected<unsigned, my_error>{arg}; })
73 .transform([](int a){ return a * 2u; })
74 .value_or(0) == arg * 2u);
75
76 auto expected = make_expected(true, arg);
77 VERIFY (expected);
78 VERIFY (expected.has_value());
79 VERIFY (expected.value() == arg);
80 VERIFY (*expected == arg);
81 VERIFY (expected.value_or(arg + 42) == arg);
82 VERIFY (expected.or_else([&](my_error e){ return std::expected<unsigned, my_error>{std::unexpected{e}}; })
83 .transform([](int a){ return a * 2u; })
84 .value_or(0) == arg * 2u);
85 }
86 end:
87 ok = inner_ok;
88 }
89 return ok;
90 }
91
92 int main()
93 {
94 volatile unsigned arg = 42;
95 return test_optional(arg)
96 && test_expected(arg) ? 0 : 1;
97 }