Debian Codegraph @

libcxx/test/std/thread/thread.stoptoken/stoptoken/equals.pass.cpp

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