xref: /llvm-project/libcxx/test/std/thread/thread.stoptoken/stoptoken/equals.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 // [[nodiscard]] bool operator==(const stop_token& lhs, const stop_token& rhs) noexcept;
14*b77e50e6SHui // Returns: true if lhs and rhs have ownership of the same stop state or if both lhs and rhs do not have ownership of a stop state; otherwise false.
15*b77e50e6SHui 
16*b77e50e6SHui // synthesized operator != also tested.
17*b77e50e6SHui 
18*b77e50e6SHui #include <cassert>
19*b77e50e6SHui #include <concepts>
20*b77e50e6SHui #include <stop_token>
21*b77e50e6SHui #include <type_traits>
22*b77e50e6SHui 
23*b77e50e6SHui #include "test_macros.h"
24*b77e50e6SHui 
25*b77e50e6SHui // LWG 3254 is related.
26*b77e50e6SHui template <class T>
27*b77e50e6SHui concept IsNoThrowEqualityComparable = requires(const T& t1, const T& t2) {
28*b77e50e6SHui   { t1 == t2 } noexcept;
29*b77e50e6SHui };
30*b77e50e6SHui 
31*b77e50e6SHui template <class T>
32*b77e50e6SHui concept IsNoThrowInequalityComparable = requires(const T& t1, const T& t2) {
33*b77e50e6SHui   { t1 != t2 } noexcept;
34*b77e50e6SHui };
35*b77e50e6SHui 
36*b77e50e6SHui static_assert(IsNoThrowEqualityComparable<std::stop_token>);
37*b77e50e6SHui static_assert(IsNoThrowInequalityComparable<std::stop_token>);
38*b77e50e6SHui 
39*b77e50e6SHui int main(int, char**) {
40*b77e50e6SHui   // both no state
41*b77e50e6SHui   {
42*b77e50e6SHui     const std::stop_token st1;
43*b77e50e6SHui     const std::stop_token st2;
44*b77e50e6SHui     assert(st1 == st2);
45*b77e50e6SHui     assert(!(st1 != st2));
46*b77e50e6SHui   }
47*b77e50e6SHui 
48*b77e50e6SHui   // only one has no state
49*b77e50e6SHui   {
50*b77e50e6SHui     std::stop_source ss;
51*b77e50e6SHui     const std::stop_token st1;
52*b77e50e6SHui     const auto st2 = ss.get_token();
53*b77e50e6SHui     assert(!(st1 == st2));
54*b77e50e6SHui     assert(st1 != st2);
55*b77e50e6SHui   }
56*b77e50e6SHui 
57*b77e50e6SHui   // both has states. same source
58*b77e50e6SHui   {
59*b77e50e6SHui     std::stop_source ss;
60*b77e50e6SHui     const auto st1 = ss.get_token();
61*b77e50e6SHui     const auto st2 = ss.get_token();
62*b77e50e6SHui     assert(st1 == st2);
63*b77e50e6SHui     assert(!(st1 != st2));
64*b77e50e6SHui   }
65*b77e50e6SHui 
66*b77e50e6SHui   // both has states. different sources with same states
67*b77e50e6SHui   {
68*b77e50e6SHui     std::stop_source ss1;
69*b77e50e6SHui     auto ss2 = ss1;
70*b77e50e6SHui     const auto st1 = ss1.get_token();
71*b77e50e6SHui     const auto st2 = ss2.get_token();
72*b77e50e6SHui     assert(st1 == st2);
73*b77e50e6SHui     assert(!(st1 != st2));
74*b77e50e6SHui   }
75*b77e50e6SHui 
76*b77e50e6SHui   // both has states. different sources with different states
77*b77e50e6SHui   {
78*b77e50e6SHui     std::stop_source ss1;
79*b77e50e6SHui     std::stop_source ss2;
80*b77e50e6SHui     const auto st1 = ss1.get_token();
81*b77e50e6SHui     const auto st2 = ss2.get_token();
82*b77e50e6SHui     assert(!(st1 == st2));
83*b77e50e6SHui     assert(st1 != st2);
84*b77e50e6SHui   }
85*b77e50e6SHui 
86*b77e50e6SHui   return 0;
87*b77e50e6SHui }
88