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: no-threads
10// UNSUPPORTED: libcpp-has-no-experimental-stop_token
11// UNSUPPORTED: c++03, c++11, c++14, c++17
12// XFAIL: availability-synchronization_library-missing
13
14// stop_source& operator=(stop_source&& rhs) noexcept;
15
16#include <cassert>
17#include <concepts>
18#include <stop_token>
19#include <type_traits>
20#include <utility>
21
22#include "test_macros.h"
23
24static_assert(std::is_nothrow_move_assignable_v<std::stop_source>);
25
26int main(int, char**) {
27 // have two different states
28 {
29 std::stop_source ss1;
30 std::stop_source ss2;
31
32 assert(ss1 != ss2);
33
34 ss2.request_stop();
35
36 assert(!ss1.stop_requested());
37 assert(ss2.stop_requested());
38
39 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
40 assert(&ref == &ss1);
41
42 assert(ss1.stop_requested());
43 assert(!ss2.stop_possible());
44 assert(!ss2.stop_requested());
45 }
46
47 // this has no state
48 {
49 std::stop_source ss1{std::nostopstate};
50 std::stop_source ss2;
51
52 assert(ss1 != ss2);
53
54 ss2.request_stop();
55
56 assert(!ss1.stop_requested());
57 assert(!ss1.stop_possible());
58 assert(ss2.stop_requested());
59 assert(ss2.stop_possible());
60
61 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
62 assert(&ref == &ss1);
63
64 assert(ss1.stop_requested());
65 assert(ss1.stop_possible());
66 assert(!ss2.stop_requested());
67 assert(!ss2.stop_possible());
68 }
69
70 // other has no state
71 {
72 std::stop_source ss1;
73 std::stop_source ss2{std::nostopstate};
74
75 assert(ss1 != ss2);
76
77 ss1.request_stop();
78
79 assert(ss1.stop_requested());
80 assert(ss1.stop_possible());
81 assert(!ss2.stop_requested());
82 assert(!ss2.stop_possible());
83
84 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
85 assert(&ref == &ss1);
86
87 assert(ss1 == ss2);
88 assert(!ss1.stop_requested());
89 assert(!ss1.stop_possible());
90 assert(!ss2.stop_requested());
91 assert(!ss2.stop_possible());
92 }
93
94 // both no state
95 {
96 std::stop_source ss1{std::nostopstate};
97 std::stop_source ss2{std::nostopstate};
98
99 assert(ss1 == ss2);
100
101 assert(!ss1.stop_requested());
102 assert(!ss1.stop_possible());
103 assert(!ss2.stop_requested());
104 assert(!ss2.stop_possible());
105
106 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
107 assert(&ref == &ss1);
108
109 assert(ss1 == ss2);
110 assert(!ss1.stop_requested());
111 assert(!ss1.stop_possible());
112 assert(!ss2.stop_requested());
113 assert(!ss2.stop_possible());
114 }
115
116 // self assignment
117 {
118 std::stop_source ss;
119 auto& self = ss;
120
121 assert(!ss.stop_requested());
122
123 std::same_as<std::stop_source&> decltype(auto) ref = ss = std::move(self);
124 assert(&ref == &ss);
125
126 assert(!ss.stop_requested());
127
128 ss.request_stop();
129 assert(ss.stop_requested());
130 }
131
132 return 0;
133}
134

source code of libcxx/test/std/thread/thread.stoptoken/stopsource/move.copy.pass.cpp