brintos

brintos / llvm-project-archived public Read only

0
0
Text · 2.8 KiB · 9bf677b Raw
115 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// <list>10 11// void      remove(const value_type& value); // pre-c++2012// size_type remove(const value_type& value); // c++20 and later; constexpr since C++2613 14#include <list>15#include <cassert>16 17#include "test_macros.h"18#include "min_allocator.h"19 20struct S {21  TEST_CONSTEXPR_CXX20 S(int i) : i_(new int(i)) {}22  TEST_CONSTEXPR_CXX20 S(const S& rhs) : i_(new int(*rhs.i_)) {}23  TEST_CONSTEXPR_CXX14 S& operator=(const S& rhs) {24    *i_ = *rhs.i_;25    return *this;26  }27  TEST_CONSTEXPR_CXX20 ~S() {28    delete i_;29    i_ = NULL;30  }31  TEST_CONSTEXPR bool operator==(const S& rhs) const { return *i_ == *rhs.i_; }32  TEST_CONSTEXPR int get() const { return *i_; }33  int* i_;34};35 36TEST_CONSTEXPR_CXX26 bool test() {37  {38    int a1[] = {1, 2, 3, 4};39    int a2[] = {1, 2, 4};40    typedef std::list<int> L;41    L c(a1, a1 + 4);42#if TEST_STD_VER > 1743    assert(c.remove(3) == 1);44    ASSERT_SAME_TYPE(L::size_type, decltype(c.remove(3)));45#else46    ASSERT_SAME_TYPE(void, decltype(c.remove(3)));47    c.remove(3);48#endif49 50    assert(c == std::list<int>(a2, a2 + 3));51  }52  { // LWG issue #52653    int a1[] = {1, 2, 1, 3, 5, 8, 11};54    int a2[] = {2, 3, 5, 8, 11};55    std::list<int> c(a1, a1 + 7);56    c.remove(c.front());57    assert(c == std::list<int>(a2, a2 + 5));58  }59  {60    int a1[] = {1, 2, 1, 3, 5, 8, 11, 1};61    int a2[] = {2, 3, 5, 8, 11};62    std::list<S> c;63    for (int* ip = a1; ip < a1 + 8; ++ip)64      c.push_back(S(*ip));65#if TEST_STD_VER > 1766    assert(c.remove(c.front()) == 3);67#else68    c.remove(c.front());69#endif70    std::list<S>::const_iterator it = c.begin();71    for (int* ip = a2; ip < a2 + 5; ++ip, ++it) {72      assert(it != c.end());73      assert(*ip == it->get());74    }75    assert(it == c.end());76  }77  {78    typedef no_default_allocator<int> Alloc;79    typedef std::list<int, Alloc> List;80    int a1[] = {1, 2, 3, 4};81    int a2[] = {1, 2, 4};82    List c(a1, a1 + 4, Alloc::create());83#if TEST_STD_VER > 1784    assert(c.remove(3) == 1);85#else86    c.remove(3);87#endif88    assert(c == List(a2, a2 + 3, Alloc::create()));89  }90#if TEST_STD_VER >= 1191  {92    int a1[] = {1, 2, 3, 4};93    int a2[] = {1, 2, 4};94    std::list<int, min_allocator<int>> c(a1, a1 + 4);95#  if TEST_STD_VER > 1796    assert(c.remove(3) == 1);97#  else98    c.remove(3);99#  endif100    assert((c == std::list<int, min_allocator<int>>(a2, a2 + 3)));101  }102#endif103 104  return true;105}106 107int main(int, char**) {108  assert(test());109#if TEST_STD_VER >= 26110  static_assert(test());111#endif112 113  return 0;114}115