diff --git a/libcxx/test/libcxx/strings/basic.string/alignof.compile.pass.cpp b/libcxx/test/libcxx/strings/basic.string/alignof.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/strings/basic.string/alignof.compile.pass.cpp @@ -0,0 +1,107 @@ +//===----------------------------------------------------------------------===// +// +// 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` + +// UNSUPPORTED: c++03 + +#include + +#include "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +template +class small_pointer { + uint16_t offset; +}; + +template +class small_iter_allocator +{ +public: + using value_type = T; + using pointer = small_pointer; + using size_type = int16_t; + using difference_type = 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(alignof(std::string) == 8, ""); +static_assert(alignof(std::wstring) == 8, ""); +static_assert(alignof(min_string) == 8, ""); +static_assert(alignof(test_string) == 8, ""); +static_assert(alignof(small_string) == 2, ""); + +#ifndef TEST_HAS_NO_CHAR8_T +static_assert(alignof(std::u8string) == 8, ""); +static_assert(alignof(min_string) == 8, ""); +static_assert(alignof(test_string) == 8, ""); +static_assert(alignof(small_string) == 2, ""); +#endif + +#ifndef TEST_HAS_NO_UNICODE_CHARS +static_assert(alignof(std::u16string) == 8, ""); +static_assert(alignof(std::u32string) == 8, ""); +static_assert(alignof(min_string) == 8, ""); +static_assert(alignof(min_string) == 8, ""); +static_assert(alignof(test_string) == 8, ""); +static_assert(alignof(test_string) == 8, ""); +static_assert(alignof(small_string) == 2, ""); +static_assert(alignof(small_string) == 4, ""); +#endif + +#elif __SIZE_WIDTH__ == 32 + +static_assert(alignof(std::string) == 4, ""); +static_assert(alignof(std::wstring) == 4, ""); +static_assert(alignof(min_string) == 4, ""); +static_assert(alignof(test_string) == 4, ""); +static_assert(alignof(small_string) == 2, ""); + +#ifndef TEST_HAS_NO_CHAR8_T +static_assert(alignof(std::u8string) == 4, ""); +static_assert(alignof(min_string) == 4, ""); +static_assert(alignof(test_string) == 4, ""); +static_assert(alignof(small_string) == 2, ""); +#endif + +#ifndef TEST_HAS_NO_UNICODE_CHARS +static_assert(alignof(std::u16string) == 4, ""); +static_assert(alignof(std::u32string) == 4, ""); +static_assert(alignof(min_string) == 4, ""); +static_assert(alignof(min_string) == 4, ""); +static_assert(alignof(test_string) == 4, ""); +static_assert(alignof(test_string) == 4, ""); +static_assert(alignof(small_string) == 4, ""); +#endif + +#else +# error "size_t has an unexpected size" +#endif diff --git a/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp b/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp new file mode 100644 --- /dev/null +++ b/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp @@ -0,0 +1,106 @@ +//===----------------------------------------------------------------------===// +// +// 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 "test_macros.h" +#include "min_allocator.h" +#include "test_allocator.h" + +template +class small_pointer { + uint16_t offset; +}; + +template +class small_iter_allocator +{ +public: + using value_type = T; + using pointer = small_pointer; + using size_type = int16_t; + using difference_type = 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(std::wstring) == 24, ""); +static_assert(sizeof(min_string) == 24, ""); +static_assert(sizeof(test_string) == 32, ""); +static_assert(sizeof(small_string) == 6, ""); + +#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(std::wstring) == 12, ""); +static_assert(sizeof(min_string) == 12, ""); +static_assert(sizeof(test_string) == 16, ""); +static_assert(sizeof(small_string) == 6, ""); + +#ifndef TEST_HAS_NO_CHAR8_T +static_assert(sizeof(std::u8string) == 12, ""); +static_assert(sizeof(min_string) == 12, ""); +static_assert(sizeof(test_string) == 16, ""); +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) == 16, ""); +static_assert(sizeof(test_string) == 16, ""); +static_assert(sizeof(small_string) == 6, ""); +static_assert(sizeof(small_string) == 12, ""); +#endif + +#else +# error "size_t has an unexpected size" +#endif