Skip to content

[libc++] [test] Fix portability issues for MSVC #93259

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ L link link_to_link
TEST_VALIDATE_EXCEPTION(
std::runtime_error,
[&]([[maybe_unused]] const std::runtime_error& e) {
std::string_view what{"tzdb: requested time zone not found"};
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
// Proj1 proj1 = {}, Proj2 proj2 = {}); // since C++23

#include <algorithm>
#include <array>
#include <cassert>
#include <concepts>
#include <ranges>
Expand Down Expand Up @@ -130,10 +131,10 @@ constexpr void test_iterators() {
}

{ // range has zero length
int a[] = {};
int p[] = {3, 4, 2};
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
std::array<int, 0> a = {};
int p[] = {3, 4, 2};
auto whole = std::ranges::subrange(Iter1(a.data()), Sent1(Iter1(a.data())));
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(std::end(p))));
{
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
assert(!ret);
Expand All @@ -145,10 +146,10 @@ constexpr void test_iterators() {
}

{ // subrange has zero length
int a[] = {3, 4, 2};
int p[] = {};
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
int a[] = {3, 4, 2};
std::array<int, 0> p = {};
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(std::end(a))));
auto subrange = std::ranges::subrange(Iter2(p.data()), Sent2(Iter2(p.data())));
{
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
assert(ret);
Expand All @@ -160,10 +161,10 @@ constexpr void test_iterators() {
}

{ // range and subrange both have zero length
int a[] = {};
int p[] = {};
auto whole = std::ranges::subrange(Iter1(a), Sent1(Iter1(a)));
auto subrange = std::ranges::subrange(Iter2(p), Sent2(Iter2(p)));
std::array<int, 0> a = {};
std::array<int, 0> p = {};
auto whole = std::ranges::subrange(Iter1(a.data()), Sent1(Iter1(a.data())));
auto subrange = std::ranges::subrange(Iter2(p.data()), Sent2(Iter2(p.data())));
{
bool ret = std::ranges::contains_subrange(whole.begin(), whole.end(), subrange.begin(), subrange.end());
assert(ret);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ constexpr bool test_non_convert_to_path() {
static_assert(!std::is_constructible_v<std::fstream, const std::basic_string_view<CharT>>);

// Char* pointers
if constexpr (!std::is_same_v<CharT, char>)
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
static_assert(!std::is_constructible_v<std::fstream, const CharT*>);

// Iterators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ constexpr bool test_non_convert_to_path() {
static_assert(!std::is_constructible_v<std::ifstream, const std::basic_string_view<CharT>>);

// Char* pointers
if constexpr (!std::is_same_v<CharT, char>)
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
static_assert(!std::is_constructible_v<std::ifstream, const CharT*>);

// Iterators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ constexpr bool test_non_convert_to_path() {
static_assert(!std::is_constructible_v<std::ofstream, const std::basic_string_view<CharT>>);

// Char* pointers
if constexpr (!std::is_same_v<CharT, char>)
if constexpr (!std::is_same_v<CharT, char> && !std::is_same_v<CharT, fs::path::value_type>)
static_assert(!std::is_constructible_v<std::ofstream, const CharT*>);

// Iterators
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct unsized_it {
using difference_type = std::ptrdiff_t;

value_type& operator*() const;
unsized_it& operator++();
bool operator==(const unsized_it&) const;
difference_type operator-(const unsized_it&) const { return 0; }
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include <cassert>
#include <climits>
#include <cstdint>
#include <limits>
#include <random>
#include <type_traits>

Expand Down Expand Up @@ -67,12 +68,14 @@ T basic_gcd(T m, T n) {
template <typename Input>
void do_fuzzy_tests() {
std::mt19937 gen(1938);
std::uniform_int_distribution<Input> distrib;
using DistIntType = std::conditional_t<sizeof(Input) == 1, int, Input>; // See N4981 [rand.req.genl]/1.5
constexpr Input max_input = std::numeric_limits<Input>::max();
std::uniform_int_distribution<DistIntType> distrib(0, max_input);
Comment on lines +71 to +73
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to keep Input as is. Your commit message says that the standard prohibits int8_t, but the wording is "undefined", not "ill-formed", meaning that we're allowed to provide int8_t as a documented extension, which libc++ does.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry @cjdb , I merged before you had time to comment. This wasn't intentional.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But this is the libcxx/test/std subdirectory, which is portable. If you want to test a libc++ extension, that should be guarded with the libc++ macro (which I would be fine with).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this isn't testing uniform_int_distribution? Never mind then.


constexpr int nb_rounds = 10000;
for (int i = 0; i < nb_rounds; ++i) {
Input n = distrib(gen);
Input m = distrib(gen);
Input n = static_cast<Input>(distrib(gen));
Input m = static_cast<Input>(distrib(gen));
assert(std::gcd(n, m) == basic_gcd(n, m));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ static void set_tz(std::string zone) {
// Unlike POSIX it does not mention the string of putenv becomes part
// of the environment.

int status = _putenv_s("TZ", zone.c_str(), 1);
int status = _putenv_s("TZ", zone.c_str());
assert(status == 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ static void test_exception([[maybe_unused]] std::string_view zone) {
TEST_VALIDATE_EXCEPTION(
std::runtime_error,
[&]([[maybe_unused]] const std::runtime_error& e) {
std::string_view what{"tzdb: requested time zone not found"};
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ static void set_tz(std::string zone) {
// Unlike POSIX it does not mention the string of putenv becomes part
// of the environment.

int status = _putenv_s("TZ", zone.c_str(), 1);
int status = _putenv_s("TZ", zone.c_str());
assert(status == 0);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ static void test_exception([[maybe_unused]] std::string_view zone) {
TEST_VALIDATE_EXCEPTION(
std::runtime_error,
[&]([[maybe_unused]] const std::runtime_error& e) {
std::string_view what{"tzdb: requested time zone not found"};
[[maybe_unused]] std::string_view what{"tzdb: requested time zone not found"};
TEST_LIBCPP_REQUIRE(
e.what() == what,
TEST_WRITE_CONCATENATED("\nExpected exception ", what, "\nActual exception ", e.what(), '\n'));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ template <class T, class Tuple>
static constexpr bool can_make_from_tuple =
std::is_same_v<decltype(test_make_from_tuple<T, Tuple>(T{}, Tuple{})), uint8_t>;

#ifdef _LIBCPP_VERSION
template <class T, class Tuple>
auto test_make_from_tuple_impl(T&&, Tuple&& t)
-> decltype(std::__make_from_tuple_impl<T>(
Expand All @@ -224,6 +225,7 @@ uint32_t test_make_from_tuple_impl(...) {
template <class T, class Tuple>
static constexpr bool can_make_from_tuple_impl =
std::is_same_v<decltype(test_make_from_tuple_impl<T, Tuple>(T{}, Tuple{})), uint8_t>;
#endif // _LIBCPP_VERSION

struct A {
int a;
Expand Down Expand Up @@ -263,23 +265,23 @@ static_assert(can_make_from_tuple<float, std::tuple<double>>);
// Test std::__make_from_tuple_impl constraints.

// reinterpret_cast
static_assert(!can_make_from_tuple_impl<int*, std::tuple<A*>>);
static_assert(can_make_from_tuple_impl<A*, std::tuple<A*>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int*, std::tuple<A*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<A*, std::tuple<A*>>);

// const_cast
static_assert(!can_make_from_tuple_impl<char*, std::tuple<const char*>>);
static_assert(!can_make_from_tuple_impl<volatile char*, std::tuple<const volatile char*>>);
static_assert(can_make_from_tuple_impl<volatile char*, std::tuple<volatile char*>>);
static_assert(can_make_from_tuple_impl<char*, std::tuple<char*>>);
static_assert(can_make_from_tuple_impl<const char*, std::tuple<char*>>);
static_assert(can_make_from_tuple_impl<const volatile char*, std::tuple<volatile char*>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<char*, std::tuple<const char*>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<volatile char*, std::tuple<const volatile char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<volatile char*, std::tuple<volatile char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<char*, std::tuple<char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const char*, std::tuple<char*>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<const volatile char*, std::tuple<volatile char*>>);

// static_cast
static_assert(!can_make_from_tuple_impl<int, std::tuple<D>>);
static_assert(!can_make_from_tuple_impl<D, std::tuple<int>>);
static_assert(can_make_from_tuple_impl<long, std::tuple<int>>);
static_assert(can_make_from_tuple_impl<double, std::tuple<float>>);
static_assert(can_make_from_tuple_impl<float, std::tuple<double>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<int, std::tuple<D>>);
LIBCPP_STATIC_ASSERT(!can_make_from_tuple_impl<D, std::tuple<int>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<long, std::tuple<int>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<double, std::tuple<float>>);
LIBCPP_STATIC_ASSERT(can_make_from_tuple_impl<float, std::tuple<double>>);

} // namespace LWG3528

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -516,7 +516,7 @@ constexpr void test_swap_sfinae() {
}
}

_LIBCPP_CONSTEXPR_SINCE_CXX20 void test_swap_noexcept() {
TEST_CONSTEXPR_CXX20 void test_swap_noexcept() {
{
using V = std::variant<int, NothrowMoveable>;
static_assert(std::is_swappable_v<V> && has_swap_member<V>(), "");
Expand Down
2 changes: 1 addition & 1 deletion libcxx/test/support/msvc_stdlib_force_include.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const AssertionDialogAvoider assertion_dialog_avoider{};
#include <version>

#if _HAS_CXX23
# define TEST_STD_VER 99
# define TEST_STD_VER 23
#elif _HAS_CXX20
# define TEST_STD_VER 20
#elif _HAS_CXX17
Expand Down
Loading