Initial commit

This commit is contained in:
Yervant7
2024-01-16 19:28:29 -03:00
commit 8c0524dc0f
9054 changed files with 1081727 additions and 0 deletions
@@ -0,0 +1,14 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// The container's value type must be the same as the allocator's value type
#include <string>
std::basic_string<char, std::char_traits<char>, std::allocator<int> > s; // expected-error@*:* {{Allocator::value_type must be same type as value_type}}
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// ... manipulating sequences of any non-array trivial standard-layout types.
#include <string>
#include "test_traits.h"
struct NotTrivial {
NotTrivial() : value(3) {}
int value;
};
struct NotStandardLayout {
public:
NotStandardLayout() : one(1), two(2) {}
int sum() const { return one + two; } // silences "unused field 'two' warning"
int one;
private:
int two;
};
void f() {
{
// array
typedef char C[3];
static_assert(std::is_array<C>::value, "");
std::basic_string<C, test_traits<C> > s;
// expected-error-re@string:* {{{{(static_assert|static assertion)}} failed{{.*}}Character type of basic_string must not be an array}}
}
{
// not trivial
static_assert(!std::is_trivial<NotTrivial>::value, "");
std::basic_string<NotTrivial, test_traits<NotTrivial> > s;
// expected-error-re@string:* {{{{(static_assert|static assertion)}} failed{{.*}}Character type of basic_string must be trivial}}
}
{
// not standard layout
static_assert(!std::is_standard_layout<NotStandardLayout>::value, "");
std::basic_string<NotStandardLayout, test_traits<NotStandardLayout> > s;
// expected-error-re@string:* {{{{(static_assert|static assertion)}} failed{{.*}}Character type of basic_string must be standard-layout}}
}
}
@@ -0,0 +1,37 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// UNSUPPORTED: libcpp-has-no-incomplete-ranges
// string
#include <string>
#include <concepts>
#include <ranges>
static_assert(std::same_as<std::ranges::iterator_t<std::string>, std::string::iterator>);
static_assert(std::ranges::common_range<std::string>);
static_assert(std::ranges::random_access_range<std::string>);
static_assert(std::ranges::contiguous_range<std::string>);
static_assert(!std::ranges::view<std::string>);
static_assert(std::ranges::sized_range<std::string>);
static_assert(!std::ranges::borrowed_range<std::string>);
static_assert(std::ranges::viewable_range<std::string>);
static_assert(std::same_as<std::ranges::iterator_t<std::string const>, std::string::const_iterator>);
static_assert(std::ranges::common_range<std::string const>);
static_assert(std::ranges::random_access_range<std::string const>);
static_assert(std::ranges::contiguous_range<std::string const>);
static_assert(!std::ranges::view<std::string const>);
static_assert(std::ranges::sized_range<std::string const>);
static_assert(!std::ranges::borrowed_range<std::string const>);
static_assert(!std::ranges::viewable_range<std::string const>);
@@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_reference at(size_type pos) const; // constexpr since C++20
// reference at(size_type pos); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "min_allocator.h"
#include "test_macros.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type pos)
{
const S& cs = s;
if (pos < s.size())
{
assert(s.at(pos) == s[pos]);
assert(cs.at(pos) == cs[pos]);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
TEST_IGNORE_NODISCARD s.at(pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos >= s.size());
}
try
{
TEST_IGNORE_NODISCARD cs.at(pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos >= s.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0);
test(S("123"), 0);
test(S("123"), 1);
test(S("123"), 2);
test(S("123"), 3);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0);
test(S("123"), 0);
test(S("123"), 1);
test(S("123"), 2);
test(S("123"), 3);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const charT& back() const; // constexpr since C++20
// charT& back(); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
ASSERT_SAME_TYPE(decltype( s.back()), typename S::reference);
ASSERT_SAME_TYPE(decltype(cs.back()), typename S::const_reference);
LIBCPP_ASSERT_NOEXCEPT( s.back());
LIBCPP_ASSERT_NOEXCEPT( cs.back());
assert(&cs.back() == &cs[cs.size()-1]);
assert(&s.back() == &s[cs.size()-1]);
s.back() = typename S::value_type('z');
assert(s.back() == typename S::value_type('z'));
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S("1"));
test(S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S("1"));
test(S("1234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const charT& front() const; // constexpr since C++20
// charT& front(); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
ASSERT_SAME_TYPE(decltype( s.front()), typename S::reference);
ASSERT_SAME_TYPE(decltype(cs.front()), typename S::const_reference);
LIBCPP_ASSERT_NOEXCEPT( s.front());
LIBCPP_ASSERT_NOEXCEPT( cs.front());
assert(&cs.front() == &cs[0]);
assert(&s.front() == &s[0]);
s.front() = typename S::value_type('z');
assert(s.front() == typename S::value_type('z'));
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S("1"));
test(S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S("1"));
test(S("1234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,69 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_reference operator[](size_type pos) const; // constexpr since C++20
// reference operator[](size_type pos); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s("0123456789");
const S& cs = s;
ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
LIBCPP_ASSERT_NOEXCEPT( s[0]);
LIBCPP_ASSERT_NOEXCEPT( cs[0]);
for (S::size_type i = 0; i < cs.size(); ++i)
{
assert(s[i] == static_cast<char>('0' + i));
assert(cs[i] == s[i]);
}
assert(cs[cs.size()] == '\0');
const S s2 = S();
assert(s2[0] == '\0');
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s("0123456789");
const S& cs = s;
ASSERT_SAME_TYPE(decltype( s[0]), typename S::reference);
ASSERT_SAME_TYPE(decltype(cs[0]), typename S::const_reference);
LIBCPP_ASSERT_NOEXCEPT( s[0]);
LIBCPP_ASSERT_NOEXCEPT( cs[0]);
for (S::size_type i = 0; i < cs.size(); ++i)
{
assert(s[i] == static_cast<char>('0' + i));
assert(cs[i] == s[i]);
}
assert(cs[cs.size()] == '\0');
const S s2 = S();
assert(s2[0] == '\0');
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,75 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// size_type capacity() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_allocator.h"
#include "min_allocator.h"
#include "test_macros.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, test_allocator_statistics& alloc_stats)
{
alloc_stats.throw_after = 0;
#ifndef TEST_HAS_NO_EXCEPTIONS
try
#endif
{
while (s.size() < s.capacity())
s.push_back(typename S::value_type());
assert(s.size() == s.capacity());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
catch (...)
{
assert(false);
}
#endif
alloc_stats.throw_after = INT_MAX;
}
TEST_CONSTEXPR_CXX20 bool test() {
{
test_allocator_statistics alloc_stats;
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > S;
S s((test_allocator<char>(&alloc_stats)));
test(s, alloc_stats);
s.assign(10, 'a');
s.erase(5);
test(s, alloc_stats);
s.assign(100, 'a');
s.erase(50);
test(s, alloc_stats);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
assert(s.capacity() > 0);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,68 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void clear(); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
s.clear();
assert(s.size() == 0);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s;
test(s);
s.assign(10, 'a');
s.erase(5);
test(s);
s.assign(100, 'a');
s.erase(50);
test(s);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
test(s);
s.assign(10, 'a');
s.erase(5);
test(s);
s.assign(100, 'a');
s.erase(50);
test(s);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,54 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// bool empty() const noexcept; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
ASSERT_NOEXCEPT(s.empty());
assert(s.empty() == (s.size() == 0));
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,36 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// class deque
// bool empty() const noexcept; // constexpr since C++20
// UNSUPPORTED: c++03, c++11, c++14, c++17
#include <string>
#include "test_macros.h"
TEST_CONSTEXPR_CXX20 bool test() {
std::string c;
c.empty(); // expected-warning {{ignoring return value of function declared with 'nodiscard' attribute}}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// size_type length() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
assert(s.length() == s.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,95 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: no-exceptions
// <string>
// size_type max_size() const; // constexpr since C++20
// NOTE: asan and msan will fail for one of two reasons
// 1. If allocator_may_return_null=0 then they will fail because the allocation
// returns null.
// 2. If allocator_may_return_null=1 then they will fail because the allocation
// is too large to succeed.
// UNSUPPORTED: sanitizer-new-delete
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test1(const S& s)
{
S s2(s);
const size_t sz = s2.max_size() - 1;
try { s2.resize(sz, 'x'); }
catch ( const std::bad_alloc & ) { return ; }
assert ( s2.size() == sz );
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test2(const S& s)
{
S s2(s);
const size_t sz = s2.max_size();
try { s2.resize(sz, 'x'); }
catch ( const std::bad_alloc & ) { return ; }
assert ( s.size() == sz );
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
assert(s.max_size() >= s.size());
test1(s);
test2(s);
}
void test() {
{
typedef std::string S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#endif
}
#if TEST_STD_VER > 17
constexpr bool test_constexpr() {
std::string str;
size_t size = str.max_size();
assert(size > 0);
return true;
}
#endif
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
test_constexpr();
static_assert(test_constexpr());
#endif
return 0;
}
@@ -0,0 +1,64 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: no-exceptions
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx10.{{9|10|11}}
// Prior to http://llvm.org/D123580, there was a bug with how the max_size()
// was calculated. That was inlined into some functions in the dylib, which leads
// to failures when running this test against an older system dylib.
// XFAIL: use_system_cxx_lib && target=arm64-apple-macosx{{11.0|12.0}}
// <string>
// size_type max_size() const; // constexpr since C++20
#include <string>
#include <cassert>
#include <stdexcept>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
void
test(const S& s)
{
assert(s.max_size() >= s.size());
S s2(s);
const size_t sz = s2.max_size() + 1;
try { s2.resize(sz, 'x'); }
catch ( const std::length_error & ) { return ; }
assert ( false );
}
bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
test(S("12345678901234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
return 0;
}
@@ -0,0 +1,22 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void reserve(); // Deprecated in C++20
// UNSUPPORTED: c++03, c++11, c++14, c++17
#include <string>
int main(int, char**)
{
std::string s;
s.reserve(); // expected-warning {{'reserve' is deprecated}}
return 0;
}
@@ -0,0 +1,68 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void reserve(); // Deprecated in C++20.
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
void
test(typename S::size_type min_cap, typename S::size_type erased_index)
{
S s(min_cap, 'a');
s.erase(erased_index);
assert(s.size() == erased_index);
assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
typename S::size_type old_cap = s.capacity();
S s0 = s;
s.reserve();
LIBCPP_ASSERT(s.__invariants());
assert(s == s0);
assert(s.capacity() <= old_cap);
assert(s.capacity() >= s.size());
}
bool test() {
{
typedef std::string S;
{
test<S>(0, 0);
test<S>(10, 5);
test<S>(100, 50);
}
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
{
test<S>(0, 0);
test<S>(10, 5);
test<S>(100, 50);
}
}
#endif
return true;
}
int main(int, char**)
{
test();
return 0;
}
@@ -0,0 +1,112 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void reserve(size_type res_arg); // constexpr since C++20
// This test relies on https://llvm.org/PR45368 being fixed, which isn't in
// older Apple dylibs
// XFAIL: use_system_cxx_lib && target={{.+}}-apple-macosx{{10.9|10.10|10.11|10.12|10.13|10.14|10.15|11.0}}
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(typename S::size_type min_cap, typename S::size_type erased_index, typename S::size_type res_arg)
{
S s(min_cap, 'a');
s.erase(erased_index);
assert(s.size() == erased_index);
assert(s.capacity() >= min_cap); // Check that we really have at least this capacity.
#if TEST_STD_VER > 17
typename S::size_type old_cap = s.capacity();
#endif
S s0 = s;
if (res_arg <= s.max_size())
{
s.reserve(res_arg);
LIBCPP_ASSERT(s.__invariants());
assert(s == s0);
assert(s.capacity() >= res_arg);
assert(s.capacity() >= s.size());
#if TEST_STD_VER > 17
assert(s.capacity() >= old_cap); // reserve never shrinks as of P0966 (C++20)
#endif
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.reserve(res_arg);
LIBCPP_ASSERT(s.__invariants());
assert(false);
}
catch (std::length_error&)
{
assert(res_arg > s.max_size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
{
test<S>(0, 0, 5);
test<S>(0, 0, 10);
test<S>(0, 0, 50);
}
{
test<S>(100, 50, 5);
test<S>(100, 50, 10);
test<S>(100, 50, 50);
test<S>(100, 50, 100);
test<S>(100, 50, 1000);
test<S>(100, 50, S::npos);
}
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
{
test<S>(0, 0, 5);
test<S>(0, 0, 10);
test<S>(0, 0, 50);
}
{
test<S>(100, 50, 5);
test<S>(100, 50, 10);
test<S>(100, 50, 50);
test<S>(100, 50, 100);
test<S>(100, 50, 1000);
test<S>(100, 50, S::npos);
}
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,103 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <string>
// template<class Operation>
// void resize_and_overwrite(size_type n, Operation op)
#include <algorithm>
#include <cassert>
#include <string>
#include "make_string.h"
#include "test_macros.h"
template <class S>
constexpr void test_appending(size_t k, size_t N, size_t new_capacity) {
assert(N > k);
assert(new_capacity >= N);
auto s = S(k, 'a');
s.resize_and_overwrite(new_capacity, [&](auto* p, auto n) {
assert(n == new_capacity);
LIBCPP_ASSERT(s.size() == new_capacity);
LIBCPP_ASSERT(s.begin().base() == p);
assert(std::all_of(p, p + k, [](const auto ch) { return ch == 'a'; }));
std::fill(p + k, p + n, 'b');
p[n] = 'c'; // will be overwritten
return N;
});
const S expected = S(k, 'a') + S(N - k, 'b');
assert(s == expected);
assert(s.c_str()[N] == '\0');
}
template <class S>
constexpr void test_truncating(size_t o, size_t N) {
assert(N < o);
auto s = S(o, 'a');
s.resize_and_overwrite(N, [&](auto* p, auto n) {
assert(n == N);
LIBCPP_ASSERT(s.size() == n);
LIBCPP_ASSERT(s.begin().base() == p);
assert(std::all_of(p, p + n, [](auto ch) { return ch == 'a'; }));
p[n - 1] = 'b';
p[n] = 'c'; // will be overwritten
return n;
});
const S expected = S(N - 1, 'a') + S(1, 'b');
assert(s == expected);
assert(s.c_str()[N] == '\0');
}
template <class CharT>
constexpr bool test() {
using S = std::basic_string<CharT>;
test_appending<S>(10, 15, 15);
test_appending<S>(10, 15, 20);
test_appending<S>(10, 40, 40);
test_appending<S>(10, 40, 50);
test_appending<S>(30, 35, 35);
test_appending<S>(30, 35, 45);
test_appending<S>(10, 15, 30);
test_truncating<S>(15, 10);
test_truncating<S>(40, 35);
test_truncating<S>(40, 10);
return true;
}
void test_value_categories() {
std::string s;
s.resize_and_overwrite(10, [](char*&&, size_t&&) { return 0; });
s.resize_and_overwrite(10, [](char* const&, const size_t&) { return 0; });
struct RefQualified {
int operator()(char*, size_t) && { return 0; }
};
s.resize_and_overwrite(10, RefQualified{});
}
int main(int, char**) {
test<char>();
test<char8_t>();
test<char16_t>();
test<char32_t>();
static_assert(test<char>());
static_assert(test<char8_t>());
static_assert(test<char16_t>());
static_assert(test<char32_t>());
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
static_assert(test<wchar_t>());
#endif
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void resize(size_type n); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type n, S expected)
{
if (n <= s.max_size())
{
s.resize(n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.resize(n);
assert(false);
}
catch (std::length_error&)
{
assert(n > s.max_size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, S());
test(S(), 1, S(1, '\0'));
test(S(), 10, S(10, '\0'));
test(S(), 100, S(100, '\0'));
test(S("12345"), 0, S());
test(S("12345"), 2, S("12"));
test(S("12345"), 5, S("12345"));
test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
test(S("12345678901234567890123456789012345678901234567890"), 0, S());
test(S("12345678901234567890123456789012345678901234567890"), 10,
S("1234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 50,
S("12345678901234567890123456789012345678901234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 60,
S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
test(S(), S::npos, S("not going to happen"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0, S());
test(S(), 1, S(1, '\0'));
test(S(), 10, S(10, '\0'));
test(S(), 100, S(100, '\0'));
test(S("12345"), 0, S());
test(S("12345"), 2, S("12"));
test(S("12345"), 5, S("12345"));
test(S("12345"), 15, S("12345\0\0\0\0\0\0\0\0\0\0", 15));
test(S("12345678901234567890123456789012345678901234567890"), 0, S());
test(S("12345678901234567890123456789012345678901234567890"), 10,
S("1234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 50,
S("12345678901234567890123456789012345678901234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 60,
S("12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0", 60));
test(S(), S::npos, S("not going to happen"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void resize(size_type n, charT c); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type n, typename S::value_type c, S expected)
{
if (n <= s.max_size())
{
s.resize(n, c);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.resize(n, c);
assert(false);
}
catch (std::length_error&)
{
assert(n > s.max_size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S("a"));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S());
test(S("12345"), 2, 'a', S("12"));
test(S("12345"), 5, 'a', S("12345"));
test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
S("1234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
S("12345678901234567890123456789012345678901234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
test(S(), S::npos, 'a', S("not going to happen"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S("a"));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S());
test(S("12345"), 2, 'a', S("12"));
test(S("12345"), 5, 'a', S("12345"));
test(S("12345"), 15, 'a', S("12345aaaaaaaaaa"));
test(S("12345678901234567890123456789012345678901234567890"), 0, 'a', S());
test(S("12345678901234567890123456789012345678901234567890"), 10, 'a',
S("1234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 50, 'a',
S("12345678901234567890123456789012345678901234567890"));
test(S("12345678901234567890123456789012345678901234567890"), 60, 'a',
S("12345678901234567890123456789012345678901234567890aaaaaaaaaa"));
test(S(), S::npos, 'a', S("not going to happen"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// This test checks that we can explicitly instantiate std::string with a custom
// character type and traits and then use `shrink_to_fit`. In particular, this is
// a regression test for the bug that was reported at https://stackoverflow.com/q/69520633/627587
// and https://seedcentral.apple.com/sm/feedback_collector/radar/85053279.
// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU1 -c -o %t.tu1.o
// RUN: %{cxx} %{flags} %{compile_flags} %s %{link_flags} -DTU2 -c -o %t.tu2.o
// RUN: %{cxx} %{flags} %t.tu1.o %t.tu2.o %{link_flags} -o %t.exe
// UNSUPPORTED: no-localization
#include <cstdint>
#include <ios>
#include <string>
typedef std::uint16_t char16;
struct string16_char_traits {
typedef char16 char_type;
typedef int int_type;
typedef std::streamoff off_type;
typedef std::mbstate_t state_type;
typedef std::fpos<state_type> pos_type;
static void assign(char_type&, const char_type&) { }
static bool eq(const char_type&, const char_type&) { return false; }
static bool lt(const char_type&, const char_type&) { return false; }
static int compare(const char_type*, const char_type*, size_t) { return 0; }
static size_t length(const char_type*) { return 0; }
static const char_type* find(const char_type*, size_t, const char_type&) { return nullptr; }
static char_type* move(char_type*, const char_type*, size_t) { return nullptr; }
static char_type* copy(char_type*, const char_type*, size_t) { return nullptr; }
static char_type* assign(char_type*, size_t, char_type) { return nullptr; }
static int_type not_eof(const int_type&) { return 0; }
static char_type to_char_type(const int_type&) { return char_type(); }
static int_type to_int_type(const char_type&) { return int_type(); }
static bool eq_int_type(const int_type&, const int_type&) { return false; }
static int_type eof() { return int_type(); }
};
#if defined(TU1)
template class std::basic_string<char16, string16_char_traits>;
#else
extern template class std::basic_string<char16, string16_char_traits>;
int main() {
std::basic_string<char16, string16_char_traits> s;
s.shrink_to_fit();
}
#endif
@@ -0,0 +1,73 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void shrink_to_fit(); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
typename S::size_type old_cap = s.capacity();
S s0 = s;
s.shrink_to_fit();
LIBCPP_ASSERT(s.__invariants());
assert(s == s0);
assert(s.capacity() <= old_cap);
assert(s.capacity() >= s.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s;
test(s);
s.assign(10, 'a');
s.erase(5);
test(s);
s.assign(100, 'a');
s.erase(50);
test(s);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
test(s);
s.assign(10, 'a');
s.erase(5);
test(s);
s.assign(100, 'a');
s.erase(50);
test(s);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,53 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// size_type size() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s, typename S::size_type c)
{
assert(s.size() == c);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0);
test(S("123"), 3);
test(S("12345678901234567890123456789012345678901234567890"), 50);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0);
test(S("123"), 3);
test(S("12345678901234567890123456789012345678901234567890"), 50);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,196 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template<class _Tp>
// basic_string(const _Tp& __t, size_type __pos, size_type __n,
// const allocator_type& __a = allocator_type()); // constexpr since C++20
//
// Mostly we're testing string_view here
#include <string>
#include <string_view>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(SV sv, std::size_t pos, std::size_t n)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
typedef typename S::size_type Size;
if (pos <= sv.size())
{
S s2(sv, static_cast<Size>(pos), static_cast<Size>(n));
LIBCPP_ASSERT(s2.__invariants());
assert(pos <= sv.size());
std::size_t rlen = std::min(sv.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
S s2(sv, static_cast<Size>(pos), static_cast<Size>(n));
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(SV sv, std::size_t pos, std::size_t n, const typename S::allocator_type& a)
{
typedef typename S::traits_type T;
typedef typename S::size_type Size;
if (pos <= sv.size())
{
S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a);
LIBCPP_ASSERT(s2.__invariants());
assert(pos <= sv.size());
std::size_t rlen = std::min(sv.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), sv.data() + pos, rlen) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
S s2(sv, static_cast<Size>(pos), static_cast<Size>(n), a);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
typedef std::basic_string <char, std::char_traits<char>, A> S;
test<S,SV>(SV(), 0, 0);
test<S,SV>(SV(), 0, 1);
test<S,SV>(SV(), 1, 0);
test<S,SV>(SV(), 1, 1);
test<S,SV>(SV(), 1, 2);
test<S,SV>(SV("1"), 0, 0);
test<S,SV>(SV("1"), 0, 1);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100);
test<S,SV>(SV(), 0, 0, A(4));
test<S,SV>(SV(), 0, 1, A(4));
test<S,SV>(SV(), 1, 0, A(4));
test<S,SV>(SV(), 1, 1, A(4));
test<S,SV>(SV(), 1, 2, A(4));
test<S,SV>(SV("1"), 0, 0, A(6));
test<S,SV>(SV("1"), 0, 1, A(6));
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A(8));
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A(8));
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A(8));
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A(8));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
typedef std::basic_string <char, std::char_traits<char>, A> S;
test<S,SV>(SV(), 0, 0);
test<S,SV>(SV(), 0, 1);
test<S,SV>(SV(), 1, 0);
test<S,SV>(SV(), 1, 1);
test<S,SV>(SV(), 1, 2);
test<S,SV>(SV("1"), 0, 0);
test<S,SV>(SV("1"), 0, 1);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10);
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100);
test<S,SV>(SV(), 0, 0, A());
test<S,SV>(SV(), 0, 1, A());
test<S,SV>(SV(), 1, 0, A());
test<S,SV>(SV(), 1, 1, A());
test<S,SV>(SV(), 1, 2, A());
test<S,SV>(SV("1"), 0, 0, A());
test<S,SV>(SV("1"), 0, 1, A());
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 0, A());
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 1, A());
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 10, A());
test<S,SV>(SV("1234567890123456789012345678901234567890123456789012345678901234567890"), 50, 100, A());
}
#endif
{
typedef std::string S;
typedef std::string_view SV;
S s = "ABCD";
SV sv = "EFGH";
char arr[] = "IJKL";
S s1("CDEF", 4); // calls ctor(const char *, len)
assert(s1 == "CDEF");
S s2("QRST", 0, 3); // calls ctor(string("QRST", pos, len)
assert(s2 == "QRS");
S s3(sv, 0, std::string::npos); // calls ctor(T, pos, npos)
assert(s3 == sv);
S s4(sv, 0, 3); // calls ctor(T, pos, len)
assert(s4 == "EFG");
S s5(arr, 0, 2); // calls ctor(const char *, len)
assert(s5 == "IJ");
S s6(arr, 0); // calls ctor(const char *, len)
assert(s6 == "");
S s7(s.data(), 2); // calls ctor(const char *, len)
assert(s7 == "AB");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,106 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// explicit basic_string(const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test()
{
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
#endif
S s;
LIBCPP_ASSERT(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type());
}
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{typename S::allocator_type{}})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
#endif
S s(typename S::allocator_type(5));
LIBCPP_ASSERT(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type(5));
}
}
#if TEST_STD_VER >= 11
template <class S>
TEST_CONSTEXPR_CXX20 void
test2()
{
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == noexcept(typename S::allocator_type())), "" );
#endif
S s;
LIBCPP_ASSERT(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type());
}
{
#if TEST_STD_VER > 14
static_assert((noexcept(S{typename S::allocator_type{}})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S(typename S::allocator_type())) == std::is_nothrow_copy_constructible<typename S::allocator_type>::value), "" );
#endif
S s(typename S::allocator_type{});
LIBCPP_ASSERT(s.__invariants());
assert(s.data());
assert(s.size() == 0);
assert(s.capacity() >= s.size());
assert(s.get_allocator() == typename S::allocator_type());
}
}
#endif
TEST_CONSTEXPR_CXX20 bool test() {
test<std::basic_string<char, std::char_traits<char>, test_allocator<char> > >();
#if TEST_STD_VER >= 11
test2<std::basic_string<char, std::char_traits<char>, min_allocator<char> > >();
test2<std::basic_string<char, std::char_traits<char>, explicit_allocator<char> > >();
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
TEST_CONSTEXPR_CXX20 bool test() {
// Test that assignment from {} and {ptr, len} are allowed and are not
// ambiguous.
{
std::string s = "hello world";
s = {};
assert(s.empty());
}
{
std::string s = "hello world";
s = {"abc", 2};
assert(s == "ab");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>& operator=(charT c); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1, typename S::value_type s2)
{
typedef typename S::traits_type T;
s1 = s2;
LIBCPP_ASSERT(s1.__invariants());
assert(s1.size() == 1);
assert(T::eq(s1[0], s2));
assert(s1.capacity() >= s1.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 'a');
test(S("1"), 'a');
test(S("123456789"), 'a');
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), 'a');
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 'a');
test(S("1"), 'a');
test(S("123456789"), 'a');
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), 'a');
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,60 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const basic_string<charT,traits,Allocator>& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1)
{
S s2 = s1;
LIBCPP_ASSERT(s2.__invariants());
assert(s2 == s1);
assert(s2.capacity() >= s2.size());
assert(s2.get_allocator() == s1.get_allocator());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)));
test(S("1", A(5)));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A{}));
test(S("1", A()));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,142 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const basic_string& str, const Allocator& alloc); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
#ifndef TEST_HAS_NO_EXCEPTIONS
struct alloc_imp {
bool active;
TEST_CONSTEXPR alloc_imp() : active(true) {}
template <class T>
T* allocate(std::size_t n)
{
if (active)
return static_cast<T*>(std::malloc(n * sizeof(T)));
else
throw std::bad_alloc();
}
template <class T>
void deallocate(T* p, std::size_t) { std::free(p); }
void activate () { active = true; }
void deactivate() { active = false; }
};
template <class T>
struct poca_alloc {
typedef T value_type;
typedef std::true_type propagate_on_container_copy_assignment;
alloc_imp *imp;
TEST_CONSTEXPR poca_alloc(alloc_imp *imp_) : imp (imp_) {}
template <class U>
TEST_CONSTEXPR poca_alloc(const poca_alloc<U>& other) : imp(other.imp) {}
T* allocate (std::size_t n) { return imp->allocate<T>(n);}
void deallocate(T* p, std::size_t n) { imp->deallocate(p, n); }
};
template <typename T, typename U>
bool operator==(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs)
{
return lhs.imp == rhs.imp;
}
template <typename T, typename U>
bool operator!=(const poca_alloc<T>& lhs, const poca_alloc<U>& rhs)
{
return lhs.imp != rhs.imp;
}
template <class S>
TEST_CONSTEXPR_CXX20 void test_assign(S &s1, const S& s2)
{
try { s1 = s2; }
catch ( std::bad_alloc &) { return; }
assert(false);
}
#endif
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1, const typename S::allocator_type& a)
{
S s2(s1, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2 == s1);
assert(s2.capacity() >= s2.size());
assert(s2.get_allocator() == a);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A(3));
test(S("1"), A(5));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(), A());
test(S("1"), A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!TEST_IS_CONSTANT_EVALUATED) {
typedef poca_alloc<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
const char * p1 = "This is my first string";
const char * p2 = "This is my second string";
alloc_imp imp1;
alloc_imp imp2;
S s1(p1, A(&imp1));
S s2(p2, A(&imp2));
assert(s1 == p1);
assert(s2 == p2);
imp2.deactivate();
test_assign(s1, s2);
assert(s1 == p1);
assert(s2 == p2);
}
#endif
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,90 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(const basic_string<charT,traits,Allocator>& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1, const S& s2)
{
s1 = s2;
LIBCPP_ASSERT(s1.__invariants());
assert(s1 == s2);
assert(s1.capacity() >= s1.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#endif
#if TEST_STD_VER > 3
{ // LWG 2946
std::string s;
s = {"abc", 1};
assert(s.size() == 1);
assert(s == "a");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,43 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string() noexcept(is_nothrow_default_constructible<allocator_type>::value); // constexpr since C++20
#include <cassert>
#include <string>
#include "test_macros.h"
#include "test_allocator.h"
#if TEST_STD_VER >= 11
// Test the noexcept specification, which is a conforming extension
LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<std::string>::value, "");
LIBCPP_STATIC_ASSERT(std::is_nothrow_default_constructible<
std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
LIBCPP_STATIC_ASSERT(!std::is_nothrow_default_constructible<
std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>>>::value, "");
#endif
TEST_CONSTEXPR_CXX20 bool test() {
std::string str;
assert(str.empty());
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,63 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// ~basic_string() // implied noexcept; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
template <class T>
struct throwing_alloc
{
typedef T value_type;
throwing_alloc(const throwing_alloc&);
T *allocate(size_t);
~throwing_alloc() noexcept(false);
};
// 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;
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::wstring ws;
#endif
static_assert(std::is_nothrow_destructible<std::string>::value, "");
static_assert(std::is_nothrow_destructible<
std::basic_string<char, std::char_traits<char>, test_allocator<char>>>::value, "");
LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<
std::basic_string<char, std::char_traits<char>, throwing_alloc<char>>>::value, "");
TEST_CONSTEXPR_CXX20 bool test() {
test_allocator_statistics alloc_stats;
{
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);
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,379 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14
// <string>
// Test that the constructors offered by std::basic_string are formulated
// so they're compatible with implicit deduction guides.
#include <string>
#include <string_view>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "test_iterators.h"
#include "constexpr_char_traits.h"
template <class T, class Alloc = std::allocator<T>>
using BStr = std::basic_string<T, std::char_traits<T>, Alloc>;
// Overloads
// using A = Allocator;
// using BS = basic_string
// using BSV = basic_string_view
// ---------------
// (1) basic_string() - NOT TESTED
// (2) basic_string(A const&) - BROKEN
// (3) basic_string(size_type, CharT, const A& = A())
// (4) basic_string(BS const&, size_type, A const& = A())
// (5) basic_string(BS const&, size_type, size_type, A const& = A())
// (6) basic_string(const CharT*, size_type, A const& = A())
// (7) basic_string(const CharT*, A const& = A())
// (8) basic_string(InputIt, InputIt, A const& = A()) - BROKEN
// (9) basic_string(BS const&)
// (10) basic_string(BS const&, A const&)
// (11) basic_string(BS&&)
// (12) basic_string(BS&&, A const&)
// (13) basic_string(initializer_list<CharT>, A const& = A())
// (14) basic_string(BSV, A const& = A())
// (15) basic_string(const T&, size_type, size_type, A const& = A())
TEST_CONSTEXPR_CXX20 bool test() {
using TestSizeT = test_allocator<char>::size_type;
{ // Testing (1)
// Nothing to do. Cannot deduce without any arguments.
}
{ // Testing (2)
// This overload isn't compatible with implicit deduction guides as
// specified in the standard.
// const test_allocator<char> alloc{};
// std::basic_string s(alloc);
}
{ // Testing (3) w/o allocator
std::basic_string s(6ull, 'a');
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "aaaaaa");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w(2ull, L'b');
ASSERT_SAME_TYPE(decltype(w), std::wstring);
assert(w == L"bb");
#endif
}
{ // Testing (3) w/ allocator
std::basic_string s(6ull, 'a', test_allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), BStr<char,test_allocator<char>>);
assert(s == "aaaaaa");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w(2ull, L'b', test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
assert(w == L"bb");
#endif
}
{ // Testing (4) w/o allocator
const std::string sin("abc");
std::basic_string s(sin, (size_t)1);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "bc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win, (TestSizeT)3);
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"def");
#endif
}
{ // Testing (4) w/ allocator
const std::string sin("abc");
std::basic_string s(sin, (size_t)1, std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "bc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win, (TestSizeT)3, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"def");
#endif
}
{ // Testing (5) w/o allocator
const std::string sin("abc");
std::basic_string s(sin, (size_t)1, (size_t)3);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "bc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win, (TestSizeT)2, (TestSizeT)3);
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"cde");
#endif
}
{ // Testing (5) w/ allocator
const std::string sin("abc");
std::basic_string s(sin, (size_t)1, (size_t)3, std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "bc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win, (TestSizeT)2, (TestSizeT)3, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"cde");
#endif
}
{ // Testing (6) w/o allocator
std::basic_string s("abc", (size_t)2);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "ab");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w(L"abcdef", (size_t)3);
ASSERT_SAME_TYPE(decltype(w), std::wstring);
assert(w == L"abc");
#endif
}
{ // Testing (6) w/ allocator
std::basic_string s("abc", (size_t)2, std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "ab");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
std::char_traits<wchar_t>,
test_allocator<wchar_t>>;
std::basic_string w(L"abcdef", (TestSizeT)3, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abc");
#endif
}
{ // Testing (7) w/o allocator
std::basic_string s("abc");
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w(L"abcdef");
ASSERT_SAME_TYPE(decltype(w), std::wstring);
assert(w == L"abcdef");
#endif
}
{ // Testing (7) w/ allocator
std::basic_string s("abc", std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
std::char_traits<wchar_t>,
test_allocator<wchar_t>>;
std::basic_string w(L"abcdef", test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abcdef");
#endif
}
{ // (8) w/o allocator
using It = cpp17_input_iterator<const char*>;
const char* input = "abcdef";
std::basic_string s(It(input), It(input + 3), std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
}
{ // (8) w/ allocator
{
using Expect = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
using It = cpp17_input_iterator<const char*>;
const char* input = "abcdef";
std::basic_string s(It(input), It(input + 3), test_allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), Expect);
assert(s == "abc");
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
using It = cpp17_input_iterator<const wchar_t*>;
const wchar_t* input = L"abcdef";
std::basic_string s(It(input), It(input + 3), test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(s), ExpectW);
assert(s == L"abc");
}
#endif
}
{ // Testing (9)
const std::string sin("abc");
std::basic_string s(sin);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win);
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abcdef");
#endif
}
{ // Testing (10)
const std::string sin("abc");
std::basic_string s(sin, std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
const WStr win(L"abcdef");
std::basic_string w(win, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abcdef");
#endif
}
{ // Testing (11)
std::string sin("abc");
std::basic_string s(std::move(sin));
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
WStr win(L"abcdef");
std::basic_string w(std::move(win));
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abcdef");
#endif
}
{ // Testing (12)
std::string sin("abc");
std::basic_string s(std::move(sin), std::allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using WStr = std::basic_string<wchar_t,
constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
WStr win(L"abcdef");
std::basic_string w(std::move(win), test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), WStr);
assert(w == L"abcdef");
#endif
}
{ // Testing (13) w/o allocator
std::basic_string s({'a', 'b', 'c'});
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w({L'a', L'b', L'c'});
ASSERT_SAME_TYPE(decltype(w), std::wstring);
assert(w == L"abc");
#endif
}
{ // Testing (13) w/ allocator
std::basic_string s({'a', 'b', 'c'}, test_allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), BStr<char, test_allocator<char>>);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::basic_string w({L'a', L'b', L'c'}, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), BStr<wchar_t, test_allocator<wchar_t>>);
assert(w == L"abc");
#endif
}
{ // Testing (14) w/o allocator
std::string_view sv("abc");
std::basic_string s(sv);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using Expect = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>>;
std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
std::basic_string w(BSV);
ASSERT_SAME_TYPE(decltype(w), Expect);
assert(w == L"abcdef");
#endif
}
{ // Testing (14) w/ allocator
using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
std::string_view sv("abc");
std::basic_string s(sv, test_allocator<char>{});
ASSERT_SAME_TYPE(decltype(s), ExpectS);
assert(s == "abc");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using ExpectW = std::basic_string<wchar_t, constexpr_char_traits<wchar_t>,
test_allocator<wchar_t>>;
std::basic_string_view<wchar_t, constexpr_char_traits<wchar_t>> BSV(L"abcdef");
std::basic_string w(BSV, test_allocator<wchar_t>{});
ASSERT_SAME_TYPE(decltype(w), ExpectW);
assert(w == L"abcdef");
#endif
}
{ // Testing (15) w/o allocator
std::string s0("abc");
std::basic_string s(s0, 1, 1);
ASSERT_SAME_TYPE(decltype(s), std::string);
assert(s == "b");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
std::wstring w0(L"abcdef");
std::basic_string w(w0, 2, 2);
ASSERT_SAME_TYPE(decltype(w), std::wstring);
assert(w == L"cd");
#endif
}
{ // Testing (15) w/ allocator
using ExpectS = std::basic_string<char, std::char_traits<char>, test_allocator<char>>;
ExpectS s0("abc");
std::basic_string s(s0, 1, 1, test_allocator<char>{4});
ASSERT_SAME_TYPE(decltype(s), ExpectS);
assert(s == "b");
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
using ExpectW = std::basic_string<wchar_t, std::char_traits<wchar_t>, test_allocator<wchar_t>>;
ExpectW w0(L"abcdef");
std::basic_string w(w0, 2, 2, test_allocator<wchar_t>{6});
ASSERT_SAME_TYPE(decltype(w), ExpectW);
assert(w == L"cd");
#endif
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string(initializer_list<charT> il, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s = {'a', 'b', 'c'};
assert(s == "abc");
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
std::wstring s;
s = {L'a', L'b', L'c'};
assert(s == L"abc");
}
#endif
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s = {'a', 'b', 'c'};
assert(s == "abc");
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
typedef std::basic_string<wchar_t, std::char_traits<wchar_t>, min_allocator<wchar_t>> S;
S s;
s = {L'a', L'b', L'c'};
assert(s == L"abc");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string& operator=(initializer_list<charT> il); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s;
s = {'a', 'b', 'c'};
assert(s == "abc");
}
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
s = {'a', 'b', 'c'};
assert(s == "abc");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,145 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template<class InputIterator>
// basic_string(InputIterator begin, InputIterator end,
// const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <iterator>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "test_allocator.h"
#include "test_iterators.h"
#include "min_allocator.h"
template <class It>
TEST_CONSTEXPR_CXX20 void
test(It first, It last)
{
typedef typename std::iterator_traits<It>::value_type charT;
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::allocator_type A;
S s2(first, last);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
unsigned i = 0;
for (It it = first; it != last;) {
assert(s2[i] == *it);
++it;
++i;
}
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
template <class It, class A>
TEST_CONSTEXPR_CXX20 void
test(It first, It last, const A& a)
{
typedef typename std::iterator_traits<It>::value_type charT;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
S s2(first, last, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == static_cast<std::size_t>(std::distance(first, last)));
unsigned i = 0;
for (It it = first; it != last;) {
assert(s2[i] == *it);
++it;
++i;
}
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
const char* s = "12345678901234567890123456789012345678901234567890";
test(s, s);
test(s, s, A(2));
test(s, s+1);
test(s, s+1, A(2));
test(s, s+10);
test(s, s+10, A(2));
test(s, s+50);
test(s, s+50, A(2));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), A(2));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), A(2));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), A(2));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50), A(2));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
const char* s = "12345678901234567890123456789012345678901234567890";
test(s, s);
test(s, s, A());
test(s, s+1);
test(s, s+1, A());
test(s, s+10);
test(s, s+10, A());
test(s, s+50);
test(s, s+50, A());
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), A());
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), A());
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), A());
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50));
test(cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+50), A());
}
#endif
{
static_assert((!std::is_constructible<std::string, std::string,
std::string>::value),
"");
static_assert(
(!std::is_constructible<std::string, std::string, std::string,
std::allocator<char> >::value),
"");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,115 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// UNSUPPORTED: c++03, c++11, c++14
// template<class InputIterator,
// class Allocator = allocator<typename iterator_traits<InputIterator>::value_type>>
// basic_string(InputIterator, InputIterator, Allocator = Allocator())
// -> basic_string<typename iterator_traits<InputIterator>::value_type,
// char_traits<typename iterator_traits<InputIterator>::value_type>,
// Allocator>; // constexpr since C++20
//
// The deduction guide shall not participate in overload resolution if InputIterator
// is a type that does not qualify as an input iterator, or if Allocator is a type
// that does not qualify as an allocator.
#include <cassert>
#include <cstddef>
#include <iterator>
#include <string>
#include <type_traits>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
class NotAnIterator {};
using NotAnInputIterator = std::back_insert_iterator<std::basic_string<char16_t>>;
template <typename T>
struct NotAnAllocator { typedef T value_type; };
template <class Iter, class Alloc, class = void>
struct CanDeduce : std::false_type { };
template <class Iter, class Alloc>
struct CanDeduce<Iter, Alloc, decltype((void)
std::basic_string{std::declval<Iter>(), std::declval<Iter>(), std::declval<Alloc>()}
)> : std::true_type { };
static_assert( CanDeduce<int*, std::allocator<int>>::value);
static_assert(!CanDeduce<NotAnIterator, std::allocator<char>>::value);
static_assert(!CanDeduce<NotAnInputIterator, std::allocator<char16_t>>::value);
static_assert(!CanDeduce<wchar_t const*, NotAnAllocator<wchar_t>>::value);
TEST_CONSTEXPR_CXX20 bool test() {
{
const char* s = "12345678901234";
std::basic_string s1(s, s+10); // Can't use {} here
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == 10);
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
}
{
const char* s = "12345678901234";
std::basic_string s1{s, s+10, std::allocator<char>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == 10);
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
}
{
const wchar_t* s = L"12345678901234";
std::basic_string s1{s, s+10, test_allocator<wchar_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, wchar_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
assert(s1.size() == 10);
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
}
{
const char16_t* s = u"12345678901234";
std::basic_string s1{s, s+10, min_allocator<char16_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char16_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
assert(s1.size() == 10);
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
}
{
const char32_t* s = U"12345678901234";
std::basic_string s1{s, s+10, explicit_allocator<char32_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char32_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
assert(s1.size() == 10);
assert(s1.compare(0, s1.size(), s, s1.size()) == 0);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s0)
{
S s1 = s0;
S s2 = std::move(s0);
LIBCPP_ASSERT(s2.__invariants());
LIBCPP_ASSERT(s0.__invariants());
assert(s2 == s1);
assert(s2.capacity() >= s2.size());
assert(s2.get_allocator() == s1.get_allocator());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)));
test(S("1", A(5)));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)));
}
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A{}));
test(S("1", A()));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()));
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,87 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string(basic_string&& str, const Allocator& alloc); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s0, const typename S::allocator_type& a)
{
S s1 = s0;
S s2(std::move(s0), a);
LIBCPP_ASSERT(s2.__invariants());
LIBCPP_ASSERT(s0.__invariants());
assert(s2 == s1);
assert(s2.capacity() >= s2.size());
assert(s2.get_allocator() == a);
}
TEST_CONSTEXPR_CXX20 bool test() {
test_allocator_statistics alloc_stats;
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
test(S(), A(3, &alloc_stats));
test(S("1"), A(5, &alloc_stats));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A(7, &alloc_stats));
}
int alloc_count = alloc_stats.alloc_count;
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
S s1 ( "Twas brillig, and the slivy toves did gyre and gymbal in the wabe", A(&alloc_stats));
S s2 (std::move(s1), A(1, &alloc_stats));
}
assert ( alloc_stats.alloc_count == alloc_count );
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
#if TEST_STD_VER > 14
static_assert((noexcept(S{})), "" );
#elif TEST_STD_VER >= 11
static_assert((noexcept(S()) == std::is_nothrow_move_constructible<A>::value), "" );
#endif
test(S(), A());
test(S("1"), A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"), A());
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,107 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string& operator=(basic_string&& c)
// noexcept(
// allocator_traits<allocator_type>::propagate_on_container_move_assignment::value ||
// allocator_traits<allocator_type>::is_always_equal::value); // C++17, constexpr since C++20
//
// before C++17, we use the conforming extension
// noexcept(
// allocator_type::propagate_on_container_move_assignment::value &&
// is_nothrow_move_assignable<allocator_type>::value); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
template <class T>
struct some_alloc
{
typedef T value_type;
some_alloc(const some_alloc&);
T *allocate(size_t);
};
template <class T>
struct some_alloc2
{
typedef T value_type;
some_alloc2() {}
some_alloc2(const some_alloc2&);
T *allocate(size_t);
void deallocate(void*, unsigned) {}
typedef std::false_type propagate_on_container_move_assignment;
typedef std::true_type is_always_equal;
};
template <class T>
struct some_alloc3
{
typedef T value_type;
some_alloc3() {}
some_alloc3(const some_alloc3&);
T *allocate(size_t);
void deallocate(void*, unsigned) {}
typedef std::false_type propagate_on_container_move_assignment;
typedef std::false_type is_always_equal;
};
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string C;
static_assert(std::is_nothrow_move_assignable<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
static_assert(!std::is_nothrow_move_assignable<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C;
#if TEST_STD_VER > 14
// if the allocators are always equal, then the move assignment can be noexcept
static_assert( std::is_nothrow_move_assignable<C>::value, "");
#else
static_assert(!std::is_nothrow_move_assignable<C>::value, "");
#endif
}
#if TEST_STD_VER > 14
{
// POCMA is false, always equal
typedef std::basic_string<char, std::char_traits<char>, some_alloc2<char>> C;
static_assert( std::is_nothrow_move_assignable<C>::value, "");
}
{
// POCMA is false, not always equal
typedef std::basic_string<char, std::char_traits<char>, some_alloc3<char>> C;
static_assert(!std::is_nothrow_move_assignable<C>::value, "");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,84 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(basic_string<charT,traits,Allocator>&& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1, S s2)
{
S s0 = s2;
s1 = std::move(s2);
LIBCPP_ASSERT(s1.__invariants());
LIBCPP_ASSERT(s2.__invariants());
assert(s1 == s0);
assert(s1.capacity() >= s1.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S());
test(S("1"), S());
test(S(), S("1"));
test(S("1"), S("2"));
test(S("1"), S("2"));
test(S(),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
S("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string(basic_string&&)
// noexcept(is_nothrow_move_constructible<allocator_type>::value); // constexpr since C++20
// This tests a conforming extension
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
int main(int, char**)
{
{
typedef std::string C;
static_assert(std::is_nothrow_move_constructible<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> C;
static_assert(std::is_nothrow_move_constructible<C>::value, "");
}
{
typedef std::basic_string<char, std::char_traits<char>, limited_allocator<char, 10>> C;
#if TEST_STD_VER <= 14
static_assert(!std::is_nothrow_move_constructible<C>::value, "");
#else
static_assert( std::is_nothrow_move_constructible<C>::value, "");
#endif
}
return 0;
}
@@ -0,0 +1,21 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <string>
// basic_string(nullptr_t) = delete; // C++2b
// basic_string& operator=(nullptr_t) = delete; // C++2b
#include <string>
#include <type_traits>
static_assert(!std::is_convertible_v<decltype(nullptr), std::string>);
static_assert(!std::is_constructible_v<std::string, decltype(nullptr)>);
static_assert(!std::is_assignable_v<std::string, decltype(nullptr)>);
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const charT* s, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class charT>
TEST_CONSTEXPR_CXX20 void
test(const charT* s)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
std::size_t n = T::length(s);
S s2(s);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
assert(T::compare(s2.data(), s, n) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
template <class charT, class A>
TEST_CONSTEXPR_CXX20 void
test(const charT* s, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
std::size_t n = T::length(s);
S s2(s, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
assert(T::compare(s2.data(), s, n) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
test("");
test("", A(2));
test("1");
test("1", A(2));
test("1234567980");
test("1234567980", A(2));
test("123456798012345679801234567980123456798012345679801234567980");
test("123456798012345679801234567980123456798012345679801234567980", A(2));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
test("");
test("", A());
test("1");
test("1", A());
test("1234567980");
test("1234567980", A());
test("123456798012345679801234567980123456798012345679801234567980");
test("123456798012345679801234567980123456798012345679801234567980", A());
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,83 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// operator=(const charT* s); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s1, const typename S::value_type* s2)
{
typedef typename S::traits_type T;
s1 = s2;
LIBCPP_ASSERT(s1.__invariants());
assert(s1.size() == T::length(s2));
assert(T::compare(s1.data(), s2, s1.size()) == 0);
assert(s1.capacity() >= s1.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "");
test(S("1"), "");
test(S(), "1");
test(S("1"), "2");
test(S("1"), "2");
test(S(),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("123456789"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "");
test(S("1"), "");
test(S(), "1");
test(S("1"), "2");
test(S("1"), "2");
test(S(),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("123456789"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
"abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,104 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const charT* s, size_type n, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class charT>
TEST_CONSTEXPR_CXX20 void
test(const charT* s, unsigned n)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
S s2(s, n);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
assert(T::compare(s2.data(), s, n) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
template <class charT, class A>
TEST_CONSTEXPR_CXX20 void
test(const charT* s, unsigned n, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
S s2(s, n, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
assert(T::compare(s2.data(), s, n) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
test("", 0);
test("", 0, A(2));
test("1", 1);
test("1", 1, A(2));
test("1234567980", 10);
test("1234567980", 10, A(2));
test("123456798012345679801234567980123456798012345679801234567980", 60);
test("123456798012345679801234567980123456798012345679801234567980", 60, A(2));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
test("", 0);
test("", 0, A());
test("1", 1);
test("1", 1, A());
test("1234567980", 10);
test("1234567980", 10, A());
test("123456798012345679801234567980123456798012345679801234567980", 60);
test("123456798012345679801234567980123456798012345679801234567980", 60, A());
}
#endif
#if TEST_STD_VER > 3
{ // LWG 2946
std::string s({"abc", 1});
assert(s.size() == 1);
assert(s == "a");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,134 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(size_type n, charT c, const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class charT>
TEST_CONSTEXPR_CXX20 void
test(unsigned n, charT c)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::allocator_type A;
S s2(n, c);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
for (unsigned i = 0; i < n; ++i)
assert(s2[i] == c);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
template <class charT, class A>
TEST_CONSTEXPR_CXX20 void
test(unsigned n, charT c, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
S s2(n, c, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == n);
for (unsigned i = 0; i < n; ++i)
assert(s2[i] == c);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
template <class Tp>
TEST_CONSTEXPR_CXX20 void
test(Tp n, Tp c)
{
typedef char charT;
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::allocator_type A;
S s2(n, c);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == static_cast<std::size_t>(n));
for (int i = 0; i < n; ++i)
assert(s2[i] == c);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
template <class Tp, class A>
TEST_CONSTEXPR_CXX20 void
test(Tp n, Tp c, const A& a)
{
typedef char charT;
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
S s2(n, c, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == static_cast<std::size_t>(n));
for (int i = 0; i < n; ++i)
assert(s2[i] == c);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
test(0, 'a');
test(0, 'a', A(2));
test(1, 'a');
test(1, 'a', A(2));
test(10, 'a');
test(10, 'a', A(2));
test(100, 'a');
test(100, 'a', A(2));
test(static_cast<char>(100), static_cast<char>(65));
test(static_cast<char>(100), static_cast<char>(65), A(3));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
test(0, 'a');
test(0, 'a', A());
test(1, 'a');
test(1, 'a', A());
test(10, 'a');
test(10, 'a', A());
test(100, 'a');
test(100, 'a', A());
test(static_cast<char>(100), static_cast<char>(65));
test(static_cast<char>(100), static_cast<char>(65), A());
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,125 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); // constexpr since C++20
#include <algorithm>
#include <cassert>
#include <stdexcept>
#include <string_view>
#include <string>
#include <type_traits>
#include "min_allocator.h"
#include "test_allocator.h"
#include "test_macros.h"
static_assert(!std::is_convertible<std::string_view, std::string const&>::value, "");
static_assert(!std::is_convertible<std::string_view, std::string>::value, "");
template <class charT>
TEST_CONSTEXPR_CXX20 void
test(std::basic_string_view<charT> sv)
{
typedef std::basic_string<charT, std::char_traits<charT>, test_allocator<charT> > S;
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
{
S s2(sv);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == sv.size());
assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
{
S s2;
s2 = sv;
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == sv.size());
assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
}
template <class charT, class A>
TEST_CONSTEXPR_CXX20 void
test(std::basic_string_view<charT> sv, const A& a)
{
typedef std::basic_string<charT, std::char_traits<charT>, A> S;
typedef typename S::traits_type T;
{
S s2(sv, a);
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == sv.size());
assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
{
S s2(a);
s2 = sv;
LIBCPP_ASSERT(s2.__invariants());
assert(s2.size() == sv.size());
assert(T::compare(s2.data(), sv.data(), sv.size()) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(SV(""));
test(SV(""), A(2));
test(SV("1"));
test(SV("1") ,A(2));
test(SV("1234567980"));
test(SV("1234567980"), A(2));
test(SV("123456798012345679801234567980123456798012345679801234567980"));
test(SV("123456798012345679801234567980123456798012345679801234567980"), A(2));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(SV(""));
test(SV(""), A());
test(SV("1"));
test(SV("1") ,A());
test(SV("1234567980"));
test(SV("1234567980"), A());
test(SV("123456798012345679801234567980123456798012345679801234567980"));
test(SV("123456798012345679801234567980123456798012345679801234567980"), A());
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,84 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>& operator=(basic_string_view<charT, traits> sv); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(S s1, SV sv)
{
typedef typename S::traits_type T;
s1 = sv;
LIBCPP_ASSERT(s1.__invariants());
assert(s1.size() == sv.size());
assert(T::compare(s1.data(), sv.data(), s1.size()) == 0);
assert(s1.capacity() >= s1.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
test(S(), SV(""));
test(S("1"), SV(""));
test(S(), SV("1"));
test(S("1"), SV("2"));
test(S("1"), SV("2"));
test(S(),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
typedef std::string_view SV;
test(S(), SV(""));
test(S("1"), SV(""));
test(S(), SV("1"));
test(S("1"), SV("2"));
test(S("1"), SV("2"));
test(S(),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("123456789"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890"
"1234567890123456789012345678901234567890123456789012345678901234567890"),
SV("abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,124 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// UNSUPPORTED: c++03, c++11, c++14
// template<class charT,
// class traits,
// class Allocator = allocator<charT>
// >
// basic_string(basic_string_view<charT, traits>, const Allocator& = Allocator())
// -> basic_string<charT, traits, Allocator>;
//
// The deduction guide shall not participate in overload resolution if Allocator
// is a type that does not qualify as an allocator.
#include <string>
#include <string_view>
#include <iterator>
#include <memory>
#include <type_traits>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class StringView, class Allocator, class = void>
struct CanDeduce : std::false_type { };
template <class StringView, class Allocator>
struct CanDeduce<StringView, Allocator, decltype((void)
std::basic_string{std::declval<StringView>(), std::declval<Allocator>()}
)> : std::true_type { };
struct NotAnAllocator { };
static_assert( CanDeduce<std::string_view, std::allocator<char>>::value);
static_assert(!CanDeduce<std::string_view, NotAnAllocator>::value);
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string_view sv = "12345678901234";
std::basic_string s1(sv);
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
{
std::string_view sv = "12345678901234";
std::basic_string s1{sv, std::allocator<char>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
std::wstring_view sv = L"12345678901234";
std::basic_string s1{sv, test_allocator<wchar_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, wchar_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
#ifndef TEST_HAS_NO_CHAR8_T
{
std::u8string_view sv = u8"12345678901234";
std::basic_string s1{sv, min_allocator<char8_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char8_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");
static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
{
std::u16string_view sv = u"12345678901234";
std::basic_string s1{sv, min_allocator<char16_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char16_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
{
std::u32string_view sv = U"12345678901234";
std::basic_string s1{sv, explicit_allocator<char32_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char32_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
assert(s1.size() == sv.size());
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,128 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// UNSUPPORTED: c++03, c++11, c++14
// template<class charT,
// class traits,
// class Allocator = allocator<charT>
// >
// basic_string(basic_string_view<charT, traits>,
// typename see below::size_type,
// typename see below::size_type,
// const Allocator& = Allocator())
// -> basic_string<charT, traits, Allocator>;
//
// A size_type parameter type in a basic_string deduction guide refers to the size_type
// member type of the type deduced by the deduction guide.
//
// The deduction guide shall not participate in overload resolution if Allocator
// is a type that does not qualify as an allocator.
#include <string>
#include <string_view>
#include <iterator>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class StringView, class Size, class Allocator, class = void>
struct CanDeduce : std::false_type { };
template <class StringView, class Size, class Allocator>
struct CanDeduce<StringView, Size, Allocator, decltype((void)
std::basic_string{std::declval<StringView>(), std::declval<Size>(), std::declval<Size>(), std::declval<Allocator>()}
)> : std::true_type { };
struct NotAnAllocator { };
static_assert( CanDeduce<std::string_view, std::size_t, std::allocator<char>>::value);
static_assert(!CanDeduce<std::string_view, std::size_t, NotAnAllocator>::value);
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string_view sv = "12345678901234";
std::basic_string s1{sv, 0, 4};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
{
std::string_view sv = "12345678901234";
std::basic_string s1{sv, 0, 4, std::allocator<char>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char>>, "");
static_assert(std::is_same_v<S::allocator_type, std::allocator<char>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
{
std::wstring_view sv = L"12345678901234";
std::basic_string s1{sv, 0, 4, test_allocator<wchar_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, wchar_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<wchar_t>>, "");
static_assert(std::is_same_v<S::allocator_type, test_allocator<wchar_t>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
#ifndef TEST_HAS_NO_CHAR8_T
{
std::u8string_view sv = u8"12345678901234";
std::basic_string s1{sv, 0, 4, min_allocator<char8_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char8_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char8_t>>, "");
static_assert(std::is_same_v<S::allocator_type, min_allocator<char8_t>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
#endif
{
std::u16string_view sv = u"12345678901234";
std::basic_string s1{sv, 0, 4, min_allocator<char16_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char16_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char16_t>>, "");
static_assert(std::is_same_v<S::allocator_type, min_allocator<char16_t>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
{
std::u32string_view sv = U"12345678901234";
std::basic_string s1{sv, 0, 4, explicit_allocator<char32_t>{}};
using S = decltype(s1); // what type did we get?
static_assert(std::is_same_v<S::value_type, char32_t>, "");
static_assert(std::is_same_v<S::traits_type, std::char_traits<char32_t>>, "");
static_assert(std::is_same_v<S::allocator_type, explicit_allocator<char32_t>>, "");
assert(s1.size() == 4);
assert(s1.compare(0, s1.size(), sv.data(), s1.size()) == 0);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,233 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos, size_type n,
// const Allocator& a = Allocator()); // constexpr since C++20
//
// basic_string(const basic_string<charT,traits,Allocator>& str,
// size_type pos,
// const Allocator& a = Allocator()); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <algorithm>
#include <vector>
#include <scoped_allocator>
#include <cassert>
#include "test_macros.h"
#include "test_allocator.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S str, unsigned pos)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
if (pos <= str.size())
{
S s2(str, pos);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = str.size() - pos;
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
S s2(str, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S str, unsigned pos, unsigned n)
{
typedef typename S::traits_type T;
typedef typename S::allocator_type A;
if (pos <= str.size())
{
S s2(str, pos, n);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == A());
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
S s2(str, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a)
{
typedef typename S::traits_type T;
if (pos <= str.size())
{
S s2(str, pos, n, a);
LIBCPP_ASSERT(s2.__invariants());
typename S::size_type rlen = std::min<typename S::size_type>(str.size() - pos, n);
assert(s2.size() == rlen);
assert(T::compare(s2.data(), str.data() + pos, rlen) == 0);
assert(s2.get_allocator() == a);
assert(s2.capacity() >= s2.size());
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
S s2(str, pos, n, a);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
void test_lwg2583()
{
#if TEST_STD_VER >= 11 && !defined(TEST_HAS_NO_EXCEPTIONS)
typedef std::basic_string<char, std::char_traits<char>, test_allocator<char>> StringA;
std::vector<StringA, std::scoped_allocator_adaptor<test_allocator<StringA>>> vs;
StringA s{"1234"};
vs.emplace_back(s, 2);
try { vs.emplace_back(s, 5); }
catch (const std::out_of_range&) { return; }
assert(false);
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef test_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A(3)), 0);
test(S(A(3)), 1);
test(S("1", A(5)), 0);
test(S("1", A(5)), 1);
test(S("1", A(5)), 2);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 5);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 500);
test(S(A(3)), 0, 0);
test(S(A(3)), 0, 1);
test(S(A(3)), 1, 0);
test(S(A(3)), 1, 1);
test(S(A(3)), 1, 2);
test(S("1", A(5)), 0, 0);
test(S("1", A(5)), 0, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100);
test(S(A(3)), 0, 0, A(4));
test(S(A(3)), 0, 1, A(4));
test(S(A(3)), 1, 0, A(4));
test(S(A(3)), 1, 1, A(4));
test(S(A(3)), 1, 2, A(4));
test(S("1", A(5)), 0, 0, A(6));
test(S("1", A(5)), 0, 1, A(6));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 0, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 1, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 10, A(8));
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), 50, 100, A(8));
}
#if TEST_STD_VER >= 11
{
typedef min_allocator<char> A;
typedef std::basic_string<char, std::char_traits<char>, A> S;
test(S(A()), 0);
test(S(A()), 1);
test(S("1", A()), 0);
test(S("1", A()), 1);
test(S("1", A()), 2);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 5);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 500);
test(S(A()), 0, 0);
test(S(A()), 0, 1);
test(S(A()), 1, 0);
test(S(A()), 1, 1);
test(S(A()), 1, 2);
test(S("1", A()), 0, 0);
test(S("1", A()), 0, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10);
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100);
test(S(A()), 0, 0, A());
test(S(A()), 0, 1, A());
test(S(A()), 1, 0, A());
test(S(A()), 1, 1, A());
test(S(A()), 1, 2, A());
test(S("1", A()), 0, 0, A());
test(S("1", A()), 0, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 0, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 1, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 10, A());
test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A());
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
test_lwg2583();
return 0;
}
@@ -0,0 +1,44 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <string>
// constexpr bool contains(charT x) const noexcept;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test()
{
using S = std::string;
S s1 {};
S s2 {"abcde", 5};
ASSERT_NOEXCEPT(s1.contains('e'));
assert(!s1.contains('c'));
assert(!s1.contains('e'));
assert(!s1.contains('x'));
assert( s2.contains('c'));
assert( s2.contains('e'));
assert(!s2.contains('x'));
return true;
}
int main(int, char**)
{
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <string>
// constexpr bool contains(const CharT *x) const;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test()
{
using S = std::string;
const char* s = "abcde";
S s0;
S s1 {s + 4, 1};
S s3 {s + 2, 3};
S sNot {"xyz", 3};
assert(s0.contains(""));
assert(!s0.contains("e"));
assert( s1.contains(""));
assert(!s1.contains("d"));
assert( s1.contains("e"));
assert(!s1.contains("de"));
assert(!s1.contains("cd"));
assert(!s1.contains("cde"));
assert(!s1.contains("bcde"));
assert(!s1.contains("abcde"));
assert(!s1.contains("xyz"));
assert( s3.contains(""));
assert( s3.contains("d"));
assert( s3.contains("e"));
assert( s3.contains("de"));
assert( s3.contains("cd"));
assert(!s3.contains("ce"));
assert( s3.contains("cde"));
assert(!s3.contains("edc"));
assert(!s3.contains("bcde"));
assert(!s3.contains("abcde"));
assert(!s3.contains("xyz"));
assert( sNot.contains(""));
assert(!sNot.contains("d"));
assert(!sNot.contains("e"));
assert(!sNot.contains("de"));
assert(!sNot.contains("cd"));
assert(!sNot.contains("cde"));
assert(!sNot.contains("bcde"));
assert(!sNot.contains("abcde"));
assert( sNot.contains("xyz"));
assert(!sNot.contains("zyx"));
return true;
}
int main(int, char**)
{
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,95 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20
// <string>
// constexpr bool contains(basic_string_view x) const noexcept;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test()
{
using S = std::string;
using SV = std::string_view;
const char* s = "abcde";
S s0;
S s1 {s + 1, 1};
S s3 {s + 1, 3};
S s5 {s , 5};
S sNot {"xyz", 3};
SV sv0;
SV sv1 {s + 1, 1};
SV sv2 {s + 1, 2};
SV sv3 {s + 1, 3};
SV sv4 {s + 1, 4};
SV sv5 {s , 5};
SV svNot {"xyz", 3};
SV svNot2 {"bd" , 2};
SV svNot3 {"dcb", 3};
ASSERT_NOEXCEPT(s0.contains(sv0));
assert( s0.contains(sv0));
assert(!s0.contains(sv1));
assert( s1.contains(sv0));
assert( s1.contains(sv1));
assert(!s1.contains(sv2));
assert(!s1.contains(sv3));
assert(!s1.contains(sv4));
assert(!s1.contains(sv5));
assert(!s1.contains(svNot));
assert(!s1.contains(svNot2));
assert(!s1.contains(svNot3));
assert( s3.contains(sv0));
assert( s3.contains(sv1));
assert( s3.contains(sv2));
assert( s3.contains(sv3));
assert(!s3.contains(sv4));
assert(!s3.contains(sv5));
assert(!s3.contains(svNot));
assert(!s3.contains(svNot2));
assert(!s3.contains(svNot3));
assert( s5.contains(sv0));
assert( s5.contains(sv1));
assert( s5.contains(sv2));
assert( s5.contains(sv3));
assert( s5.contains(sv4));
assert( s5.contains(sv5));
assert(!s5.contains(svNot));
assert(!s5.contains(svNot2));
assert(!s5.contains(svNot3));
assert( sNot.contains(sv0));
assert(!sNot.contains(sv1));
assert(!sNot.contains(sv2));
assert(!sNot.contains(sv3));
assert(!sNot.contains(sv4));
assert(!sNot.contains(sv5));
assert( sNot.contains(svNot));
assert(!sNot.contains(svNot2));
assert(!sNot.contains(svNot3));
return true;
}
int main(int, char**)
{
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,42 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <string>
// constexpr bool ends_with(charT x) const noexcept;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test() {
{
typedef std::string S;
S s1 {};
S s2 { "abcde", 5 };
ASSERT_NOEXCEPT(s1.ends_with('e'));
assert (!s1.ends_with('e'));
assert (!s1.ends_with('x'));
assert ( s2.ends_with('e'));
assert (!s2.ends_with('x'));
}
return true;
}
int main(int, char**)
{
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <string>
// constexpr bool ends_with(const CharT *x) const;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test() {
{
typedef std::string S;
const char *s = "abcde";
S s0;
S s1 { s + 4, 1 };
S s2 { s + 3, 2 };
// S s3 { s + 2, 3 };
// S s4 { s + 1, 4 };
// S s5 { s, 5 };
S sNot { "def", 3 };
LIBCPP_ASSERT_NOEXCEPT(s0.ends_with(""));
assert ( s0.ends_with(""));
assert (!s0.ends_with("e"));
assert ( s1.ends_with(""));
assert ( s1.ends_with("e"));
assert (!s1.ends_with("de"));
assert (!s1.ends_with("cde"));
assert (!s1.ends_with("bcde"));
assert (!s1.ends_with("abcde"));
assert (!s1.ends_with("def"));
assert ( s2.ends_with(""));
assert ( s2.ends_with("e"));
assert ( s2.ends_with("de"));
assert (!s2.ends_with("cde"));
assert (!s2.ends_with("bcde"));
assert (!s2.ends_with("abcde"));
assert (!s2.ends_with("def"));
assert ( sNot.ends_with(""));
assert (!sNot.ends_with("e"));
assert (!sNot.ends_with("de"));
assert (!sNot.ends_with("cde"));
assert (!sNot.ends_with("bcde"));
assert (!sNot.ends_with("abcde"));
assert ( sNot.ends_with("def"));
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,79 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// <string>
// constexpr bool ends_with(basic_string_view x) const noexcept;
#include <string>
#include <cassert>
#include "test_macros.h"
constexpr bool test() {
{
typedef std::string S;
typedef std::string_view SV;
const char *s = "abcde";
S s0;
S s1 { s + 4, 1 };
S s2 { s + 3, 2 };
// S s3 { s + 2, 3 };
// S s4 { s + 1, 4 };
// S s5 { s, 5 };
S sNot { "def", 3 };
SV sv0;
SV sv1 { s + 4, 1 };
SV sv2 { s + 3, 2 };
SV sv3 { s + 2, 3 };
SV sv4 { s + 1, 4 };
SV sv5 { s , 5 };
SV svNot {"def", 3 };
ASSERT_NOEXCEPT(s0.ends_with(sv0));
assert ( s0.ends_with(sv0));
assert (!s0.ends_with(sv1));
assert ( s1.ends_with(sv0));
assert ( s1.ends_with(sv1));
assert (!s1.ends_with(sv2));
assert (!s1.ends_with(sv3));
assert (!s1.ends_with(sv4));
assert (!s1.ends_with(sv5));
assert (!s1.ends_with(svNot));
assert ( s2.ends_with(sv0));
assert ( s2.ends_with(sv1));
assert ( s2.ends_with(sv2));
assert (!s2.ends_with(sv3));
assert (!s2.ends_with(sv4));
assert (!s2.ends_with(sv5));
assert (!s2.ends_with(svNot));
assert ( sNot.ends_with(sv0));
assert (!sNot.ends_with(sv1));
assert (!sNot.ends_with(sv2));
assert (!sNot.ends_with(sv3));
assert (!sNot.ends_with(sv4));
assert (!sNot.ends_with(sv5));
assert ( sNot.ends_with(svNot));
}
return true;
}
int main(int, char**) {
test();
static_assert(test());
return 0;
}
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator begin(); // constexpr since C++20
// const_iterator begin() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
typename S::iterator b = s.begin();
typename S::const_iterator cb = cs.begin();
if (!s.empty())
{
assert(*b == s[0]);
}
assert(b == cb);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_iterator cbegin() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
typename S::const_iterator cb = s.cbegin();
if (!s.empty())
{
assert(*cb == s[0]);
}
assert(cb == s.begin());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,51 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_iterator cend() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
typename S::const_iterator ce = s.cend();
assert(ce == s.end());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_reverse_iterator crbegin() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
typename S::const_reverse_iterator cb = s.crbegin();
if (!s.empty())
{
assert(*cb == s.back());
}
assert(cb == s.rbegin());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,52 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// const_reverse_iterator crend() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(const S& s)
{
typename S::const_reverse_iterator ce = s.crend();
assert(ce == s.rend());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator end(); // constexpr since C++20
// const_iterator end() const; // constexpr since C++20
#include <string>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
typename S::iterator e = s.end();
typename S::const_iterator ce = cs.end();
if (s.empty())
{
assert(e == s.begin());
assert(ce == cs.begin());
}
assert(static_cast<std::size_t>(e - s.begin()) == s.size());
assert(static_cast<std::size_t>(ce - cs.begin()) == cs.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,77 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11, c++14, c++17
// iterator, const_iterator, reverse_iterator, const_reverse_iterator
#include <string>
#include <iterator>
using iterator = std::string::iterator;
using const_iterator = std::string::const_iterator;
using reverse_iterator = std::string::reverse_iterator;
using const_reverse_iterator = std::string::const_reverse_iterator;
using value_type = char;
static_assert(std::contiguous_iterator<iterator>);
static_assert(std::indirectly_writable<iterator, value_type>);
static_assert(std::sentinel_for<iterator, iterator>);
static_assert(std::sentinel_for<iterator, const_iterator>);
static_assert(!std::sentinel_for<iterator, reverse_iterator>);
static_assert(!std::sentinel_for<iterator, const_reverse_iterator>);
static_assert(std::sized_sentinel_for<iterator, iterator>);
static_assert(std::sized_sentinel_for<iterator, const_iterator>);
static_assert(!std::sized_sentinel_for<iterator, reverse_iterator>);
static_assert(!std::sized_sentinel_for<iterator, const_reverse_iterator>);
static_assert( std::indirectly_movable<iterator, iterator>);
static_assert( std::indirectly_movable_storable<iterator, iterator>);
static_assert(!std::indirectly_movable<iterator, const_iterator>);
static_assert(!std::indirectly_movable_storable<iterator, const_iterator>);
static_assert( std::indirectly_movable<iterator, reverse_iterator>);
static_assert( std::indirectly_movable_storable<iterator, reverse_iterator>);
static_assert(!std::indirectly_movable<iterator, const_reverse_iterator>);
static_assert(!std::indirectly_movable_storable<iterator, const_reverse_iterator>);
static_assert( std::indirectly_copyable<iterator, iterator>);
static_assert( std::indirectly_copyable_storable<iterator, iterator>);
static_assert(!std::indirectly_copyable<iterator, const_iterator>);
static_assert(!std::indirectly_copyable_storable<iterator, const_iterator>);
static_assert( std::indirectly_copyable<iterator, reverse_iterator>);
static_assert( std::indirectly_copyable_storable<iterator, reverse_iterator>);
static_assert(!std::indirectly_copyable<iterator, const_reverse_iterator>);
static_assert(!std::indirectly_copyable_storable<iterator, const_reverse_iterator>);
static_assert( std::indirectly_swappable<iterator, iterator>);
static_assert(std::contiguous_iterator<const_iterator>);
static_assert(!std::indirectly_writable<const_iterator, value_type>);
static_assert(std::sentinel_for<const_iterator, iterator>);
static_assert(std::sentinel_for<const_iterator, const_iterator>);
static_assert(!std::sentinel_for<const_iterator, reverse_iterator>);
static_assert(!std::sentinel_for<const_iterator, const_reverse_iterator>);
static_assert(std::sized_sentinel_for<const_iterator, iterator>);
static_assert(std::sized_sentinel_for<const_iterator, const_iterator>);
static_assert(!std::sized_sentinel_for<const_iterator, reverse_iterator>);
static_assert(!std::sized_sentinel_for<const_iterator, const_reverse_iterator>);
static_assert( std::indirectly_movable<const_iterator, iterator>);
static_assert( std::indirectly_movable_storable<const_iterator, iterator>);
static_assert(!std::indirectly_movable<const_iterator, const_iterator>);
static_assert(!std::indirectly_movable_storable<const_iterator, const_iterator>);
static_assert( std::indirectly_movable<const_iterator, reverse_iterator>);
static_assert( std::indirectly_movable_storable<const_iterator, reverse_iterator>);
static_assert(!std::indirectly_movable<const_iterator, const_reverse_iterator>);
static_assert(!std::indirectly_movable_storable<const_iterator, const_reverse_iterator>);
static_assert( std::indirectly_copyable<const_iterator, iterator>);
static_assert( std::indirectly_copyable_storable<const_iterator, iterator>);
static_assert(!std::indirectly_copyable<const_iterator, const_iterator>);
static_assert(!std::indirectly_copyable_storable<const_iterator, const_iterator>);
static_assert( std::indirectly_copyable<const_iterator, reverse_iterator>);
static_assert( std::indirectly_copyable_storable<const_iterator, reverse_iterator>);
static_assert(!std::indirectly_copyable<const_iterator, const_reverse_iterator>);
static_assert(!std::indirectly_copyable_storable<const_iterator, const_reverse_iterator>);
static_assert(!std::indirectly_swappable<const_iterator, const_iterator>);
@@ -0,0 +1,85 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03, c++11
// <string>
// iterator begin(); // constexpr since C++20
// iterator end(); // constexpr since C++20
// const_iterator begin() const; // constexpr since C++20
// const_iterator end() const; // constexpr since C++20
// const_iterator cbegin() const; // constexpr since C++20
// const_iterator cend() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
template<class C>
TEST_CONSTEXPR_CXX20 void test()
{
{ // N3644 testing
typename C::iterator ii1{}, ii2{};
typename C::iterator ii4 = ii1;
typename C::const_iterator cii{};
assert ( ii1 == ii2 );
assert ( ii1 == ii4 );
assert (!(ii1 != ii2 ));
assert ( (ii1 == cii ));
assert ( (cii == ii1 ));
assert (!(ii1 != cii ));
assert (!(cii != ii1 ));
assert (!(ii1 < cii ));
assert (!(cii < ii1 ));
assert ( (ii1 <= cii ));
assert ( (cii <= ii1 ));
assert (!(ii1 > cii ));
assert (!(cii > ii1 ));
assert ( (ii1 >= cii ));
assert ( (cii >= ii1 ));
assert (cii - ii1 == 0);
assert (ii1 - cii == 0);
}
{
C a;
typename C::iterator i1 = a.begin();
typename C::iterator i2;
i2 = i1;
assert ( i1 == i2 );
}
}
TEST_CONSTEXPR_CXX20 bool test() {
test<std::string>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<std::wstring>();
#endif
#ifndef TEST_HAS_NO_CHAR8_T
test<std::u8string>();
#endif
test<std::u16string>();
test<std::u32string>();
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,59 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// reverse_iterator rbegin(); // constexpr since C++20
// const_reverse_iterator rbegin() const; // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
typename S::reverse_iterator b = s.rbegin();
typename S::const_reverse_iterator cb = cs.rbegin();
if (!s.empty())
{
assert(*b == s.back());
}
assert(b == cb);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,62 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// reverse_iterator rend(); // constexpr since C++20
// const_reverse_iterator rend() const; // constexpr since C++20
#include <string>
#include <cassert>
#include <cstddef>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s)
{
const S& cs = s;
typename S::reverse_iterator e = s.rend();
typename S::const_reverse_iterator ce = cs.rend();
if (s.empty())
{
assert(e == s.rbegin());
assert(ce == cs.rbegin());
}
assert(static_cast<std::size_t>(e - s.rbegin()) == s.size());
assert(static_cast<std::size_t>(ce - cs.rbegin()) == cs.size());
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S());
test(S("123"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S());
test(S("123"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,46 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
#include <cassert>
#include <string>
#include "test_macros.h"
struct Incomplete;
template<class T> struct Holder { T t; };
template<class T>
struct Charlike {
char ch_;
TEST_CONSTEXPR Charlike(char ch) : ch_(ch) {}
TEST_CONSTEXPR operator char() const { return ch_; }
};
TEST_CONSTEXPR_CXX20 bool test() {
std::string s;
Charlike<Holder<Incomplete> > a[] = {'m', 'a', 'h', 'i'};
s.append(a, a+4);
s.assign(a, a+4);
s.insert(s.begin(), a, a+4);
s.replace(s.begin(), s.begin()+4, a, a+4);
assert(s == "mahimahi");
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,210 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template <class T>
// basic_string& append(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
#include <string>
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
{
if (pos <= sv.size())
{
s.append(sv, pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.append(sv, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test_npos(S s, SV sv, typename S::size_type pos, S expected)
{
if (pos <= sv.size())
{
s.append(sv, pos);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.append(sv, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
test(S(), SV(), 0, 0, S());
test(S(), SV(), 1, 0, S());
test(S(), SV("12345"), 0, 3, S("123"));
test(S(), SV("12345"), 1, 4, S("2345"));
test(S(), SV("12345"), 3, 15, S("45"));
test(S(), SV("12345"), 5, 15, S(""));
test(S(), SV("12345"), 6, 15, S("not happening"));
test(S(), SV("12345678901234567890"), 0, 0, S());
test(S(), SV("12345678901234567890"), 1, 1, S("2"));
test(S(), SV("12345678901234567890"), 2, 3, S("345"));
test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), SV(), 0, 0, S("12345"));
test(S("12345"), SV("12345"), 2, 2, S("1234534"));
test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
S("123456789012345678906789012345"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(S(), SV(), 0, 0, S());
test(S(), SV(), 1, 0, S());
test(S(), SV("12345"), 0, 3, S("123"));
test(S(), SV("12345"), 1, 4, S("2345"));
test(S(), SV("12345"), 3, 15, S("45"));
test(S(), SV("12345"), 5, 15, S(""));
test(S(), SV("12345"), 6, 15, S("not happening"));
test(S(), SV("12345678901234567890"), 0, 0, S());
test(S(), SV("12345678901234567890"), 1, 1, S("2"));
test(S(), SV("12345678901234567890"), 2, 3, S("345"));
test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), SV(), 0, 0, S("12345"));
test(S("12345"), SV("12345"), 2, 2, S("1234534"));
test(S("12345"), SV("1234567890"), 0, 100, S("123451234567890"));
test(S("12345678901234567890"), SV(), 0, 0, S("12345678901234567890"));
test(S("12345678901234567890"), SV("12345"), 1, 3, S("12345678901234567890234"));
test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
S("123456789012345678906789012345"));
}
#endif
{
typedef std::string S;
typedef std::string_view SV;
test_npos(S(), SV(), 0, S());
test_npos(S(), SV(), 1, S());
test_npos(S(), SV("12345"), 0, S("12345"));
test_npos(S(), SV("12345"), 1, S("2345"));
test_npos(S(), SV("12345"), 3, S("45"));
test_npos(S(), SV("12345"), 5, S(""));
test_npos(S(), SV("12345"), 6, S("not happening"));
}
{
std::string s;
std::string_view sv = "EFGH";
char arr[] = "IJKL";
s.append("CDEF", 0); // calls append(const char *, len)
assert(s == "");
s.clear();
s.append("QRST", 0, std::string::npos); // calls append(string("QRST"), pos, npos)
assert(s == "QRST");
s.clear();
s.append(sv, 0); // calls append(T, pos, npos)
assert(s == sv);
s.clear();
s.append(sv, 0, std::string::npos); // calls append(T, pos, npos)
assert(s == sv);
s.clear();
s.append(arr, 0); // calls append(const char *, len)
assert(s == "");
s.clear();
s.append(arr, 0, std::string::npos); // calls append(string("IJKL"), pos, npos)
assert(s == "IJKL");
s.clear();
s.append(arr, 0); // calls append(const char *, len)
assert(s == "");
s.clear();
}
{
std::string s = "ABCD";
std::string_view sv = s;
s.append(sv);
assert(s == "ABCDABCD");
sv = s;
s.append(sv, 0, std::string::npos);
assert(s == "ABCDABCDABCDABCD");
sv = s;
s.append(sv, sv.size());
assert(s == "ABCDABCDABCDABCD");
}
{
std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string_view sv = s;
s.append(sv);
assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
sv = s;
s.append(sv, 0, std::string::npos);
assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string& append(initializer_list<charT> il); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s("123");
s.append({'a', 'b', 'c'});
assert(s == "123abc");
}
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s("123");
s.append({'a', 'b', 'c'});
assert(s == "123abc");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,266 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template<class InputIterator>
// basic_string& append(InputIterator first, InputIterator last); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "min_allocator.h"
template <class S, class It>
TEST_CONSTEXPR_CXX20 void
test(S s, It first, It last, S expected)
{
s.append(first, last);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Widget { operator char() const { throw 42; } };
template <class S, class It>
TEST_CONSTEXPR_CXX20 void
test_exceptions(S s, It first, It last)
{
S original = s;
typename S::iterator begin = s.begin();
typename S::iterator end = s.end();
try {
s.append(first, last);
assert(false);
} catch (...) {}
// Part of "no effects" is that iterators and pointers
// into the string must not have been invalidated.
LIBCPP_ASSERT(s.__invariants());
assert(s == original);
assert(s.begin() == begin);
assert(s.end() == end);
}
#endif
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), s, s, S());
test(S(), s, s+1, S("A"));
test(S(), s, s+10, S("ABCDEFGHIJ"));
test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), s, s, S("12345"));
test(S("12345"), s, s+1, S("12345A"));
test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), s, s, S("1234567890"));
test(S("1234567890"), s, s+1, S("1234567890A"));
test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), s, s, S("12345678901234567890"));
test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
test(S("12345678901234567890"), s, s+52,
S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("12345"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("12345A"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("12345ABCDEFGHIJ"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("1234567890"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("1234567890A"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("12345678901234567890"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("12345678901234567890""A"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("12345678901234567890""ABCDEFGHIJ"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), s, s, S());
test(S(), s, s+1, S("A"));
test(S(), s, s+10, S("ABCDEFGHIJ"));
test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), s, s, S("12345"));
test(S("12345"), s, s+1, S("12345A"));
test(S("12345"), s, s+10, S("12345ABCDEFGHIJ"));
test(S("12345"), s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), s, s, S("1234567890"));
test(S("1234567890"), s, s+1, S("1234567890A"));
test(S("1234567890"), s, s+10, S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), s, s+52, S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), s, s, S("12345678901234567890"));
test(S("12345678901234567890"), s, s+1, S("12345678901234567890""A"));
test(S("12345678901234567890"), s, s+10, S("12345678901234567890""ABCDEFGHIJ"));
test(S("12345678901234567890"), s, s+52,
S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("12345"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("12345A"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("12345ABCDEFGHIJ"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("1234567890"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("1234567890A"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S("12345678901234567890"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("12345678901234567890""A"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("12345678901234567890""ABCDEFGHIJ"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345678901234567890""ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
typedef std::string S;
typedef ThrowingIterator<char> TIter;
typedef cpp17_input_iterator<TIter> IIter;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter()));
test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter()));
test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter()));
test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
Widget w[100];
test_exceptions(S(), w, w+100);
}
#endif
{ // test appending to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.append(s_short.begin(), s_short.end());
assert(s_short == "123/123/");
s_short.append(s_short.begin(), s_short.end());
assert(s_short == "123/123/123/123/");
s_short.append(s_short.begin(), s_short.end());
assert(s_short == "123/123/123/123/123/123/123/123/");
s_long.append(s_long.begin(), s_long.end());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
}
{ // test appending a different type
typedef std::string S;
const uint8_t p[] = "ABCD";
S s;
s.append(p, p + 4);
assert(s == "ABCD");
}
{ // regression-test appending to self in sneaky ways
std::string s_short = "hello";
std::string s_long = "Lorem ipsum dolor sit amet, consectetur/";
std::string s_othertype = "hello";
std::string s_sneaky = "hello";
test(s_short, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1,
std::string("hello\0", 6));
test(s_long, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1,
std::string("Lorem ipsum dolor sit amet, consectetur/\0", 41));
s_sneaky.reserve(12);
test(s_sneaky, s_sneaky.data(), s_sneaky.data() + 6, std::string("hellohello\0", 11));
if (!TEST_IS_CONSTANT_EVALUATED) {
const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data());
test(s_othertype, first + 2, first + 5, std::string("hellollo"));
}
}
{ // test with a move iterator that returns char&&
typedef forward_iterator<const char*> It;
typedef std::move_iterator<It> MoveIt;
const char p[] = "ABCD";
std::string s;
s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
assert(s == "ABCD");
}
{ // test with a move iterator that returns char&&
typedef const char* It;
typedef std::move_iterator<It> MoveIt;
const char p[] = "ABCD";
std::string s;
s.append(MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
assert(s == "ABCD");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,90 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>& append(const charT* s); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, const typename S::value_type* str, S expected)
{
s.append(str);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "", S());
test(S(), "12345", S("12345"));
test(S(), "12345678901234567890", S("12345678901234567890"));
test(S("12345"), "", S("12345"));
test(S("12345"), "12345", S("1234512345"));
test(S("12345"), "1234567890", S("123451234567890"));
test(S("12345678901234567890"), "", S("12345678901234567890"));
test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
test(S("12345678901234567890"), "12345678901234567890",
S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "", S());
test(S(), "12345", S("12345"));
test(S(), "12345678901234567890", S("12345678901234567890"));
test(S("12345"), "", S("12345"));
test(S("12345"), "12345", S("1234512345"));
test(S("12345"), "1234567890", S("123451234567890"));
test(S("12345678901234567890"), "", S("12345678901234567890"));
test(S("12345678901234567890"), "12345", S("1234567890123456789012345"));
test(S("12345678901234567890"), "12345678901234567890",
S("1234567890123456789012345678901234567890"));
}
#endif
{ // test appending to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.append(s_short.c_str());
assert(s_short == "123/123/");
s_short.append(s_short.c_str());
assert(s_short == "123/123/123/123/");
s_short.append(s_short.c_str());
assert(s_short == "123/123/123/123/123/123/123/123/");
s_long.append(s_long.c_str());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// append(const charT* s, size_type n); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, const typename S::value_type* str, typename S::size_type n, S expected)
{
s.append(str, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "", 0, S());
test(S(), "12345", 3, S("123"));
test(S(), "12345", 4, S("1234"));
test(S(), "12345678901234567890", 0, S());
test(S(), "12345678901234567890", 1, S("1"));
test(S(), "12345678901234567890", 3, S("123"));
test(S(), "12345678901234567890", 20, S("12345678901234567890"));
test(S("12345"), "", 0, S("12345"));
test(S("12345"), "12345", 5, S("1234512345"));
test(S("12345"), "1234567890", 10, S("123451234567890"));
test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
test(S("12345678901234567890"), "12345678901234567890", 20,
S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "", 0, S());
test(S(), "12345", 3, S("123"));
test(S(), "12345", 4, S("1234"));
test(S(), "12345678901234567890", 0, S());
test(S(), "12345678901234567890", 1, S("1"));
test(S(), "12345678901234567890", 3, S("123"));
test(S(), "12345678901234567890", 20, S("12345678901234567890"));
test(S("12345"), "", 0, S("12345"));
test(S("12345"), "12345", 5, S("1234512345"));
test(S("12345"), "1234567890", 10, S("123451234567890"));
test(S("12345678901234567890"), "", 0, S("12345678901234567890"));
test(S("12345678901234567890"), "12345", 5, S("1234567890123456789012345"));
test(S("12345678901234567890"), "12345678901234567890", 20,
S("1234567890123456789012345678901234567890"));
}
#endif
{ // test appending to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.append(s_short.data(), s_short.size());
assert(s_short == "123/123/");
s_short.append(s_short.data(), s_short.size());
assert(s_short == "123/123/123/123/");
s_short.append(s_short.data(), s_short.size());
assert(s_short == "123/123/123/123/123/123/123/123/");
s_long.append(s_long.data(), s_long.size());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,70 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void push_back(charT c) // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
struct veryLarge
{
long long a;
char b;
};
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::value_type c, S expected)
{
s.push_back(c);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 'a', S(1, 'a'));
test(S("12345"), 'a', S("12345a"));
test(S("12345678901234567890"), 'a', S("12345678901234567890a"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 'a', S(1, 'a'));
test(S("12345"), 'a', S("12345a"));
test(S("12345678901234567890"), 'a', S("12345678901234567890a"));
}
#endif
{
// https://llvm.org/PR31454
std::basic_string<veryLarge> s;
veryLarge vl = {};
s.push_back(vl);
s.push_back(vl);
s.push_back(vl);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// append(size_type n, charT c); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type n, typename S::value_type c, S expected)
{
s.append(n, c);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S(1, 'a'));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S("12345"));
test(S("12345"), 1, 'a', S("12345a"));
test(S("12345"), 10, 'a', S("12345aaaaaaaaaa"));
test(S("12345678901234567890"), 0, 'a', S("12345678901234567890"));
test(S("12345678901234567890"), 1, 'a', S("12345678901234567890a"));
test(S("12345678901234567890"), 10, 'a', S("12345678901234567890aaaaaaaaaa"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S(1, 'a'));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S("12345"));
test(S("12345"), 1, 'a', S("12345a"));
test(S("12345"), 10, 'a', S("12345aaaaaaaaaa"));
test(S("12345678901234567890"), 0, 'a', S("12345678901234567890"));
test(S("12345678901234567890"), 1, 'a', S("12345678901234567890a"));
test(S("12345678901234567890"), 10, 'a', S("12345678901234567890aaaaaaaaaa"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// append(const basic_string<charT,traits>& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S str, S expected)
{
s.append(str);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S("12345"));
test(S("12345"), S("12345"), S("1234512345"));
test(S("12345"), S("1234567890"), S("123451234567890"));
test(S("12345"), S("12345678901234567890"), S("1234512345678901234567890"));
test(S("1234567890"), S(), S("1234567890"));
test(S("1234567890"), S("12345"), S("123456789012345"));
test(S("1234567890"), S("1234567890"), S("12345678901234567890"));
test(S("1234567890"), S("12345678901234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), S(), S("12345678901234567890"));
test(S("12345678901234567890"), S("12345"), S("1234567890123456789012345"));
test(S("12345678901234567890"), S("1234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S("12345"));
test(S("12345"), S("12345"), S("1234512345"));
test(S("12345"), S("1234567890"), S("123451234567890"));
test(S("12345"), S("12345678901234567890"), S("1234512345678901234567890"));
test(S("1234567890"), S(), S("1234567890"));
test(S("1234567890"), S("12345"), S("123456789012345"));
test(S("1234567890"), S("1234567890"), S("12345678901234567890"));
test(S("1234567890"), S("12345678901234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), S(), S("12345678901234567890"));
test(S("12345678901234567890"), S("12345"), S("1234567890123456789012345"));
test(S("12345678901234567890"), S("1234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("1234567890123456789012345678901234567890"));
}
#endif
#if TEST_STD_VER > 3
{ // LWG 2946
std::string s;
s.append({"abc", 1});
assert(s.size() == 1);
assert(s == "a");
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,147 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// append(const basic_string<charT,traits>& str, size_type pos, size_type n = npos); // constexpr since C++20
// the "= npos" was added for C++14
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
{
if (pos <= str.size())
{
s.append(str, pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.append(str, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test_npos(S s, S str, typename S::size_type pos, S expected)
{
if (pos <= str.size())
{
s.append(str, pos);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.append(str, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S(), 0, 0, S());
test(S(), S(), 1, 0, S());
test(S(), S("12345"), 0, 3, S("123"));
test(S(), S("12345"), 1, 4, S("2345"));
test(S(), S("12345"), 3, 15, S("45"));
test(S(), S("12345"), 5, 15, S(""));
test(S(), S("12345"), 6, 15, S("not happening"));
test(S(), S("12345678901234567890"), 0, 0, S());
test(S(), S("12345678901234567890"), 1, 1, S("2"));
test(S(), S("12345678901234567890"), 2, 3, S("345"));
test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), S(), 0, 0, S("12345"));
test(S("12345"), S("12345"), 2, 2, S("1234534"));
test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
S("123456789012345678906789012345"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S(), 0, 0, S());
test(S(), S(), 1, 0, S());
test(S(), S("12345"), 0, 3, S("123"));
test(S(), S("12345"), 1, 4, S("2345"));
test(S(), S("12345"), 3, 15, S("45"));
test(S(), S("12345"), 5, 15, S(""));
test(S(), S("12345"), 6, 15, S("not happening"));
test(S(), S("12345678901234567890"), 0, 0, S());
test(S(), S("12345678901234567890"), 1, 1, S("2"));
test(S(), S("12345678901234567890"), 2, 3, S("345"));
test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), S(), 0, 0, S("12345"));
test(S("12345"), S("12345"), 2, 2, S("1234534"));
test(S("12345"), S("1234567890"), 0, 100, S("123451234567890"));
test(S("12345678901234567890"), S(), 0, 0, S("12345678901234567890"));
test(S("12345678901234567890"), S("12345"), 1, 3, S("12345678901234567890234"));
test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
S("123456789012345678906789012345"));
}
#endif
{
typedef std::string S;
test_npos(S(), S(), 0, S());
test_npos(S(), S(), 1, S());
test_npos(S(), S("12345"), 0, S("12345"));
test_npos(S(), S("12345"), 1, S("2345"));
test_npos(S(), S("12345"), 3, S("45"));
test_npos(S(), S("12345"), 5, S(""));
test_npos(S(), S("12345"), 6, S("not happening"));
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,93 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// append(basic_string_view<charT,traits> sv); // constexpr since C++20
#include <string>
#include <string_view>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(S s, SV sv, S expected)
{
s.append(sv);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
test(S(), SV(), S());
test(S(), SV("12345"), S("12345"));
test(S(), SV("1234567890"), S("1234567890"));
test(S(), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), SV(), S("12345"));
test(S("12345"), SV("12345"), S("1234512345"));
test(S("12345"), SV("1234567890"), S("123451234567890"));
test(S("12345"), SV("12345678901234567890"), S("1234512345678901234567890"));
test(S("1234567890"), SV(), S("1234567890"));
test(S("1234567890"), SV("12345"), S("123456789012345"));
test(S("1234567890"), SV("1234567890"), S("12345678901234567890"));
test(S("1234567890"), SV("12345678901234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), SV(), S("12345678901234567890"));
test(S("12345678901234567890"), SV("12345"), S("1234567890123456789012345"));
test(S("12345678901234567890"), SV("1234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), SV("12345678901234567890"),
S("1234567890123456789012345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(S(), SV(), S());
test(S(), SV("12345"), S("12345"));
test(S(), SV("1234567890"), S("1234567890"));
test(S(), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), SV(), S("12345"));
test(S("12345"), SV("12345"), S("1234512345"));
test(S("12345"), SV("1234567890"), S("123451234567890"));
test(S("12345"), SV("12345678901234567890"), S("1234512345678901234567890"));
test(S("1234567890"), SV(), S("1234567890"));
test(S("1234567890"), SV("12345"), S("123456789012345"));
test(S("1234567890"), SV("1234567890"), S("12345678901234567890"));
test(S("1234567890"), SV("12345678901234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), SV(), S("12345678901234567890"));
test(S("12345678901234567890"), SV("12345"), S("1234567890123456789012345"));
test(S("12345678901234567890"), SV("1234567890"), S("123456789012345678901234567890"));
test(S("12345678901234567890"), SV("12345678901234567890"),
S("1234567890123456789012345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,205 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template <class T>
// basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17, constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(S s, SV sv, typename S::size_type pos, typename S::size_type n, S expected)
{
if (pos <= sv.size())
{
s.assign(sv, pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.assign(sv, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test_npos(S s, SV sv, typename S::size_type pos, S expected)
{
if (pos <= sv.size())
{
s.assign(sv, pos);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.assign(sv, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > sv.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
test(S(), SV(), 0, 0, S());
test(S(), SV(), 1, 0, S());
test(S(), SV("12345"), 0, 3, S("123"));
test(S(), SV("12345"), 1, 4, S("2345"));
test(S(), SV("12345"), 3, 15, S("45"));
test(S(), SV("12345"), 5, 15, S(""));
test(S(), SV("12345"), 6, 15, S("not happening"));
test(S(), SV("12345678901234567890"), 0, 0, S());
test(S(), SV("12345678901234567890"), 1, 1, S("2"));
test(S(), SV("12345678901234567890"), 2, 3, S("345"));
test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), SV(), 0, 0, S());
test(S("12345"), SV("12345"), 2, 2, S("34"));
test(S("12345"), SV("1234567890"), 0, 100, S("1234567890"));
test(S("12345678901234567890"), SV(), 0, 0, S());
test(S("12345678901234567890"), SV("12345"), 1, 3, S("234"));
test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
S("6789012345"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(S(), SV(), 0, 0, S());
test(S(), SV(), 1, 0, S());
test(S(), SV("12345"), 0, 3, S("123"));
test(S(), SV("12345"), 1, 4, S("2345"));
test(S(), SV("12345"), 3, 15, S("45"));
test(S(), SV("12345"), 5, 15, S(""));
test(S(), SV("12345"), 6, 15, S("not happening"));
test(S(), SV("12345678901234567890"), 0, 0, S());
test(S(), SV("12345678901234567890"), 1, 1, S("2"));
test(S(), SV("12345678901234567890"), 2, 3, S("345"));
test(S(), SV("12345678901234567890"), 12, 13, S("34567890"));
test(S(), SV("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), SV(), 0, 0, S());
test(S("12345"), SV("12345"), 2, 2, S("34"));
test(S("12345"), SV("1234567890"), 0, 100, S("1234567890"));
test(S("12345678901234567890"), SV(), 0, 0, S());
test(S("12345678901234567890"), SV("12345"), 1, 3, S("234"));
test(S("12345678901234567890"), SV("12345678901234567890"), 5, 10,
S("6789012345"));
}
#endif
{
typedef std::string S;
typedef std::string_view SV;
test_npos(S(), SV(), 0, S());
test_npos(S(), SV(), 1, S());
test_npos(S(), SV("12345"), 0, S("12345"));
test_npos(S(), SV("12345"), 1, S("2345"));
test_npos(S(), SV("12345"), 3, S("45"));
test_npos(S(), SV("12345"), 5, S(""));
test_npos(S(), SV("12345"), 6, S("not happening"));
}
{
std::string s = "ABCD";
std::string_view sv = "EFGH";
char arr[] = "IJKL";
s.assign("CDEF", 0); // calls assign(const char *, len)
assert(s == "");
s.clear();
s.assign("QRST", 0, std::string::npos); // calls assign(string("QRST", pos, len)
assert(s == "QRST");
s.clear();
s.assign(sv, 0); // calls assign(T, pos, npos)
assert(s == sv);
s.clear();
s.assign(sv, 0, std::string::npos); // calls assign(T, pos, npos)
assert(s == sv);
s.clear();
s.assign(arr, 0); // calls assign(const char *, len)
assert(s == "");
s.clear();
s.assign(arr, 0, std::string::npos); // calls assign(string("IJKL"), pos, npos)
assert(s == "IJKL");
s.clear();
s.assign(arr, 0); // calls assign(const char *, len)
assert(s == "");
s.clear();
}
{
std::string s = "ABCD";
std::string_view sv = s;
s.assign(sv);
assert(s == "ABCD");
sv = s;
s.assign(sv, 0, std::string::npos);
assert(s == "ABCD");
}
{
std::string s = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
std::string_view sv = s;
s.assign(sv);
assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
sv = s;
s.assign(sv, 0, std::string::npos);
assert(s == "ABCDEFGHIJKLMNOPQRSTUVWXYZ");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,45 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// basic_string& assign(initializer_list<charT> il); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s("123");
s.assign({'a', 'b', 'c'});
assert(s == "abc");
}
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s("123");
s.assign({'a', 'b', 'c'});
assert(s == "abc");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,237 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template<class InputIterator>
// basic_string& assign(InputIterator first, InputIterator last); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "min_allocator.h"
template <class S, class It>
TEST_CONSTEXPR_CXX20 void
test(S s, It first, It last, S expected)
{
s.assign(first, last);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Widget { operator char() const { throw 42; } };
template <class S, class It>
void
test_exceptions(S s, It first, It last)
{
S original = s;
typename S::iterator begin = s.begin();
typename S::iterator end = s.end();
try {
s.assign(first, last);
assert(false);
} catch (...) {}
// Part of "no effects" is that iterators and pointers
// into the string must not have been invalidated.
LIBCPP_ASSERT(s.__invariants());
assert(s == original);
assert(s.begin() == begin);
assert(s.end() == end);
}
#endif
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), s, s, S());
test(S(), s, s+1, S("A"));
test(S(), s, s+10, S("ABCDEFGHIJ"));
test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), s, s, S());
test(S("12345"), s, s+1, S("A"));
test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), s, s, S());
test(S("1234567890"), s, s+1, S("A"));
test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), s, s, S());
test(S("12345678901234567890"), s, s+1, S("A"));
test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
test(S("12345678901234567890"), s, s+52,
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), s, s, S());
test(S(), s, s+1, S("A"));
test(S(), s, s+10, S("ABCDEFGHIJ"));
test(S(), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), s, s, S());
test(S("12345"), s, s+1, S("A"));
test(S("12345"), s, s+10, S("ABCDEFGHIJ"));
test(S("12345"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), s, s, S());
test(S("1234567890"), s, s+1, S("A"));
test(S("1234567890"), s, s+10, S("ABCDEFGHIJ"));
test(S("1234567890"), s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), s, s, S());
test(S("12345678901234567890"), s, s+1, S("A"));
test(S("12345678901234567890"), s, s+10, S("ABCDEFGHIJ"));
test(S("12345678901234567890"), s, s+52,
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S(), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("12345"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("1234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s),
S());
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1),
S("A"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10),
S("ABCDEFGHIJ"));
test(S("12345678901234567890"), cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
typedef std::string S;
typedef ThrowingIterator<char> TIter;
typedef cpp17_input_iterator<TIter> IIter;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test_exceptions(S(), IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter()));
test_exceptions(S(), IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter()));
test_exceptions(S(), IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter()));
test_exceptions(S(), TIter(s, s+10, 4, TIter::TAIncrement), TIter());
test_exceptions(S(), TIter(s, s+10, 5, TIter::TADereference), TIter());
test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter());
Widget w[100];
test_exceptions(S(), w, w+100);
}
#endif
{ // test assigning to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.assign(s_short.begin(), s_short.end());
assert(s_short == "123/");
s_short.assign(s_short.begin() + 2, s_short.end());
assert(s_short == "3/");
s_long.assign(s_long.begin(), s_long.end());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
s_long.assign(s_long.begin() + 30, s_long.end());
assert(s_long == "nsectetur/");
}
{ // test assigning a different type
typedef std::string S;
const uint8_t p[] = "ABCD";
S s;
s.assign(p, p + 4);
assert(s == "ABCD");
}
{ // regression-test assigning to self in sneaky ways
std::string sneaky = "hello";
sneaky.resize(sneaky.capacity(), 'x');
std::string expected = sneaky + std::string(1, '\0');
test(sneaky, sneaky.data(), sneaky.data() + sneaky.size() + 1, expected);
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,88 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>& assign(const charT* s); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, const typename S::value_type* str, S expected)
{
s.assign(str);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "", S());
test(S(), "12345", S("12345"));
test(S(), "12345678901234567890", S("12345678901234567890"));
test(S("12345"), "", S());
test(S("12345"), "12345", S("12345"));
test(S("12345"), "1234567890", S("1234567890"));
test(S("12345678901234567890"), "", S());
test(S("12345678901234567890"), "12345", S("12345"));
test(S("12345678901234567890"), "12345678901234567890",
S("12345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "", S());
test(S(), "12345", S("12345"));
test(S(), "12345678901234567890", S("12345678901234567890"));
test(S("12345"), "", S());
test(S("12345"), "12345", S("12345"));
test(S("12345"), "1234567890", S("1234567890"));
test(S("12345678901234567890"), "", S());
test(S("12345678901234567890"), "12345", S("12345"));
test(S("12345678901234567890"), "12345678901234567890",
S("12345678901234567890"));
}
#endif
{ // test assignment to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.assign(s_short.c_str());
assert(s_short == "123/");
s_short.assign(s_short.c_str() + 2);
assert(s_short == "3/");
s_long.assign(s_long.c_str() + 30);
assert(s_long == "nsectetur/");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,99 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(const charT* s, size_type n); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, const typename S::value_type* str, typename S::size_type n, S expected)
{
s.assign(str, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), "", 0, S());
test(S(), "12345", 3, S("123"));
test(S(), "12345", 4, S("1234"));
test(S(), "12345678901234567890", 0, S());
test(S(), "12345678901234567890", 1, S("1"));
test(S(), "12345678901234567890", 3, S("123"));
test(S(), "12345678901234567890", 20, S("12345678901234567890"));
test(S("12345"), "", 0, S());
test(S("12345"), "12345", 5, S("12345"));
test(S("12345"), "1234567890", 10, S("1234567890"));
test(S("12345678901234567890"), "", 0, S());
test(S("12345678901234567890"), "12345", 5, S("12345"));
test(S("12345678901234567890"), "12345678901234567890", 20,
S("12345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), "", 0, S());
test(S(), "12345", 3, S("123"));
test(S(), "12345", 4, S("1234"));
test(S(), "12345678901234567890", 0, S());
test(S(), "12345678901234567890", 1, S("1"));
test(S(), "12345678901234567890", 3, S("123"));
test(S(), "12345678901234567890", 20, S("12345678901234567890"));
test(S("12345"), "", 0, S());
test(S("12345"), "12345", 5, S("12345"));
test(S("12345"), "1234567890", 10, S("1234567890"));
test(S("12345678901234567890"), "", 0, S());
test(S("12345678901234567890"), "12345", 5, S("12345"));
test(S("12345678901234567890"), "12345678901234567890", 20,
S("12345678901234567890"));
}
#endif
{ // test assign to self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.assign(s_short.data(), s_short.size());
assert(s_short == "123/");
s_short.assign(s_short.data() + 2, s_short.size() - 2);
assert(s_short == "3/");
s_long.assign(s_long.data(), s_long.size());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/");
s_long.assign(s_long.data() + 2, 8 );
assert(s_long == "rem ipsu");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,91 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(basic_string<charT,traits>&& str); // constexpr since C++20
#include <string>
#include <utility>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S str, S expected)
{
s.assign(std::move(str));
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S());
test(S("12345"), S("12345"), S("12345"));
test(S("12345"), S("1234567890"), S("1234567890"));
test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), S(), S());
test(S("1234567890"), S("12345"), S("12345"));
test(S("1234567890"), S("1234567890"), S("1234567890"));
test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), S(), S());
test(S("12345678901234567890"), S("12345"), S("12345"));
test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("12345678901234567890"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S());
test(S("12345"), S("12345"), S("12345"));
test(S("12345"), S("1234567890"), S("1234567890"));
test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), S(), S());
test(S("1234567890"), S("12345"), S("12345"));
test(S("1234567890"), S("1234567890"), S("1234567890"));
test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), S(), S());
test(S("12345678901234567890"), S("12345"), S("12345"));
test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("12345678901234567890"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,74 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(size_type n, charT c); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type n, typename S::value_type c, S expected)
{
s.assign(n, c);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S(1, 'a'));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S());
test(S("12345"), 1, 'a', S(1, 'a'));
test(S("12345"), 10, 'a', S(10, 'a'));
test(S("12345678901234567890"), 0, 'a', S());
test(S("12345678901234567890"), 1, 'a', S(1, 'a'));
test(S("12345678901234567890"), 10, 'a', S(10, 'a'));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), 0, 'a', S());
test(S(), 1, 'a', S(1, 'a'));
test(S(), 10, 'a', S(10, 'a'));
test(S(), 100, 'a', S(100, 'a'));
test(S("12345"), 0, 'a', S());
test(S("12345"), 1, 'a', S(1, 'a'));
test(S("12345"), 10, 'a', S(10, 'a'));
test(S("12345678901234567890"), 0, 'a', S());
test(S("12345678901234567890"), 1, 'a', S(1, 'a'));
test(S("12345678901234567890"), 10, 'a', S(10, 'a'));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,127 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(const basic_string<charT,traits>& str); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "test_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S str, S expected)
{
s.assign(str);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
template <class S>
TEST_CONSTEXPR_CXX20 void
testAlloc(S s, S str, const typename S::allocator_type& a)
{
s.assign(str);
LIBCPP_ASSERT(s.__invariants());
assert(s == str);
assert(s.get_allocator() == a);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S());
test(S("12345"), S("12345"), S("12345"));
test(S("12345"), S("1234567890"), S("1234567890"));
test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), S(), S());
test(S("1234567890"), S("12345"), S("12345"));
test(S("1234567890"), S("1234567890"), S("1234567890"));
test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), S(), S());
test(S("12345678901234567890"), S("12345"), S("12345"));
test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("12345678901234567890"));
testAlloc(S(), S(), std::allocator<char>());
testAlloc(S(), S("12345"), std::allocator<char>());
testAlloc(S(), S("1234567890"), std::allocator<char>());
testAlloc(S(), S("12345678901234567890"), std::allocator<char>());
}
{ // LWG#5579 make sure assign takes the allocators where appropriate
typedef other_allocator<char> A; // has POCCA --> true
typedef std::basic_string<char, std::char_traits<char>, A> S;
testAlloc(S(A(5)), S(A(3)), A(3));
testAlloc(S(A(5)), S("1"), A());
testAlloc(S(A(5)), S("1", A(7)), A(7));
testAlloc(S(A(5)), S("1234567890123456789012345678901234567890123456789012345678901234567890", A(7)), A(7));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S(), S());
test(S(), S("12345"), S("12345"));
test(S(), S("1234567890"), S("1234567890"));
test(S(), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), S(), S());
test(S("12345"), S("12345"), S("12345"));
test(S("12345"), S("1234567890"), S("1234567890"));
test(S("12345"), S("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), S(), S());
test(S("1234567890"), S("12345"), S("12345"));
test(S("1234567890"), S("1234567890"), S("1234567890"));
test(S("1234567890"), S("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), S(), S());
test(S("12345678901234567890"), S("12345"), S("12345"));
test(S("12345678901234567890"), S("1234567890"), S("1234567890"));
test(S("12345678901234567890"), S("12345678901234567890"),
S("12345678901234567890"));
testAlloc(S(), S(), min_allocator<char>());
testAlloc(S(), S("12345"), min_allocator<char>());
testAlloc(S(), S("1234567890"), min_allocator<char>());
testAlloc(S(), S("12345678901234567890"), min_allocator<char>());
}
#endif
#if TEST_STD_VER > 14
{
typedef std::string S;
static_assert(noexcept(S().assign(S())), ""); // LWG#2063
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,147 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(const basic_string<charT,traits>& str, size_type pos, size_type n=npos); // constexpr since C++20
// the =npos was added for C++14
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected)
{
if (pos <= str.size())
{
s.assign(str, pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.assign(str, pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test_npos(S s, S str, typename S::size_type pos, S expected)
{
if (pos <= str.size())
{
s.assign(str, pos);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.assign(str, pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(), S(), 0, 0, S());
test(S(), S(), 1, 0, S());
test(S(), S("12345"), 0, 3, S("123"));
test(S(), S("12345"), 1, 4, S("2345"));
test(S(), S("12345"), 3, 15, S("45"));
test(S(), S("12345"), 5, 15, S(""));
test(S(), S("12345"), 6, 15, S("not happening"));
test(S(), S("12345678901234567890"), 0, 0, S());
test(S(), S("12345678901234567890"), 1, 1, S("2"));
test(S(), S("12345678901234567890"), 2, 3, S("345"));
test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), S(), 0, 0, S());
test(S("12345"), S("12345"), 2, 2, S("34"));
test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
test(S("12345678901234567890"), S(), 0, 0, S());
test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
S("6789012345"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(), S(), 0, 0, S());
test(S(), S(), 1, 0, S());
test(S(), S("12345"), 0, 3, S("123"));
test(S(), S("12345"), 1, 4, S("2345"));
test(S(), S("12345"), 3, 15, S("45"));
test(S(), S("12345"), 5, 15, S(""));
test(S(), S("12345"), 6, 15, S("not happening"));
test(S(), S("12345678901234567890"), 0, 0, S());
test(S(), S("12345678901234567890"), 1, 1, S("2"));
test(S(), S("12345678901234567890"), 2, 3, S("345"));
test(S(), S("12345678901234567890"), 12, 13, S("34567890"));
test(S(), S("12345678901234567890"), 21, 13, S("not happening"));
test(S("12345"), S(), 0, 0, S());
test(S("12345"), S("12345"), 2, 2, S("34"));
test(S("12345"), S("1234567890"), 0, 100, S("1234567890"));
test(S("12345678901234567890"), S(), 0, 0, S());
test(S("12345678901234567890"), S("12345"), 1, 3, S("234"));
test(S("12345678901234567890"), S("12345678901234567890"), 5, 10,
S("6789012345"));
}
#endif
{
typedef std::string S;
test_npos(S(), S(), 0, S());
test_npos(S(), S(), 1, S());
test_npos(S(), S("12345"), 0, S("12345"));
test_npos(S(), S("12345"), 1, S("2345"));
test_npos(S(), S("12345"), 3, S("45"));
test_npos(S(), S("12345"), 5, S(""));
test_npos(S(), S("12345"), 6, S("not happening"));
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,115 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// assign(basic_string_view<charT,traits> sv); // constexpr since C++20
#include <string>
#include <string_view>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
#include "test_allocator.h"
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
test(S s, SV sv, S expected)
{
s.assign(sv);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
template <class S, class SV>
TEST_CONSTEXPR_CXX20 void
testAlloc(S s, SV sv, const typename S::allocator_type& a)
{
s.assign(sv);
LIBCPP_ASSERT(s.__invariants());
assert(s == sv);
assert(s.get_allocator() == a);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
typedef std::string_view SV;
test(S(), SV(), S());
test(S(), SV("12345"), S("12345"));
test(S(), SV("1234567890"), S("1234567890"));
test(S(), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), SV(), S());
test(S("12345"), SV("12345"), S("12345"));
test(S("12345"), SV("1234567890"), S("1234567890"));
test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), SV(), S());
test(S("1234567890"), SV("12345"), S("12345"));
test(S("1234567890"), SV("1234567890"), S("1234567890"));
test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), SV(), S());
test(S("12345678901234567890"), SV("12345"), S("12345"));
test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
test(S("12345678901234567890"), SV("12345678901234567890"),
S("12345678901234567890"));
testAlloc(S(), SV(), std::allocator<char>());
testAlloc(S(), SV("12345"), std::allocator<char>());
testAlloc(S(), SV("1234567890"), std::allocator<char>());
testAlloc(S(), SV("12345678901234567890"), std::allocator<char>());
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string <char, std::char_traits<char>, min_allocator<char>> S;
typedef std::basic_string_view<char, std::char_traits<char> > SV;
test(S(), SV(), S());
test(S(), SV("12345"), S("12345"));
test(S(), SV("1234567890"), S("1234567890"));
test(S(), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345"), SV(), S());
test(S("12345"), SV("12345"), S("12345"));
test(S("12345"), SV("1234567890"), S("1234567890"));
test(S("12345"), SV("12345678901234567890"), S("12345678901234567890"));
test(S("1234567890"), SV(), S());
test(S("1234567890"), SV("12345"), S("12345"));
test(S("1234567890"), SV("1234567890"), S("1234567890"));
test(S("1234567890"), SV("12345678901234567890"), S("12345678901234567890"));
test(S("12345678901234567890"), SV(), S());
test(S("12345678901234567890"), SV("12345"), S("12345"));
test(S("12345678901234567890"), SV("1234567890"), S("1234567890"));
test(S("12345678901234567890"), SV("12345678901234567890"),
S("12345678901234567890"));
testAlloc(S(), SV(), min_allocator<char>());
testAlloc(S(), SV("12345"), min_allocator<char>());
testAlloc(S(), SV("1234567890"), min_allocator<char>());
testAlloc(S(), SV("12345678901234567890"), min_allocator<char>());
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,191 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// size_type copy(charT* s, size_type n, size_type pos = 0) const; // constexpr since C++20
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S str, typename S::value_type* s, typename S::size_type n,
typename S::size_type pos)
{
const S& cs = str;
if (pos <= cs.size())
{
typename S::size_type r = cs.copy(s, n, pos);
typename S::size_type rlen = std::min(n, cs.size() - pos);
assert(r == rlen);
for (r = 0; r < rlen; ++r)
assert(S::traits_type::eq(cs[pos+r], s[r]));
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
typename S::size_type r = cs.copy(s, n, pos);
((void)r); // Prevent unused warning
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > str.size());
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
char s[50];
test(S(""), s, 0, 0);
test(S(""), s, 0, 1);
test(S(""), s, 1, 0);
test(S("abcde"), s, 0, 0);
test(S("abcde"), s, 0, 1);
test(S("abcde"), s, 0, 2);
test(S("abcde"), s, 0, 4);
test(S("abcde"), s, 0, 5);
test(S("abcde"), s, 0, 6);
test(S("abcde"), s, 1, 0);
test(S("abcde"), s, 1, 1);
test(S("abcde"), s, 1, 2);
test(S("abcde"), s, 1, 4);
test(S("abcde"), s, 1, 5);
test(S("abcde"), s, 2, 0);
test(S("abcde"), s, 2, 1);
test(S("abcde"), s, 2, 2);
test(S("abcde"), s, 2, 4);
test(S("abcde"), s, 4, 0);
test(S("abcde"), s, 4, 1);
test(S("abcde"), s, 4, 2);
test(S("abcde"), s, 5, 0);
test(S("abcde"), s, 5, 1);
test(S("abcde"), s, 6, 0);
test(S("abcdefghijklmnopqrst"), s, 0, 0);
test(S("abcdefghijklmnopqrst"), s, 0, 1);
test(S("abcdefghijklmnopqrst"), s, 0, 2);
test(S("abcdefghijklmnopqrst"), s, 0, 10);
test(S("abcdefghijklmnopqrst"), s, 0, 19);
test(S("abcdefghijklmnopqrst"), s, 0, 20);
test(S("abcdefghijklmnopqrst"), s, 0, 21);
test(S("abcdefghijklmnopqrst"), s, 1, 0);
test(S("abcdefghijklmnopqrst"), s, 1, 1);
test(S("abcdefghijklmnopqrst"), s, 1, 2);
test(S("abcdefghijklmnopqrst"), s, 1, 9);
test(S("abcdefghijklmnopqrst"), s, 1, 18);
test(S("abcdefghijklmnopqrst"), s, 1, 19);
test(S("abcdefghijklmnopqrst"), s, 1, 20);
test(S("abcdefghijklmnopqrst"), s, 2, 0);
test(S("abcdefghijklmnopqrst"), s, 2, 1);
test(S("abcdefghijklmnopqrst"), s, 2, 2);
test(S("abcdefghijklmnopqrst"), s, 2, 9);
test(S("abcdefghijklmnopqrst"), s, 2, 17);
test(S("abcdefghijklmnopqrst"), s, 2, 18);
test(S("abcdefghijklmnopqrst"), s, 2, 19);
test(S("abcdefghijklmnopqrst"), s, 10, 0);
test(S("abcdefghijklmnopqrst"), s, 10, 1);
test(S("abcdefghijklmnopqrst"), s, 10, 2);
test(S("abcdefghijklmnopqrst"), s, 10, 5);
test(S("abcdefghijklmnopqrst"), s, 10, 9);
test(S("abcdefghijklmnopqrst"), s, 10, 10);
test(S("abcdefghijklmnopqrst"), s, 10, 11);
test(S("abcdefghijklmnopqrst"), s, 19, 0);
test(S("abcdefghijklmnopqrst"), s, 19, 1);
test(S("abcdefghijklmnopqrst"), s, 19, 2);
test(S("abcdefghijklmnopqrst"), s, 20, 0);
test(S("abcdefghijklmnopqrst"), s, 20, 1);
test(S("abcdefghijklmnopqrst"), s, 21, 0);
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
char s[50];
test(S(""), s, 0, 0);
test(S(""), s, 0, 1);
test(S(""), s, 1, 0);
test(S("abcde"), s, 0, 0);
test(S("abcde"), s, 0, 1);
test(S("abcde"), s, 0, 2);
test(S("abcde"), s, 0, 4);
test(S("abcde"), s, 0, 5);
test(S("abcde"), s, 0, 6);
test(S("abcde"), s, 1, 0);
test(S("abcde"), s, 1, 1);
test(S("abcde"), s, 1, 2);
test(S("abcde"), s, 1, 4);
test(S("abcde"), s, 1, 5);
test(S("abcde"), s, 2, 0);
test(S("abcde"), s, 2, 1);
test(S("abcde"), s, 2, 2);
test(S("abcde"), s, 2, 4);
test(S("abcde"), s, 4, 0);
test(S("abcde"), s, 4, 1);
test(S("abcde"), s, 4, 2);
test(S("abcde"), s, 5, 0);
test(S("abcde"), s, 5, 1);
test(S("abcde"), s, 6, 0);
test(S("abcdefghijklmnopqrst"), s, 0, 0);
test(S("abcdefghijklmnopqrst"), s, 0, 1);
test(S("abcdefghijklmnopqrst"), s, 0, 2);
test(S("abcdefghijklmnopqrst"), s, 0, 10);
test(S("abcdefghijklmnopqrst"), s, 0, 19);
test(S("abcdefghijklmnopqrst"), s, 0, 20);
test(S("abcdefghijklmnopqrst"), s, 0, 21);
test(S("abcdefghijklmnopqrst"), s, 1, 0);
test(S("abcdefghijklmnopqrst"), s, 1, 1);
test(S("abcdefghijklmnopqrst"), s, 1, 2);
test(S("abcdefghijklmnopqrst"), s, 1, 9);
test(S("abcdefghijklmnopqrst"), s, 1, 18);
test(S("abcdefghijklmnopqrst"), s, 1, 19);
test(S("abcdefghijklmnopqrst"), s, 1, 20);
test(S("abcdefghijklmnopqrst"), s, 2, 0);
test(S("abcdefghijklmnopqrst"), s, 2, 1);
test(S("abcdefghijklmnopqrst"), s, 2, 2);
test(S("abcdefghijklmnopqrst"), s, 2, 9);
test(S("abcdefghijklmnopqrst"), s, 2, 17);
test(S("abcdefghijklmnopqrst"), s, 2, 18);
test(S("abcdefghijklmnopqrst"), s, 2, 19);
test(S("abcdefghijklmnopqrst"), s, 10, 0);
test(S("abcdefghijklmnopqrst"), s, 10, 1);
test(S("abcdefghijklmnopqrst"), s, 10, 2);
test(S("abcdefghijklmnopqrst"), s, 10, 5);
test(S("abcdefghijklmnopqrst"), s, 10, 9);
test(S("abcdefghijklmnopqrst"), s, 10, 10);
test(S("abcdefghijklmnopqrst"), s, 10, 11);
test(S("abcdefghijklmnopqrst"), s, 19, 0);
test(S("abcdefghijklmnopqrst"), s, 19, 1);
test(S("abcdefghijklmnopqrst"), s, 19, 2);
test(S("abcdefghijklmnopqrst"), s, 20, 0);
test(S("abcdefghijklmnopqrst"), s, 20, 1);
test(S("abcdefghijklmnopqrst"), s, 21, 0);
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,76 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator erase(const_iterator p); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::difference_type pos, S expected)
{
typename S::const_iterator p = s.begin() + pos;
typename S::iterator i = s.erase(p);
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
assert(i - s.begin() == pos);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S("abcde"), 0, S("bcde"));
test(S("abcde"), 1, S("acde"));
test(S("abcde"), 2, S("abde"));
test(S("abcde"), 4, S("abcd"));
test(S("abcdefghij"), 0, S("bcdefghij"));
test(S("abcdefghij"), 1, S("acdefghij"));
test(S("abcdefghij"), 5, S("abcdeghij"));
test(S("abcdefghij"), 9, S("abcdefghi"));
test(S("abcdefghijklmnopqrst"), 0, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S("abcde"), 0, S("bcde"));
test(S("abcde"), 1, S("acde"));
test(S("abcde"), 2, S("abde"));
test(S("abcde"), 4, S("abcd"));
test(S("abcdefghij"), 0, S("bcdefghij"));
test(S("abcdefghij"), 1, S("acdefghij"));
test(S("abcdefghij"), 5, S("abcdeghij"));
test(S("abcdefghij"), 9, S("abcdefghi"));
test(S("abcdefghijklmnopqrst"), 0, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,161 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator erase(const_iterator first, const_iterator last); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::difference_type pos, typename S::difference_type n, S expected)
{
typename S::const_iterator first = s.cbegin() + pos;
typename S::const_iterator last = s.cbegin() + pos + n;
typename S::iterator i = s.erase(first, last);
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
assert(i - s.begin() == pos);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(""), 0, 0, S(""));
test(S("abcde"), 0, 0, S("abcde"));
test(S("abcde"), 0, 1, S("bcde"));
test(S("abcde"), 0, 2, S("cde"));
test(S("abcde"), 0, 4, S("e"));
test(S("abcde"), 0, 5, S(""));
test(S("abcde"), 1, 0, S("abcde"));
test(S("abcde"), 1, 1, S("acde"));
test(S("abcde"), 1, 2, S("ade"));
test(S("abcde"), 1, 3, S("ae"));
test(S("abcde"), 1, 4, S("a"));
test(S("abcde"), 2, 0, S("abcde"));
test(S("abcde"), 2, 1, S("abde"));
test(S("abcde"), 2, 2, S("abe"));
test(S("abcde"), 2, 3, S("ab"));
test(S("abcde"), 4, 0, S("abcde"));
test(S("abcde"), 4, 1, S("abcd"));
test(S("abcde"), 5, 0, S("abcde"));
test(S("abcdefghij"), 0, 0, S("abcdefghij"));
test(S("abcdefghij"), 0, 1, S("bcdefghij"));
test(S("abcdefghij"), 0, 5, S("fghij"));
test(S("abcdefghij"), 0, 9, S("j"));
test(S("abcdefghij"), 0, 10, S(""));
test(S("abcdefghij"), 1, 0, S("abcdefghij"));
test(S("abcdefghij"), 1, 1, S("acdefghij"));
test(S("abcdefghij"), 1, 4, S("afghij"));
test(S("abcdefghij"), 1, 8, S("aj"));
test(S("abcdefghij"), 1, 9, S("a"));
test(S("abcdefghij"), 5, 0, S("abcdefghij"));
test(S("abcdefghij"), 5, 1, S("abcdeghij"));
test(S("abcdefghij"), 5, 2, S("abcdehij"));
test(S("abcdefghij"), 5, 4, S("abcdej"));
test(S("abcdefghij"), 5, 5, S("abcde"));
test(S("abcdefghij"), 9, 0, S("abcdefghij"));
test(S("abcdefghij"), 9, 1, S("abcdefghi"));
test(S("abcdefghij"), 10, 0, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(""), 0, 0, S(""));
test(S("abcde"), 0, 0, S("abcde"));
test(S("abcde"), 0, 1, S("bcde"));
test(S("abcde"), 0, 2, S("cde"));
test(S("abcde"), 0, 4, S("e"));
test(S("abcde"), 0, 5, S(""));
test(S("abcde"), 1, 0, S("abcde"));
test(S("abcde"), 1, 1, S("acde"));
test(S("abcde"), 1, 2, S("ade"));
test(S("abcde"), 1, 3, S("ae"));
test(S("abcde"), 1, 4, S("a"));
test(S("abcde"), 2, 0, S("abcde"));
test(S("abcde"), 2, 1, S("abde"));
test(S("abcde"), 2, 2, S("abe"));
test(S("abcde"), 2, 3, S("ab"));
test(S("abcde"), 4, 0, S("abcde"));
test(S("abcde"), 4, 1, S("abcd"));
test(S("abcde"), 5, 0, S("abcde"));
test(S("abcdefghij"), 0, 0, S("abcdefghij"));
test(S("abcdefghij"), 0, 1, S("bcdefghij"));
test(S("abcdefghij"), 0, 5, S("fghij"));
test(S("abcdefghij"), 0, 9, S("j"));
test(S("abcdefghij"), 0, 10, S(""));
test(S("abcdefghij"), 1, 0, S("abcdefghij"));
test(S("abcdefghij"), 1, 1, S("acdefghij"));
test(S("abcdefghij"), 1, 4, S("afghij"));
test(S("abcdefghij"), 1, 8, S("aj"));
test(S("abcdefghij"), 1, 9, S("a"));
test(S("abcdefghij"), 5, 0, S("abcdefghij"));
test(S("abcdefghij"), 5, 1, S("abcdeghij"));
test(S("abcdefghij"), 5, 2, S("abcdehij"));
test(S("abcdefghij"), 5, 4, S("abcdej"));
test(S("abcdefghij"), 5, 5, S("abcde"));
test(S("abcdefghij"), 9, 0, S("abcdefghij"));
test(S("abcdefghij"), 9, 1, S("abcdefghi"));
test(S("abcdefghij"), 10, 0, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,56 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// void pop_back(); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S expected)
{
s.pop_back();
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S("abcde"), S("abcd"));
test(S("abcdefghij"), S("abcdefghi"));
test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrs"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S("abcde"), S("abcd"));
test(S("abcdefghij"), S("abcdefghi"));
test(S("abcdefghijklmnopqrst"), S("abcdefghijklmnopqrs"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,312 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// erase(size_type pos = 0, size_type n = npos); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type pos, typename S::size_type n, S expected)
{
const typename S::size_type old_size = s.size();
S s0 = s;
if (pos <= old_size)
{
s.erase(pos, n);
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.erase(pos, n);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > old_size);
assert(s == s0);
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type pos, S expected)
{
const typename S::size_type old_size = s.size();
S s0 = s;
if (pos <= old_size)
{
s.erase(pos);
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.erase(pos);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > old_size);
assert(s == s0);
}
}
#endif
}
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, S expected)
{
s.erase();
LIBCPP_ASSERT(s.__invariants());
assert(s[s.size()] == typename S::value_type());
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(""), 0, 0, S(""));
test(S(""), 0, 1, S(""));
test(S(""), 1, 0, S("can't happen"));
test(S("abcde"), 0, 0, S("abcde"));
test(S("abcde"), 0, 1, S("bcde"));
test(S("abcde"), 0, 2, S("cde"));
test(S("abcde"), 0, 4, S("e"));
test(S("abcde"), 0, 5, S(""));
test(S("abcde"), 0, 6, S(""));
test(S("abcde"), 1, 0, S("abcde"));
test(S("abcde"), 1, 1, S("acde"));
test(S("abcde"), 1, 2, S("ade"));
test(S("abcde"), 1, 3, S("ae"));
test(S("abcde"), 1, 4, S("a"));
test(S("abcde"), 1, 5, S("a"));
test(S("abcde"), 2, 0, S("abcde"));
test(S("abcde"), 2, 1, S("abde"));
test(S("abcde"), 2, 2, S("abe"));
test(S("abcde"), 2, 3, S("ab"));
test(S("abcde"), 2, 4, S("ab"));
test(S("abcde"), 4, 0, S("abcde"));
test(S("abcde"), 4, 1, S("abcd"));
test(S("abcde"), 4, 2, S("abcd"));
test(S("abcde"), 5, 0, S("abcde"));
test(S("abcde"), 5, 1, S("abcde"));
test(S("abcde"), 6, 0, S("can't happen"));
test(S("abcdefghij"), 0, 0, S("abcdefghij"));
test(S("abcdefghij"), 0, 1, S("bcdefghij"));
test(S("abcdefghij"), 0, 5, S("fghij"));
test(S("abcdefghij"), 0, 9, S("j"));
test(S("abcdefghij"), 0, 10, S(""));
test(S("abcdefghij"), 0, 11, S(""));
test(S("abcdefghij"), 1, 0, S("abcdefghij"));
test(S("abcdefghij"), 1, 1, S("acdefghij"));
test(S("abcdefghij"), 1, 4, S("afghij"));
test(S("abcdefghij"), 1, 8, S("aj"));
test(S("abcdefghij"), 1, 9, S("a"));
test(S("abcdefghij"), 1, 10, S("a"));
test(S("abcdefghij"), 5, 0, S("abcdefghij"));
test(S("abcdefghij"), 5, 1, S("abcdeghij"));
test(S("abcdefghij"), 5, 2, S("abcdehij"));
test(S("abcdefghij"), 5, 4, S("abcdej"));
test(S("abcdefghij"), 5, 5, S("abcde"));
test(S("abcdefghij"), 5, 6, S("abcde"));
test(S("abcdefghij"), 9, 0, S("abcdefghij"));
test(S("abcdefghij"), 9, 1, S("abcdefghi"));
test(S("abcdefghij"), 9, 2, S("abcdefghi"));
test(S("abcdefghij"), 10, 0, S("abcdefghij"));
test(S("abcdefghij"), 10, 1, S("abcdefghij"));
test(S("abcdefghij"), 11, 0, S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
test(S("abcdefghijklmnopqrst"), 0, 21, S(""));
test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
test(S("abcdefghijklmnopqrst"), 1, 20, S("a"));
test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 10, 11, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 19, 2, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, 1, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 21, 0, S("can't happen"));
test(S(""), 0, S(""));
test(S(""), 1, S("can't happen"));
test(S("abcde"), 0, S(""));
test(S("abcde"), 1, S("a"));
test(S("abcde"), 2, S("ab"));
test(S("abcde"), 4, S("abcd"));
test(S("abcde"), 5, S("abcde"));
test(S("abcde"), 6, S("can't happen"));
test(S("abcdefghij"), 0, S(""));
test(S("abcdefghij"), 1, S("a"));
test(S("abcdefghij"), 5, S("abcde"));
test(S("abcdefghij"), 9, S("abcdefghi"));
test(S("abcdefghij"), 10, S("abcdefghij"));
test(S("abcdefghij"), 11, S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, S(""));
test(S("abcdefghijklmnopqrst"), 1, S("a"));
test(S("abcdefghijklmnopqrst"), 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 21, S("can't happen"));
test(S(""), S(""));
test(S("abcde"), S(""));
test(S("abcdefghij"), S(""));
test(S("abcdefghijklmnopqrst"), S(""));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(""), 0, 0, S(""));
test(S(""), 0, 1, S(""));
test(S(""), 1, 0, S("can't happen"));
test(S("abcde"), 0, 0, S("abcde"));
test(S("abcde"), 0, 1, S("bcde"));
test(S("abcde"), 0, 2, S("cde"));
test(S("abcde"), 0, 4, S("e"));
test(S("abcde"), 0, 5, S(""));
test(S("abcde"), 0, 6, S(""));
test(S("abcde"), 1, 0, S("abcde"));
test(S("abcde"), 1, 1, S("acde"));
test(S("abcde"), 1, 2, S("ade"));
test(S("abcde"), 1, 3, S("ae"));
test(S("abcde"), 1, 4, S("a"));
test(S("abcde"), 1, 5, S("a"));
test(S("abcde"), 2, 0, S("abcde"));
test(S("abcde"), 2, 1, S("abde"));
test(S("abcde"), 2, 2, S("abe"));
test(S("abcde"), 2, 3, S("ab"));
test(S("abcde"), 2, 4, S("ab"));
test(S("abcde"), 4, 0, S("abcde"));
test(S("abcde"), 4, 1, S("abcd"));
test(S("abcde"), 4, 2, S("abcd"));
test(S("abcde"), 5, 0, S("abcde"));
test(S("abcde"), 5, 1, S("abcde"));
test(S("abcde"), 6, 0, S("can't happen"));
test(S("abcdefghij"), 0, 0, S("abcdefghij"));
test(S("abcdefghij"), 0, 1, S("bcdefghij"));
test(S("abcdefghij"), 0, 5, S("fghij"));
test(S("abcdefghij"), 0, 9, S("j"));
test(S("abcdefghij"), 0, 10, S(""));
test(S("abcdefghij"), 0, 11, S(""));
test(S("abcdefghij"), 1, 0, S("abcdefghij"));
test(S("abcdefghij"), 1, 1, S("acdefghij"));
test(S("abcdefghij"), 1, 4, S("afghij"));
test(S("abcdefghij"), 1, 8, S("aj"));
test(S("abcdefghij"), 1, 9, S("a"));
test(S("abcdefghij"), 1, 10, S("a"));
test(S("abcdefghij"), 5, 0, S("abcdefghij"));
test(S("abcdefghij"), 5, 1, S("abcdeghij"));
test(S("abcdefghij"), 5, 2, S("abcdehij"));
test(S("abcdefghij"), 5, 4, S("abcdej"));
test(S("abcdefghij"), 5, 5, S("abcde"));
test(S("abcdefghij"), 5, 6, S("abcde"));
test(S("abcdefghij"), 9, 0, S("abcdefghij"));
test(S("abcdefghij"), 9, 1, S("abcdefghi"));
test(S("abcdefghij"), 9, 2, S("abcdefghi"));
test(S("abcdefghij"), 10, 0, S("abcdefghij"));
test(S("abcdefghij"), 10, 1, S("abcdefghij"));
test(S("abcdefghij"), 11, 0, S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 1, S("bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, S("klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 19, S("t"));
test(S("abcdefghijklmnopqrst"), 0, 20, S(""));
test(S("abcdefghijklmnopqrst"), 0, 21, S(""));
test(S("abcdefghijklmnopqrst"), 1, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 1, S("acdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 9, S("aklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 18, S("at"));
test(S("abcdefghijklmnopqrst"), 1, 19, S("a"));
test(S("abcdefghijklmnopqrst"), 1, 20, S("a"));
test(S("abcdefghijklmnopqrst"), 10, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 1, S("abcdefghijlmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, S("abcdefghijpqrst"));
test(S("abcdefghijklmnopqrst"), 10, 9, S("abcdefghijt"));
test(S("abcdefghijklmnopqrst"), 10, 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 10, 11, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 1, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 19, 2, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, 0, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, 1, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 21, 0, S("can't happen"));
test(S(""), 0, S(""));
test(S(""), 1, S("can't happen"));
test(S("abcde"), 0, S(""));
test(S("abcde"), 1, S("a"));
test(S("abcde"), 2, S("ab"));
test(S("abcde"), 4, S("abcd"));
test(S("abcde"), 5, S("abcde"));
test(S("abcde"), 6, S("can't happen"));
test(S("abcdefghij"), 0, S(""));
test(S("abcdefghij"), 1, S("a"));
test(S("abcdefghij"), 5, S("abcde"));
test(S("abcdefghij"), 9, S("abcdefghi"));
test(S("abcdefghij"), 10, S("abcdefghij"));
test(S("abcdefghij"), 11, S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, S(""));
test(S("abcdefghijklmnopqrst"), 1, S("a"));
test(S("abcdefghijklmnopqrst"), 10, S("abcdefghij"));
test(S("abcdefghijklmnopqrst"), 19, S("abcdefghijklmnopqrs"));
test(S("abcdefghijklmnopqrst"), 20, S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 21, S("can't happen"));
test(S(""), S(""));
test(S("abcde"), S(""));
test(S("abcdefghij"), S(""));
test(S("abcdefghijklmnopqrst"), S(""));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,86 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator insert(const_iterator p, charT c); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S& s, typename S::const_iterator p, typename S::value_type c, S expected)
{
bool sufficient_cap = s.size() < s.capacity();
typename S::difference_type pos = p - s.begin();
typename S::iterator i = s.insert(p, c);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
assert(i - s.begin() == pos);
assert(*i == c);
if (sufficient_cap)
assert(i == p);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
S s;
test(s, s.begin(), '1', S("1"));
test(s, s.begin(), 'a', S("a1"));
test(s, s.end(), 'b', S("a1b"));
test(s, s.end()-1, 'c', S("a1cb"));
test(s, s.end()-2, 'd', S("a1dcb"));
test(s, s.end()-3, '2', S("a12dcb"));
test(s, s.end()-4, '3', S("a132dcb"));
test(s, s.end()-5, '4', S("a1432dcb"));
test(s, s.begin()+1, '5', S("a51432dcb"));
test(s, s.begin()+2, '6', S("a561432dcb"));
test(s, s.begin()+3, '7', S("a5671432dcb"));
test(s, s.begin()+4, 'A', S("a567A1432dcb"));
test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s;
test(s, s.begin(), '1', S("1"));
test(s, s.begin(), 'a', S("a1"));
test(s, s.end(), 'b', S("a1b"));
test(s, s.end()-1, 'c', S("a1cb"));
test(s, s.end()-2, 'd', S("a1dcb"));
test(s, s.end()-3, '2', S("a12dcb"));
test(s, s.end()-4, '3', S("a132dcb"));
test(s, s.end()-5, '4', S("a1432dcb"));
test(s, s.begin()+1, '5', S("a51432dcb"));
test(s, s.begin()+2, '6', S("a561432dcb"));
test(s, s.begin()+3, '7', S("a5671432dcb"));
test(s, s.begin()+4, 'A', S("a567A1432dcb"));
test(s, s.begin()+5, 'B', S("a567AB1432dcb"));
test(s, s.begin()+6, 'C', S("a567ABC1432dcb"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,48 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// UNSUPPORTED: c++03
// <string>
// iterator insert(const_iterator p, initializer_list<charT> il); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
TEST_CONSTEXPR_CXX20 bool test() {
{
std::string s("123456");
std::string::iterator i = s.insert(s.begin() + 3, {'a', 'b', 'c'});
assert(i - s.begin() == 3);
assert(s == "123abc456");
}
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
S s("123456");
S::iterator i = s.insert(s.begin() + 3, {'a', 'b', 'c'});
assert(i - s.begin() == 3);
assert(s == "123abc456");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,245 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// template<class InputIterator>
// iterator insert(const_iterator p, InputIterator first, InputIterator last); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
#include "min_allocator.h"
template <class S, class It>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::difference_type pos, It first, It last, S expected)
{
typename S::const_iterator p = s.cbegin() + pos;
typename S::iterator i = s.insert(p, first, last);
LIBCPP_ASSERT(s.__invariants());
assert(i - s.begin() == pos);
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
struct Widget { operator char() const { throw 42; } };
template <class S, class It>
TEST_CONSTEXPR_CXX20 void
test_exceptions(S s, typename S::difference_type pos, It first, It last)
{
typename S::const_iterator p = s.cbegin() + pos;
S original = s;
typename S::iterator begin = s.begin();
typename S::iterator end = s.end();
try {
s.insert(p, first, last);
assert(false);
} catch (...) {}
// Part of "no effects" is that iterators and pointers
// into the string must not have been invalidated.
LIBCPP_ASSERT(s.__invariants());
assert(s == original);
assert(s.begin() == begin);
assert(s.end() == end);
}
#endif
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), 0, s, s, S());
test(S(), 0, s, s+1, S("A"));
test(S(), 0, s, s+10, S("ABCDEFGHIJ"));
test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), 0, s, s, S("12345"));
test(S("12345"), 1, s, s+1, S("1A2345"));
test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5"));
test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), 0, s, s, S("1234567890"));
test(S("1234567890"), 1, s, s+1, S("1A234567890"));
test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
test(S("12345678901234567890"), 3, s, s, S("12345678901234567890"));
test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890"));
test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890"));
test(S("12345678901234567890"), 20, s, s+52,
S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("ABCDEFGHIJ"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("12345"));
test(S("12345"), 1, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("1A2345"));
test(S("12345"), 4, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5"));
test(S("12345"), 5, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("1234567890"));
test(S("1234567890"), 1, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("1A234567890"));
test(S("1234567890"), 10, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), 8, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
test(S("12345678901234567890"), 3, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("12345678901234567890"));
test(S("12345678901234567890"), 3, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("123A45678901234567890"));
test(S("12345678901234567890"), 15, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890"));
test(S("12345678901234567890"), 20, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test(S(), 0, s, s, S());
test(S(), 0, s, s+1, S("A"));
test(S(), 0, s, s+10, S("ABCDEFGHIJ"));
test(S(), 0, s, s+52, S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), 0, s, s, S("12345"));
test(S("12345"), 1, s, s+1, S("1A2345"));
test(S("12345"), 4, s, s+10, S("1234ABCDEFGHIJ5"));
test(S("12345"), 5, s, s+52, S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), 0, s, s, S("1234567890"));
test(S("1234567890"), 1, s, s+1, S("1A234567890"));
test(S("1234567890"), 10, s, s+10, S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), 8, s, s+52, S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
test(S("12345678901234567890"), 3, s, s, S("12345678901234567890"));
test(S("12345678901234567890"), 3, s, s+1, S("123A45678901234567890"));
test(S("12345678901234567890"), 15, s, s+10, S("123456789012345ABCDEFGHIJ67890"));
test(S("12345678901234567890"), 20, s, s+52,
S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S());
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("A"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("ABCDEFGHIJ"));
test(S(), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("12345"), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("12345"));
test(S("12345"), 1, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("1A2345"));
test(S("12345"), 4, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("1234ABCDEFGHIJ5"));
test(S("12345"), 5, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("12345ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
test(S("1234567890"), 0, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("1234567890"));
test(S("1234567890"), 1, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("1A234567890"));
test(S("1234567890"), 10, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("1234567890ABCDEFGHIJ"));
test(S("1234567890"), 8, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52), S("12345678ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz90"));
test(S("12345678901234567890"), 3, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s), S("12345678901234567890"));
test(S("12345678901234567890"), 3, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+1), S("123A45678901234567890"));
test(S("12345678901234567890"), 15, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+10), S("123456789012345ABCDEFGHIJ67890"));
test(S("12345678901234567890"), 20, cpp17_input_iterator<const char*>(s), cpp17_input_iterator<const char*>(s+52),
S("12345678901234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"));
}
#endif
#ifndef TEST_HAS_NO_EXCEPTIONS
if (!TEST_IS_CONSTANT_EVALUATED) { // test iterator operations that throw
typedef std::string S;
typedef ThrowingIterator<char> TIter;
typedef cpp17_input_iterator<TIter> IIter;
const char* s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
test_exceptions(S(), 0, IIter(TIter(s, s+10, 4, TIter::TAIncrement)), IIter(TIter()));
test_exceptions(S(), 0, IIter(TIter(s, s+10, 5, TIter::TADereference)), IIter(TIter()));
test_exceptions(S(), 0, IIter(TIter(s, s+10, 6, TIter::TAComparison)), IIter(TIter()));
test_exceptions(S(), 0, TIter(s, s+10, 4, TIter::TAIncrement), TIter());
test_exceptions(S(), 0, TIter(s, s+10, 5, TIter::TADereference), TIter());
test_exceptions(S(), 0, TIter(s, s+10, 6, TIter::TAComparison), TIter());
Widget w[100];
test_exceptions(S(), 0, w, w+100);
}
#endif
{ // test inserting into self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
assert(s_short == "123/123/");
s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
assert(s_short == "123/123/123/123/");
s_short.insert(s_short.begin(), s_short.begin(), s_short.end());
assert(s_short == "123/123/123/123/123/123/123/123/");
s_long.insert(s_long.begin(), s_long.begin(), s_long.end());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
}
{ // test assigning a different type
typedef std::string S;
const uint8_t p[] = "ABCD";
S s;
s.insert(s.begin(), p, p + 4);
assert(s == "ABCD");
}
if (!TEST_IS_CONSTANT_EVALUATED) { // regression-test inserting into self in sneaky ways
std::string s_short = "hello";
std::string s_long = "Lorem ipsum dolor sit amet, consectetur/";
std::string s_othertype = "hello";
const unsigned char *first = reinterpret_cast<const unsigned char*>(s_othertype.data());
test(s_short, 0, s_short.data() + s_short.size(), s_short.data() + s_short.size() + 1,
std::string("\0hello", 6));
test(s_long, 0, s_long.data() + s_long.size(), s_long.data() + s_long.size() + 1,
std::string("\0Lorem ipsum dolor sit amet, consectetur/", 41));
test(s_othertype, 1, first + 2, first + 5, std::string("hlloello"));
}
{ // test with a move iterator that returns char&&
typedef cpp17_input_iterator<const char*> It;
typedef std::move_iterator<It> MoveIt;
const char p[] = "ABCD";
std::string s;
s.insert(s.begin(), MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
assert(s == "ABCD");
}
{ // test with a move iterator that returns char&&
typedef forward_iterator<const char*> It;
typedef std::move_iterator<It> MoveIt;
const char p[] = "ABCD";
std::string s;
s.insert(s.begin(), MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
assert(s == "ABCD");
}
{ // test with a move iterator that returns char&&
typedef const char* It;
typedef std::move_iterator<It> MoveIt;
const char p[] = "ABCD";
std::string s;
s.insert(s.begin(), MoveIt(It(std::begin(p))), MoveIt(It(std::end(p) - 1)));
assert(s == "ABCD");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,180 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// iterator insert(const_iterator p, size_type n, charT c); // constexpr since C++20
#include <string>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::difference_type pos, typename S::size_type n,
typename S::value_type c, S expected)
{
typename S::const_iterator p = s.cbegin() + pos;
typename S::iterator i = s.insert(p, n, c);
LIBCPP_ASSERT(s.__invariants());
assert(i - s.begin() == pos);
assert(s == expected);
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(""), 0, 0, '1', S(""));
test(S(""), 0, 5, '1', S("11111"));
test(S(""), 0, 10, '1', S("1111111111"));
test(S(""), 0, 20, '1', S("11111111111111111111"));
test(S("abcde"), 0, 0, '1', S("abcde"));
test(S("abcde"), 0, 5, '1', S("11111abcde"));
test(S("abcde"), 0, 10, '1', S("1111111111abcde"));
test(S("abcde"), 0, 20, '1', S("11111111111111111111abcde"));
test(S("abcde"), 1, 0, '1', S("abcde"));
test(S("abcde"), 1, 5, '1', S("a11111bcde"));
test(S("abcde"), 1, 10, '1', S("a1111111111bcde"));
test(S("abcde"), 1, 20, '1', S("a11111111111111111111bcde"));
test(S("abcde"), 2, 0, '1', S("abcde"));
test(S("abcde"), 2, 5, '1', S("ab11111cde"));
test(S("abcde"), 2, 10, '1', S("ab1111111111cde"));
test(S("abcde"), 2, 20, '1', S("ab11111111111111111111cde"));
test(S("abcde"), 4, 0, '1', S("abcde"));
test(S("abcde"), 4, 5, '1', S("abcd11111e"));
test(S("abcde"), 4, 10, '1', S("abcd1111111111e"));
test(S("abcde"), 4, 20, '1', S("abcd11111111111111111111e"));
test(S("abcde"), 5, 0, '1', S("abcde"));
test(S("abcde"), 5, 5, '1', S("abcde11111"));
test(S("abcde"), 5, 10, '1', S("abcde1111111111"));
test(S("abcde"), 5, 20, '1', S("abcde11111111111111111111"));
test(S("abcdefghij"), 0, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 0, 5, '1', S("11111abcdefghij"));
test(S("abcdefghij"), 0, 10, '1', S("1111111111abcdefghij"));
test(S("abcdefghij"), 0, 20, '1', S("11111111111111111111abcdefghij"));
test(S("abcdefghij"), 1, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 1, 5, '1', S("a11111bcdefghij"));
test(S("abcdefghij"), 1, 10, '1', S("a1111111111bcdefghij"));
test(S("abcdefghij"), 1, 20, '1', S("a11111111111111111111bcdefghij"));
test(S("abcdefghij"), 5, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 5, 5, '1', S("abcde11111fghij"));
test(S("abcdefghij"), 5, 10, '1', S("abcde1111111111fghij"));
test(S("abcdefghij"), 5, 20, '1', S("abcde11111111111111111111fghij"));
test(S("abcdefghij"), 9, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 9, 5, '1', S("abcdefghi11111j"));
test(S("abcdefghij"), 9, 10, '1', S("abcdefghi1111111111j"));
test(S("abcdefghij"), 9, 20, '1', S("abcdefghi11111111111111111111j"));
test(S("abcdefghij"), 10, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 10, 5, '1', S("abcdefghij11111"));
test(S("abcdefghij"), 10, 10, '1', S("abcdefghij1111111111"));
test(S("abcdefghij"), 10, 20, '1', S("abcdefghij11111111111111111111"));
test(S("abcdefghijklmnopqrst"), 0, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 5, '1', S("11111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, '1', S("1111111111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 20, '1', S("11111111111111111111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 5, '1', S("a11111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 10, '1', S("a1111111111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 20, '1', S("a11111111111111111111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, '1', S("abcdefghij11111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 10, '1', S("abcdefghij1111111111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 20, '1', S("abcdefghij11111111111111111111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 5, '1', S("abcdefghijklmnopqrs11111t"));
test(S("abcdefghijklmnopqrst"), 19, 10, '1', S("abcdefghijklmnopqrs1111111111t"));
test(S("abcdefghijklmnopqrst"), 19, 20, '1', S("abcdefghijklmnopqrs11111111111111111111t"));
test(S("abcdefghijklmnopqrst"), 20, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, 5, '1', S("abcdefghijklmnopqrst11111"));
test(S("abcdefghijklmnopqrst"), 20, 10, '1', S("abcdefghijklmnopqrst1111111111"));
test(S("abcdefghijklmnopqrst"), 20, 20, '1', S("abcdefghijklmnopqrst11111111111111111111"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(""), 0, 0, '1', S(""));
test(S(""), 0, 5, '1', S("11111"));
test(S(""), 0, 10, '1', S("1111111111"));
test(S(""), 0, 20, '1', S("11111111111111111111"));
test(S("abcde"), 0, 0, '1', S("abcde"));
test(S("abcde"), 0, 5, '1', S("11111abcde"));
test(S("abcde"), 0, 10, '1', S("1111111111abcde"));
test(S("abcde"), 0, 20, '1', S("11111111111111111111abcde"));
test(S("abcde"), 1, 0, '1', S("abcde"));
test(S("abcde"), 1, 5, '1', S("a11111bcde"));
test(S("abcde"), 1, 10, '1', S("a1111111111bcde"));
test(S("abcde"), 1, 20, '1', S("a11111111111111111111bcde"));
test(S("abcde"), 2, 0, '1', S("abcde"));
test(S("abcde"), 2, 5, '1', S("ab11111cde"));
test(S("abcde"), 2, 10, '1', S("ab1111111111cde"));
test(S("abcde"), 2, 20, '1', S("ab11111111111111111111cde"));
test(S("abcde"), 4, 0, '1', S("abcde"));
test(S("abcde"), 4, 5, '1', S("abcd11111e"));
test(S("abcde"), 4, 10, '1', S("abcd1111111111e"));
test(S("abcde"), 4, 20, '1', S("abcd11111111111111111111e"));
test(S("abcde"), 5, 0, '1', S("abcde"));
test(S("abcde"), 5, 5, '1', S("abcde11111"));
test(S("abcde"), 5, 10, '1', S("abcde1111111111"));
test(S("abcde"), 5, 20, '1', S("abcde11111111111111111111"));
test(S("abcdefghij"), 0, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 0, 5, '1', S("11111abcdefghij"));
test(S("abcdefghij"), 0, 10, '1', S("1111111111abcdefghij"));
test(S("abcdefghij"), 0, 20, '1', S("11111111111111111111abcdefghij"));
test(S("abcdefghij"), 1, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 1, 5, '1', S("a11111bcdefghij"));
test(S("abcdefghij"), 1, 10, '1', S("a1111111111bcdefghij"));
test(S("abcdefghij"), 1, 20, '1', S("a11111111111111111111bcdefghij"));
test(S("abcdefghij"), 5, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 5, 5, '1', S("abcde11111fghij"));
test(S("abcdefghij"), 5, 10, '1', S("abcde1111111111fghij"));
test(S("abcdefghij"), 5, 20, '1', S("abcde11111111111111111111fghij"));
test(S("abcdefghij"), 9, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 9, 5, '1', S("abcdefghi11111j"));
test(S("abcdefghij"), 9, 10, '1', S("abcdefghi1111111111j"));
test(S("abcdefghij"), 9, 20, '1', S("abcdefghi11111111111111111111j"));
test(S("abcdefghij"), 10, 0, '1', S("abcdefghij"));
test(S("abcdefghij"), 10, 5, '1', S("abcdefghij11111"));
test(S("abcdefghij"), 10, 10, '1', S("abcdefghij1111111111"));
test(S("abcdefghij"), 10, 20, '1', S("abcdefghij11111111111111111111"));
test(S("abcdefghijklmnopqrst"), 0, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 5, '1', S("11111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 10, '1', S("1111111111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, 20, '1', S("11111111111111111111abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 5, '1', S("a11111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 10, '1', S("a1111111111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, 20, '1', S("a11111111111111111111bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 5, '1', S("abcdefghij11111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 10, '1', S("abcdefghij1111111111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, 20, '1', S("abcdefghij11111111111111111111klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, 5, '1', S("abcdefghijklmnopqrs11111t"));
test(S("abcdefghijklmnopqrst"), 19, 10, '1', S("abcdefghijklmnopqrs1111111111t"));
test(S("abcdefghijklmnopqrst"), 19, 20, '1', S("abcdefghijklmnopqrs11111111111111111111t"));
test(S("abcdefghijklmnopqrst"), 20, 0, '1', S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, 5, '1', S("abcdefghijklmnopqrst11111"));
test(S("abcdefghijklmnopqrst"), 20, 10, '1', S("abcdefghijklmnopqrst1111111111"));
test(S("abcdefghijklmnopqrst"), 20, 20, '1', S("abcdefghijklmnopqrst11111111111111111111"));
}
#endif
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}
@@ -0,0 +1,247 @@
//===----------------------------------------------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
// <string>
// basic_string<charT,traits,Allocator>&
// insert(size_type pos, const charT* s); // constexpr since C++20
#include <string>
#include <stdexcept>
#include <cassert>
#include "test_macros.h"
#include "min_allocator.h"
template <class S>
TEST_CONSTEXPR_CXX20 void
test(S s, typename S::size_type pos, const typename S::value_type* str, S expected)
{
const typename S::size_type old_size = s.size();
S s0 = s;
if (pos <= old_size)
{
s.insert(pos, str);
LIBCPP_ASSERT(s.__invariants());
assert(s == expected);
}
#ifndef TEST_HAS_NO_EXCEPTIONS
else if (!TEST_IS_CONSTANT_EVALUATED)
{
try
{
s.insert(pos, str);
assert(false);
}
catch (std::out_of_range&)
{
assert(pos > old_size);
assert(s == s0);
}
}
#endif
}
TEST_CONSTEXPR_CXX20 bool test() {
{
typedef std::string S;
test(S(""), 0, "", S(""));
test(S(""), 0, "12345", S("12345"));
test(S(""), 0, "1234567890", S("1234567890"));
test(S(""), 0, "12345678901234567890", S("12345678901234567890"));
test(S(""), 1, "", S("can't happen"));
test(S(""), 1, "12345", S("can't happen"));
test(S(""), 1, "1234567890", S("can't happen"));
test(S(""), 1, "12345678901234567890", S("can't happen"));
test(S("abcde"), 0, "", S("abcde"));
test(S("abcde"), 0, "12345", S("12345abcde"));
test(S("abcde"), 0, "1234567890", S("1234567890abcde"));
test(S("abcde"), 0, "12345678901234567890", S("12345678901234567890abcde"));
test(S("abcde"), 1, "", S("abcde"));
test(S("abcde"), 1, "12345", S("a12345bcde"));
test(S("abcde"), 1, "1234567890", S("a1234567890bcde"));
test(S("abcde"), 1, "12345678901234567890", S("a12345678901234567890bcde"));
test(S("abcde"), 2, "", S("abcde"));
test(S("abcde"), 2, "12345", S("ab12345cde"));
test(S("abcde"), 2, "1234567890", S("ab1234567890cde"));
test(S("abcde"), 2, "12345678901234567890", S("ab12345678901234567890cde"));
test(S("abcde"), 4, "", S("abcde"));
test(S("abcde"), 4, "12345", S("abcd12345e"));
test(S("abcde"), 4, "1234567890", S("abcd1234567890e"));
test(S("abcde"), 4, "12345678901234567890", S("abcd12345678901234567890e"));
test(S("abcde"), 5, "", S("abcde"));
test(S("abcde"), 5, "12345", S("abcde12345"));
test(S("abcde"), 5, "1234567890", S("abcde1234567890"));
test(S("abcde"), 5, "12345678901234567890", S("abcde12345678901234567890"));
test(S("abcde"), 6, "", S("can't happen"));
test(S("abcde"), 6, "12345", S("can't happen"));
test(S("abcde"), 6, "1234567890", S("can't happen"));
test(S("abcde"), 6, "12345678901234567890", S("can't happen"));
test(S("abcdefghij"), 0, "", S("abcdefghij"));
test(S("abcdefghij"), 0, "12345", S("12345abcdefghij"));
test(S("abcdefghij"), 0, "1234567890", S("1234567890abcdefghij"));
test(S("abcdefghij"), 0, "12345678901234567890", S("12345678901234567890abcdefghij"));
test(S("abcdefghij"), 1, "", S("abcdefghij"));
test(S("abcdefghij"), 1, "12345", S("a12345bcdefghij"));
test(S("abcdefghij"), 1, "1234567890", S("a1234567890bcdefghij"));
test(S("abcdefghij"), 1, "12345678901234567890", S("a12345678901234567890bcdefghij"));
test(S("abcdefghij"), 5, "", S("abcdefghij"));
test(S("abcdefghij"), 5, "12345", S("abcde12345fghij"));
test(S("abcdefghij"), 5, "1234567890", S("abcde1234567890fghij"));
test(S("abcdefghij"), 5, "12345678901234567890", S("abcde12345678901234567890fghij"));
test(S("abcdefghij"), 9, "", S("abcdefghij"));
test(S("abcdefghij"), 9, "12345", S("abcdefghi12345j"));
test(S("abcdefghij"), 9, "1234567890", S("abcdefghi1234567890j"));
test(S("abcdefghij"), 9, "12345678901234567890", S("abcdefghi12345678901234567890j"));
test(S("abcdefghij"), 10, "", S("abcdefghij"));
test(S("abcdefghij"), 10, "12345", S("abcdefghij12345"));
test(S("abcdefghij"), 10, "1234567890", S("abcdefghij1234567890"));
test(S("abcdefghij"), 10, "12345678901234567890", S("abcdefghij12345678901234567890"));
test(S("abcdefghij"), 11, "", S("can't happen"));
test(S("abcdefghij"), 11, "12345", S("can't happen"));
test(S("abcdefghij"), 11, "1234567890", S("can't happen"));
test(S("abcdefghij"), 11, "12345678901234567890", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "12345", S("12345abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "1234567890", S("1234567890abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "12345678901234567890", S("12345678901234567890abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "12345", S("a12345bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "1234567890", S("a1234567890bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "12345678901234567890", S("a12345678901234567890bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "12345", S("abcdefghij12345klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "1234567890", S("abcdefghij1234567890klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "12345678901234567890", S("abcdefghij12345678901234567890klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, "12345", S("abcdefghijklmnopqrs12345t"));
test(S("abcdefghijklmnopqrst"), 19, "1234567890", S("abcdefghijklmnopqrs1234567890t"));
test(S("abcdefghijklmnopqrst"), 19, "12345678901234567890", S("abcdefghijklmnopqrs12345678901234567890t"));
test(S("abcdefghijklmnopqrst"), 20, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, "12345", S("abcdefghijklmnopqrst12345"));
test(S("abcdefghijklmnopqrst"), 20, "1234567890", S("abcdefghijklmnopqrst1234567890"));
test(S("abcdefghijklmnopqrst"), 20, "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
test(S("abcdefghijklmnopqrst"), 21, "", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "12345", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "1234567890", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "12345678901234567890", S("can't happen"));
}
#if TEST_STD_VER >= 11
{
typedef std::basic_string<char, std::char_traits<char>, min_allocator<char>> S;
test(S(""), 0, "", S(""));
test(S(""), 0, "12345", S("12345"));
test(S(""), 0, "1234567890", S("1234567890"));
test(S(""), 0, "12345678901234567890", S("12345678901234567890"));
test(S(""), 1, "", S("can't happen"));
test(S(""), 1, "12345", S("can't happen"));
test(S(""), 1, "1234567890", S("can't happen"));
test(S(""), 1, "12345678901234567890", S("can't happen"));
test(S("abcde"), 0, "", S("abcde"));
test(S("abcde"), 0, "12345", S("12345abcde"));
test(S("abcde"), 0, "1234567890", S("1234567890abcde"));
test(S("abcde"), 0, "12345678901234567890", S("12345678901234567890abcde"));
test(S("abcde"), 1, "", S("abcde"));
test(S("abcde"), 1, "12345", S("a12345bcde"));
test(S("abcde"), 1, "1234567890", S("a1234567890bcde"));
test(S("abcde"), 1, "12345678901234567890", S("a12345678901234567890bcde"));
test(S("abcde"), 2, "", S("abcde"));
test(S("abcde"), 2, "12345", S("ab12345cde"));
test(S("abcde"), 2, "1234567890", S("ab1234567890cde"));
test(S("abcde"), 2, "12345678901234567890", S("ab12345678901234567890cde"));
test(S("abcde"), 4, "", S("abcde"));
test(S("abcde"), 4, "12345", S("abcd12345e"));
test(S("abcde"), 4, "1234567890", S("abcd1234567890e"));
test(S("abcde"), 4, "12345678901234567890", S("abcd12345678901234567890e"));
test(S("abcde"), 5, "", S("abcde"));
test(S("abcde"), 5, "12345", S("abcde12345"));
test(S("abcde"), 5, "1234567890", S("abcde1234567890"));
test(S("abcde"), 5, "12345678901234567890", S("abcde12345678901234567890"));
test(S("abcde"), 6, "", S("can't happen"));
test(S("abcde"), 6, "12345", S("can't happen"));
test(S("abcde"), 6, "1234567890", S("can't happen"));
test(S("abcde"), 6, "12345678901234567890", S("can't happen"));
test(S("abcdefghij"), 0, "", S("abcdefghij"));
test(S("abcdefghij"), 0, "12345", S("12345abcdefghij"));
test(S("abcdefghij"), 0, "1234567890", S("1234567890abcdefghij"));
test(S("abcdefghij"), 0, "12345678901234567890", S("12345678901234567890abcdefghij"));
test(S("abcdefghij"), 1, "", S("abcdefghij"));
test(S("abcdefghij"), 1, "12345", S("a12345bcdefghij"));
test(S("abcdefghij"), 1, "1234567890", S("a1234567890bcdefghij"));
test(S("abcdefghij"), 1, "12345678901234567890", S("a12345678901234567890bcdefghij"));
test(S("abcdefghij"), 5, "", S("abcdefghij"));
test(S("abcdefghij"), 5, "12345", S("abcde12345fghij"));
test(S("abcdefghij"), 5, "1234567890", S("abcde1234567890fghij"));
test(S("abcdefghij"), 5, "12345678901234567890", S("abcde12345678901234567890fghij"));
test(S("abcdefghij"), 9, "", S("abcdefghij"));
test(S("abcdefghij"), 9, "12345", S("abcdefghi12345j"));
test(S("abcdefghij"), 9, "1234567890", S("abcdefghi1234567890j"));
test(S("abcdefghij"), 9, "12345678901234567890", S("abcdefghi12345678901234567890j"));
test(S("abcdefghij"), 10, "", S("abcdefghij"));
test(S("abcdefghij"), 10, "12345", S("abcdefghij12345"));
test(S("abcdefghij"), 10, "1234567890", S("abcdefghij1234567890"));
test(S("abcdefghij"), 10, "12345678901234567890", S("abcdefghij12345678901234567890"));
test(S("abcdefghij"), 11, "", S("can't happen"));
test(S("abcdefghij"), 11, "12345", S("can't happen"));
test(S("abcdefghij"), 11, "1234567890", S("can't happen"));
test(S("abcdefghij"), 11, "12345678901234567890", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 0, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "12345", S("12345abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "1234567890", S("1234567890abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 0, "12345678901234567890", S("12345678901234567890abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "12345", S("a12345bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "1234567890", S("a1234567890bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 1, "12345678901234567890", S("a12345678901234567890bcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "12345", S("abcdefghij12345klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "1234567890", S("abcdefghij1234567890klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 10, "12345678901234567890", S("abcdefghij12345678901234567890klmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 19, "12345", S("abcdefghijklmnopqrs12345t"));
test(S("abcdefghijklmnopqrst"), 19, "1234567890", S("abcdefghijklmnopqrs1234567890t"));
test(S("abcdefghijklmnopqrst"), 19, "12345678901234567890", S("abcdefghijklmnopqrs12345678901234567890t"));
test(S("abcdefghijklmnopqrst"), 20, "", S("abcdefghijklmnopqrst"));
test(S("abcdefghijklmnopqrst"), 20, "12345", S("abcdefghijklmnopqrst12345"));
test(S("abcdefghijklmnopqrst"), 20, "1234567890", S("abcdefghijklmnopqrst1234567890"));
test(S("abcdefghijklmnopqrst"), 20, "12345678901234567890", S("abcdefghijklmnopqrst12345678901234567890"));
test(S("abcdefghijklmnopqrst"), 21, "", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "12345", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "1234567890", S("can't happen"));
test(S("abcdefghijklmnopqrst"), 21, "12345678901234567890", S("can't happen"));
}
#endif
{ // test inserting into self
typedef std::string S;
S s_short = "123/";
S s_long = "Lorem ipsum dolor sit amet, consectetur/";
s_short.insert(0, s_short.c_str());
assert(s_short == "123/123/");
s_short.insert(0, s_short.c_str());
assert(s_short == "123/123/123/123/");
s_short.insert(0, s_short.c_str());
assert(s_short == "123/123/123/123/123/123/123/123/");
s_long.insert(0, s_long.c_str());
assert(s_long == "Lorem ipsum dolor sit amet, consectetur/Lorem ipsum dolor sit amet, consectetur/");
}
return true;
}
int main(int, char**)
{
test();
#if TEST_STD_VER > 17
static_assert(test());
#endif
return 0;
}

Some files were not shown because too many files have changed in this diff Show More