brintos

brintos / llvm-project-archived public Read only

0
0
Text · 5.0 KiB · e28484e Raw
179 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++03 && !stdlib=libc++10// ADDITIONAL_COMPILE_FLAGS(has-fconstexpr-steps): -fconstexpr-steps=200000011 12// <algorithm>13 14// template<InputIterator InIter, typename OutIter>15//   requires OutputIterator<OutIter, RvalueOf<InIter::reference>::type>16//   OutIter17//   move(InIter first, InIter last, OutIter result);18 19#include <algorithm>20#include <cassert>21#include <iterator>22#include <memory>23#include <vector>24 25#include "MoveOnly.h"26#include "test_iterators.h"27#include "test_macros.h"28#include "type_algorithms.h"29 30class PaddedBase {31public:32  TEST_CONSTEXPR PaddedBase(std::int16_t a, std::int8_t b) : a_(a), b_(b) {}33 34  std::int16_t a_;35  std::int8_t b_;36};37 38class Derived : public PaddedBase {39public:40  TEST_CONSTEXPR Derived(std::int16_t a, std::int8_t b, std::int8_t c) : PaddedBase(a, b), c_(c) {}41 42  std::int8_t c_;43};44 45template <class InIter>46struct Test {47  template <class OutIter>48  TEST_CONSTEXPR_CXX20 void operator()() {49    const unsigned N = 1000;50    int ia[N]        = {};51    for (unsigned i = 0; i < N; ++i)52      ia[i] = i;53    int ib[N] = {0};54 55    OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));56    assert(base(r) == ib + N);57    for (unsigned i = 0; i < N; ++i)58      assert(ia[i] == ib[i]);59  }60};61 62struct TestOutIters {63  template <class InIter>64  TEST_CONSTEXPR_CXX20 void operator()() {65    types::for_each(66        types::concatenate_t<types::cpp17_input_iterator_list<int*>, types::type_list<cpp17_output_iterator<int*> > >(),67        Test<InIter>());68  }69};70 71template <class InIter>72struct Test1 {73  template <class OutIter>74  TEST_CONSTEXPR_CXX23 void operator()() {75    const unsigned N = 100;76    std::unique_ptr<int> ia[N];77    for (unsigned i = 0; i < N; ++i)78      ia[i].reset(new int(i));79    std::unique_ptr<int> ib[N];80 81    OutIter r = std::move(InIter(ia), InIter(ia + N), OutIter(ib));82    assert(base(r) == ib + N);83    for (unsigned i = 0; i < N; ++i)84      assert(*ib[i] == static_cast<int>(i));85  }86};87 88struct Test1OutIters {89  template <class InIter>90  TEST_CONSTEXPR_CXX23 void operator()() {91    types::for_each(types::concatenate_t<types::cpp17_input_iterator_list<std::unique_ptr<int>*>,92                                         types::type_list<cpp17_output_iterator<std::unique_ptr<int>*> > >(),93                    Test1<InIter>());94  }95};96 97TEST_CONSTEXPR_CXX20 bool test_vector_bool(std::size_t N) {98  std::vector<bool> v(N, false);99  for (std::size_t i = 0; i < N; i += 2)100    v[i] = true;101 102  { // Test move with aligned bytes103    std::vector<bool> in(v);104    std::vector<bool> out(N);105    std::move(in.begin(), in.end(), out.begin());106    assert(out == v);107  }108  { // Test move with unaligned bytes109    std::vector<bool> in(v);110    std::vector<bool> out(N);111    std::move(in.begin() + 4, in.end(), out.begin());112    for (std::size_t i = 0; i < N - 4; ++i)113      assert(v[i + 4] == out[i]);114  }115 116  return true;117}118 119TEST_CONSTEXPR_CXX20 bool test() {120  types::for_each(types::cpp17_input_iterator_list<int*>(), TestOutIters());121  if (TEST_STD_AT_LEAST_23_OR_RUNTIME_EVALUATED)122    types::for_each(types::cpp17_input_iterator_list<std::unique_ptr<int>*>(), Test1OutIters());123  { // Make sure that padding bits aren't copied124    Derived src(1, 2, 3);125    Derived dst(4, 5, 6);126    std::move(static_cast<PaddedBase*>(&src), static_cast<PaddedBase*>(&src) + 1, static_cast<PaddedBase*>(&dst));127    assert(dst.a_ == 1);128    assert(dst.b_ == 2);129    assert(dst.c_ == 6);130  }131  { // Make sure that overlapping ranges can be copied132    int a[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};133    std::move(a + 3, a + 10, a);134    int expected[] = {4, 5, 6, 7, 8, 9, 10, 8, 9, 10};135    assert(std::equal(a, a + 10, expected));136  }137  { // Make sure that the algorithm works with move-only types138    // When non-trivial139    {140      MoveOnly from[3] = {1, 2, 3};141      MoveOnly to[3]   = {};142      std::move(std::begin(from), std::end(from), std::begin(to));143      assert(to[0] == MoveOnly(1));144      assert(to[1] == MoveOnly(2));145      assert(to[2] == MoveOnly(3));146    }147    // When trivial148    {149      TrivialMoveOnly from[3] = {1, 2, 3};150      TrivialMoveOnly to[3]   = {};151      std::move(std::begin(from), std::end(from), std::begin(to));152      assert(to[0] == TrivialMoveOnly(1));153      assert(to[1] == TrivialMoveOnly(2));154      assert(to[2] == TrivialMoveOnly(3));155    }156  }157 158  { // Test vector<bool>::iterator optimization159    assert(test_vector_bool(8));160    assert(test_vector_bool(19));161    assert(test_vector_bool(32));162    assert(test_vector_bool(49));163    assert(test_vector_bool(64));164    assert(test_vector_bool(199));165    assert(test_vector_bool(256));166  }167 168  return true;169}170 171int main(int, char**) {172  test();173#if TEST_STD_VER >= 20174  static_assert(test());175#endif176 177  return 0;178}179