[libcxx-commits] [libcxx] 2619ce8 - [libc++] Test the size of basic_string
Nikolas Klauser via libcxx-commits
libcxx-commits at lists.llvm.org
Thu Jul 14 07:58:02 PDT 2022
Author: Nikolas Klauser
Date: 2022-07-14T16:57:51+02:00
New Revision: 2619ce8b7e78bc1bafe0f7600215c730fec6b263
URL: https://github.com/llvm/llvm-project/commit/2619ce8b7e78bc1bafe0f7600215c730fec6b263
DIFF: https://github.com/llvm/llvm-project/commit/2619ce8b7e78bc1bafe0f7600215c730fec6b263.diff
LOG: [libc++] Test the size of basic_string
Reviewed By: ldionne, #libc
Spies: hubert.reinterpretcast, arichardson, mstorsjo, libcxx-commits
Differential Revision: https://reviews.llvm.org/D127672
Added:
libcxx/test/libcxx/strings/basic.string/alignof.compile.pass.cpp
libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp
Modified:
Removed:
################################################################################
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
index 0000000000000..d679e7a1b7e31
--- /dev/null
+++ b/libcxx/test/libcxx/strings/basic.string/alignof.compile.pass.cpp
@@ -0,0 +1,136 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <string>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+template <class T>
+class small_pointer {
+ uint16_t offset;
+};
+
+template <class T>
+class small_iter_allocator {
+public:
+ using value_type = T;
+ using pointer = small_pointer<T>;
+ using size_type = int16_t;
+ using
diff erence_type = int16_t;
+
+ small_iter_allocator() TEST_NOEXCEPT {}
+
+ template <class U>
+ small_iter_allocator(small_iter_allocator<U>) 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 <class CharT>
+using min_string = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT>>;
+
+template <class CharT>
+using test_string = std::basic_string<CharT, std::char_traits<CharT>, test_allocator<CharT>>;
+
+template <class CharT>
+using small_string = std::basic_string<CharT, std::char_traits<CharT>, small_iter_allocator<CharT>>;
+
+#if __SIZE_WIDTH__ == 64
+
+static_assert(alignof(std::string) == 8, "");
+static_assert(alignof(min_string<char>) == 8, "");
+static_assert(alignof(test_string<char>) == 8, "");
+static_assert(alignof(small_string<char>) == 2, "");
+
+# ifndef TEST_HAS_NO_WIDE_CHARACTERS
+# if __WCHAR_WIDTH__ == 32
+static_assert(alignof(std::wstring) == 8, "");
+static_assert(alignof(min_string<wchar_t>) == 8, "");
+static_assert(alignof(test_string<wchar_t>) == 8, "");
+static_assert(alignof(small_string<wchar_t>) == 4, "");
+# elif __WCHAR_WIDTH__ == 16
+static_assert(alignof(std::wstring) == 8, "");
+static_assert(alignof(min_string<wchar_t>) == 8, "");
+static_assert(alignof(test_string<wchar_t>) == 8, "");
+static_assert(alignof(small_string<wchar_t>) == 2, "");
+# else
+# error "Unexpected wchar_t width"
+# endif
+# endif
+
+# ifndef TEST_HAS_NO_CHAR8_T
+static_assert(alignof(std::u8string) == 8, "");
+static_assert(alignof(min_string<char8_t>) == 8, "");
+static_assert(alignof(test_string<char8_t>) == 8, "");
+static_assert(alignof(small_string<char8_t>) == 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<char16_t>) == 8, "");
+static_assert(alignof(min_string<char32_t>) == 8, "");
+static_assert(alignof(test_string<char16_t>) == 8, "");
+static_assert(alignof(test_string<char32_t>) == 8, "");
+static_assert(alignof(small_string<char16_t>) == 2, "");
+static_assert(alignof(small_string<char32_t>) == 4, "");
+# endif
+
+#elif __SIZE_WIDTH__ == 32
+
+static_assert(alignof(std::string) == 4, "");
+static_assert(alignof(min_string<char>) == 4, "");
+static_assert(alignof(test_string<char>) == 4, "");
+static_assert(alignof(small_string<char>) == 2, "");
+
+# ifndef TEST_HAS_NO_WIDE_CHARACTERS
+# if __WCHAR_WIDTH__ == 32
+static_assert(alignof(std::wstring) == 4, "");
+static_assert(alignof(min_string<wchar_t>) == 4, "");
+static_assert(alignof(test_string<wchar_t>) == 4, "");
+static_assert(alignof(small_string<wchar_t>) == 4, "");
+# elif __WCHAR_WIDTH__ == 16
+static_assert(alignof(std::wstring) == 4, "");
+static_assert(alignof(min_string<wchar_t>) == 4, "");
+static_assert(alignof(test_string<wchar_t>) == 4, "");
+static_assert(alignof(small_string<wchar_t>) == 2, "");
+# else
+# error "Unexpected wchar_t width"
+# endif
+# endif
+
+# ifndef TEST_HAS_NO_CHAR8_T
+static_assert(alignof(std::u8string) == 4, "");
+static_assert(alignof(min_string<char8_t>) == 4, "");
+static_assert(alignof(test_string<char8_t>) == 4, "");
+static_assert(alignof(small_string<char8_t>) == 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<char16_t>) == 4, "");
+static_assert(alignof(min_string<char32_t>) == 4, "");
+static_assert(alignof(test_string<char16_t>) == 4, "");
+static_assert(alignof(test_string<char32_t>) == 4, "");
+static_assert(alignof(small_string<char32_t>) == 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
index 0000000000000..9024d11c598e0
--- /dev/null
+++ b/libcxx/test/libcxx/strings/basic.string/sizeof.compile.pass.cpp
@@ -0,0 +1,135 @@
+//===----------------------------------------------------------------------===//
+//
+// 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 <string>
+
+#include "test_macros.h"
+#include "min_allocator.h"
+#include "test_allocator.h"
+
+template <class T>
+class small_pointer {
+ uint16_t offset;
+};
+
+template <class T>
+class small_iter_allocator {
+public:
+ using value_type = T;
+ using pointer = small_pointer<T>;
+ using size_type = int16_t;
+ using
diff erence_type = int16_t;
+
+ small_iter_allocator() TEST_NOEXCEPT {}
+
+ template <class U>
+ small_iter_allocator(small_iter_allocator<U>) 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 <class CharT>
+using min_string = std::basic_string<CharT, std::char_traits<CharT>, min_allocator<CharT> >;
+
+template <class CharT>
+using test_string = std::basic_string<CharT, std::char_traits<CharT>, test_allocator<CharT> >;
+
+template <class CharT>
+using small_string = std::basic_string<CharT, std::char_traits<CharT>, small_iter_allocator<CharT> >;
+
+#if __SIZE_WIDTH__ == 64
+
+static_assert(sizeof(std::string) == 24, "");
+static_assert(sizeof(min_string<char>) == 24, "");
+static_assert(sizeof(test_string<char>) == 32, "");
+static_assert(sizeof(small_string<char>) == 6, "");
+
+# ifndef TEST_HAS_NO_WIDE_CHARACTERS
+# if __WCHAR_WIDTH__ == 32
+static_assert(sizeof(std::wstring) == 24, "");
+static_assert(sizeof(min_string<wchar_t>) == 24, "");
+static_assert(sizeof(test_string<wchar_t>) == 32, "");
+static_assert(sizeof(small_string<wchar_t>) == 12, "");
+# elif __WCHAR_WIDTH__ == 16
+static_assert(sizeof(std::wstring) == 24, "");
+static_assert(sizeof(min_string<wchar_t>) == 24, "");
+static_assert(sizeof(test_string<wchar_t>) == 32, "");
+static_assert(sizeof(small_string<wchar_t>) == 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<char8_t>) == 24, "");
+static_assert(sizeof(test_string<char8_t>) == 32, "");
+static_assert(sizeof(small_string<char8_t>) == 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<char16_t>) == 24, "");
+static_assert(sizeof(min_string<char32_t>) == 24, "");
+static_assert(sizeof(test_string<char16_t>) == 32, "");
+static_assert(sizeof(test_string<char32_t>) == 32, "");
+static_assert(sizeof(small_string<char16_t>) == 6, "");
+static_assert(sizeof(small_string<char32_t>) == 12, "");
+# endif
+
+#elif __SIZE_WIDTH__ == 32
+
+static_assert(sizeof(std::string) == 12, "");
+static_assert(sizeof(min_string<char>) == 12, "");
+static_assert(sizeof(test_string<char>) == 24, "");
+static_assert(sizeof(small_string<char>) == 6, "");
+
+# ifndef TEST_HAS_NO_WIDE_CHARACTERS
+# if __WCHAR_WIDTH__ == 32
+static_assert(sizeof(std::wstring) == 12, "");
+static_assert(sizeof(min_string<wchar_t>) == 12, "");
+static_assert(sizeof(test_string<wchar_t>) == 24, "");
+static_assert(sizeof(small_string<wchar_t>) == 12, "");
+# elif __WCHAR_WIDTH__ == 16
+static_assert(sizeof(std::wstring) == 12, "");
+static_assert(sizeof(min_string<wchar_t>) == 12, "");
+static_assert(sizeof(test_string<wchar_t>) == 24, "");
+static_assert(sizeof(small_string<wchar_t>) == 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<char8_t>) == 12, "");
+static_assert(sizeof(test_string<char8_t>) == 24, "");
+static_assert(sizeof(small_string<char>) == 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<char16_t>) == 12, "");
+static_assert(sizeof(min_string<char32_t>) == 12, "");
+static_assert(sizeof(test_string<char16_t>) == 24, "");
+static_assert(sizeof(test_string<char32_t>) == 24, "");
+static_assert(sizeof(small_string<char16_t>) == 6, "");
+static_assert(sizeof(small_string<char32_t>) == 12, "");
+# endif
+
+#else
+# error "size_t has an unexpected size"
+#endif
More information about the libcxx-commits
mailing list