libabigail/tests/test-cxx-compat.cc
Matthias Maennich c92d724e01 abg-cxx-compat: add simplified version of std::optional
In the absence (but desire) of std::optional<T>, add a simplified
version of it to abg_compat:: in case we are compiling with a pre-C++17
standard. Otherwise use std::optional from <optional> directly.

This is being used by a later patch and serves as a prerequisite.
It only serves the purpose of being a compatibility implementation and
does not claim to be complete at all. Just enough for the project's
needs.

	* include/abg-cxx-compat.h (abg_compat::optional): Add new class.
	* tests/tests-cxx-compat.cc: Add new test cases.

Reviewed-by: Giuliano Procida <gprocida@google.com>
Signed-off-by: Matthias Maennich <maennich@google.com>
2021-03-09 10:41:10 +01:00

67 lines
1.4 KiB
C++

// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
// -*- Mode: C++ -*-
//
// Copyright (C) 2020 Google, Inc.
//
// Author: Matthias Maennich
/// @file
///
/// This program tests libabigail's CXX compatibility layer.
#include "lib/catch.hpp"
#include "abg-cxx-compat.h"
using abg_compat::optional;
TEST_CASE("OptionalConstruction", "[abg_compat::optional]")
{
optional<bool> opt1;
REQUIRE_FALSE(opt1.has_value());
optional<bool> opt2(true);
REQUIRE(opt2.has_value());
CHECK(opt2.value() == true);
optional<bool> opt3(false);
REQUIRE(opt3.has_value());
CHECK(opt3.value() == false);
}
TEST_CASE("OptionalValue", "[abg_compat::optional]")
{
optional<bool> opt;
REQUIRE_FALSE(opt.has_value());
REQUIRE_THROWS(opt.value());
opt = true;
REQUIRE_NOTHROW(opt.value());
CHECK(opt.value() == true);
}
TEST_CASE("OptionalValueOr", "[abg_compat::optional]")
{
optional<std::string> opt;
REQUIRE_FALSE(opt.has_value());
const std::string& mine = "mine";
// Ensure we get a copy of our own value.
CHECK(opt.value_or(mine) == mine);
// Now set the value
const std::string& other = "other";
opt = other;
CHECK(opt.value_or(mine) != mine);
CHECK(opt.value_or(mine) == other);
}
TEST_CASE("OptionalDeref", "[abg_compat::optional]")
{
optional<std::string> opt("asdf");
REQUIRE(opt.has_value());
CHECK(*opt == "asdf");
CHECK(opt->size() == 4);
}