[libcxx-commits] [libcxx] [RFC][libc++][test] Improves testing diagnostics. (PR #88766)

Mark de Wever via libcxx-commits libcxx-commits at lists.llvm.org
Mon Apr 15 10:52:08 PDT 2024


https://github.com/mordante created https://github.com/llvm/llvm-project/pull/88766

Fixes: https://github.com/llvm/llvm-project/issues/88622

>From 67e2259d12daf44fe699b85b1205eac9a2845fc7 Mon Sep 17 00:00:00 2001
From: Mark de Wever <koraq at xs4all.nl>
Date: Mon, 15 Apr 2024 19:50:20 +0200
Subject: [PATCH] [RFC][libc++][test] Improves testing diagnostics.

Fixes: https://github.com/llvm/llvm-project/issues/88622
---
 libcxx/docs/TestingLibcxx.rst                 | 27 +++++++++++++++++++
 .../get_info.sys_time.rule_selection.pass.cpp | 14 +++++-----
 libcxx/test/support/concat_macros.h           |  5 ++++
 3 files changed, 39 insertions(+), 7 deletions(-)

diff --git a/libcxx/docs/TestingLibcxx.rst b/libcxx/docs/TestingLibcxx.rst
index 50ee9d4ee400b4..96ec42eb41bf5f 100644
--- a/libcxx/docs/TestingLibcxx.rst
+++ b/libcxx/docs/TestingLibcxx.rst
@@ -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
 ----------
diff --git a/libcxx/test/libcxx/time/time.zone/time.zone.timezone/time.zone.members/get_info.sys_time.rule_selection.pass.cpp b/libcxx/test/libcxx/time/time.zone/time.zone.timezone/time.zone.members/get_info.sys_time.rule_selection.pass.cpp
index accd5bcdc89e26..5977bfd1f11427 100644
--- a/libcxx/test/libcxx/time/time.zone/time.zone.timezone/time.zone.members/get_info.sys_time.rule_selection.pass.cpp
+++ b/libcxx/test/libcxx/time/time.zone/time.zone.timezone/time.zone.members/get_info.sys_time.rule_selection.pass.cpp
@@ -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 ***** *****/
@@ -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
 )");
 
diff --git a/libcxx/test/support/concat_macros.h b/libcxx/test/support/concat_macros.h
index d7340b8faf6e56..3f608d5bc21229 100644
--- a/libcxx/test/support/concat_macros.h
+++ b/libcxx/test/support/concat_macros.h
@@ -10,6 +10,7 @@
 #define TEST_SUPPORT_CONCAT_MACROS_H
 
 #include <cstdio>
+#include <source_location>
 #include <string>
 
 #include "assert_macros.h"
@@ -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; };
 



More information about the libcxx-commits mailing list