Stephan T. Lavavej bf5cdd6358
[libc++][test] Fix issues found by MSVC's STL (#131787)
* libcxx/test/support/min_allocator.h
+ Fix `tiny_size_allocator::rebind` which mistakenly said `T` instead of
`U`.
*
libcxx/test/std/algorithms/alg.modifying.operations/alg.partitions/stable_partition.pass.cpp
  + `std::stable_partition` requires bidirectional iterators.
* libcxx/test/std/containers/sequences/vector.bool/max_size.pass.cpp
+ Fix allocator type given to `std::vector<bool>`. The element types are
required to match, [N5008](https://isocpp.org/files/papers/N5008.pdf)
\[container.alloc.reqmts\]/5: "*Mandates:* `allocator_type::value_type`
is the same as `X::value_type`."
* libcxx/test/std/time/time.clock/time.clock.utc/types.compile.pass.cpp
+ Mark `is_steady` as `[[maybe_unused]]`, as it appears within
`LIBCPP_STATIC_ASSERT` only.
*
libcxx/test/std/algorithms/alg.modifying.operations/alg.rotate/rotate.pass.cpp
*
libcxx/test/std/algorithms/alg.modifying.operations/alg.swap/swap_ranges.pass.cpp
* libcxx/test/std/utilities/utility/utility.swap/swap_array.pass.cpp
+ Fix MSVC warning C4127 "conditional expression is constant".
`TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED` was introduced for this
purpose, so it should be used consistently.
* libcxx/test/std/numerics/numeric.ops/numeric.ops.gcd/gcd.pass.cpp
+ Fix `gcd()` precondition violation for `signed char`. This test case
was causing `-128` to be passed as a `signed char` to `gcd()`, which is
forbidden.
* libcxx/test/std/containers/sequences/array/assert.iterators.pass.cpp
*
libcxx/test/std/containers/sequences/vector/vector.modifiers/assert.push_back.invalidation.pass.cpp
*
libcxx/test/std/input.output/iostream.format/print.fun/no_file_description.pass.cpp
+ Split some REQUIRES and XFAIL lines. This is a "nice to have" for
MSVC's internal test harness, which is extremely simple and looks for
exact comment matches to skip tests. We can recognize the specific lines
"REQUIRES: has-unix-headers" and "XFAIL: msvc", but it's a headache to
maintain if they're chained with other conditions.
* libcxx/test/support/sized_allocator.h
+ Fix x86 truncation warnings. `std::allocator` takes `std::size_t`, so
we need to `static_cast`.
*
libcxx/test/std/input.output/file.streams/fstreams/ifstream.members/offset_range.pass.cpp
+ Fix x86 truncation warning. `std::min()` is returning
`std::streamoff`, which was being unnecessarily narrowed to
`std::size_t`.
*
libcxx/test/std/algorithms/alg.sorting/alg.merge/inplace_merge_comp.pass.cpp
+ Fix MSVC warning C4127 "conditional expression is constant" for an
always-true branch. This was very recently introduced by #129008 making
`N` constexpr. As it's a local constant just nine lines above, we don't
need to test whether 100 is greater than 0.
2025-03-20 15:53:25 +08:00

147 lines
5.1 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
//
//===----------------------------------------------------------------------===//
// REQUIRES: has-unix-headers
// REQUIRES: libcpp-has-abi-bounded-iterators-in-std-array
// UNSUPPORTED: c++03
// UNSUPPORTED: libcpp-hardening-mode=none
// XFAIL: libcpp-hardening-mode=debug && availability-verbose_abort-missing
// <array>
// Make sure that std::array's iterators check for OOB accesses when the right hardening settings
// are enabled.
#include <array>
#include <cstddef>
#include <iterator>
#include "check_assertion.h"
template <typename Iter>
void test_iterator(Iter begin, Iter end) {
std::ptrdiff_t distance = std::distance(begin, end);
// Dereferencing an iterator at the end.
{
TEST_LIBCPP_ASSERT_FAILURE(*end, "__static_bounded_iter::operator*: Attempt to dereference an iterator at the end");
TEST_LIBCPP_ASSERT_FAILURE(
end.operator->(), "__static_bounded_iter::operator->: Attempt to dereference an iterator at the end");
}
// Incrementing an iterator past the end.
{
auto it = end;
TEST_LIBCPP_ASSERT_FAILURE(it++, "__static_bounded_iter::operator++: Attempt to advance an iterator past the end");
it = end;
TEST_LIBCPP_ASSERT_FAILURE(++it, "__static_bounded_iter::operator++: Attempt to advance an iterator past the end");
}
// Decrementing an iterator past the start.
{
auto it = begin;
TEST_LIBCPP_ASSERT_FAILURE(it--, "__static_bounded_iter::operator--: Attempt to rewind an iterator past the start");
it = begin;
TEST_LIBCPP_ASSERT_FAILURE(--it, "__static_bounded_iter::operator--: Attempt to rewind an iterator past the start");
}
// Advancing past the end with operator+= and operator+.
{
[[maybe_unused]] const char* msg = "__static_bounded_iter::operator+=: Attempt to advance an iterator past the end";
auto it = end;
TEST_LIBCPP_ASSERT_FAILURE(it += 1, msg);
TEST_LIBCPP_ASSERT_FAILURE(end + 1, msg);
it = begin;
TEST_LIBCPP_ASSERT_FAILURE(it += (distance + 1), msg);
TEST_LIBCPP_ASSERT_FAILURE(begin + (distance + 1), msg);
}
// Advancing past the end with operator-= and operator-.
{
[[maybe_unused]] const char* msg = "__static_bounded_iter::operator-=: Attempt to advance an iterator past the end";
auto it = end;
TEST_LIBCPP_ASSERT_FAILURE(it -= (-1), msg);
TEST_LIBCPP_ASSERT_FAILURE(end - (-1), msg);
it = begin;
TEST_LIBCPP_ASSERT_FAILURE(it -= (-distance - 1), msg);
TEST_LIBCPP_ASSERT_FAILURE(begin - (-distance - 1), msg);
}
// Rewinding past the start with operator+= and operator+.
{
[[maybe_unused]] const char* msg =
"__static_bounded_iter::operator+=: Attempt to rewind an iterator past the start";
auto it = begin;
TEST_LIBCPP_ASSERT_FAILURE(it += (-1), msg);
TEST_LIBCPP_ASSERT_FAILURE(begin + (-1), msg);
it = end;
TEST_LIBCPP_ASSERT_FAILURE(it += (-distance - 1), msg);
TEST_LIBCPP_ASSERT_FAILURE(end + (-distance - 1), msg);
}
// Rewinding past the start with operator-= and operator-.
{
[[maybe_unused]] const char* msg =
"__static_bounded_iter::operator-=: Attempt to rewind an iterator past the start";
auto it = begin;
TEST_LIBCPP_ASSERT_FAILURE(it -= 1, msg);
TEST_LIBCPP_ASSERT_FAILURE(begin - 1, msg);
it = end;
TEST_LIBCPP_ASSERT_FAILURE(it -= (distance + 1), msg);
TEST_LIBCPP_ASSERT_FAILURE(end - (distance + 1), msg);
}
// Out-of-bounds operator[].
{
[[maybe_unused]] const char* end_msg =
"__static_bounded_iter::operator[]: Attempt to index an iterator at or past the end";
[[maybe_unused]] const char* past_end_msg =
"__static_bounded_iter::operator[]: Attempt to index an iterator at or past the end";
[[maybe_unused]] const char* past_start_msg =
"__static_bounded_iter::operator[]: Attempt to index an iterator past the start";
TEST_LIBCPP_ASSERT_FAILURE(begin[distance], end_msg);
TEST_LIBCPP_ASSERT_FAILURE(begin[distance + 1], past_end_msg);
TEST_LIBCPP_ASSERT_FAILURE(begin[-1], past_start_msg);
TEST_LIBCPP_ASSERT_FAILURE(begin[-99], past_start_msg);
if (distance > 0) {
auto it = begin + 1;
TEST_LIBCPP_ASSERT_FAILURE(it[distance - 1], end_msg);
TEST_LIBCPP_ASSERT_FAILURE(it[distance], past_end_msg);
TEST_LIBCPP_ASSERT_FAILURE(it[-2], past_start_msg);
TEST_LIBCPP_ASSERT_FAILURE(it[-99], past_start_msg);
}
}
}
int main(int, char**) {
// Empty array
{
std::array<int, 0> array = {};
// array::iterator
test_iterator(array.begin(), array.end());
// array::const_iterator
test_iterator(array.cbegin(), array.cend());
}
// Non-empty array
{
std::array<int, 10> array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
// array::iterator
test_iterator(array.begin(), array.end());
// array::const_iterator
test_iterator(array.cbegin(), array.cend());
}
return 0;
}