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
11// <thread>
12
13// class thread::id
14
15// bool operator==(thread::id x, thread::id y) noexcept;
16// bool operator!=(thread::id x, thread::id y) noexcept;
17// bool operator< (thread::id x, thread::id y) noexcept;
18// bool operator<=(thread::id x, thread::id y) noexcept;
19// bool operator> (thread::id x, thread::id y) noexcept;
20// bool operator>=(thread::id x, thread::id y) noexcept;
21// strong_ordering operator<=>(thread::id x, thread::id y) noexcept;
22
23#include <thread>
24#include <cassert>
25
26#include "test_macros.h"
27#include "test_comparisons.h"
28
29int main(int, char**) {
30 AssertComparisonsAreNoexcept<std::thread::id>();
31 AssertComparisonsReturnBool<std::thread::id>();
32#if TEST_STD_VER > 17
33 AssertOrderAreNoexcept<std::thread::id>();
34 AssertOrderReturn<std::strong_ordering, std::thread::id>();
35#endif
36
37 std::thread::id id1;
38 std::thread::id id2;
39 std::thread::id id3 = std::this_thread::get_id();
40
41 // `id1` and `id2` should compare equal
42 assert(testComparisons(id1, id2, /*isEqual*/ true, /*isLess*/ false));
43#if TEST_STD_VER > 17
44 assert(testOrder(id1, id2, std::strong_ordering::equal));
45#endif
46
47 // Test `t1` and `t3` which are not equal
48 bool isLess = id1 < id3;
49 assert(testComparisons(id1, id3, /*isEqual*/ false, isLess));
50#if TEST_STD_VER > 17
51 assert(testOrder(id1, id3, isLess ? std::strong_ordering::less : std::strong_ordering::greater));
52#endif
53
54 // Regression tests for https://github.com/llvm/llvm-project/issues/56187
55 // libc++ previously declared the comparison operators as hidden friends
56 // which was non-conforming.
57 assert(std::operator==(id1, id2));
58#if TEST_STD_VER <= 17
59 assert(!std::operator!=(id1, id2));
60 assert(!std::operator<(id1, id2));
61 assert(std::operator<=(id1, id2));
62 assert(!std::operator>(id1, id2));
63 assert(std::operator>=(id1, id2));
64#else
65 assert(std::operator<=>(id1, id2) == std::strong_ordering::equal);
66#endif
67
68 return 0;
69}
70

source code of libcxx/test/std/thread/thread.threads/thread.thread.class/thread.thread.id/cmp.pass.cpp