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 #include <string> 12 13 #include "test_macros.h" 14 #include "min_allocator.h" 15 #include "test_allocator.h" 16 17 template <class T> 18 class small_pointer { 19 uint16_t offset; 20 }; 21 22 template <class T> 23 class small_iter_allocator { 24 public: 25 using value_type = T; 26 using pointer = small_pointer<T>; 27 using size_type = int16_t; 28 using difference_type = int16_t; 29 small_iter_allocator()30 small_iter_allocator() TEST_NOEXCEPT {} 31 32 template <class U> small_iter_allocator(small_iter_allocator<U>)33 small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {} 34 35 T* allocate(std::size_t n); 36 void deallocate(T* p, std::size_t); 37 operator ==(small_iter_allocator,small_iter_allocator)38 friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; } operator !=(small_iter_allocator,small_iter_allocator)39 friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; } 40 }; 41 42 template <class CharT> 43 using min_string = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT> >; 44 45 template <class CharT> 46 using test_string = std::basic_string<CharT, std::char_traits<CharT>, test_allocator<CharT> >; 47 48 template <class CharT> 49 using small_string = std::basic_string<CharT, std::char_traits<CharT>, small_iter_allocator<CharT> >; 50 51 #if __SIZE_WIDTH__ == 64 52 53 static_assert(sizeof(std::string) == 24, ""); 54 static_assert(sizeof(min_string<char>) == 24, ""); 55 static_assert(sizeof(test_string<char>) == 32, ""); 56 static_assert(sizeof(small_string<char>) == 6, ""); 57 58 # ifndef TEST_HAS_NO_WIDE_CHARACTERS 59 # if __WCHAR_WIDTH__ == 32 60 static_assert(sizeof(std::wstring) == 24, ""); 61 static_assert(sizeof(min_string<wchar_t>) == 24, ""); 62 static_assert(sizeof(test_string<wchar_t>) == 32, ""); 63 static_assert(sizeof(small_string<wchar_t>) == 12, ""); 64 # elif __WCHAR_WIDTH__ == 16 65 static_assert(sizeof(std::wstring) == 24, ""); 66 static_assert(sizeof(min_string<wchar_t>) == 24, ""); 67 static_assert(sizeof(test_string<wchar_t>) == 32, ""); 68 static_assert(sizeof(small_string<wchar_t>) == 6, ""); 69 # else 70 # error "Unexpected wchar_t width" 71 # endif 72 # endif 73 74 # ifndef TEST_HAS_NO_CHAR8_T 75 static_assert(sizeof(std::u8string) == 24, ""); 76 static_assert(sizeof(min_string<char8_t>) == 24, ""); 77 static_assert(sizeof(test_string<char8_t>) == 32, ""); 78 static_assert(sizeof(small_string<char8_t>) == 6, ""); 79 # endif 80 81 # ifndef TEST_HAS_NO_UNICODE_CHARS 82 static_assert(sizeof(std::u16string) == 24, ""); 83 static_assert(sizeof(std::u32string) == 24, ""); 84 static_assert(sizeof(min_string<char16_t>) == 24, ""); 85 static_assert(sizeof(min_string<char32_t>) == 24, ""); 86 static_assert(sizeof(test_string<char16_t>) == 32, ""); 87 static_assert(sizeof(test_string<char32_t>) == 32, ""); 88 static_assert(sizeof(small_string<char16_t>) == 6, ""); 89 static_assert(sizeof(small_string<char32_t>) == 12, ""); 90 # endif 91 92 #elif __SIZE_WIDTH__ == 32 93 94 static_assert(sizeof(std::string) == 12, ""); 95 static_assert(sizeof(min_string<char>) == 12, ""); 96 static_assert(sizeof(test_string<char>) == 24, ""); 97 static_assert(sizeof(small_string<char>) == 6, ""); 98 99 # ifndef TEST_HAS_NO_WIDE_CHARACTERS 100 # if __WCHAR_WIDTH__ == 32 101 static_assert(sizeof(std::wstring) == 12, ""); 102 static_assert(sizeof(min_string<wchar_t>) == 12, ""); 103 static_assert(sizeof(test_string<wchar_t>) == 24, ""); 104 static_assert(sizeof(small_string<wchar_t>) == 12, ""); 105 # elif __WCHAR_WIDTH__ == 16 106 static_assert(sizeof(std::wstring) == 12, ""); 107 static_assert(sizeof(min_string<wchar_t>) == 12, ""); 108 static_assert(sizeof(test_string<wchar_t>) == 24, ""); 109 static_assert(sizeof(small_string<wchar_t>) == 6, ""); 110 # else 111 # error "Unexpected wchar_t width" 112 # endif 113 # endif 114 115 # ifndef TEST_HAS_NO_CHAR8_T 116 static_assert(sizeof(std::u8string) == 12, ""); 117 static_assert(sizeof(min_string<char8_t>) == 12, ""); 118 static_assert(sizeof(test_string<char8_t>) == 24, ""); 119 static_assert(sizeof(small_string<char>) == 6, ""); 120 # endif 121 122 # ifndef TEST_HAS_NO_UNICODE_CHARS 123 static_assert(sizeof(std::u16string) == 12, ""); 124 static_assert(sizeof(std::u32string) == 12, ""); 125 static_assert(sizeof(min_string<char16_t>) == 12, ""); 126 static_assert(sizeof(min_string<char32_t>) == 12, ""); 127 static_assert(sizeof(test_string<char16_t>) == 24, ""); 128 static_assert(sizeof(test_string<char32_t>) == 24, ""); 129 static_assert(sizeof(small_string<char16_t>) == 6, ""); 130 static_assert(sizeof(small_string<char32_t>) == 12, ""); 131 # endif 132 133 #else 134 # error "size_t has an unexpected size" 135 #endif 136