132 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// stop_source& operator=(stop_source&& rhs) noexcept;13 14#include <cassert>15#include <concepts>16#include <stop_token>17#include <type_traits>18#include <utility>19 20#include "test_macros.h"21 22static_assert(std::is_nothrow_move_assignable_v<std::stop_source>);23 24int main(int, char**) {25 // have two different states26 {27 std::stop_source ss1;28 std::stop_source ss2;29 30 assert(ss1 != ss2);31 32 ss2.request_stop();33 34 assert(!ss1.stop_requested());35 assert(ss2.stop_requested());36 37 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);38 assert(&ref == &ss1);39 40 assert(ss1.stop_requested());41 assert(!ss2.stop_possible());42 assert(!ss2.stop_requested());43 }44 45 // this has no state46 {47 std::stop_source ss1{std::nostopstate};48 std::stop_source ss2;49 50 assert(ss1 != ss2);51 52 ss2.request_stop();53 54 assert(!ss1.stop_requested());55 assert(!ss1.stop_possible());56 assert(ss2.stop_requested());57 assert(ss2.stop_possible());58 59 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);60 assert(&ref == &ss1);61 62 assert(ss1.stop_requested());63 assert(ss1.stop_possible());64 assert(!ss2.stop_requested());65 assert(!ss2.stop_possible());66 }67 68 // other has no state69 {70 std::stop_source ss1;71 std::stop_source ss2{std::nostopstate};72 73 assert(ss1 != ss2);74 75 ss1.request_stop();76 77 assert(ss1.stop_requested());78 assert(ss1.stop_possible());79 assert(!ss2.stop_requested());80 assert(!ss2.stop_possible());81 82 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);83 assert(&ref == &ss1);84 85 assert(ss1 == ss2);86 assert(!ss1.stop_requested());87 assert(!ss1.stop_possible());88 assert(!ss2.stop_requested());89 assert(!ss2.stop_possible());90 }91 92 // both no state93 {94 std::stop_source ss1{std::nostopstate};95 std::stop_source ss2{std::nostopstate};96 97 assert(ss1 == ss2);98 99 assert(!ss1.stop_requested());100 assert(!ss1.stop_possible());101 assert(!ss2.stop_requested());102 assert(!ss2.stop_possible());103 104 std::same_as<std::stop_source&> decltype(auto) ref = ss1 = std::move(ss2);105 assert(&ref == &ss1);106 107 assert(ss1 == ss2);108 assert(!ss1.stop_requested());109 assert(!ss1.stop_possible());110 assert(!ss2.stop_requested());111 assert(!ss2.stop_possible());112 }113 114 // self assignment115 {116 std::stop_source ss;117 auto& self = ss;118 119 assert(!ss.stop_requested());120 121 std::same_as<std::stop_source&> decltype(auto) ref = ss = std::move(self);122 assert(&ref == &ss);123 124 assert(!ss.stop_requested());125 126 ss.request_stop();127 assert(ss.stop_requested());128 }129 130 return 0;131}132