brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · e23b6cf Raw
97 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// void resize(size_type n); // constexpr since C++2012 13#include <string>14#include <stdexcept>15#include <cassert>16 17#include "asan_testing.h"18#include "make_string.h"19#include "min_allocator.h"20#include "test_macros.h"21 22template <class S>23TEST_CONSTEXPR_CXX20 void test(S s, typename S::size_type n, S expected) {24  s.resize(n);25  LIBCPP_ASSERT(s.__invariants());26  assert(s == expected);27  LIBCPP_ASSERT(is_string_asan_correct(s));28}29 30template <class CharT, class Alloc>31TEST_CONSTEXPR_CXX20 void test_string() {32  {33    using string_type = std::basic_string<CharT, std::char_traits<CharT>, Alloc>;34    test(string_type(), 0, string_type());35    test(string_type(), 1, string_type(1, '\0'));36    test(string_type(), 10, string_type(10, '\0'));37    test(string_type(), 100, string_type(100, '\0'));38    test(string_type(MAKE_CSTRING(CharT, "12345")), 0, string_type());39    test(string_type(MAKE_CSTRING(CharT, "12345")), 2, string_type(MAKE_CSTRING(CharT, "12")));40    test(string_type(MAKE_CSTRING(CharT, "12345")), 5, string_type(MAKE_CSTRING(CharT, "12345")));41    test(string_type(MAKE_CSTRING(CharT, "12345")),42         15,43         string_type(MAKE_CSTRING(CharT, "12345\0\0\0\0\0\0\0\0\0\0"), 15));44    test(string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890")), 0, string_type());45    test(string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890")),46         10,47         string_type(MAKE_CSTRING(CharT, "1234567890")));48    test(string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890")),49         50,50         string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890")));51    test(52        string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890")),53        60,54        string_type(MAKE_CSTRING(CharT, "12345678901234567890123456789012345678901234567890\0\0\0\0\0\0\0\0\0\0"), 60));55  }56 57#ifndef TEST_HAS_NO_EXCEPTIONS58  if (!TEST_IS_CONSTANT_EVALUATED) {59    std::basic_string<CharT, std::char_traits<CharT>, Alloc> str;60    try {61      str.resize(std::string::npos);62      assert(false);63    } catch (const std::length_error&) {64    }65  }66#endif67 68  { // check that string can grow to max_size()69    std::basic_string<CharT, std::char_traits<CharT>, tiny_size_allocator<32, CharT> > str;70    str.resize(str.max_size());71    assert(str.size() == str.max_size());72  }73}74 75TEST_CONSTEXPR_CXX20 bool test() {76  test_string<char, std::allocator<char> >();77#if TEST_STD_VER >= 1178  test_string<char, min_allocator<char>>();79  test_string<char, safe_allocator<char>>();80#endif81 82#ifndef TEST_HAS_NO_WIDE_CHARACTERS83  test_string<wchar_t, std::allocator<wchar_t> >();84#endif85 86  return true;87}88 89int main(int, char**) {90  test();91#if TEST_STD_VER > 1792  static_assert(test());93#endif94 95  return 0;96}97