146 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8 9// Ensure that we never change the size or alignment of `basic_string`10 11#include <cstdint>12#include <iterator>13#include <string>14 15#include "test_macros.h"16#include "min_allocator.h"17#include "test_allocator.h"18 19template <class T>20class small_pointer {21public:22 using value_type = T;23 using difference_type = std::int16_t;24 using pointer = small_pointer;25 using reference = T&;26 using iterator_category = std::random_access_iterator_tag;27 28private:29 std::uint16_t offset;30};31 32template <class T>33class small_iter_allocator {34public:35 using value_type = T;36 using pointer = small_pointer<T>;37 using size_type = std::int16_t;38 using difference_type = std::int16_t;39 40 small_iter_allocator() TEST_NOEXCEPT {}41 42 template <class U>43 small_iter_allocator(small_iter_allocator<U>) TEST_NOEXCEPT {}44 45 T* allocate(std::size_t n);46 void deallocate(T* p, std::size_t);47 48 friend bool operator==(small_iter_allocator, small_iter_allocator) { return true; }49 friend bool operator!=(small_iter_allocator, small_iter_allocator) { return false; }50};51 52template <class CharT>53using min_string = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT> >;54 55template <class CharT>56using test_string = std::basic_string<CharT, std::char_traits<CharT>, test_allocator<CharT> >;57 58template <class CharT>59using small_string = std::basic_string<CharT, std::char_traits<CharT>, small_iter_allocator<CharT> >;60 61#if __SIZE_WIDTH__ == 6462 63static_assert(sizeof(std::string) == 24, "");64static_assert(sizeof(min_string<char>) == 24, "");65static_assert(sizeof(test_string<char>) == 32, "");66static_assert(sizeof(small_string<char>) == 6, "");67 68# ifndef TEST_HAS_NO_WIDE_CHARACTERS69# if __WCHAR_WIDTH__ == 3270static_assert(sizeof(std::wstring) == 24, "");71static_assert(sizeof(min_string<wchar_t>) == 24, "");72static_assert(sizeof(test_string<wchar_t>) == 32, "");73static_assert(sizeof(small_string<wchar_t>) == 12, "");74# elif __WCHAR_WIDTH__ == 1675static_assert(sizeof(std::wstring) == 24, "");76static_assert(sizeof(min_string<wchar_t>) == 24, "");77static_assert(sizeof(test_string<wchar_t>) == 32, "");78static_assert(sizeof(small_string<wchar_t>) == 6, "");79# else80# error "Unexpected wchar_t width"81# endif82# endif83 84# ifndef TEST_HAS_NO_CHAR8_T85static_assert(sizeof(std::u8string) == 24, "");86static_assert(sizeof(min_string<char8_t>) == 24, "");87static_assert(sizeof(test_string<char8_t>) == 32, "");88static_assert(sizeof(small_string<char8_t>) == 6, "");89# endif90 91# ifndef TEST_HAS_NO_UNICODE_CHARS92static_assert(sizeof(std::u16string) == 24, "");93static_assert(sizeof(std::u32string) == 24, "");94static_assert(sizeof(min_string<char16_t>) == 24, "");95static_assert(sizeof(min_string<char32_t>) == 24, "");96static_assert(sizeof(test_string<char16_t>) == 32, "");97static_assert(sizeof(test_string<char32_t>) == 32, "");98static_assert(sizeof(small_string<char16_t>) == 6, "");99static_assert(sizeof(small_string<char32_t>) == 12, "");100# endif101 102#elif __SIZE_WIDTH__ == 32103 104static_assert(sizeof(std::string) == 12, "");105static_assert(sizeof(min_string<char>) == 12, "");106static_assert(sizeof(test_string<char>) == 24, "");107static_assert(sizeof(small_string<char>) == 6, "");108 109# ifndef TEST_HAS_NO_WIDE_CHARACTERS110# if __WCHAR_WIDTH__ == 32111static_assert(sizeof(std::wstring) == 12, "");112static_assert(sizeof(min_string<wchar_t>) == 12, "");113static_assert(sizeof(test_string<wchar_t>) == 24, "");114static_assert(sizeof(small_string<wchar_t>) == 12, "");115# elif __WCHAR_WIDTH__ == 16116static_assert(sizeof(std::wstring) == 12, "");117static_assert(sizeof(min_string<wchar_t>) == 12, "");118static_assert(sizeof(test_string<wchar_t>) == 24, "");119static_assert(sizeof(small_string<wchar_t>) == 6, "");120# else121# error "Unexpected wchar_t width"122# endif123# endif124 125# ifndef TEST_HAS_NO_CHAR8_T126static_assert(sizeof(std::u8string) == 12, "");127static_assert(sizeof(min_string<char8_t>) == 12, "");128static_assert(sizeof(test_string<char8_t>) == 24, "");129static_assert(sizeof(small_string<char>) == 6, "");130# endif131 132# ifndef TEST_HAS_NO_UNICODE_CHARS133static_assert(sizeof(std::u16string) == 12, "");134static_assert(sizeof(std::u32string) == 12, "");135static_assert(sizeof(min_string<char16_t>) == 12, "");136static_assert(sizeof(min_string<char32_t>) == 12, "");137static_assert(sizeof(test_string<char16_t>) == 24, "");138static_assert(sizeof(test_string<char32_t>) == 24, "");139static_assert(sizeof(small_string<char16_t>) == 6, "");140static_assert(sizeof(small_string<char32_t>) == 12, "");141# endif142 143#else144# error "std::size_t has an unexpected size"145#endif146