//===----------------------------------------------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // Ensure that we never change the size or alignment of `basic_string` #include #include #include #include "test_macros.h" #include "min_allocator.h" #include "test_allocator.h" template class small_pointer { public: using value_type = T; using difference_type = std::int16_t; using pointer = small_pointer; using reference = T&; using iterator_category = std::random_access_iterator_tag; private: std::uint16_t offset; }; template class small_iter_allocator { public: using value_type = T; using pointer = small_pointer; using size_type = std::int16_t; using difference_type = std::int16_t; small_iter_allocator() TEST_NOEXCEPT {} template small_iter_allocator(small_iter_allocator) TEST_NOEXCEPT {} T* allocate(std::size_t n); void deallocate(T* p, std::size_t); friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; } friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; } }; template using min_string = std::basic_string, min_allocator >; template using test_string = std::basic_string, test_allocator >; template using small_string = std::basic_string, small_iter_allocator >; #if __SIZE_WIDTH__ == 64 static_assert(sizeof(std::string) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(small_string) == 6, ""); # ifndef TEST_HAS_NO_WIDE_CHARACTERS # if __WCHAR_WIDTH__ == 32 static_assert(sizeof(std::wstring) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(small_string) == 12, ""); # elif __WCHAR_WIDTH__ == 16 static_assert(sizeof(std::wstring) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(small_string) == 6, ""); # else # error "Unexpected wchar_t width" # endif # endif # ifndef TEST_HAS_NO_CHAR8_T static_assert(sizeof(std::u8string) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(small_string) == 6, ""); # endif # ifndef TEST_HAS_NO_UNICODE_CHARS static_assert(sizeof(std::u16string) == 24, ""); static_assert(sizeof(std::u32string) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(min_string) == 24, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(test_string) == 32, ""); static_assert(sizeof(small_string) == 6, ""); static_assert(sizeof(small_string) == 12, ""); # endif #elif __SIZE_WIDTH__ == 32 static_assert(sizeof(std::string) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(small_string) == 6, ""); # ifndef TEST_HAS_NO_WIDE_CHARACTERS # if __WCHAR_WIDTH__ == 32 static_assert(sizeof(std::wstring) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(small_string) == 12, ""); # elif __WCHAR_WIDTH__ == 16 static_assert(sizeof(std::wstring) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(small_string) == 6, ""); # else # error "Unexpected wchar_t width" # endif # endif # ifndef TEST_HAS_NO_CHAR8_T static_assert(sizeof(std::u8string) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(small_string) == 6, ""); # endif # ifndef TEST_HAS_NO_UNICODE_CHARS static_assert(sizeof(std::u16string) == 12, ""); static_assert(sizeof(std::u32string) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(min_string) == 12, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(test_string) == 24, ""); static_assert(sizeof(small_string) == 6, ""); static_assert(sizeof(small_string) == 12, ""); # endif #else # error "std::size_t has an unexpected size" #endif