brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.9 KiB · 590ab43 Raw
104 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// UNSUPPORTED: c++0310 11// <deque>12 13// template <class... Args> reference emplace_back(Args&&... args);14// return type is 'reference' in C++17; 'void' before15 16#include "asan_testing.h"17#include <deque>18#include <cstddef>19#include <cassert>20 21#include "test_macros.h"22#include "../../../Emplaceable.h"23#include "min_allocator.h"24#include "test_allocator.h"25 26template <class C>27C make(int size, int start = 0) {28  const int b = 4096 / sizeof(int);29  int init    = 0;30  if (start > 0) {31    init = (start + 1) / b + ((start + 1) % b != 0);32    init *= b;33    --init;34  }35  C c(init);36  for (int i = 0; i < init - start; ++i)37    c.pop_back();38  for (int i = 0; i < size; ++i)39    c.push_back(Emplaceable());40  for (int i = 0; i < start; ++i)41    c.pop_front();42  return c;43}44 45template <class C>46void test(C& c1) {47  typedef typename C::iterator I;48  std::size_t c1_osize = c1.size();49#if TEST_STD_VER > 1450  typedef typename C::reference Ref;51  Ref ref = c1.emplace_back(Emplaceable(1, 2.5));52#else53  c1.emplace_back(Emplaceable(1, 2.5));54#endif55  assert(c1.size() == c1_osize + 1);56  assert(std::distance(c1.begin(), c1.end()) == static_cast<std::ptrdiff_t>(c1.size()));57  I i = c1.end();58  assert(*--i == Emplaceable(1, 2.5));59  LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c1));60#if TEST_STD_VER > 1461  assert(&(*i) == &ref);62#endif63}64 65template <class C>66void testN(int start, int N) {67  C c1 = make<C>(N, start);68  test(c1);69}70 71int main(int, char**) {72  {73    int rng[]   = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};74    const int N = sizeof(rng) / sizeof(rng[0]);75    for (int i = 0; i < N; ++i)76      for (int j = 0; j < N; ++j)77        testN<std::deque<Emplaceable> >(rng[i], rng[j]);78  }79  {80    int rng[]   = {0, 1, 2, 3, 1023, 1024, 1025, 2047, 2048, 2049};81    const int N = sizeof(rng) / sizeof(rng[0]);82    for (int i = 0; i < N; ++i)83      for (int j = 0; j < N; ++j)84        testN<std::deque<Emplaceable, min_allocator<Emplaceable>> >(rng[i], rng[j]);85  }86  {87    std::deque<Tag_X, TaggingAllocator<Tag_X>> c;88    c.emplace_back();89    assert(c.size() == 1);90    LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));91    c.emplace_back(1, 2, 3);92    assert(c.size() == 2);93    LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));94    c.emplace_front();95    assert(c.size() == 3);96    LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));97    c.emplace_front(1, 2, 3);98    assert(c.size() == 4);99    LIBCPP_ASSERT(is_double_ended_contiguous_container_asan_correct(c));100  }101 102  return 0;103}104