brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.2 KiB · ecdc397 Raw
85 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// Make sure the size we allocate and deallocate match. See https://github.com/llvm/llvm-project/pull/90292.12 13#include <string>14#include <cassert>15#include <cstddef>16#include <cstdint>17#include <type_traits>18 19#include "test_macros.h"20 21static std::uint64_t allocated_;22static std::uint64_t deallocated_;23 24template <class T, class Sz>25struct test_alloc {26  typedef Sz size_type;27  typedef typename std::make_signed<Sz>::type difference_type;28  typedef T value_type;29  typedef value_type* pointer;30  typedef const value_type* const_pointer;31  typedef typename std::add_lvalue_reference<value_type>::type reference;32  typedef typename std::add_lvalue_reference<const value_type>::type const_reference;33 34  template <class U>35  struct rebind {36    typedef test_alloc<U, Sz> other;37  };38 39  TEST_CONSTEXPR test_alloc() TEST_NOEXCEPT {}40 41  template <class U>42  TEST_CONSTEXPR test_alloc(const test_alloc<U, Sz>&) TEST_NOEXCEPT {}43 44  pointer allocate(size_type n, const void* = nullptr) {45    allocated_ += n;46    return std::allocator<value_type>().allocate(static_cast<std::size_t>(n));47  }48 49  void deallocate(pointer p, size_type s) {50    deallocated_ += s;51    std::allocator<value_type>().deallocate(p, static_cast<std::size_t>(s));52  }53 54  template <class U>55  friend TEST_CONSTEXPR bool operator==(const test_alloc&, const test_alloc<U, Sz>&) TEST_NOEXCEPT {56    return true;57  }58 59#if TEST_STD_VER < 2060  template <class U>61  friend TEST_CONSTEXPR bool operator!=(const test_alloc&, const test_alloc<U, Sz>&) TEST_NOEXCEPT {62    return false;63  }64#endif65};66 67template <class Sz>68void test() {69  for (unsigned int i = 1; i < 1000; ++i) {70    {71      std::basic_string<char, std::char_traits<char>, test_alloc<char, Sz> > s(i, 't');72      (void)s;73    }74    assert(allocated_ == deallocated_);75  }76}77 78int main(int, char**) {79  test<uint32_t>();80  test<uint64_t>();81  test<size_t>();82 83  return 0;84}85