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