Add PR check to suggest alternatives to using undef (#118506)
[llvm-project.git] / libcxx / test / std / algorithms / alg.modifying.operations / alg.move / contiguous_trivial_optimization.pass.cpp
blobfe0b7ad30379dd4ed16e41cb03fadaaab275ba8e
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-exception
6 //
7 //===----------------------------------------------------------------------===//
9 // UNSUPPORTED: c++03
11 // <algorithm>
13 // We optimize std::copy(_backward) and std::move(_backward) into memmove
14 // when the iterator is trivial and contiguous and the type in question
15 // is also trivially (copyable, movable). This test verifies that the
16 // optimization never eliminates an actually non-trivial copy or move.
18 #include <algorithm>
19 #include <iterator>
20 #include <cassert>
22 #include "test_macros.h"
23 #include "test_iterators.h"
25 struct TMBNTC {
26 int *p;
27 constexpr TMBNTC(int& copies) : p(&copies) {}
28 constexpr TMBNTC(const TMBNTC&) = default;
29 TEST_CONSTEXPR_CXX14 TMBNTC& operator=(TMBNTC&&) = default;
30 TEST_CONSTEXPR_CXX14 TMBNTC& operator=(const TMBNTC&) { ++*p; return *this; }
33 TEST_CONSTEXPR_CXX20 bool
34 test_trivial_moveassign_but_no_trivial_copyassign()
36 int copies = 0;
37 TMBNTC ia[] = { copies, copies, copies, copies };
38 TMBNTC ib[] = { copies, copies, copies, copies };
39 std::copy(ia, ia+4, ib);
40 assert(copies == 4);
41 copies = 0;
42 std::copy_backward(ia, ia+4, ib+4);
43 assert(copies == 4);
45 copies = 0;
46 std::copy(std::make_move_iterator(ia), std::make_move_iterator(ia+4), ib);
47 assert(copies == 0);
48 std::copy_backward(std::make_move_iterator(ia), std::make_move_iterator(ia+4), ib+4);
49 assert(copies == 0);
51 std::move(ia, ia+4, ib);
52 assert(copies == 0);
53 std::move_backward(ia, ia+4, ib+4);
54 assert(copies == 0);
56 return true;
59 struct TCBNTM {
60 int *p;
61 constexpr TCBNTM(int& moves) : p(&moves) {}
62 constexpr TCBNTM(const TCBNTM&) = default;
63 TEST_CONSTEXPR_CXX14 TCBNTM& operator=(TCBNTM&&) { ++*p; return *this; }
64 TEST_CONSTEXPR_CXX14 TCBNTM& operator=(const TCBNTM&) = default;
67 TEST_CONSTEXPR_CXX20 bool
68 test_trivial_copyassign_but_no_trivial_moveassign()
70 int moves = 0;
71 TCBNTM ia[] = { moves, moves, moves, moves };
72 TCBNTM ib[] = { moves, moves, moves, moves };
73 std::move(ia, ia+4, ib);
74 assert(moves == 4);
75 moves = 0;
76 std::move_backward(ia, ia+4, ib+4);
77 assert(moves == 4);
79 moves = 0;
80 std::copy(std::make_move_iterator(ia), std::make_move_iterator(ia+4), ib);
81 assert(moves == 4);
82 moves = 0;
83 std::copy_backward(std::make_move_iterator(ia), std::make_move_iterator(ia+4), ib+4);
84 assert(moves == 4);
86 moves = 0;
87 std::copy(ia, ia+4, ib);
88 assert(moves == 0);
89 std::copy_backward(ia, ia+4, ib+4);
90 assert(moves == 0);
92 return true;
95 int main(int, char**)
97 test_trivial_moveassign_but_no_trivial_copyassign();
98 test_trivial_copyassign_but_no_trivial_moveassign();
100 #if TEST_STD_VER > 17
101 static_assert(test_trivial_moveassign_but_no_trivial_copyassign());
102 static_assert(test_trivial_copyassign_but_no_trivial_moveassign());
103 #endif
105 return 0;