Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / test / std / thread / thread.jthread / swap.free.pass.cpp
blob01c8ccd659687af10d368aa305acb525ace1014a
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
14 // friend void swap(jthread& x, jthread& y) noexcept;
16 #include <cassert>
17 #include <thread>
18 #include <type_traits>
20 #include "make_test_thread.h"
21 #include "test_macros.h"
23 template <class T>
24 concept IsFreeSwapNoexcept = requires(T& a, T& b) {
25 { swap(a, b) } noexcept;
28 static_assert(IsFreeSwapNoexcept<std::jthread>);
30 int main(int, char**) {
31 // x is default constructed
33 std::jthread t1;
34 std::jthread t2 = support::make_test_jthread([] {});
35 const auto originalId2 = t2.get_id();
36 swap(t1, t2);
38 assert(t1.get_id() == originalId2);
39 assert(t2.get_id() == std::jthread::id());
42 // y is default constructed
44 std::jthread t1 = support::make_test_jthread([] {});
45 std::jthread t2{};
46 const auto originalId1 = t1.get_id();
47 swap(t1, t2);
49 assert(t1.get_id() == std::jthread::id());
50 assert(t2.get_id() == originalId1);
53 // both not default constructed
55 std::jthread t1 = support::make_test_jthread([] {});
56 std::jthread t2 = support::make_test_jthread([] {});
57 const auto originalId1 = t1.get_id();
58 const auto originalId2 = t2.get_id();
59 swap(t1, t2);
61 assert(t1.get_id() == originalId2);
62 assert(t2.get_id() == originalId1);
65 // both default constructed
67 std::jthread t1;
68 std::jthread t2;
69 swap(t1, t2);
71 assert(t1.get_id() == std::jthread::id());
72 assert(t2.get_id() == std::jthread::id());
75 return 0;