diff options
Diffstat (limited to 'test/std/strings/basic.string/string.modifiers/string_assign')
6 files changed, 382 insertions, 9 deletions
diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/T_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/T_size_size.pass.cpp new file mode 100644 index 0000000000000..bf51d816e86ee --- /dev/null +++ b/test/std/strings/basic.string/string.modifiers/string_assign/T_size_size.pass.cpp @@ -0,0 +1,195 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// template <class T> +// basic_string& assign(const T& t, size_type pos, size_type n=npos); // C++17 + +#include <string> +#include <stdexcept> +#include <cassert> + +#include "test_macros.h" +#include "min_allocator.h" + +template <class S, class SV> +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 + { + try + { + s.assign(sv, pos, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > sv.size()); + } + } +#endif +} + +template <class S, class SV> +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 + { + try + { + s.assign(sv, pos); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > sv.size()); + } + } +#endif +} + +int main() +{ + { + 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"); + } +} diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp index e6a57519f34be..e5312e6db6ce3 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/iterator.pass.cpp @@ -179,4 +179,30 @@ int main() test_exceptions(S(), TIter(s, s+10, 6, TIter::TAComparison), TIter()); } #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"); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp index 386dee6d58087..b0876b388d833 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/pointer.pass.cpp @@ -61,4 +61,18 @@ int main() 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/"); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp index 0eeb9266f40fa..97a9990f5180f 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/pointer_size.pass.cpp @@ -70,4 +70,20 @@ int main() 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"); + } } diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp index d9d3cb422ae50..2ad37f311de55 100644 --- a/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp +++ b/test/std/strings/basic.string/string.modifiers/string_assign/string_size_size.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // <string> // basic_string<charT,traits,Allocator>& @@ -25,34 +24,52 @@ template <class S> void test(S s, S str, typename S::size_type pos, typename S::size_type n, S expected) { - try + if (pos <= str.size()) { s.assign(str, pos, n); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= str.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > str.size()); + try + { + s.assign(str, pos, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > str.size()); + } } +#endif } template <class S> void test_npos(S s, S str, typename S::size_type pos, S expected) { - try + if (pos <= str.size()) { s.assign(str, pos); LIBCPP_ASSERT(s.__invariants()); - assert(pos <= str.size()); assert(s == expected); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > str.size()); + try + { + s.assign(str, pos); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > str.size()); + } } +#endif } int main() diff --git a/test/std/strings/basic.string/string.modifiers/string_assign/string_view.pass.cpp b/test/std/strings/basic.string/string.modifiers/string_assign/string_view.pass.cpp new file mode 100644 index 0000000000000..e56b094a36e96 --- /dev/null +++ b/test/std/strings/basic.string/string.modifiers/string_assign/string_view.pass.cpp @@ -0,0 +1,105 @@ +//===----------------------------------------------------------------------===// +// +// The LLVM Compiler Infrastructure +// +// This file is dual licensed under the MIT and the University of Illinois Open +// Source Licenses. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// + +// <string> + +// basic_string<charT,traits,Allocator>& +// assign(basic_string_view<charT,traits> sv); + +#include <string> +#include <string_view> +#include <cassert> + +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +template <class S, class SV> +void +test(S s, SV sv, S expected) +{ + s.assign(sv); + LIBCPP_ASSERT(s.__invariants()); + assert(s == expected); +} + +template <class S, class SV> +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); +} + +int main() +{ + { + 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 +} |
