From 3d853b0e83e4af8b7af8fac1fde0229469733305 Mon Sep 17 00:00:00 2001 From: Andrey Semashev Date: Thu, 1 Sep 2016 22:42:29 +0300 Subject: [PATCH] Reworked to_string test to verify the overload with a custom allocator even when explicit conversion operators are not supported. Made the custom allocator more standard-compliant. --- test/string_view_test2.cpp | 117 ++++++++++++++++++++++++++++--------- 1 file changed, 88 insertions(+), 29 deletions(-) diff --git a/test/string_view_test2.cpp b/test/string_view_test2.cpp index 971dee9..b9675d2 100644 --- a/test/string_view_test2.cpp +++ b/test/string_view_test2.cpp @@ -7,13 +7,14 @@ For more information, see http://www.boost.org */ +#include // for placement new #include -#include // for std::strchr -#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS +#include // for NULL, std::size_t, std::ptrdiff_t +#include // for std::strchr and std::strcmp #include // for std::malloc and std::free -#endif #include +#include #define BOOST_TEST_MAIN #include @@ -86,7 +87,7 @@ void reverse ( const char *arg ) { BOOST_CHECK ( std::equal ( sr1.begin (), sr1.end (), string2.begin ())); } -// This helper function eliminates signed vs. unsigned warnings +// This helper function eliminates signed vs. unsigned warnings string_view::size_type ptr_diff ( const char *res, const char *base ) { BOOST_CHECK ( res >= base ); return static_cast ( res - base ); @@ -115,7 +116,7 @@ void find ( const char *arg ) { ++p; } -// Look for pairs on characters (searching from the start) +// Look for pairs on characters (searching from the start) sr1 = arg; p = arg; while ( *p && *(p+1)) { @@ -244,29 +245,86 @@ void find ( const char *arg ) { } -#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS template -class alloc_holder{ +class custom_allocator { public: typedef T value_type; - explicit alloc_holder(){} - T *allocate(std::size_t n){ - return reinterpret_cast(std::malloc(sizeof(T) * n)); - } - void deallocate(T *p, std::size_t n){ + typedef T* pointer; + typedef const T* const_pointer; + typedef void* void_pointer; + typedef const void* const_void_pointer; + typedef std::size_t size_type; + typedef std::ptrdiff_t difference_type; + typedef T& reference; + typedef const T& const_reference; + + template + struct rebind { + typedef custom_allocator other; + }; + + custom_allocator() BOOST_NOEXCEPT {} + template + custom_allocator(custom_allocator const&) BOOST_NOEXCEPT {} + + pointer allocate(size_type n) const { + return static_cast(std::malloc(sizeof(value_type) * n)); + } + void deallocate(pointer p, size_type) const BOOST_NOEXCEPT { std::free(p); - } -}; + } + + pointer address(reference value) const BOOST_NOEXCEPT { + return &value; + } + + const_pointer address(const_reference value) const BOOST_NOEXCEPT { + return &value; + } + + BOOST_CONSTEXPR size_type max_size() const BOOST_NOEXCEPT { + return (~(size_type)0u) / sizeof(value_type); + } + +#if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) +#if !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) + template + void construct(U* ptr, Args&&... args) const { + ::new((void*)ptr) U(static_cast(args)...); + } +#else + template + void construct(U* ptr, V&& value) const { + ::new((void*)ptr) U(static_cast(value)); + } +#endif +#else + template + void construct(U* ptr, const V& value) const { + ::new((void*)ptr) U(value); + } +#endif + + template + void construct(U* ptr) const { + ::new((void*)ptr) U(); + } + + template + void destroy(U* ptr) const { + (void)ptr; + ptr->~U(); + } + }; template -bool operator==(const alloc_holder &, const alloc_holder &){ +BOOST_CONSTEXPR bool operator==(const custom_allocator &, const custom_allocator &) BOOST_NOEXCEPT { return true; -} + } template -bool operator!=(const alloc_holder &, const alloc_holder &){ +BOOST_CONSTEXPR bool operator!=(const custom_allocator &, const custom_allocator &) BOOST_NOEXCEPT { return false; -} -#endif + } void to_string ( const char *arg ) { string_view sr1; @@ -275,15 +333,16 @@ void to_string ( const char *arg ) { str1.assign ( arg ); sr1 = arg; -// str2 = sr1.to_string > (); +// str2 = sr1.to_string > (); str2 = sr1.to_string (); BOOST_CHECK ( str1 == str2 ); -#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS - std::string str3 = static_cast ( sr1 ); - BOOST_CHECK ( str1 == str3 ); + std::basic_string, custom_allocator > str3 = sr1.to_string(custom_allocator()); + BOOST_CHECK ( std::strcmp(str1.c_str(), str3.c_str()) == 0 ); - std::basic_string, alloc_holder> alloc_str = sr1.to_string(alloc_holder()); +#ifndef BOOST_NO_CXX11_EXPLICIT_CONVERSION_OPERATORS + std::string str4 = static_cast ( sr1 ); + BOOST_CHECK ( str1 == str4 ); #endif } @@ -294,11 +353,11 @@ void compare ( const char *arg ) { str1.assign ( arg ); sr1 = arg; - BOOST_CHECK ( sr1 == sr1); // compare string_view and string_view - BOOST_CHECK ( sr1 == str1); // compare string and string_view - BOOST_CHECK ( str1 == sr1 ); // compare string_view and string - BOOST_CHECK ( sr1 == arg ); // compare string_view and pointer - BOOST_CHECK ( arg == sr1 ); // compare pointer and string_view + BOOST_CHECK ( sr1 == sr1); // compare string_view and string_view + BOOST_CHECK ( sr1 == str1); // compare string and string_view + BOOST_CHECK ( str1 == sr1 ); // compare string_view and string + BOOST_CHECK ( sr1 == arg ); // compare string_view and pointer + BOOST_CHECK ( arg == sr1 ); // compare pointer and string_view if ( sr1.size () > 0 ) { (*str1.rbegin())++;