1 //===----------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 // Ensure that we never change the size or alignment of `basic_string` 10 11 // UNSUPPORTED: c++03 12 13 #include <string> 14 15 #include "test_macros.h" 16 #include "min_allocator.h" 17 #include "test_allocator.h" 18 19 template <class T> 20 class small_pointer { 21 std::uint16_t offset; 22 }; 23 24 template <class T> 25 class small_iter_allocator { 26 public: 27 using value_type = T; 28 using pointer = small_pointer<T>; 29 using size_type = std::int16_t; 30 using difference_type = std::int16_t; 31 small_iter_allocator()32 small_iter_allocator() TEST_NOEXCEPT {} 33 34 template <class U> small_iter_allocator(small_iter_allocator<U>)35 small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {} 36 37 T* allocate(std::size_t n); 38 void deallocate(T* p, std::size_t); 39 operator ==(small_iter_allocator,small_iter_allocator)40 friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; } operator !=(small_iter_allocator,small_iter_allocator)41 friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; } 42 }; 43 44 template <class CharT> 45 using min_string = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT>>; 46 47 template <class CharT> 48 using test_string = std::basic_string<CharT, std::char_traits<CharT>, test_allocator<CharT>>; 49 50 template <class CharT> 51 using small_string = std::basic_string<CharT, std::char_traits<CharT>, small_iter_allocator<CharT>>; 52 53 #if __SIZE_WIDTH__ == 64 54 55 static_assert(alignof(std::string) == 8, ""); 56 static_assert(alignof(min_string<char>) == 8, ""); 57 static_assert(alignof(test_string<char>) == 8, ""); 58 static_assert(alignof(small_string<char>) == 2, ""); 59 60 # ifndef TEST_HAS_NO_WIDE_CHARACTERS 61 # if __WCHAR_WIDTH__ == 32 62 static_assert(alignof(std::wstring) == 8, ""); 63 static_assert(alignof(min_string<wchar_t>) == 8, ""); 64 static_assert(alignof(test_string<wchar_t>) == 8, ""); 65 static_assert(alignof(small_string<wchar_t>) == 4, ""); 66 # elif __WCHAR_WIDTH__ == 16 67 static_assert(alignof(std::wstring) == 8, ""); 68 static_assert(alignof(min_string<wchar_t>) == 8, ""); 69 static_assert(alignof(test_string<wchar_t>) == 8, ""); 70 static_assert(alignof(small_string<wchar_t>) == 2, ""); 71 # else 72 # error "Unexpected wchar_t width" 73 # endif 74 # endif 75 76 # ifndef TEST_HAS_NO_CHAR8_T 77 static_assert(alignof(std::u8string) == 8, ""); 78 static_assert(alignof(min_string<char8_t>) == 8, ""); 79 static_assert(alignof(test_string<char8_t>) == 8, ""); 80 static_assert(alignof(small_string<char8_t>) == 2, ""); 81 # endif 82 83 # ifndef TEST_HAS_NO_UNICODE_CHARS 84 static_assert(alignof(std::u16string) == 8, ""); 85 static_assert(alignof(std::u32string) == 8, ""); 86 static_assert(alignof(min_string<char16_t>) == 8, ""); 87 static_assert(alignof(min_string<char32_t>) == 8, ""); 88 static_assert(alignof(test_string<char16_t>) == 8, ""); 89 static_assert(alignof(test_string<char32_t>) == 8, ""); 90 static_assert(alignof(small_string<char16_t>) == 2, ""); 91 static_assert(alignof(small_string<char32_t>) == 4, ""); 92 # endif 93 94 #elif __SIZE_WIDTH__ == 32 95 96 static_assert(alignof(std::string) == 4, ""); 97 static_assert(alignof(min_string<char>) == 4, ""); 98 static_assert(alignof(test_string<char>) == 4, ""); 99 static_assert(alignof(small_string<char>) == 2, ""); 100 101 # ifndef TEST_HAS_NO_WIDE_CHARACTERS 102 # if __WCHAR_WIDTH__ == 32 103 static_assert(alignof(std::wstring) == 4, ""); 104 static_assert(alignof(min_string<wchar_t>) == 4, ""); 105 static_assert(alignof(test_string<wchar_t>) == 4, ""); 106 static_assert(alignof(small_string<wchar_t>) == 4, ""); 107 # elif __WCHAR_WIDTH__ == 16 108 static_assert(alignof(std::wstring) == 4, ""); 109 static_assert(alignof(min_string<wchar_t>) == 4, ""); 110 static_assert(alignof(test_string<wchar_t>) == 4, ""); 111 static_assert(alignof(small_string<wchar_t>) == 2, ""); 112 # else 113 # error "Unexpected wchar_t width" 114 # endif 115 # endif 116 117 # ifndef TEST_HAS_NO_CHAR8_T 118 static_assert(alignof(std::u8string) == 4, ""); 119 static_assert(alignof(min_string<char8_t>) == 4, ""); 120 static_assert(alignof(test_string<char8_t>) == 4, ""); 121 static_assert(alignof(small_string<char8_t>) == 2, ""); 122 # endif 123 124 # ifndef TEST_HAS_NO_UNICODE_CHARS 125 static_assert(alignof(std::u16string) == 4, ""); 126 static_assert(alignof(std::u32string) == 4, ""); 127 static_assert(alignof(min_string<char16_t>) == 4, ""); 128 static_assert(alignof(min_string<char32_t>) == 4, ""); 129 static_assert(alignof(test_string<char16_t>) == 4, ""); 130 static_assert(alignof(test_string<char32_t>) == 4, ""); 131 static_assert(alignof(small_string<char32_t>) == 4, ""); 132 # endif 133 134 #else 135 # error "std::size_t has an unexpected size" 136 #endif 137