| 1 |
//===----------------------------------------------------------------------===// |
| 2 |
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 3 |
// See https://llvm.org/LICENSE.txt for license information. |
| 4 |
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 5 |
// |
| 6 |
//===----------------------------------------------------------------------===// |
| 7 |
|
| 8 |
// UNSUPPORTED: c++03, c++11, c++14, c++17, c++20 |
| 9 |
// UNSUPPORTED: no-threads |
| 10 |
|
| 11 |
// UNSUPPORTED: GCC-ALWAYS_INLINE-FIXME |
| 12 |
|
| 13 |
// <thread> |
| 14 |
|
| 15 |
// template<class charT> |
| 16 |
// struct formatter<thread::id, charT>; |
| 17 |
|
| 18 |
// template<class FormatContext> |
| 19 |
// typename FormatContext::iterator |
| 20 |
// format(const T& r, FormatContext& ctx) const; |
| 21 |
|
| 22 |
// Note this tests the basics of this function. It's tested in more detail in |
| 23 |
// the format functions test. |
| 24 |
|
| 25 |
#include <cassert> |
| 26 |
#include <concepts> |
| 27 |
#include <iterator> |
| 28 |
#include <thread> |
| 29 |
|
| 30 |
#include "test_format_context.h" |
| 31 |
#include "test_macros.h" |
| 32 |
#include "make_string.h" |
| 33 |
|
| 34 |
#define SV(S) MAKE_STRING_VIEW(CharT, S) |
| 35 |
|
| 36 |
template <class StringViewT> |
| 37 |
void test_format(StringViewT expected, std::thread::id arg) { |
| 38 |
using CharT = typename StringViewT::value_type; |
| 39 |
using String = std::basic_string<CharT>; |
| 40 |
using OutIt = std::back_insert_iterator<String>; |
| 41 |
using FormatCtxT = std::basic_format_context<OutIt, CharT>; |
| 42 |
|
| 43 |
const std::formatter<std::thread::id, CharT> formatter; |
| 44 |
|
| 45 |
String result; |
| 46 |
OutIt out = std::back_inserter(result); |
| 47 |
FormatCtxT format_ctx = test_format_context_create<OutIt, CharT>(out, std::make_format_args<FormatCtxT>(arg)); |
| 48 |
formatter.format(arg, format_ctx); |
| 49 |
assert(result == expected); |
| 50 |
} |
| 51 |
|
| 52 |
template <class CharT> |
| 53 |
void test_fmt() { |
| 54 |
#if !defined(__APPLE__) && !defined(__FreeBSD__) |
| 55 |
test_format(SV("0"), std::thread::id()); |
| 56 |
#else |
| 57 |
test_format(SV("0x0"), std::thread::id()); |
| 58 |
#endif |
| 59 |
} |
| 60 |
|
| 61 |
void test() { |
| 62 |
test_fmt<char>(); |
| 63 |
#ifndef TEST_HAS_NO_WIDE_CHARACTERS |
| 64 |
test_fmt<wchar_t>(); |
| 65 |
#endif |
| 66 |
} |
| 67 |
|
| 68 |
int main(int, char**) { |
| 69 |
test(); |
| 70 |
|
| 71 |
return 0; |
| 72 |
} |