1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- Mode: C++ -*-
3 //
4 // Copyright (C) 2020 Google, Inc.
5 //
6 // Author: Matthias Maennich
7 
8 /// @file
9 ///
10 /// This program tests libabigail's CXX compatibility layer.
11 
12 #include "lib/catch.hpp"
13 
14 #include "abg-cxx-compat.h"
15 
16 using abg_compat::optional;
17 
18 TEST_CASE("OptionalConstruction", "[abg_compat::optional]")
19 {
20   optional<bool> opt1;
21   REQUIRE_FALSE(opt1.has_value());
22 
23   optional<bool> opt2(true);
24   REQUIRE(opt2.has_value());
25   CHECK(opt2.value() == true);
26 
27   optional<bool> opt3(false);
28   REQUIRE(opt3.has_value());
29   CHECK(opt3.value() == false);
30 }
31 
32 TEST_CASE("OptionalValue", "[abg_compat::optional]")
33 {
34   optional<bool> opt;
35   REQUIRE_FALSE(opt.has_value());
36   REQUIRE_THROWS(opt.value());
37 
38   opt = true;
39   REQUIRE_NOTHROW(opt.value());
40   CHECK(opt.value() == true);
41 }
42 
43 TEST_CASE("OptionalValueOr", "[abg_compat::optional]")
44 {
45   optional<std::string> opt;
46   REQUIRE_FALSE(opt.has_value());
47 
48   const std::string& mine = "mine";
49   // Ensure we get a copy of our own value.
50   CHECK(opt.value_or(mine) == mine);
51 
52   // Now set the value
53   const std::string& other = "other";
54   opt = other;
55   CHECK(opt.value_or(mine) != mine);
56   CHECK(opt.value_or(mine) == other);
57 }
58 
59 TEST_CASE("OptionalDeref", "[abg_compat::optional]")
60 {
61   optional<std::string> opt("asdf");
62   REQUIRE(opt.has_value());
63 
64   CHECK(*opt == "asdf");
65   CHECK(opt->size() == 4);
66 }
67