diff options
author | Marshall Clow <mclow.lists@gmail.com> | 2016-07-21 05:31:24 +0000 |
---|---|---|
committer | Marshall Clow <mclow.lists@gmail.com> | 2016-07-21 05:31:24 +0000 |
commit | 053d81ceebe9a54255c49801b88ab61b5d03060a (patch) | |
tree | ea1a55b676d67b77caa7e980a2cb8ccb5ab81a69 /libcxx/test/std/strings/string.view/string.view.cons | |
parent | ed7b7d7efe9e67561f7f90ebcd65fee1e24f113f (diff) | |
download | bcm5719-llvm-053d81ceebe9a54255c49801b88ab61b5d03060a.tar.gz bcm5719-llvm-053d81ceebe9a54255c49801b88ab61b5d03060a.zip |
Implement std::string_view as described in http://wg21.link/P0254R1. Reviewed as https://reviews.llvm.org/D21459
llvm-svn: 276238
Diffstat (limited to 'libcxx/test/std/strings/string.view/string.view.cons')
6 files changed, 313 insertions, 0 deletions
diff --git a/libcxx/test/std/strings/string.view/string.view.cons/default.pass.cpp b/libcxx/test/std/strings/string.view/string.view.cons/default.pass.cpp new file mode 100644 index 00000000000..54a0690e48e --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/default.pass.cpp @@ -0,0 +1,46 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// constexpr basic_string_view () noexcept; + +#include <string_view> +#include <cassert> + +template<typename T> +void test () { +#if _LIBCPP_STD_VER > 11 + { + constexpr T sv1; + static_assert ( sv1.size() == 0, "" ); + static_assert ( sv1.empty(), ""); + } +#endif + + { + T sv1; + assert ( sv1.size() == 0 ); + assert ( sv1.empty()); + } +} + +int main () { + typedef std::string_view string_view; + typedef std::u16string_view u16string_view; + typedef std::u32string_view u32string_view; + typedef std::wstring_view wstring_view; + + test<string_view> (); + test<u16string_view> (); + test<u32string_view> (); + test<wstring_view> (); + +} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_literal.pass.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_literal.pass.cpp new file mode 100644 index 00000000000..07427b1b079 --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_literal.pass.cpp @@ -0,0 +1,64 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// constexpr basic_string_view(const _CharT* _s) +// : __data (_s), __size(_Traits::length(_s)) {} + + +#include <string_view> +#include <string> +#include <cassert> + +#include "constexpr_char_traits.hpp" + +template<typename CharT> +size_t StrLen ( const CharT *s ) { + size_t retVal = 0; + while ( *s != 0 ) { ++retVal; ++s; } + return retVal; + } + +template<typename CharT> +void test ( const CharT *s ) { + std::basic_string_view<CharT> sv1 ( s ); + assert ( sv1.size() == StrLen( s )); + assert ( sv1.data() == s ); + } + + +int main () { + + test ( "QBCDE" ); + test ( "A" ); + test ( "" ); + + test ( L"QBCDE" ); + test ( L"A" ); + test ( L"" ); + +#if TEST_STD_VER >= 11 + test ( u"QBCDE" ); + test ( u"A" ); + test ( u"" ); + + test ( U"QBCDE" ); + test ( U"A" ); + test ( U"" ); +#endif + +#if _LIBCPP_STD_VER > 11 + { + constexpr std::basic_string_view<char, constexpr_char_traits<char>> sv1 ( "ABCDE" ); + static_assert ( sv1.size() == 5, ""); + } +#endif +} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_ptr_len.pass.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_ptr_len.pass.cpp new file mode 100644 index 00000000000..b96bd4c72d1 --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_ptr_len.pass.cpp @@ -0,0 +1,83 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// constexpr basic_string_view(const _CharT* _s, size_type _len) +// : __data (_s), __size(_len) {} + + +#include <string_view> +#include <string> +#include <cassert> + +#include "test_macros.h" + +template<typename CharT> +void test ( const CharT *s, size_t sz ) { + { + std::basic_string_view<CharT> sv1 ( s, sz ); + assert ( sv1.size() == sz ); + assert ( sv1.data() == s ); + } +} + +int main () { + + test ( "QBCDE", 5 ); + test ( "QBCDE", 2 ); + test ( "", 0 ); +#if _LIBCPP_STD_VER > 11 + { + constexpr const char *s = "QBCDE"; + constexpr std::basic_string_view<char> sv1 ( s, 2 ); + static_assert ( sv1.size() == 2, "" ); + static_assert ( sv1.data() == s, "" ); + } +#endif + + test ( L"QBCDE", 5 ); + test ( L"QBCDE", 2 ); + test ( L"", 0 ); +#if _LIBCPP_STD_VER > 11 + { + constexpr const wchar_t *s = L"QBCDE"; + constexpr std::basic_string_view<wchar_t> sv1 ( s, 2 ); + static_assert ( sv1.size() == 2, "" ); + static_assert ( sv1.data() == s, "" ); + } +#endif + +#if TEST_STD_VER >= 11 + test ( u"QBCDE", 5 ); + test ( u"QBCDE", 2 ); + test ( u"", 0 ); +#if _LIBCPP_STD_VER > 11 + { + constexpr const char16_t *s = u"QBCDE"; + constexpr std::basic_string_view<char16_t> sv1 ( s, 2 ); + static_assert ( sv1.size() == 2, "" ); + static_assert ( sv1.data() == s, "" ); + } +#endif + + test ( U"QBCDE", 5 ); + test ( U"QBCDE", 2 ); + test ( U"", 0 ); +#if _LIBCPP_STD_VER > 11 + { + constexpr const char32_t *s = U"QBCDE"; + constexpr std::basic_string_view<char32_t> sv1 ( s, 2 ); + static_assert ( sv1.size() == 2, "" ); + static_assert ( sv1.data() == s, "" ); + } +#endif +#endif +} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_string.pass.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_string.pass.cpp new file mode 100644 index 00000000000..3ca6ea16f5b --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_string.pass.cpp @@ -0,0 +1,56 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// template<class Allocator> +// basic_string_view(const basic_string<_CharT, _Traits, Allocator>& _str) noexcept + + +#include <string_view> +#include <string> +#include <cassert> + +#include "test_macros.h" + +struct dummy_char_traits : public std::char_traits<char> {}; + +template<typename CharT, typename Traits> +void test ( const std::basic_string<CharT, Traits> &str ) { + std::basic_string_view<CharT, Traits> sv1 ( str ); + assert ( sv1.size() == str.size()); + assert ( sv1.data() == str.data()); +} + +int main () { + + test ( std::string("QBCDE") ); + test ( std::string("") ); + test ( std::string() ); + + test ( std::wstring(L"QBCDE") ); + test ( std::wstring(L"") ); + test ( std::wstring() ); + +#if TEST_STD_VER >= 11 + test ( std::u16string{u"QBCDE"} ); + test ( std::u16string{u""} ); + test ( std::u16string{} ); + + test ( std::u32string{U"QBCDE"} ); + test ( std::u32string{U""} ); + test ( std::u32string{} ); +#endif + + test ( std::basic_string<char, dummy_char_traits>("QBCDE") ); + test ( std::basic_string<char, dummy_char_traits>("") ); + test ( std::basic_string<char, dummy_char_traits>() ); + +} diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_string1.fail.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_string1.fail.cpp new file mode 100644 index 00000000000..0f33dd87bca --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_string1.fail.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// template<class Allocator> +// basic_string_view(const basic_string<_CharT, _Traits, Allocator>& _str) noexcept + +#include <string_view> +#include <string> +#include <cassert> + +struct dummy_char_traits : public std::char_traits<char> {}; + +int main () { + using string_view = std::basic_string_view<char>; + using string = std:: basic_string <char, dummy_char_traits>; + + { + string s{"QBCDE"}; + string_view sv1 ( s ); + assert ( sv1.size() == s.size()); + assert ( sv1.data() == s.data()); + } +}
\ No newline at end of file diff --git a/libcxx/test/std/strings/string.view/string.view.cons/from_string2.fail.cpp b/libcxx/test/std/strings/string.view/string.view.cons/from_string2.fail.cpp new file mode 100644 index 00000000000..6c9ac6436ef --- /dev/null +++ b/libcxx/test/std/strings/string.view/string.view.cons/from_string2.fail.cpp @@ -0,0 +1,32 @@ +//===----------------------------------------------------------------------===// +// +// 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_view> + +// template<class Allocator> +// basic_string_view(const basic_string<_CharT, _Traits, Allocator>& _str) noexcept + +#include <string_view> +#include <string> +#include <cassert> + +struct dummy_char_traits : public std::char_traits<char> {}; + +int main () { + using string_view = std::basic_string_view<char, dummy_char_traits>; + using string = std:: basic_string <char>; + + { + string s{"QBCDE"}; + string_view sv1 ( s ); + assert ( sv1.size() == s.size()); + assert ( sv1.data() == s.data()); + } +} |