Please use GitHub pull requests for new patches. Avoid migrating existing patches. Phabricator shutdown timeline
Differential D110494 Diff 375090 libcxx/test/std/utilities/format/format.functions/vformat_to.pass.cpp
Changeset View
Changeset View
Standalone View
Standalone View
libcxx/test/std/utilities/format/format.functions/vformat_to.pass.cpp
Show All 12 Lines | |||||
// The tests write fixed size buffer, make sure it remains in bounds. | // The tests write fixed size buffer, make sure it remains in bounds. | ||||
// ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1 | // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DEBUG=1 | ||||
// UNSUPPORTED: libcxx-no-debug-mode | // UNSUPPORTED: libcxx-no-debug-mode | ||||
// <format> | // <format> | ||||
// template<class Out> | // template<class Out> | ||||
// Out vformat_to(Out out, string_view fmt, | // Out vformat_to(Out out, string_view fmt, format_args args); | ||||
// format_args_t<type_identity_t<Out>, char> args); | |||||
// template<class Out> | // template<class Out> | ||||
// Out vformat_to(Out out, wstring_view fmt, | // Out vformat_to(Out out, wstring_view fmt, wformat_args_t args); | ||||
// format_args_t<type_identity_t<Out>, wchar_t> args); | |||||
#include <format> | #include <format> | ||||
#include <algorithm> | #include <algorithm> | ||||
#include <cassert> | #include <cassert> | ||||
#include <list> | #include <list> | ||||
#include <vector> | #include <vector> | ||||
#include "test_macros.h" | #include "test_macros.h" | ||||
#include "format_tests.h" | #include "format_tests.h" | ||||
auto test = []<class CharT, class... Args>(std::basic_string<CharT> expected, | auto test = []<class CharT, class... Args>(std::basic_string<CharT> expected, | ||||
std::basic_string<CharT> fmt, | std::basic_string<CharT> fmt, | ||||
const Args&... args) { | const Args&... args) { | ||||
{ | { | ||||
std::basic_string<CharT> out(expected.size(), CharT(' ')); | std::basic_string<CharT> out(expected.size(), CharT(' ')); | ||||
auto it = std::vformat_to( | auto it = std::vformat_to(out.begin(), fmt, | ||||
out.begin(), fmt, | std::make_format_args<context_t<CharT>>(args...)); | ||||
std::make_format_args<std::basic_format_context< | |||||
typename std::basic_string<CharT>::iterator, CharT>>(args...)); | |||||
assert(it == out.end()); | assert(it == out.end()); | ||||
assert(out == expected); | assert(out == expected); | ||||
} | } | ||||
{ | { | ||||
std::list<CharT> out; | std::list<CharT> out; | ||||
std::vformat_to( | std::vformat_to(std::back_inserter(out), fmt, | ||||
std::back_inserter(out), fmt, | std::make_format_args<context_t<CharT>>(args...)); | ||||
std::make_format_args<std::basic_format_context< | |||||
std::back_insert_iterator<std::list<CharT>>, CharT>>(args...)); | |||||
assert( | assert( | ||||
std::equal(out.begin(), out.end(), expected.begin(), expected.end())); | std::equal(out.begin(), out.end(), expected.begin(), expected.end())); | ||||
} | } | ||||
{ | { | ||||
std::vector<CharT> out; | std::vector<CharT> out; | ||||
std::vformat_to( | std::vformat_to(std::back_inserter(out), fmt, | ||||
std::back_inserter(out), fmt, | std::make_format_args<context_t<CharT>>(args...)); | ||||
std::make_format_args<std::basic_format_context< | |||||
std::back_insert_iterator<std::vector<CharT>>, CharT>>(args...)); | |||||
assert( | assert( | ||||
std::equal(out.begin(), out.end(), expected.begin(), expected.end())); | std::equal(out.begin(), out.end(), expected.begin(), expected.end())); | ||||
} | } | ||||
{ | { | ||||
assert(expected.size() < 4096 && "Update the size of the buffer."); | assert(expected.size() < 4096 && "Update the size of the buffer."); | ||||
CharT out[4096]; | CharT out[4096]; | ||||
CharT* it = std::vformat_to( | CharT* it = std::vformat_to( | ||||
out, fmt, | out, fmt, std::make_format_args<context_t<CharT>>(args...)); | ||||
std::make_format_args<std::basic_format_context<CharT*, CharT>>( | |||||
args...)); | |||||
assert(std::distance(out, it) == int(expected.size())); | assert(std::distance(out, it) == int(expected.size())); | ||||
// Convert to std::string since output contains '\0' for boolean tests. | // Convert to std::string since output contains '\0' for boolean tests. | ||||
assert(std::basic_string<CharT>(out, it) == expected); | assert(std::basic_string<CharT>(out, it) == expected); | ||||
} | } | ||||
}; | }; | ||||
auto test_exception = []<class CharT, class... Args>( | auto test_exception = []<class CharT, class... Args>( | ||||
std::string_view what, std::basic_string<CharT> fmt, const Args&... args) { | std::string_view what, std::basic_string<CharT> fmt, const Args&... args) { | ||||
#ifndef TEST_HAS_NO_EXCEPTIONS | #ifndef TEST_HAS_NO_EXCEPTIONS | ||||
try { | try { | ||||
std::basic_string<CharT> out; | std::basic_string<CharT> out; | ||||
std::vformat_to( | std::vformat_to(std::back_inserter(out), fmt, | ||||
std::back_inserter(out), fmt, | std::make_format_args<context_t<CharT>>(args...)); | ||||
std::make_format_args<std::basic_format_context< | |||||
std::back_insert_iterator<std::basic_string<CharT>>, CharT>>( | |||||
args...)); | |||||
assert(false); | assert(false); | ||||
} catch (std::format_error& e) { | } catch (std::format_error& e) { | ||||
LIBCPP_ASSERT(e.what() == what); | LIBCPP_ASSERT(e.what() == what); | ||||
return; | return; | ||||
} | } | ||||
assert(false); | assert(false); | ||||
#else | #else | ||||
(void)what; | (void)what; | ||||
Show All 13 Lines |