Files
clang-p2996/libcxx/test/std/utilities/optional/optional.monadic/and_then.pass.cpp
Louis Dionne f0fc8c4878 [libc++] Use named Lit features to flag back-deployment XFAILs
Instead of writing something like `XFAIL: use_system_cxx_lib && target=...`
to XFAIL back-deployment tests, introduce named Lit features like
`availability-shared_mutex-missing` to represent those. This makes the
XFAIL annotations leaner, and solves the problem of XFAIL comments
potentially getting out of sync. This would also make it easier for
another vendor to add their own annotations to the test suite by simply
changing how the feature is defined for their OS releases, instead
of having to modify hundreds of tests to add repetitive annotations.

This doesn't touch *all* annotations -- only annotations that were widely
duplicated are given named features (e.g. when filesystem or shared_mutex
were introduced). I still think it probably doesn't make sense to have a
named feature for every single fix we make to the dylib.

This is in essence a revert of 2659663, but since then the test suite
has changed significantly. Back when I did 2659663, the configuration
files we have for the test suite right now were being bootstrapped and
it wasn't clear how to provide these features for back-deployment in
that context. Since then, we have a streamlined way of defining these
features in `features.py` and that doesn't impact the ability for a
configuration file to stay minimal.

The original motivation for this change was that I am about to propose
a change that would touch essentially all XFAIL annotations for back-deployment
in the test suite, and this greatly reduces the number of lines changed
by that upcoming change, in addition to making the test suite generally
better.

Differential Revision: https://reviews.llvm.org/D146359
2023-03-27 12:44:26 -04:00

267 lines
8.0 KiB
C++

//===----------------------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// XFAIL: availability-bad_optional_access-missing && !no-exceptions
// <optional>
// template<class F> constexpr auto and_then(F&&) &;
// template<class F> constexpr auto and_then(F&&) &&;
// template<class F> constexpr auto and_then(F&&) const&;
// template<class F> constexpr auto and_then(F&&) const&&;
#include <cassert>
#include <optional>
#include "test_macros.h"
struct LVal {
constexpr std::optional<int> operator()(int&) { return 1; }
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct CLVal {
std::optional<int> operator()(int&) = delete;
constexpr std::optional<int> operator()(const int&) { return 1; }
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct RVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
constexpr std::optional<int> operator()(int&&) { return 1; }
std::optional<int> operator()(const int&&) = delete;
};
struct CRVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
constexpr std::optional<int> operator()(const int&&) { return 1; }
};
struct RefQual {
constexpr std::optional<int> operator()(int) & { return 1; }
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct CRefQual {
std::optional<int> operator()(int) & = delete;
constexpr std::optional<int> operator()(int) const& { return 1; }
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct RVRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
constexpr std::optional<int> operator()(int) && { return 1; }
std::optional<int> operator()(int) const&& = delete;
};
struct RVCRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
constexpr std::optional<int> operator()(int) const&& { return 1; }
};
struct NOLVal {
constexpr std::optional<int> operator()(int&) { return std::nullopt; }
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct NOCLVal {
std::optional<int> operator()(int&) = delete;
constexpr std::optional<int> operator()(const int&) { return std::nullopt; }
std::optional<int> operator()(int&&) = delete;
std::optional<int> operator()(const int&&) = delete;
};
struct NORVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
constexpr std::optional<int> operator()(int&&) { return std::nullopt; }
std::optional<int> operator()(const int&&) = delete;
};
struct NOCRVal {
std::optional<int> operator()(int&) = delete;
std::optional<int> operator()(const int&) = delete;
std::optional<int> operator()(int&&) = delete;
constexpr std::optional<int> operator()(const int&&) { return std::nullopt; }
};
struct NORefQual {
constexpr std::optional<int> operator()(int) & { return std::nullopt; }
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct NOCRefQual {
std::optional<int> operator()(int) & = delete;
constexpr std::optional<int> operator()(int) const& { return std::nullopt; }
std::optional<int> operator()(int) && = delete;
std::optional<int> operator()(int) const&& = delete;
};
struct NORVRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
constexpr std::optional<int> operator()(int) && { return std::nullopt; }
std::optional<int> operator()(int) const&& = delete;
};
struct NORVCRefQual {
std::optional<int> operator()(int) & = delete;
std::optional<int> operator()(int) const& = delete;
std::optional<int> operator()(int) && = delete;
constexpr std::optional<int> operator()(int) const&& { return std::nullopt; }
};
struct NoCopy {
NoCopy() = default;
NoCopy(const NoCopy&) { assert(false); }
std::optional<int> operator()(const NoCopy&&) { return 1; }
};
struct NonConst {
std::optional<int> non_const() { return 1; }
};
constexpr void test_val_types() {
// Test & overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.and_then(LVal{}) == 1);
assert(i.and_then(NOLVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(LVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
RefQual l{};
assert(i.and_then(l) == 1);
NORefQual nl{};
assert(i.and_then(nl) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
}
}
// Test const& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(i.and_then(CLVal{}) == 1);
assert(i.and_then(NOCLVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(CLVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const CRefQual l{};
assert(i.and_then(l) == 1);
const NOCRefQual nl{};
assert(i.and_then(nl) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(l)), std::optional<int>);
}
}
// Test && overload
{
// Without & qualifier on F's operator()
{
std::optional<int> i{0};
assert(std::move(i).and_then(RVal{}) == 1);
assert(std::move(i).and_then(NORVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(std::move(i).and_then(RVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
std::optional<int> i{0};
assert(i.and_then(RVRefQual{}) == 1);
assert(i.and_then(NORVRefQual{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(RVRefQual{})), std::optional<int>);
}
}
// Test const&& overload
{
// Without & qualifier on F's operator()
{
const std::optional<int> i{0};
assert(std::move(i).and_then(CRVal{}) == 1);
assert(std::move(i).and_then(NOCRVal{}) == std::nullopt);
ASSERT_SAME_TYPE(decltype(std::move(i).and_then(CRVal{})), std::optional<int>);
}
//With & qualifier on F's operator()
{
const std::optional<int> i{0};
const RVCRefQual l{};
assert(i.and_then(std::move(l)) == 1);
const NORVCRefQual nl{};
assert(i.and_then(std::move(nl)) == std::nullopt);
ASSERT_SAME_TYPE(decltype(i.and_then(std::move(l))), std::optional<int>);
}
}
}
// check that the lambda body is not instantiated during overload resolution
constexpr void test_sfinae() {
std::optional<NonConst> opt{};
auto l = [](auto&& x) { return x.non_const(); };
opt.and_then(l);
std::move(opt).and_then(l);
}
constexpr bool test() {
test_val_types();
std::optional<int> opt{};
const auto& copt = opt;
const auto never_called = [](int) {
assert(false);
return std::optional<int>{};
};
opt.and_then(never_called);
std::move(opt).and_then(never_called);
copt.and_then(never_called);
std::move(copt).and_then(never_called);
std::optional<NoCopy> nc;
const auto& cnc = nc;
std::move(cnc).and_then(NoCopy{});
std::move(nc).and_then(NoCopy{});
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}