Require target lra in gcc.dg/pr108095.c
[official-gcc.git] / gcc / rust / util / rust-optional-test.cc
blob9fbbe7d5f3c726fe36cb8cd30956dc7b55087297
1 // Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 // This file is part of GCC.
5 // GCC is free software; you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation; either version 3, or (at your option) any later
8 // version.
10 // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 // for more details.
15 // You should have received a copy of the GNU General Public License
16 // along with GCC; see the file COPYING3. If not see
17 // <http://www.gnu.org/licenses/>.
19 #include "rust-system.h"
20 #include "rust-optional.h"
21 #include "selftest.h"
23 #if CHECKING_P
25 static void
26 rust_optional_create ()
28 auto opt = Rust::Optional<int>::some (15);
30 ASSERT_TRUE (opt.is_some ());
31 ASSERT_EQ (opt.get (), 15);
33 Rust::Optional<int> const_opt = Rust::Optional<int>::some (15);
34 const int &value = const_opt.get ();
36 ASSERT_EQ (value, 15);
39 static void
40 rust_optional_operators ()
42 auto opt = Rust::Optional<int>::some (15);
44 // as bool
45 ASSERT_TRUE (opt);
47 // deref
48 ASSERT_EQ (*opt, 15);
50 class Methodable
52 public:
53 int method () { return 15; }
56 auto m_opt = Rust::Optional<Methodable>::some (Methodable ());
57 ASSERT_EQ (m_opt->method (), 15);
60 static void
61 rust_optional_take ()
63 auto opt = Rust::Optional<int>::some (15);
64 auto value = opt.take ();
66 ASSERT_EQ (value, 15);
67 ASSERT_TRUE (opt.is_none ());
70 static void
71 rust_optional_map ()
73 auto opt = Rust::Optional<int>::some (15);
74 auto twice = opt.map<int> ([] (int value) { return value * 2; });
76 ASSERT_FALSE (opt);
77 ASSERT_TRUE (twice);
78 ASSERT_EQ (*twice, 30);
81 static void
82 rust_optional_reference ()
84 auto value = std::vector<std::string> ();
85 value.emplace_back ("rust");
86 value.emplace_back ("+");
87 value.emplace_back ("gcc");
88 value.emplace_back ("=");
89 value.emplace_back ("<3");
91 auto opt = Rust::Optional<std::vector<std::string> &>::some (value);
93 ASSERT_EQ (opt->at (0), "rust");
94 ASSERT_EQ (opt->at (2), "gcc");
97 #endif /* #if CHECKING_P */
99 void
100 rust_optional_test ()
102 #if CHECKING_P
103 rust_optional_create ();
104 rust_optional_operators ();
105 rust_optional_take ();
106 rust_optional_map ();
107 rust_optional_reference ();
109 #endif /* #if CHECKING_P */