Skip to content

Commit

Permalink
[libc++][test] Make some string tests MSVC-friendly
Browse files Browse the repository at this point in the history
* Using one-or-two letter names for globals is asking for shadowing warnings.
* MSVCSTL's container proxy allocations strike again
* MSVCSTL's `<string>` doesn't define `std::out_of_range`
* `basic_string::substr` takes two arguments of type `size_type`. Let's use that type instead of `size_t` and `ptrdiff_t` to avoid narrowing warnings.

Differential Revision: https://reviews.llvm.org/D141253
  • Loading branch information
CaseyCarter committed Jan 9, 2023
1 parent 9f4a9d3 commit 6b90f67
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
Expand Up @@ -29,9 +29,9 @@ struct throwing_alloc

// Test that it's possible to take the address of basic_string's destructors
// by creating globals which will register their destructors with cxa_atexit.
std::string s;
std::string unused_string;
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::wstring ws;
std::wstring unused_wide_string;
#endif

static_assert(std::is_nothrow_destructible<std::string>::value, "");
Expand All @@ -45,7 +45,8 @@ TEST_CONSTEXPR_CXX20 bool test() {
{
std::basic_string<char, std::char_traits<char>, test_allocator<char>> str2((test_allocator<char>(&alloc_stats)));
str2 = "long long string so no SSO";
assert(alloc_stats.alloc_count == 1);
assert(alloc_stats.alloc_count > 0);
LIBCPP_ASSERT(alloc_stats.alloc_count == 1);
}
assert(alloc_stats.alloc_count == 0);

Expand Down
Expand Up @@ -13,6 +13,7 @@
// constexpr basic_string substr(size_type pos = 0, size_type n = npos) &&;

#include <algorithm>
#include <stdexcept>
#include <string>

#include "constexpr_char_traits.h"
Expand All @@ -26,15 +27,15 @@ constexpr struct should_throw_exception_t {
} should_throw_exception;

template <class S>
constexpr void test(S orig, size_t pos, ptrdiff_t n, S expected) {
constexpr void test(S orig, typename S::size_type pos, typename S::size_type n, const S expected) {
S str = std::move(orig).substr(pos, n);
LIBCPP_ASSERT(orig.__invariants());
LIBCPP_ASSERT(str.__invariants());
assert(str == expected);
}

template <class S>
constexpr void test(S orig, size_t pos, ptrdiff_t n, should_throw_exception_t) {
constexpr void test(S orig, typename S::size_type pos, typename S::size_type n, should_throw_exception_t) {
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!std::is_constant_evaluated()) {
try {
Expand Down

0 comments on commit 6b90f67

Please sign in to comment.