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
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
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"
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);
40 rust_optional_operators ()
42 auto opt
= Rust::Optional
<int>::some (15);
53 int method () { return 15; }
56 auto m_opt
= Rust::Optional
<Methodable
>::some (Methodable ());
57 ASSERT_EQ (m_opt
->method (), 15);
63 auto opt
= Rust::Optional
<int>::some (15);
64 auto value
= opt
.take ();
66 ASSERT_EQ (value
, 15);
67 ASSERT_TRUE (opt
.is_none ());
73 auto opt
= Rust::Optional
<int>::some (15);
74 auto twice
= opt
.map
<int> ([] (int value
) { return value
* 2; });
78 ASSERT_EQ (*twice
, 30);
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 */
100 rust_optional_test ()
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 */