xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stopsource/move.copy.pass.cpp (revision 121ed5c1985356436d0040dbe81bca26992b1fae)
1*b77e50e6SHui //===----------------------------------------------------------------------===//
2*b77e50e6SHui //
3*b77e50e6SHui // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*b77e50e6SHui // See https://llvm.org/LICENSE.txt for license information.
5*b77e50e6SHui // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*b77e50e6SHui //
7*b77e50e6SHui //===----------------------------------------------------------------------===//
8*b77e50e6SHui //
9*b77e50e6SHui // UNSUPPORTED: no-threads
10*b77e50e6SHui // UNSUPPORTED: c++03, c++11, c++14, c++17
11*b77e50e6SHui // XFAIL: availability-synchronization_library-missing
12*b77e50e6SHui 
13*b77e50e6SHui // stop_source& operator=(stop_source&& rhs) noexcept;
14*b77e50e6SHui 
15*b77e50e6SHui #include <cassert>
16*b77e50e6SHui #include <concepts>
17*b77e50e6SHui #include <stop_token>
18*b77e50e6SHui #include <type_traits>
19*b77e50e6SHui #include <utility>
20*b77e50e6SHui 
21*b77e50e6SHui #include "test_macros.h"
22*b77e50e6SHui 
23*b77e50e6SHui static_assert(std::is_nothrow_move_assignable_v<std::stop_source>);
24*b77e50e6SHui 
25*b77e50e6SHui int main(int, char**) {
26*b77e50e6SHui   // have two different states
27*b77e50e6SHui   {
28*b77e50e6SHui     std::stop_source ss1;
29*b77e50e6SHui     std::stop_source ss2;
30*b77e50e6SHui 
31*b77e50e6SHui     assert(ss1 != ss2);
32*b77e50e6SHui 
33*b77e50e6SHui     ss2.request_stop();
34*b77e50e6SHui 
35*b77e50e6SHui     assert(!ss1.stop_requested());
36*b77e50e6SHui     assert(ss2.stop_requested());
37*b77e50e6SHui 
38*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
39*b77e50e6SHui     assert(&ref == &ss1);
40*b77e50e6SHui 
41*b77e50e6SHui     assert(ss1.stop_requested());
42*b77e50e6SHui     assert(!ss2.stop_possible());
43*b77e50e6SHui     assert(!ss2.stop_requested());
44*b77e50e6SHui   }
45*b77e50e6SHui 
46*b77e50e6SHui   // this has no state
47*b77e50e6SHui   {
48*b77e50e6SHui     std::stop_source ss1{std::nostopstate};
49*b77e50e6SHui     std::stop_source ss2;
50*b77e50e6SHui 
51*b77e50e6SHui     assert(ss1 != ss2);
52*b77e50e6SHui 
53*b77e50e6SHui     ss2.request_stop();
54*b77e50e6SHui 
55*b77e50e6SHui     assert(!ss1.stop_requested());
56*b77e50e6SHui     assert(!ss1.stop_possible());
57*b77e50e6SHui     assert(ss2.stop_requested());
58*b77e50e6SHui     assert(ss2.stop_possible());
59*b77e50e6SHui 
60*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
61*b77e50e6SHui     assert(&ref == &ss1);
62*b77e50e6SHui 
63*b77e50e6SHui     assert(ss1.stop_requested());
64*b77e50e6SHui     assert(ss1.stop_possible());
65*b77e50e6SHui     assert(!ss2.stop_requested());
66*b77e50e6SHui     assert(!ss2.stop_possible());
67*b77e50e6SHui   }
68*b77e50e6SHui 
69*b77e50e6SHui   // other has no state
70*b77e50e6SHui   {
71*b77e50e6SHui     std::stop_source ss1;
72*b77e50e6SHui     std::stop_source ss2{std::nostopstate};
73*b77e50e6SHui 
74*b77e50e6SHui     assert(ss1 != ss2);
75*b77e50e6SHui 
76*b77e50e6SHui     ss1.request_stop();
77*b77e50e6SHui 
78*b77e50e6SHui     assert(ss1.stop_requested());
79*b77e50e6SHui     assert(ss1.stop_possible());
80*b77e50e6SHui     assert(!ss2.stop_requested());
81*b77e50e6SHui     assert(!ss2.stop_possible());
82*b77e50e6SHui 
83*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
84*b77e50e6SHui     assert(&ref == &ss1);
85*b77e50e6SHui 
86*b77e50e6SHui     assert(ss1 == ss2);
87*b77e50e6SHui     assert(!ss1.stop_requested());
88*b77e50e6SHui     assert(!ss1.stop_possible());
89*b77e50e6SHui     assert(!ss2.stop_requested());
90*b77e50e6SHui     assert(!ss2.stop_possible());
91*b77e50e6SHui   }
92*b77e50e6SHui 
93*b77e50e6SHui   // both no state
94*b77e50e6SHui   {
95*b77e50e6SHui     std::stop_source ss1{std::nostopstate};
96*b77e50e6SHui     std::stop_source ss2{std::nostopstate};
97*b77e50e6SHui 
98*b77e50e6SHui     assert(ss1 == ss2);
99*b77e50e6SHui 
100*b77e50e6SHui     assert(!ss1.stop_requested());
101*b77e50e6SHui     assert(!ss1.stop_possible());
102*b77e50e6SHui     assert(!ss2.stop_requested());
103*b77e50e6SHui     assert(!ss2.stop_possible());
104*b77e50e6SHui 
105*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);
106*b77e50e6SHui     assert(&ref == &ss1);
107*b77e50e6SHui 
108*b77e50e6SHui     assert(ss1 == ss2);
109*b77e50e6SHui     assert(!ss1.stop_requested());
110*b77e50e6SHui     assert(!ss1.stop_possible());
111*b77e50e6SHui     assert(!ss2.stop_requested());
112*b77e50e6SHui     assert(!ss2.stop_possible());
113*b77e50e6SHui   }
114*b77e50e6SHui 
115*b77e50e6SHui   // self assignment
116*b77e50e6SHui   {
117*b77e50e6SHui     std::stop_source ss;
118*b77e50e6SHui     auto& self = ss;
119*b77e50e6SHui 
120*b77e50e6SHui     assert(!ss.stop_requested());
121*b77e50e6SHui 
122*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss = std::move(self);
123*b77e50e6SHui     assert(&ref == &ss);
124*b77e50e6SHui 
125*b77e50e6SHui     assert(!ss.stop_requested());
126*b77e50e6SHui 
127*b77e50e6SHui     ss.request_stop();
128*b77e50e6SHui     assert(ss.stop_requested());
129*b77e50e6SHui   }
130*b77e50e6SHui 
131*b77e50e6SHui   return 0;
132*b77e50e6SHui }
133