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