Skip to content

Commit 7918e62

Browse files
authored
[libc++] Test suite portability improvements (#98527)
This patch contains a number of small portability improvements for the test suite, making it easier to run the test suite with other standard library implementations. - Guard checks for _LIBCPP_HARDENING_MODE to avoid -Wundef - Avoid defining _LIBCPP_HARDENING_MODE even when no hardening mode is specified -- we should use the default mode of the library in that case. - Add missing includes and qualify a few function calls. - Avoid opening namespace std to forward declare stdlib containers. The test suite should represent user code, and user code isn't allowed to do that.
1 parent dd4658f commit 7918e62

File tree

11 files changed

+47
-69
lines changed

11 files changed

+47
-69
lines changed

libcxx/test/std/algorithms/alg.sorting/alg.clamp/ranges.clamp.pass.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,9 @@ constexpr bool test() {
108108
return value;
109109
};
110110
assert(std::ranges::clamp(3, 2, 4, std::ranges::less{}, projection_function) == 3);
111-
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
111+
#if defined(_LIBCPP_HARDENING_MODE) && \
112+
_LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_EXTENSIVE && \
113+
_LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
112114
assert(counter <= 3);
113115
#endif
114116
}

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/complexity.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ int main(int, char**) {
6969
std::sort_heap(first, last);
7070
LIBCPP_ASSERT(stats.copied == 0);
7171
LIBCPP_ASSERT(stats.moved <= 2 * n + n * logn);
72-
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
72+
#if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
7373
LIBCPP_ASSERT(stats.compared <= n * logn);
7474
#else
7575
LIBCPP_ASSERT(stats.compared <= 2 * n * logn + debug_comparisons);

libcxx/test/std/algorithms/alg.sorting/alg.heap.operations/sort.heap/ranges_sort_heap.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ void test_complexity() {
246246
std::ranges::sort_heap(first, last, &MyInt::Comp);
247247
LIBCPP_ASSERT(stats.copied == 0);
248248
LIBCPP_ASSERT(stats.moved <= 2 * n + n * logn);
249-
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
249+
#if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
250250
LIBCPP_ASSERT(stats.compared <= n * logn);
251251
#else
252252
LIBCPP_ASSERT(stats.compared <= 2 * n * logn + debug_comparisons);

libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ test_one(unsigned N, unsigned M)
7979
assert(ia[0] == static_cast<int>(N)-1);
8080
assert(ia[N-1] == 0);
8181
assert(std::is_sorted(ia, ia+N, std::greater<value_type>()));
82-
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
82+
#if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
8383
assert(pred.count() <= (N-1));
8484
#endif
8585
}

libcxx/test/std/algorithms/alg.sorting/alg.three.way/lexicographical_compare_three_way_comp.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ constexpr void test_comparator_invocation_count() {
156156
// The comparator is invoked only `min(left.size(), right.size())` times
157157
test_lexicographical_compare<const int*, const int*>(
158158
std::array{0, 1, 2}, std::array{0, 1, 2, 3}, compare_last_digit_counting, std::strong_ordering::less);
159-
#if _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
159+
#if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE != _LIBCPP_HARDENING_MODE_DEBUG
160160
assert(compare_invocation_count <= 3);
161161
#else
162162
assert(compare_invocation_count <= 6);

libcxx/test/std/containers/views/views.span/span.cons/deduct.pass.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ void test_iterator_sentinel() {
5050
assert(s.data() == std::data(arr));
5151
}
5252

53-
#if _LIBCPP_STD_VER >= 26
53+
#if TEST_STD_VER >= 26
5454
// P3029R1: deduction from `integral_constant`
5555
{
5656
std::span s{std::begin(arr), std::integral_constant<size_t, 3>{}};

libcxx/test/std/localization/locale.categories/category.time/locale.time.get.byname/get_date.pass.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ int main(int, char**)
110110
err = std::ios_base::goodbit;
111111
t = std::tm();
112112
I i = f.get_date(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t);
113-
#if _LIBCPP_VERSION
113+
#if defined(_LIBCPP_VERSION)
114114
// libc++ points to the '/' after the month.
115115
assert(base(i) == in+2);
116116
#else
@@ -129,7 +129,7 @@ int main(int, char**)
129129
err = std::ios_base::goodbit;
130130
t = std::tm();
131131
I i = f.get_date(I(in), I(in+sizeof(in)/sizeof(in[0])-1), ios, err, &t);
132-
#if _LIBCPP_VERSION
132+
#if defined(_LIBCPP_VERSION)
133133
// libc++ points to the '/' after the month.
134134
assert(base(i) == in+2);
135135
#else

libcxx/test/support/check_assertion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,7 +392,7 @@ bool ExpectDeath(DeathCause expected_cause, const char* stmt, Func&& func) {
392392
#define EXPECT_STD_TERMINATE(...) \
393393
assert( ExpectDeath(DeathCause::StdTerminate, #__VA_ARGS__, __VA_ARGS__) )
394394

395-
#if _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
395+
#if defined(_LIBCPP_HARDENING_MODE) && _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_DEBUG
396396
#define TEST_LIBCPP_ASSERT_FAILURE(expr, message) \
397397
assert(( ExpectDeath(DeathCause::VerboseAbort, #expr, [&]() { (void)(expr); }, MakeAssertionMessageMatcher(message)) ))
398398
#else

libcxx/test/support/container_test_types.h

Lines changed: 8 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,16 @@
8787

8888
#include <cassert>
8989
#include <cstddef>
90+
#include <deque>
9091
#include <functional>
92+
#include <list>
93+
#include <map>
9194
#include <new>
95+
#include <set>
96+
#include <unordered_map>
97+
#include <unordered_set>
9298
#include <utility>
99+
#include <vector>
93100

94101
#include "test_macros.h"
95102

@@ -420,12 +427,7 @@ bool operator <(CopyInsertable<ID> const& L, CopyInsertable<ID> const& R) {
420427
return L.data < R.data;
421428
}
422429

423-
424-
#ifdef _LIBCPP_BEGIN_NAMESPACE_STD
425-
_LIBCPP_BEGIN_NAMESPACE_STD
426-
#else
427430
namespace std {
428-
#endif
429431
template <int ID>
430432
struct hash< ::CopyInsertable<ID> > {
431433
typedef ::CopyInsertable<ID> argument_type;
@@ -435,34 +437,7 @@ namespace std {
435437
return arg.data;
436438
}
437439
};
438-
template <class T, class Alloc>
439-
class vector;
440-
template <class T, class Alloc>
441-
class deque;
442-
template <class T, class Alloc>
443-
class list;
444-
template <class _Key, class _Value, class _Less, class _Alloc>
445-
class map;
446-
template <class _Key, class _Value, class _Less, class _Alloc>
447-
class multimap;
448-
template <class _Value, class _Less, class _Alloc>
449-
class set;
450-
template <class _Value, class _Less, class _Alloc>
451-
class multiset;
452-
template <class _Key, class _Value, class _Hash, class _Equals, class _Alloc>
453-
class unordered_map;
454-
template <class _Key, class _Value, class _Hash, class _Equals, class _Alloc>
455-
class unordered_multimap;
456-
template <class _Value, class _Hash, class _Equals, class _Alloc>
457-
class unordered_set;
458-
template <class _Value, class _Hash, class _Equals, class _Alloc>
459-
class unordered_multiset;
460-
461-
#ifdef _LIBCPP_END_NAMESPACE_STD
462-
_LIBCPP_END_NAMESPACE_STD
463-
#else
464-
} // end namespace std
465-
#endif
440+
}
466441

467442
// TCT - Test container type
468443
namespace TCT {

libcxx/test/support/filesystem_test_helper.h

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <chrono>
1919
#include <cstdint>
2020
#include <cstdio> // for printf
21+
#include <cstring>
2122
#include <string>
2223
#include <system_error>
2324
#include <type_traits>
@@ -232,9 +233,9 @@ struct scoped_test_env
232233
if (size >
233234
static_cast<typename std::make_unsigned<utils::off64_t>::type>(
234235
std::numeric_limits<utils::off64_t>::max())) {
235-
fprintf(stderr, "create_file(%s, %ju) too large\n",
236-
filename.c_str(), size);
237-
abort();
236+
std::fprintf(stderr, "create_file(%s, %ju) too large\n",
237+
filename.c_str(), size);
238+
std::abort();
238239
}
239240

240241
#if defined(_WIN32) || defined(__MVS__)
@@ -244,20 +245,20 @@ struct scoped_test_env
244245
#endif
245246
FILE* file = utils::fopen64(filename.c_str(), "w" FOPEN_CLOEXEC_FLAG);
246247
if (file == nullptr) {
247-
fprintf(stderr, "fopen %s failed: %s\n", filename.c_str(),
248-
strerror(errno));
249-
abort();
248+
std::fprintf(stderr, "fopen %s failed: %s\n", filename.c_str(),
249+
std::strerror(errno));
250+
std::abort();
250251
}
251252

252253
if (utils::ftruncate64(
253254
fileno(file), static_cast<utils::off64_t>(size)) == -1) {
254-
fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
255-
size, strerror(errno));
256-
fclose(file);
257-
abort();
255+
std::fprintf(stderr, "ftruncate %s %ju failed: %s\n", filename.c_str(),
256+
size, std::strerror(errno));
257+
std::fclose(file);
258+
std::abort();
258259
}
259260

260-
fclose(file);
261+
std::fclose(file);
261262
return filename;
262263
}
263264

@@ -616,10 +617,9 @@ struct ExceptionChecker {
616617
}();
617618
assert(format == Err.what());
618619
if (format != Err.what()) {
619-
fprintf(stderr,
620-
"filesystem_error::what() does not match expected output:\n");
621-
fprintf(stderr, " expected: \"%s\"\n", format.c_str());
622-
fprintf(stderr, " actual: \"%s\"\n\n", Err.what());
620+
std::fprintf(stderr, "filesystem_error::what() does not match expected output:\n");
621+
std::fprintf(stderr, " expected: \"%s\"\n", format.c_str());
622+
std::fprintf(stderr, " actual: \"%s\"\n\n", Err.what());
623623
}
624624
}
625625

@@ -639,23 +639,23 @@ inline fs::path GetWindowsInaccessibleDir() {
639639
continue;
640640
// Basic sanity checks on the directory_entry
641641
if (!ent.exists() || !ent.is_directory()) {
642-
fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
643-
"but doesn't behave as expected, skipping tests "
644-
"regarding it\n", dir.string().c_str());
642+
std::fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
643+
"but doesn't behave as expected, skipping tests "
644+
"regarding it\n", dir.string().c_str());
645645
return fs::path();
646646
}
647647
// Check that it indeed is inaccessible as expected
648648
(void)fs::exists(ent, ec);
649649
if (!ec) {
650-
fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
651-
"but seems to be accessible, skipping tests "
652-
"regarding it\n", dir.string().c_str());
650+
std::fprintf(stderr, "The expected inaccessible directory \"%s\" was found "
651+
"but seems to be accessible, skipping tests "
652+
"regarding it\n", dir.string().c_str());
653653
return fs::path();
654654
}
655655
return ent;
656656
}
657-
fprintf(stderr, "No inaccessible directory \"%s\" found, skipping tests "
658-
"regarding it\n", dir.string().c_str());
657+
std::fprintf(stderr, "No inaccessible directory \"%s\" found, skipping tests "
658+
"regarding it\n", dir.string().c_str());
659659
return fs::path();
660660
}
661661

0 commit comments

Comments
 (0)