[libcxx-commits] [libcxx] d23f609 - [libc++][test] Adds format string helper.
Mark de Wever via libcxx-commits
libcxx-commits at lists.llvm.org
Tue Sep 20 09:58:45 PDT 2022
Author: Mark de Wever
Date: 2022-09-20T18:58:37+02:00
New Revision: d23f609d9c33ea57aec1789c8eafeb4d6a4b1be1
URL: https://github.com/llvm/llvm-project/commit/d23f609d9c33ea57aec1789c8eafeb4d6a4b1be1
DIFF: https://github.com/llvm/llvm-project/commit/d23f609d9c33ea57aec1789c8eafeb4d6a4b1be1.diff
LOG: [libc++][test] Adds format string helper.
Update the formatter day tests to the new style.
Other test will be done separately.
Reviewed By: #libc, ldionne
Differential Revision: https://reviews.llvm.org/D134031
Added:
libcxx/test/support/test_format_string.h
Modified:
libcxx/test/std/time/time.syn/formatter.day.pass.cpp
libcxx/test/std/time/time.syn/formatter_tests.h
Removed:
################################################################################
diff --git a/libcxx/test/std/time/time.syn/formatter.day.pass.cpp b/libcxx/test/std/time/time.syn/formatter.day.pass.cpp
index 980b8bae1ddf8..75a0e1e97a1ca 100644
--- a/libcxx/test/std/time/time.syn/formatter.day.pass.cpp
+++ b/libcxx/test/std/time/time.syn/formatter.day.pass.cpp
@@ -39,60 +39,60 @@ static void test_no_chrono_specs() {
using namespace std::literals::chrono_literals;
// Valid day
- check.template operator()<"{}">(SV("01"), 1d);
- check.template operator()<"{:*^4}">(SV("*01*"), 1d);
- check.template operator()<"{:*>3}">(SV("*01"), 1d);
+ check(SV("01"), SV("{}"), 1d);
+ check(SV("*01*"), SV("{:*^4}"), 1d);
+ check(SV("*01"), SV("{:*>3}"), 1d);
// Invalid day
- check.template operator()<"{}">(SV("00 is not a valid day"), 0d);
- check.template operator()<"{:*^23}">(SV("*00 is not a valid day*"), 0d);
+ check(SV("00 is not a valid day"), SV("{}"), 0d);
+ check(SV("*00 is not a valid day*"), SV("{:*^23}"), 0d);
}
template <class CharT>
static void test_valid_values() {
using namespace std::literals::chrono_literals;
- constexpr string_literal fmt{"{:%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}"};
- constexpr string_literal lfmt{"{:L%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}"};
+ constexpr std::basic_string_view<CharT> fmt = SV("{:%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}");
+ constexpr std::basic_string_view<CharT> lfmt = SV("{:L%%d='%d'%t%%Od='%Od'%t%%e='%e'%t%%Oe='%Oe'%n}");
const std::locale loc(LOCALE_ja_JP_UTF_8);
std::locale::global(std::locale(LOCALE_fr_FR_UTF_8));
// Non localized output using C-locale
- check.template operator()<fmt>(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), 0d);
- check.template operator()<fmt>(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), 1d);
- check.template operator()<fmt>(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), 31d);
+ check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), fmt, 0d);
+ check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), fmt, 1d);
+ check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), fmt, 31d);
#if defined(_AIX)
- check.template operator()<fmt>(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), 255d);
+ check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d);
#else
- check.template operator()<fmt>(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), 255d);
+ check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d);
#endif
// Use the global locale (fr_FR)
- check.template operator()<lfmt>(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), 0d);
- check.template operator()<lfmt>(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), 1d);
- check.template operator()<lfmt>(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), 31d);
+ check(SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d);
+ check(SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt, 1d);
+ check(SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt, 31d);
#if defined(_AIX)
- check.template operator()<lfmt>(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), 255d);
+ check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), lfmt, 255d);
#else
- check.template operator()<lfmt>(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), 255d);
+ check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt, 255d);
#endif
// Use supplied locale (ja_JP). This locale has a
diff erent alternate on some platforms.
#if defined(__APPLE__) || defined(_AIX)
- lcheck.template operator()<lfmt>(loc, SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), 0d);
- lcheck.template operator()<lfmt>(loc, SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), 1d);
- lcheck.template operator()<lfmt>(loc, SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), 31d);
+ check(loc, SV("%d='00'\t%Od='00'\t%e=' 0'\t%Oe=' 0'\n"), lfmt, 0d);
+ check(loc, SV("%d='01'\t%Od='01'\t%e=' 1'\t%Oe=' 1'\n"), lfmt, 1d);
+ check(loc, SV("%d='31'\t%Od='31'\t%e='31'\t%Oe='31'\n"), lfmt, 31d);
# if defined(_AIX)
- check.template operator()<fmt>(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), 255d);
+ check(SV("%d='55'\t%Od='55'\t%e='55'\t%Oe='55'\n"), fmt, 255d);
# else
- check.template operator()<fmt>(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), 255d);
+ check(SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), fmt, 255d);
# endif
#else // defined(__APPLE__) || defined(_AIX)
- lcheck.template operator()<lfmt>(loc, SV("%d='00'\t%Od='〇'\t%e=' 0'\t%Oe='〇'\n"), 0d);
- lcheck.template operator()<lfmt>(loc, SV("%d='01'\t%Od='一'\t%e=' 1'\t%Oe='一'\n"), 1d);
- lcheck.template operator()<lfmt>(loc, SV("%d='31'\t%Od='三十一'\t%e='31'\t%Oe='三十一'\n"), 31d);
- lcheck.template operator()<lfmt>(loc, SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), 255d);
+ check(loc, SV("%d='00'\t%Od='〇'\t%e=' 0'\t%Oe='〇'\n"), lfmt, 0d);
+ check(loc, SV("%d='01'\t%Od='一'\t%e=' 1'\t%Oe='一'\n"), lfmt, 1d);
+ check(loc, SV("%d='31'\t%Od='三十一'\t%e='31'\t%Oe='三十一'\n"), lfmt, 31d);
+ check(loc, SV("%d='255'\t%Od='255'\t%e='255'\t%Oe='255'\n"), lfmt, 255d);
#endif // defined(__APPLE__) || defined(_AIX)
std::locale::global(std::locale::classic());
diff --git a/libcxx/test/std/time/time.syn/formatter_tests.h b/libcxx/test/std/time/time.syn/formatter_tests.h
index 3fe10e9c80f76..b66b2a856abea 100644
--- a/libcxx/test/std/time/time.syn/formatter_tests.h
+++ b/libcxx/test/std/time/time.syn/formatter_tests.h
@@ -10,6 +10,8 @@
#include "make_string.h"
#include "string_literal.h"
+#include "test_format_string.h"
+#include "test_macros.h"
#include <algorithm>
#include <cassert>
@@ -28,50 +30,52 @@ template <class CharT>
using format_context = std::format_context;
#endif
-inline constexpr auto check = []<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...);
+template <class CharT, class... Args>
+void check(std::basic_string_view<CharT> expected, test_format_string<CharT, Args...> fmt, Args&&... args) {
+ std::basic_string<CharT> out = std::format(fmt, std::forward<Args>(args)...);
if constexpr (std::same_as<CharT, char>)
if (out != expected)
- std::cerr << "\nFormat string " << fmt.template sv<CharT>() << "\nExpected output " << expected
- << "\nActual output " << out << '\n';
+ std::cerr << "\nFormat string " << fmt.get() << "\nExpected output " << expected << "\nActual output " << out
+ << '\n';
assert(out == expected);
-};
+}
-inline constexpr auto lcheck = []<string_literal fmt, class CharT, class... Args>(
- const std::locale& loc, std::basic_string_view<CharT> expected, const Args&... args) constexpr {
- std::basic_string<CharT> out = std::format(loc, fmt.template sv<CharT>(), args...);
+template <class CharT, class... Args>
+void check(const std::locale& loc,
+ std::basic_string_view<CharT> expected,
+ test_format_string<CharT, Args...> fmt,
+ Args&&... args) {
+ std::basic_string<CharT> out = std::format(loc, fmt, std::forward<Args>(args)...);
if constexpr (std::same_as<CharT, char>)
if (out != expected)
- std::cerr << "\nFormat string " << fmt.template sv<CharT>() << "\nExpected output " << expected
- << "\nActual output " << out << '\n';
+ std::cerr << "\nFormat string " << fmt.get() << "\nExpected output " << expected << "\nActual output " << out
+ << '\n';
assert(out == expected);
-};
+}
-inline constexpr auto check_exception =
- []<class CharT, class... Args>(
- [[maybe_unused]] std::string_view what,
- [[maybe_unused]] std::basic_string_view<CharT> fmt,
- [[maybe_unused]] const Args&... args) {
+template <class CharT, class... Args>
+void check_exception([[maybe_unused]] std::string_view what,
+ [[maybe_unused]] std::basic_string_view<CharT> fmt,
+ [[maybe_unused]] const Args&... args) {
#ifndef TEST_HAS_NO_EXCEPTIONS
- try {
- TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<format_context<CharT>>(args...));
- if constexpr (std::same_as<CharT, char>)
- std::cerr << "\nFormat string " << fmt << "\nDidn't throw an exception.\n";
- assert(false);
- } catch (const std::format_error& e) {
+ try {
+ TEST_IGNORE_NODISCARD std::vformat(fmt, std::make_format_args<format_context<CharT>>(args...));
+ if constexpr (std::same_as<CharT, char>)
+ std::cerr << "\nFormat string " << fmt << "\nDidn't throw an exception.\n";
+ assert(false);
+ } catch (const std::format_error& e) {
# if defined(_LIBCPP_VERSION)
- if constexpr (std::same_as<CharT, char>)
- if (e.what() != what)
- std::cerr << "\nFormat string " << fmt << "\nExpected exception " << what << "\nActual exception "
- << e.what() << '\n';
- assert(e.what() == what);
+ if constexpr (std::same_as<CharT, char>)
+ if (e.what() != what)
+ std::cerr << "\nFormat string " << fmt << "\nExpected exception " << what << "\nActual exception "
+ << e.what() << '\n';
+ assert(e.what() == what);
# endif
- return;
- }
- assert(false);
+ return;
+ }
+ assert(false);
#endif
- };
+}
template <class CharT, class T>
void check_invalid_type(const std::set<std::basic_string_view<CharT>>& valid_types,
diff --git a/libcxx/test/support/test_format_string.h b/libcxx/test/support/test_format_string.h
new file mode 100644
index 0000000000000..49e94c7dbfc78
--- /dev/null
+++ b/libcxx/test/support/test_format_string.h
@@ -0,0 +1,48 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef SUPPORT_TEST_FORMAT_STRING_HPP
+#define SUPPORT_TEST_FORMAT_STRING_HPP
+
+#include <concepts>
+#include <format>
+#include <type_traits>
+
+#include "test_macros.h"
+
+#if TEST_STD_VER < 20
+# error "The format header requires at least C++20"
+#endif
+
+// Wrapper for basic_format_string.
+//
+// This layer of indirection is used since it's not possible to use
+// std::basic_format_string<CharT, Args...> in the test function directly.
+//
+// In C++20 the basic-format-string was an exposition only type. In C++23 is
+// has been replaced with basic_format_string. Both libc++ and MSVC STL offer
+// it as an extension in C++20.
+#if TEST_STD_VER > 20 || defined(_LIBCPP_VERSION) || defined(_MSVC_STL_VERSION)
+# ifndef TEST_HAS_NO_WIDE_CHARACTERS
+template <class CharT, class... Args>
+using test_format_string =
+ std::conditional_t<std::same_as<CharT, char>, std::format_string<Args...>, std::wformat_string<Args...>>;
+# else
+template <class CharT, class... Args>
+using test_format_string = std::format_string<Args...>;
+# endif
+
+#else // TEST_STD_VER > 20 || defined(_LIBCPP_VERSION) || defined( _MSVC_STL_VERSION)
+
+# error
+"Please create a vendor specific version of the test typedef and file a review at https://reviews.llvm.org/"
+
+#endif // TEST_STD_VER > 20 || defined(_LIBCPP_VERSION) || defined( _MSVC_STL_VERSION)
+
+#endif // SUPPORT_TEST_FORMAT_STRING_HPP
More information about the libcxx-commits
mailing list