| 1 |
// { dg-do run { target c++11 } } |
| 2 |
// { dg-additional-options "-pthread" { target pthread } } |
| 3 |
// { dg-require-gthreads "" } |
| 4 |
|
| 5 |
#include <thread> |
| 6 |
|
| 7 |
#include <ostream> |
| 8 |
#include <format> |
| 9 |
|
| 10 |
void |
| 11 |
test_no_includes(std::ostream& out) |
| 12 |
{ |
| 13 |
std::thread::id i{}; |
| 14 |
// Check stream insertion works without including <sstream>: |
| 15 |
out << i; |
| 16 |
#if __cpp_lib_formatters >= 202302 |
| 17 |
// PR libstdc++/115099 - compilation error: format thread::id |
| 18 |
// Verify we can use std::thread::id with std::format without <sstream>: |
| 19 |
(void) std::format("{}", i); |
| 20 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 21 |
(void) std::format(L"{}", i); |
| 22 |
#endif |
| 23 |
#endif |
| 24 |
} |
| 25 |
|
| 26 |
#include <sstream> |
| 27 |
#include <testsuite_hooks.h> |
| 28 |
|
| 29 |
void |
| 30 |
test01() |
| 31 |
{ |
| 32 |
std::ostringstream out; |
| 33 |
std::thread::id i{}, j{}; |
| 34 |
out << i; |
| 35 |
auto s0 = out.str(); |
| 36 |
VERIFY( s0 == "thread::id of a non-executing thread" ); |
| 37 |
out.str(""); |
| 38 |
out << j; |
| 39 |
VERIFY( out.str() == s0 ); |
| 40 |
|
| 41 |
std::thread t1([]{}); |
| 42 |
j = t1.get_id(); |
| 43 |
out.str(""); |
| 44 |
out << j; |
| 45 |
auto s1 = out.str(); |
| 46 |
VERIFY( s1 != s0 ); |
| 47 |
auto j2 = j; |
| 48 |
out.str(""); |
| 49 |
out << j2; |
| 50 |
VERIFY( out.str() == s1 ); |
| 51 |
|
| 52 |
std::thread t2([]{}); |
| 53 |
j2 = t2.get_id(); |
| 54 |
out.str(""); |
| 55 |
out << j2; |
| 56 |
auto s2 = out.str(); |
| 57 |
VERIFY( s2 != s0 ); |
| 58 |
VERIFY( s2 != s1 ); |
| 59 |
|
| 60 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 61 |
std::wostringstream wout; |
| 62 |
wout << i; |
| 63 |
auto ws0 = wout.str(); |
| 64 |
wout.str(L""); |
| 65 |
wout << j; |
| 66 |
auto ws1 = wout.str(); |
| 67 |
wout.str(L""); |
| 68 |
wout << j2; |
| 69 |
auto ws2 = wout.str(); |
| 70 |
wout.str(L""); |
| 71 |
|
| 72 |
wout << s0.c_str() << ' ' << s1.c_str() << ' ' << s2.c_str(); |
| 73 |
VERIFY( wout.str() == (ws0 + L' ' + ws1 + L' ' + ws2) ); |
| 74 |
#endif |
| 75 |
|
| 76 |
t1.join(); |
| 77 |
t2.join(); |
| 78 |
} |
| 79 |
|
| 80 |
void |
| 81 |
test02() |
| 82 |
{ |
| 83 |
#if __cpp_lib_formatters >= 202302 |
| 84 |
|
| 85 |
static_assert( std::is_default_constructible_v<std::formatter<std::thread::id, char>> ); |
| 86 |
|
| 87 |
std::thread t1([]{}); |
| 88 |
std::thread t2([]{}); |
| 89 |
std::ostringstream out; |
| 90 |
std::thread::id i{}; |
| 91 |
std::thread::id j = t1.get_id(); |
| 92 |
std::thread::id k = t2.get_id(); |
| 93 |
out << i << ' ' << j << ' ' << k; |
| 94 |
VERIFY( std::format("{} {} {}", i, j, k) == out.str() ); |
| 95 |
|
| 96 |
out.str(""); |
| 97 |
out << j; |
| 98 |
auto s1 = out.str(); |
| 99 |
auto len = s1.size(); |
| 100 |
out.str(""); |
| 101 |
|
| 102 |
std::string s2; |
| 103 |
// with width |
| 104 |
s2 = std::format("{0:{1}}", j, len + 2); |
| 105 |
VERIFY( s2 == (" " + s1) ); |
| 106 |
// with align + width |
| 107 |
s2 = std::format("{0:>{1}}", j, len + 2); |
| 108 |
VERIFY( s2 == (" " + s1) ); |
| 109 |
s2 = std::format("{0:<{1}}", j, len + 2); |
| 110 |
VERIFY( s2 == (s1 + " ") ); |
| 111 |
// with fill-and-align + width |
| 112 |
s2 = std::format("{0:x^{1}}", j, len + 5); |
| 113 |
VERIFY( s2 == ("xx" + s1 + "xxx") ); |
| 114 |
|
| 115 |
#ifdef _GLIBCXX_USE_WCHAR_T |
| 116 |
static_assert( std::is_default_constructible_v<std::formatter<std::thread::id, wchar_t>> ); |
| 117 |
auto ws1 = std::format(L"{}", j); |
| 118 |
VERIFY( ws1.length() == len ); |
| 119 |
#endif |
| 120 |
|
| 121 |
t1.join(); |
| 122 |
t2.join(); |
| 123 |
#elif __cplusplus >= 202302L |
| 124 |
# error "Feature-test macro for formatters has wrong value in <thread>" |
| 125 |
#endif |
| 126 |
} |
| 127 |
|
| 128 |
int main() |
| 129 |
{ |
| 130 |
test01(); |
| 131 |
test02(); |
| 132 |
} |