xref: /llvm-project/libcxx/test/std/ranges/ranges_robust_against_no_unique_address.pass.cpp (revision 120b0bfbf0bade430fa9b19d78025ccd1d6148d0)
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 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++03, c++11, c++14, c++17
10 
11 // Test that views that use __movable_box do not overwrite overlapping subobjects.
12 // https://github.com/llvm/llvm-project/issues/70506
13 
14 #include <cassert>
15 #include <ranges>
16 
17 #include "test_macros.h"
18 
19 struct Pred {
20   alignas(128) bool a{};
21 
22   Pred() noexcept            = default;
23   Pred(const Pred&) noexcept = default;
24   Pred(Pred&&) noexcept      = default;
25 
26   Pred& operator=(const Pred&) = delete;
27   Pred& operator=(Pred&&)      = delete;
28 
operator ()Pred29   constexpr bool operator()(const auto&...) const { return true; }
30 };
31 
32 struct View : std::ranges::view_base {
beginView33   constexpr int* begin() const { return nullptr; }
endView34   constexpr int* end() const { return nullptr; }
35 };
36 
37 template <class View>
38 struct S {
39   [[no_unique_address]] View view{};
40   char c = 42;
41 };
42 
43 template <class View>
testOne()44 constexpr void testOne() {
45   S<View> s1;
46   assert(s1.c == 42);
47   s1.view = View{};
48   assert(s1.c == 42);
49 }
50 
test()51 constexpr bool test() {
52   testOne<std::ranges::transform_view<View, Pred>>();
53   testOne<std::ranges::filter_view<View, Pred>>();
54   testOne<std::ranges::drop_while_view<View, Pred>>();
55   testOne<std::ranges::take_while_view<View, Pred>>();
56   testOne<std::ranges::single_view<Pred>>();
57 
58 #if TEST_STD_VER >= 23
59   testOne<std::ranges::chunk_by_view<View, Pred>>();
60   testOne<std::ranges::repeat_view<Pred>>();
61 #endif
62   return true;
63 }
64 
main(int,char **)65 int main(int, char**) {
66   static_assert(test());
67   test();
68 
69   return 0;
70 }
71