Skip to content

Commit

Permalink
[libc++] Fix tests on musl (llvm#85085) (llvm#86934)
Browse files Browse the repository at this point in the history
One or two of the tests need slight tweaks to make them pass when
building with musl.

This patch is a re-application of b61fb18 which was reverted in 0847c90
because it broke the build.

rdar://118885724

Co-authored-by: Alastair Houghton <[email protected]>
  • Loading branch information
ldionne and al45tair authored Apr 3, 2024
1 parent ca48d4d commit d1f5850
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ int main(int, char**)
errno = E2BIG; // something that message will never generate
const std::error_category& e_cat1 = std::generic_category();
const std::string msg = e_cat1.message(-1);
// Exact message format varies by platform.
#if defined(_AIX)
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0);
#elif defined(_NEWLIB_VERSION)
LIBCPP_ASSERT(msg.empty());
#else
LIBCPP_ASSERT(msg.rfind("Unknown error", 0) == 0);
// Exact message format varies by platform. We can't detect
// some of these (Musl in particular) using the preprocessor,
// so accept a few sensible messages. Newlib unfortunately
// responds with an empty message, which we probably want to
// treat as a failure code otherwise, but we can detect that
// with the preprocessor.
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
|| msg.rfind("No error information", 0) == 0 // Musl
|| msg.rfind("Unknown error", 0) == 0 // Glibc
#if defined(_NEWLIB_VERSION)
|| msg.empty()
#endif
);
assert(errno == E2BIG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ int main(int, char**) {
errno = E2BIG; // something that message will never generate
const std::error_category& e_cat1 = std::system_category();
const std::string msg = e_cat1.message(-1);
// Exact message format varies by platform.
#if defined(_AIX)
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0);
#elif defined(_NEWLIB_VERSION)
LIBCPP_ASSERT(msg.empty());
#else
LIBCPP_ASSERT(msg.rfind("Unknown error", 0) == 0);
// Exact message format varies by platform. We can't detect
// some of these (Musl in particular) using the preprocessor,
// so accept a few sensible messages. Newlib unfortunately
// responds with an empty message, which we probably want to
// treat as a failure code otherwise, but we can detect that
// with the preprocessor.
LIBCPP_ASSERT(msg.rfind("Error -1 occurred", 0) == 0 // AIX
|| msg.rfind("No error information", 0) == 0 // Musl
|| msg.rfind("Unknown error", 0) == 0 // Glibc
#if defined(_NEWLIB_VERSION)
|| msg.empty()
#endif
);
assert(errno == E2BIG);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,11 @@
// iter_type put(iter_type s, ios_base& iob, char_type fill, long double v) const;

// XFAIL: win32-broken-printf-g-precision
// XFAIL: LIBCXX-PICOLIBC-FIXME

// Needs more investigation, but this is probably failing on Android M (API 23)
// and up because the printf formatting of NAN changed.
// XFAIL: LIBCXX-ANDROID-FIXME && !android-device-api={{21|22}}

#include <locale>
#include <ios>
#include <cassert>
#include <cstdio>
#include <streambuf>
#include <cmath>
#include "test_macros.h"
Expand Down Expand Up @@ -8934,11 +8930,10 @@ void test4()
char str[200];
std::locale lc = std::locale::classic();
std::locale lg(lc, new my_numpunct);
#ifdef _AIX
std::string inf = "INF";
#else
std::string inf = "inf";
#endif

// This should match the underlying C library
std::snprintf(str, sizeof(str), "%f", INFINITY);
std::string inf = str;

const my_facet f(1);
{
Expand Down Expand Up @@ -10727,24 +10722,24 @@ void test5()
std::locale lc = std::locale::classic();
std::locale lg(lc, new my_numpunct);
const my_facet f(1);
#if defined(_AIX)
std::string nan= "NaNQ";
std::string NaN = "NaNQ";
std::string nan_padding25 = "*********************";
std::string pnan_sign = "+";
std::string pnan_padding25 = "********************";
#else
std::string nan= "nan";
std::string NaN = "NAN";
std::string nan_padding25 = "**********************";
#if defined(TEST_HAS_GLIBC) || defined(_WIN32)
std::string pnan_sign = "+";
std::string pnan_padding25 = "*********************";
#else
std::string pnan_sign = "";
std::string pnan_padding25 = "**********************";
#endif
#endif

// The output here depends on the underlying C library, so work out what
// that does.
std::snprintf(str, sizeof(str), "%f", std::nan(""));
std::string nan = str;

std::snprintf(str, sizeof(str), "%F", std::nan(""));
std::string NaN = str;

std::snprintf(str, sizeof(str), "%+f", std::nan(""));
std::string pnan_sign;
if (str[0] == '+') {
pnan_sign = "+";
}

std::string nan_padding25 = std::string(25 - nan.length(), '*');
std::string pnan_padding25 = std::string(25 - nan.length() - pnan_sign.length(), '*');

{
long double v = std::nan("");
std::ios ios(0);
Expand Down

0 comments on commit d1f5850

Please sign in to comment.