xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stopsource/assign.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=(const 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 
20*b77e50e6SHui #include "test_macros.h"
21*b77e50e6SHui 
22*b77e50e6SHui static_assert(std::is_nothrow_copy_assignable_v<std::stop_source>);
23*b77e50e6SHui 
24*b77e50e6SHui int main(int, char**) {
25*b77e50e6SHui   // have two different states
26*b77e50e6SHui   {
27*b77e50e6SHui     std::stop_source ss1;
28*b77e50e6SHui     std::stop_source ss2;
29*b77e50e6SHui 
30*b77e50e6SHui     assert(ss1 != ss2);
31*b77e50e6SHui 
32*b77e50e6SHui     ss2.request_stop();
33*b77e50e6SHui 
34*b77e50e6SHui     assert(!ss1.stop_requested());
35*b77e50e6SHui     assert(ss2.stop_requested());
36*b77e50e6SHui 
37*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = ss2;
38*b77e50e6SHui     assert(&ref == &ss1);
39*b77e50e6SHui 
40*b77e50e6SHui     assert(ss1 == ss2);
41*b77e50e6SHui     assert(ss1.stop_requested());
42*b77e50e6SHui     assert(ss2.stop_requested());
43*b77e50e6SHui   }
44*b77e50e6SHui 
45*b77e50e6SHui   // this has no state
46*b77e50e6SHui   {
47*b77e50e6SHui     std::stop_source ss1{std::nostopstate};
48*b77e50e6SHui     std::stop_source ss2;
49*b77e50e6SHui 
50*b77e50e6SHui     assert(ss1 != ss2);
51*b77e50e6SHui 
52*b77e50e6SHui     ss2.request_stop();
53*b77e50e6SHui 
54*b77e50e6SHui     assert(!ss1.stop_requested());
55*b77e50e6SHui     assert(!ss1.stop_possible());
56*b77e50e6SHui     assert(ss2.stop_requested());
57*b77e50e6SHui     assert(ss2.stop_possible());
58*b77e50e6SHui 
59*b77e50e6SHui     std::same_as<std::stop_source&> decltype(auto) ref = ss1 = ss2;
60*b77e50e6SHui     assert(&ref == &ss1);
61*b77e50e6SHui 
62*b77e50e6SHui     assert(ss1 == ss2);
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 = 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 = 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 = 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