diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2017-01-02 19:18:58 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2017-01-02 19:18:58 +0000 |
commit | 53a420fba21cf1644972b34dcd811a43cdb8368d (patch) | |
tree | 66a19f6f8b65215772549a51d688492ab8addc0d /test/std/strings/basic.string/string.cons | |
parent | b50f1549701eb950921e5d6f2e55ba1a1dadbb43 (diff) |
Notes
Diffstat (limited to 'test/std/strings/basic.string/string.cons')
8 files changed, 368 insertions, 29 deletions
diff --git a/test/std/strings/basic.string/string.cons/T_size_size.pass.cpp b/test/std/strings/basic.string/string.cons/T_size_size.pass.cpp new file mode 100644 index 000000000000..312e4d27fef1 --- /dev/null +++ b/test/std/strings/basic.string/string.cons/T_size_size.pass.cpp @@ -0,0 +1,186 @@ +//===----------------------------------------------------------------------===// +// +// 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 _Tp> +// basic_string(const _Tp& __t, size_type __pos, size_type __n, +// const allocator_type& __a = allocator_type()); +// +// 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> +void +test(SV sv, unsigned pos, unsigned n) +{ + typedef typename S::traits_type T; + typedef typename S::allocator_type A; + if (pos <= sv.size()) + { + S s2(sv, pos, n); + LIBCPP_ASSERT(s2.__invariants()); + assert(pos <= sv.size()); + unsigned rlen = std::min<unsigned>(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 + { + try + { + S s2(sv, pos, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > sv.size()); + } + } +#endif +} + +template <class S, class SV> +void +test(SV sv, unsigned pos, unsigned n, const typename S::allocator_type& a) +{ + typedef typename S::traits_type T; + typedef typename S::allocator_type A; + if (pos <= sv.size()) + { + S s2(sv, pos, n, a); + LIBCPP_ASSERT(s2.__invariants()); + assert(pos <= sv.size()); + unsigned rlen = std::min<unsigned>(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 + { + try + { + S s2(sv, pos, n, a); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > sv.size()); + } + } +#endif +} + +int main() +{ + + { + 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"); + } +} diff --git a/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp b/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp index c4ac1f1a47bb..0c6362d96863 100644 --- a/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp +++ b/test/std/strings/basic.string/string.cons/dtor_noexcept.pass.cpp @@ -16,6 +16,7 @@ #include <string> #include <cassert> +#include "test_macros.h" #include "test_allocator.h" template <class T> @@ -26,6 +27,11 @@ struct some_alloc ~some_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; +std::wstring ws; + int main() { { @@ -38,6 +44,6 @@ int main() } { typedef std::basic_string<char, std::char_traits<char>, some_alloc<char>> C; - static_assert(!std::is_nothrow_destructible<C>::value, ""); + LIBCPP_STATIC_ASSERT(!std::is_nothrow_destructible<C>::value, ""); } } diff --git a/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp index cb0792ab23b3..1b10224cd9e7 100644 --- a/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/iter_alloc.pass.cpp @@ -16,6 +16,7 @@ #include <string> #include <iterator> #include <cassert> +#include <cstddef> #include "test_macros.h" #include "test_allocator.h" @@ -32,7 +33,7 @@ test(It first, It last) typedef typename S::allocator_type A; S s2(first, last); LIBCPP_ASSERT(s2.__invariants()); - assert(s2.size() == std::distance(first, last)); + assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); unsigned i = 0; for (It it = first; it != last; ++it, ++i) assert(s2[i] == *it); @@ -49,7 +50,7 @@ test(It first, It last, const A& a) typedef typename S::traits_type T; S s2(first, last, a); LIBCPP_ASSERT(s2.__invariants()); - assert(s2.size() == std::distance(first, last)); + assert(s2.size() == static_cast<std::size_t>(std::distance(first, last))); unsigned i = 0; for (It it = first; it != last; ++it, ++i) assert(s2[i] == *it); diff --git a/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp index b678247fb241..f56780095b7f 100644 --- a/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/pointer_alloc.pass.cpp @@ -15,6 +15,7 @@ #include <stdexcept> #include <algorithm> #include <cassert> +#include <cstddef> #include "test_macros.h" #include "test_allocator.h" @@ -27,7 +28,7 @@ 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; - unsigned n = T::length(s); + std::size_t n = T::length(s); S s2(s); LIBCPP_ASSERT(s2.__invariants()); assert(s2.size() == n); @@ -42,7 +43,7 @@ test(const charT* s, const A& a) { typedef std::basic_string<charT, std::char_traits<charT>, A> S; typedef typename S::traits_type T; - unsigned n = T::length(s); + std::size_t n = T::length(s); S s2(s, a); LIBCPP_ASSERT(s2.__invariants()); assert(s2.size() == n); diff --git a/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp b/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp index 60d41b1d35be..60443e9f358d 100644 --- a/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp +++ b/test/std/strings/basic.string/string.cons/size_char_alloc.pass.cpp @@ -15,6 +15,7 @@ #include <stdexcept> #include <algorithm> #include <cassert> +#include <cstddef> #include "test_macros.h" #include "test_allocator.h" @@ -61,8 +62,8 @@ test(Tp n, Tp c) 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.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()); @@ -77,8 +78,8 @@ test(Tp n, Tp c, const A& a) typedef typename S::traits_type T; S s2(n, c, a); LIBCPP_ASSERT(s2.__invariants()); - assert(s2.size() == n); - for (unsigned i = 0; i < n; ++i) + 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()); @@ -102,8 +103,8 @@ int main() test(100, 'a'); test(100, 'a', A(2)); - test(100, 65); - test(100, 65, A(3)); + 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 { @@ -122,8 +123,8 @@ int main() test(100, 'a'); test(100, 'a', A()); - test(100, 65); - test(100, 65, A()); + test(static_cast<char>(100), static_cast<char>(65)); + test(static_cast<char>(100), static_cast<char>(65), A()); } #endif } diff --git a/test/std/strings/basic.string/string.cons/string_view.fail.cpp b/test/std/strings/basic.string/string.cons/string_view.fail.cpp new file mode 100644 index 000000000000..70459b2dcfb4 --- /dev/null +++ b/test/std/strings/basic.string/string.cons/string_view.fail.cpp @@ -0,0 +1,23 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); + +#include <string> +#include <string_view> + +void foo ( const string &s ) {} + +int main() +{ + std::string_view sv = "ABCDE"; + foo(sv); // requires implicit conversion from string_view to string +} diff --git a/test/std/strings/basic.string/string.cons/string_view.pass.cpp b/test/std/strings/basic.string/string.cons/string_view.pass.cpp new file mode 100644 index 000000000000..d0e1a1699a22 --- /dev/null +++ b/test/std/strings/basic.string/string.cons/string_view.pass.cpp @@ -0,0 +1,89 @@ +//===----------------------------------------------------------------------===// +// +// 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> + +// explicit basic_string(basic_string_view<CharT, traits> sv, const Allocator& a = Allocator()); + +#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 charT> +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()); +} + +template <class charT, class A> +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()); +} + +int main() +{ + { + 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 +} diff --git a/test/std/strings/basic.string/string.cons/substr.pass.cpp b/test/std/strings/basic.string/string.cons/substr.pass.cpp index a10239bb3414..4fd974273de9 100644 --- a/test/std/strings/basic.string/string.cons/substr.pass.cpp +++ b/test/std/strings/basic.string/string.cons/substr.pass.cpp @@ -7,7 +7,6 @@ // //===----------------------------------------------------------------------===// -// XFAIL: libcpp-no-exceptions // <string> // basic_string(const basic_string<charT,traits,Allocator>& str, @@ -35,21 +34,31 @@ test(S str, unsigned pos) { typedef typename S::traits_type T; typedef typename S::allocator_type A; - try + + if (pos <= str.size()) { S s2(str, pos); LIBCPP_ASSERT(s2.__invariants()); - assert(pos <= str.size()); - unsigned rlen = str.size() - pos; + 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()); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > str.size()); + try + { + S s2(str, pos); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > str.size()); + } } +#endif } template <class S> @@ -58,21 +67,30 @@ test(S str, unsigned pos, unsigned n) { typedef typename S::traits_type T; typedef typename S::allocator_type A; - try + if (pos <= str.size()) { S s2(str, pos, n); LIBCPP_ASSERT(s2.__invariants()); - assert(pos <= str.size()); - unsigned rlen = std::min<unsigned>(str.size() - pos, n); + 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()); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > str.size()); + try + { + S s2(str, pos, n); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > str.size()); + } } +#endif } template <class S> @@ -81,24 +99,35 @@ test(S str, unsigned pos, unsigned n, const typename S::allocator_type& a) { typedef typename S::traits_type T; typedef typename S::allocator_type A; - try + + if (pos <= str.size()) { S s2(str, pos, n, a); LIBCPP_ASSERT(s2.__invariants()); - assert(pos <= str.size()); - unsigned rlen = std::min<unsigned>(str.size() - pos, n); + 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()); } - catch (std::out_of_range&) +#ifndef TEST_HAS_NO_EXCEPTIONS + else { - assert(pos > str.size()); + try + { + S s2(str, pos, n, a); + assert(false); + } + catch (std::out_of_range&) + { + assert(pos > str.size()); + } } +#endif } #if TEST_STD_VER >= 11 +#ifndef TEST_HAS_NO_EXCEPTIONS void test2583() { // LWG #2583 typedef std::basic_string<char, std::char_traits<char>, test_allocator<char> > StringA; @@ -111,6 +140,7 @@ void test2583() assert(false); } #endif +#endif int main() { @@ -192,6 +222,8 @@ int main() test(S("1234567890123456789012345678901234567890123456789012345678901234567890", A()), 50, 100, A()); } +#ifndef TEST_HAS_NO_EXCEPTIONS test2583(); #endif +#endif } |