]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/testsuite/30_threads/stop_token/stop_source.cc
libstdc++: Remove dg-options "-std=gnu++20" from 30_threads tests
[thirdparty/gcc.git] / libstdc++-v3 / testsuite / 30_threads / stop_token / stop_source.cc
1 // Copyright (C) 2019-2023 Free Software Foundation, Inc.
2 //
3 // This file is part of the GNU ISO C++ Library. This library is free
4 // software; you can redistribute it and/or modify it under the
5 // terms of the GNU General Public License as published by the
6 // Free Software Foundation; either version 3, or (at your option)
7 // any later version.
8
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13
14 // You should have received a copy of the GNU General Public License along
15 // with this library; see the file COPYING3. If not see
16 // <http://www.gnu.org/licenses/>.
17
18 // { dg-add-options libatomic }
19 // { dg-do run { target c++20 } }
20
21 #include <stop_token>
22 #include <testsuite_hooks.h>
23
24 void
25 test01()
26 {
27 std::stop_source ssrc;
28 VERIFY( ssrc.stop_possible() );
29 VERIFY( !ssrc.stop_requested() );
30 VERIFY( ssrc == ssrc );
31 VERIFY( !(ssrc != ssrc) );
32
33 std::stop_source copy(ssrc);
34 VERIFY( copy.stop_possible() );
35 VERIFY( !copy.stop_requested() );
36 VERIFY( ssrc.stop_possible() );
37 VERIFY( !ssrc.stop_requested() );
38 VERIFY( copy == ssrc );
39 VERIFY( !(copy != ssrc) );
40 VERIFY( ssrc == ssrc );
41 VERIFY( !(ssrc != ssrc) );
42
43 std::stop_source move(std::move(ssrc));
44 VERIFY( move.stop_possible() );
45 VERIFY( !move.stop_requested() );
46 VERIFY( copy.stop_possible() );
47 VERIFY( !copy.stop_requested() );
48 VERIFY( !ssrc.stop_possible() );
49 VERIFY( !ssrc.stop_requested() );
50 VERIFY( !(move == ssrc) );
51 VERIFY( move != ssrc );
52 VERIFY( ssrc == ssrc );
53 VERIFY( !(ssrc != ssrc) );
54 VERIFY( move == copy );
55 VERIFY( !(move != copy) );
56 VERIFY( !(copy == ssrc) );
57 VERIFY( copy != ssrc );
58 }
59
60 void
61 test02()
62 {
63 // stop_source(nostopstate_t) constructor is explicit:
64 static_assert(!std::is_convertible_v<std::nostopstate_t, std::stop_source>);
65
66 std::stop_source ssrc(std::nostopstate);
67 VERIFY( !ssrc.stop_possible() );
68 VERIFY( !ssrc.stop_requested() );
69 VERIFY( ssrc == ssrc );
70 VERIFY( !(ssrc != ssrc) );
71
72 std::stop_source copy(ssrc);
73 VERIFY( !copy.stop_possible() );
74 VERIFY( !copy.stop_requested() );
75 VERIFY( !ssrc.stop_possible() );
76 VERIFY( !ssrc.stop_requested() );
77 VERIFY( copy == ssrc );
78 VERIFY( !(copy != ssrc) );
79 VERIFY( ssrc == ssrc );
80 VERIFY( !(ssrc != ssrc) );
81
82 std::stop_source move(std::move(ssrc));
83 VERIFY( !move.stop_possible() );
84 VERIFY( !move.stop_requested() );
85 VERIFY( !copy.stop_possible() );
86 VERIFY( !copy.stop_requested() );
87 VERIFY( !ssrc.stop_possible() );
88 VERIFY( !ssrc.stop_requested() );
89 VERIFY( move == ssrc );
90 VERIFY( !(move != ssrc) );
91 VERIFY( ssrc == ssrc );
92 VERIFY( !(ssrc != ssrc) );
93 VERIFY( move == copy );
94 VERIFY( !(move != copy) );
95 VERIFY( copy == ssrc );
96 VERIFY( !(copy != ssrc) );
97 }
98
99 void
100 test03()
101 {
102 std::stop_source s1;
103 std::stop_source copy(s1);
104 s1.request_stop();
105 std::stop_source s2(std::nostopstate);
106 VERIFY( s1 != s2 );
107
108 s1.swap(s2);
109 VERIFY( !s1.stop_possible() );
110 VERIFY( !s1.stop_requested() );
111 VERIFY( s2.stop_possible() );
112 VERIFY( s2.stop_requested() );
113 VERIFY( s1 != s2 );
114 VERIFY( s2 == copy );
115
116 swap(s1, s2);
117 VERIFY( s1.stop_possible() );
118 VERIFY( s1.stop_requested() );
119 VERIFY( !s2.stop_possible() );
120 VERIFY( !s2.stop_requested() );
121 VERIFY( s1 == copy );
122 }
123
124 int main()
125 {
126 test01();
127 test02();
128 test03();
129 }