Skip to content

[RFC][libc++][test] Improves testing diagnostics. #88766

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions libcxx/docs/TestingLibcxx.rst
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,33 @@ additional headers.
Since it is not expected to add this to existing tests no effort was
taken to make it work in earlier language versions.

The Standard does not provide a ``operator<<`` for ``std::source_location`` this
header adds one. Using ``std::source_location`` allows printing messages that
are easier to trace to the origin. When the expected result is not unique, the
origin of the call in the source file can be written to the output. For example:

.. code-block:: cpp

void test(int input,
int expected,
std::source_location loc = std::source_location::current()) {
int result = ...;
TEST_REQUIRE(result == expected,
TEST_WRITE_CONCATENATED(loc,
"\nExpected output ",
expected,
"\nActual output ",
result,
'\n'));

}

int main(int, char**) {
test(10, 100);
test(20, 100);

return 0;
}

Test names
----------
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,19 +83,19 @@ static const std::chrono::tzdb& parse(std::string_view input) {
return std::chrono::time_point_cast<std::chrono::seconds>(static_cast<std::chrono::sys_days>(result)) + h + m + s;
}

static void assert_equal(const std::chrono::sys_info& lhs, const std::chrono::sys_info& rhs) {
static void assert_equal(const std::chrono::sys_info& lhs, const std::chrono::sys_info& rhs, std::source_location loc = std::source_location::current()) {
TEST_REQUIRE(lhs.begin == rhs.begin,
TEST_WRITE_CONCATENATED("\nBegin:\nExpected output ", lhs.begin, "\nActual output ", rhs.begin, '\n'));
TEST_WRITE_CONCATENATED(loc,"\nBegin:\nExpected output ", lhs.begin, "\nActual output ", rhs.begin, '\n'));
TEST_REQUIRE(lhs.end == rhs.end,
TEST_WRITE_CONCATENATED("\nEnd:\nExpected output ", lhs.end, "\nActual output ", rhs.end, '\n'));
TEST_WRITE_CONCATENATED(loc,"\nEnd:\nExpected output ", lhs.end, "\nActual output ", rhs.end, '\n'));
TEST_REQUIRE(
lhs.offset == rhs.offset,
TEST_WRITE_CONCATENATED("\nOffset:\nExpected output ", lhs.offset, "\nActual output ", rhs.offset, '\n'));
TEST_WRITE_CONCATENATED(loc,"\nOffset:\nExpected output ", lhs.offset, "\nActual output ", rhs.offset, '\n'));
TEST_REQUIRE(lhs.save == rhs.save,
TEST_WRITE_CONCATENATED("\nSave:\nExpected output ", lhs.save, "\nActual output ", rhs.save, '\n'));
TEST_WRITE_CONCATENATED(loc,"\nSave:\nExpected output ", lhs.save, "\nActual output ", rhs.save, '\n'));
TEST_REQUIRE(
lhs.abbrev == rhs.abbrev,
TEST_WRITE_CONCATENATED("\nAbbrev:\nExpected output ", lhs.abbrev, "\nActual output ", rhs.abbrev, '\n'));
TEST_WRITE_CONCATENATED(loc,"\nAbbrev:\nExpected output ", lhs.abbrev, "\nActual output ", rhs.abbrev, '\n'));
}

/***** ***** TESTS ***** *****/
Expand All @@ -109,7 +109,7 @@ int main(int, const char**) {
Z Test 0 - LMT 1900
0 Rule %s

R Rule 1900 max - Mar 1 2u 1 Summer
R Rule 1900 max - Mar 1 2u 1 sUMMER
R Rule 1900 max - Oct 1 2u 0 Winter
)");

Expand Down
5 changes: 5 additions & 0 deletions libcxx/test/support/concat_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#define TEST_SUPPORT_CONCAT_MACROS_H

#include <cstdio>
#include <source_location>
#include <string>

#include "assert_macros.h"
Expand Down Expand Up @@ -134,6 +135,10 @@ OutIt test_transcode(InIt first, InIt last, OutIt out_it) {
return out_it;
}

std::ostream& operator<<(std::ostream& os, const std::source_location& loc) {
return os << loc.file_name() << ':' << loc.line() << ':' << loc.column();
}

template <class T>
concept test_streamable = requires(std::stringstream& stream, T&& value) { stream << value; };

Expand Down