brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.0 KiB · 73825ef Raw
118 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// <string>10 11// This test ensures that the correct max_size() is returned depending on the platform.12 13#include <algorithm>14#include <cassert>15#include <cstddef>16#include <string>17 18#include "test_macros.h"19 20// alignment of the string heap buffer is hardcoded to 821static const std::size_t alignment = 8;22 23template <class = int>24TEST_CONSTEXPR_CXX20 void full_size() {25  std::string str;26  assert(str.max_size() == std::numeric_limits<std::size_t>::max() - alignment - 1);27 28#ifndef TEST_HAS_NO_CHAR8_T29  std::u8string u8str;30  assert(u8str.max_size() == std::numeric_limits<std::size_t>::max() - alignment - 1);31#endif32 33#ifndef TEST_HAS_NO_WIDE_CHARACTERS34  std::wstring wstr;35  assert(wstr.max_size() ==36         ((std::numeric_limits<std::size_t>::max() / sizeof(wchar_t) - alignment) & ~std::size_t(1)) - 1);37#endif38 39  std::u16string u16str;40  std::u32string u32str;41  assert(u16str.max_size() == ((std::numeric_limits<std::size_t>::max() / 2 - alignment) & ~std::size_t(1)) - 1);42  assert(u32str.max_size() == ((std::numeric_limits<std::size_t>::max() / 4 - alignment) & ~std::size_t(1)) - 1);43}44 45template <class = int>46TEST_CONSTEXPR_CXX20 void half_size() {47  std::string str;48  assert(str.max_size() == std::numeric_limits<std::size_t>::max() / 2 - alignment - 1);49 50#ifndef TEST_HAS_NO_CHAR8_T51  std::u8string u8str;52  assert(u8str.max_size() == std::numeric_limits<std::size_t>::max() / 2 - alignment - 1);53#endif54 55#ifndef TEST_HAS_NO_WIDE_CHARACTERS56  std::wstring wstr;57  assert(wstr.max_size() ==58         std::numeric_limits<std::size_t>::max() / std::max<size_t>(2ul, sizeof(wchar_t)) - alignment - 1);59#endif60 61  std::u16string u16str;62  std::u32string u32str;63  assert(u16str.max_size() == std::numeric_limits<std::size_t>::max() / 2 - alignment - 1);64  assert(u32str.max_size() == std::numeric_limits<std::size_t>::max() / 4 - alignment - 1);65}66 67TEST_CONSTEXPR_CXX20 bool test() {68#if _LIBCPP_ABI_VERSION == 169 70#  if defined(__x86_64__) || defined(__i386__)71  full_size();72#  elif defined(__APPLE__) && defined(__aarch64__)73  half_size();74#  elif defined(__arm__) || defined(__aarch64__)75#    ifdef __BIG_ENDIAN__76  half_size();77#    else78  full_size();79#    endif80#  elif defined(__powerpc__) || defined(__powerpc64__)81#    ifdef __BIG_ENDIAN__82  half_size();83#    else84  full_size();85#    endif86#  elif defined(__sparc64__)87  half_size();88#  elif defined(__riscv)89  full_size();90#  elif defined(_WIN32)91  full_size();92#  else93#    error "Your target system seems to be unsupported."94#  endif95 96#else97 98#  if defined(__arm__) || defined(__aarch64__)99#    ifdef __BIG_ENDIAN__100  full_size();101#    else102  half_size();103#    endif104#  else105  half_size();106#  endif107 108#endif109 110  return true;111}112 113int main(int, char**) {114  test();115 116  return 0;117}118