Skip to content

Commit

Permalink
[NFC][libc++][format] Improves tests.
Browse files Browse the repository at this point in the history
This is mainly to improve the readability of the tests. As a side
effects the tests run faster too,

Reviewed By: ldionne, #libc

Differential Revision: https://reviews.llvm.org/D135288
  • Loading branch information
mordante committed Oct 11, 2022
1 parent 4e5568d commit 6195bdb
Show file tree
Hide file tree
Showing 15 changed files with 3,718 additions and 3,693 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@
#include "test_macros.h"
#include "format_tests.h"
#include "string_literal.h"

auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
const Args&... args) constexpr {
std::basic_string<CharT> out = std::format(std::locale(), fmt.template sv<CharT>(), args...);
if constexpr (std::same_as<CharT, char>)
if (out != expected)
std::cerr << "\nFormat string " << fmt.template sv<char>() << "\nExpected output " << expected
<< "\nActual output " << out << '\n';
assert(out == expected);
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
#include "test_format_string.h"

auto test =
[]<class CharT, class... Args>(
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
std::basic_string<CharT> out = std::format(std::locale(), fmt, std::forward<Args>(args)...);
if constexpr (std::same_as<CharT, char>)
if (out != expected)
std::cerr << "\nFormat string " << fmt.get() << "\nExpected output " << expected << "\nActual output "
<< out << '\n';
assert(out == expected);
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
// After P2216 most exceptions thrown by std::format become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format.locale.verify.cpp
Expand Down
22 changes: 12 additions & 10 deletions libcxx/test/std/utilities/format/format.functions/format.pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,27 @@
#include "test_macros.h"
#include "format_tests.h"
#include "string_literal.h"
#include "test_format_string.h"

#ifndef TEST_HAS_NO_LOCALIZATION
# include <iostream>
# include <type_traits>
#endif

auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
const Args&... args) constexpr {
std::basic_string<CharT> out = std::format(fmt.template sv<CharT>(), args...);
auto test =
[]<class CharT, class... Args>(
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
#ifndef TEST_HAS_NO_LOCALIZATION
if constexpr (std::same_as<CharT, char>)
if (out != expected)
std::cerr << "\nFormat string " << fmt.template sv<char>() << "\nExpected output " << expected
<< "\nActual output " << out << '\n';
if constexpr (std::same_as<CharT, char>)
if (out != expected)
std::cerr << "\nFormat string " << fmt.get() << "\nExpected output " << expected << "\nActual output "
<< out << '\n';
#endif
assert(out == expected);
};
assert(out == expected);
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
// After P2216 most exceptions thrown by std::format become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format.verify.cpp
Expand Down
3,244 changes: 1,631 additions & 1,613 deletions libcxx/test/std/utilities/format/format.functions/format_tests.h

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,38 @@
#include "test_macros.h"
#include "format_tests.h"
#include "string_literal.h"
#include "test_format_string.h"

auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
const Args&... args) constexpr {
{
std::basic_string<CharT> out(expected.size(), CharT(' '));
auto it = std::format_to(out.begin(), std::locale(), fmt.template sv<CharT>(), args...);
assert(it == out.end());
assert(out == expected);
}
{
std::list<CharT> out;
std::format_to(std::back_inserter(out), std::locale(), fmt.template sv<CharT>(), args...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
std::vector<CharT> out;
std::format_to(std::back_inserter(out), std::locale(), fmt.template sv<CharT>(), args...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
assert(expected.size() < 4096 && "Update the size of the buffer.");
CharT out[4096];
CharT* it = std::format_to(out, std::locale(), fmt.template sv<CharT>(), args...);
assert(std::distance(out, it) == int(expected.size()));
// Convert to std::string since output contains '\0' for boolean tests.
assert(std::basic_string<CharT>(out, it) == expected);
}
};
auto test =
[]<class CharT, class... Args>(
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
{
std::basic_string<CharT> out(expected.size(), CharT(' '));
auto it = std::format_to(out.begin(), std::locale(), fmt, std::forward<Args>(args)...);
assert(it == out.end());
assert(out == expected);
}
{
std::list<CharT> out;
std::format_to(std::back_inserter(out), std::locale(), fmt, std::forward<Args>(args)...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
std::vector<CharT> out;
std::format_to(std::back_inserter(out), std::locale(), fmt, std::forward<Args>(args)...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
assert(expected.size() < 4096 && "Update the size of the buffer.");
CharT out[4096];
CharT* it = std::format_to(out, std::locale(), fmt, std::forward<Args>(args)...);
assert(std::distance(out, it) == int(expected.size()));
// Convert to std::string since output contains '\0' for boolean tests.
assert(std::basic_string<CharT>(out, it) == expected);
}
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
// After P2216 most exceptions thrown by std::format_to become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format_to.locale.verify.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,36 +28,38 @@
#include "test_macros.h"
#include "format_tests.h"
#include "string_literal.h"
#include "test_format_string.h"

auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
const Args&... args) constexpr {
{
std::basic_string<CharT> out(expected.size(), CharT(' '));
auto it = std::format_to(out.begin(), fmt.template sv<CharT>(), args...);
assert(it == out.end());
assert(out == expected);
}
{
std::list<CharT> out;
std::format_to(std::back_inserter(out), fmt.template sv<CharT>(), args...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
std::vector<CharT> out;
std::format_to(std::back_inserter(out), fmt.template sv<CharT>(), args...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
assert(expected.size() < 4096 && "Update the size of the buffer.");
CharT out[4096];
CharT* it = std::format_to(out, fmt.template sv<CharT>(), args...);
assert(std::distance(out, it) == int(expected.size()));
// Convert to std::string since output contains '\0' for boolean tests.
assert(std::basic_string<CharT>(out, it) == expected);
}
};
auto test =
[]<class CharT, class... Args>(
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
{
std::basic_string<CharT> out(expected.size(), CharT(' '));
auto it = std::format_to(out.begin(), fmt, std::forward<Args>(args)...);
assert(it == out.end());
assert(out == expected);
}
{
std::list<CharT> out;
std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
std::vector<CharT> out;
std::format_to(std::back_inserter(out), fmt, std::forward<Args>(args)...);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.end()));
}
{
assert(expected.size() < 4096 && "Update the size of the buffer.");
CharT out[4096];
CharT* it = std::format_to(out, fmt, std::forward<Args>(args)...);
assert(std::distance(out, it) == int(expected.size()));
// Convert to std::string since output contains '\0' for boolean tests.
assert(std::basic_string<CharT>(out, it) == expected);
}
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
// After P2216 most exceptions thrown by std::format become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format.verify.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,72 +33,74 @@
#include "test_macros.h"
#include "format_tests.h"
#include "string_literal.h"
#include "test_format_string.h"

auto test = []<string_literal fmt, class CharT, class... Args>(std::basic_string_view<CharT> expected,
const Args&... args) constexpr {
{
std::list<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 0, std::locale(), fmt.template sv<CharT>(), args...);
// To avoid signedness warnings make sure formatted_size uses the same type
// as result.size.
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt.template sv<CharT>(), args...);
auto test =
[]<class CharT, class... Args>(
std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) constexpr {
{
std::list<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 0, std::locale(), fmt, std::forward<Args>(args)...);
// To avoid signedness warnings make sure formatted_size uses the same type
// as result.size.
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...);

assert(result.size == formatted_size);
assert(out.empty());
}
{
std::vector<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 5, std::locale(), fmt.template sv<CharT>(), args...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt.template sv<CharT>(), args...);
diff_type size = std::min<diff_type>(5, formatted_size);
assert(result.size == formatted_size);
assert(out.empty());
}
{
std::vector<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 5, std::locale(), fmt, std::forward<Args>(args)...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...);
diff_type size = std::min<diff_type>(5, formatted_size);

assert(result.size == formatted_size);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.begin() + size));
}
{
std::basic_string<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 1000, std::locale(), fmt.template sv<CharT>(), args...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt.template sv<CharT>(), args...);
diff_type size = std::min<diff_type>(1000, formatted_size);
assert(result.size == formatted_size);
assert(std::equal(out.begin(), out.end(), expected.begin(), expected.begin() + size));
}
{
std::basic_string<CharT> out;
std::format_to_n_result result =
std::format_to_n(std::back_inserter(out), 1000, std::locale(), fmt, std::forward<Args>(args)...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...);
diff_type size = std::min<diff_type>(1000, formatted_size);

assert(result.size == formatted_size);
assert(out == expected.substr(0, size));
}
{
// Test the returned iterator.
std::basic_string<CharT> out(10, CharT(' '));
std::format_to_n_result result =
std::format_to_n(out.begin(), 10, std::locale(), fmt.template sv<CharT>(), args...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt.template sv<CharT>(), args...);
diff_type size = std::min<diff_type>(10, formatted_size);
assert(result.size == formatted_size);
assert(out == expected.substr(0, size));
}
{
// Test the returned iterator.
std::basic_string<CharT> out(10, CharT(' '));
std::format_to_n_result result =
std::format_to_n(out.begin(), 10, std::locale(), fmt, std::forward<Args>(args)...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...);
diff_type size = std::min<diff_type>(10, formatted_size);

assert(result.size == formatted_size);
assert(result.out == out.begin() + size);
assert(out.substr(0, size) == expected.substr(0, size));
}
{
static_assert(std::is_signed_v<std::iter_difference_t<CharT*>>,
"If the difference type isn't negative the test will fail "
"due to using a large positive value.");
CharT buffer[1] = {CharT(0)};
std::format_to_n_result result = std::format_to_n(buffer, -1, std::locale(), fmt.template sv<CharT>(), args...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt.template sv<CharT>(), args...);
assert(result.size == formatted_size);
assert(result.out == out.begin() + size);
assert(out.substr(0, size) == expected.substr(0, size));
}
{
static_assert(std::is_signed_v<std::iter_difference_t<CharT*>>,
"If the difference type isn't negative the test will fail "
"due to using a large positive value.");
CharT buffer[1] = {CharT(0)};
std::format_to_n_result result = std::format_to_n(buffer, -1, std::locale(), fmt, std::forward<Args>(args)...);
using diff_type = decltype(result.size);
diff_type formatted_size = std::formatted_size(std::locale(), fmt, std::forward<Args>(args)...);

assert(result.size == formatted_size);
assert(result.out == buffer);
assert(buffer[0] == CharT(0));
}
};
assert(result.size == formatted_size);
assert(result.out == buffer);
assert(buffer[0] == CharT(0));
}
};

auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, const Args&...) {
auto test_exception = []<class CharT, class... Args>(std::string_view, std::basic_string_view<CharT>, Args&&...) {
// After P2216 most exceptions thrown by std::format_to_n become ill-formed.
// Therefore this tests does nothing.
// A basic ill-formed test is done in format_to_n.locale.verify.cpp
Expand Down

0 comments on commit 6195bdb

Please sign in to comment.