#include <format>
#include <cassert>
#include <vector>
#include "make_string.h"
#include "test_macros.h"
#include "string_literal.h"
#include "test_format_string.h"
#ifndef TEST_HAS_NO_LOCALIZATION
# include <iostream>
# include <type_traits>
#endif
#define SV(S) MAKE_STRING_VIEW(CharT, S)
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)...);
#ifndef TEST_HAS_NO_LOCALIZATION
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);
};
template <class CharT>
static void test_single_code_point_fill() {
check(SV("* *"), SV("{:*^3}"), SV(" "));
check(SV("*~*"), SV("{:*^3}"), SV("~"));
check(SV("*\u00a1*"), SV("{:*^3}"), SV("\u00a1"));
check(SV("*\u07ff*"), SV("{:*^3}"), SV("\u07ff"));
check(SV("*\u0800*"), SV("{:*^3}"), SV("\u0800"));
check(SV("*\ufffd*"), SV("{:*^3}"), SV("\ufffd"));
check(SV("*\u1100*"), SV("{:*^4}"), SV("\u1100"));
check(SV("*\u115f*"), SV("{:*^4}"), SV("\u115f"));
check(SV("*\u2329*"), SV("{:*^4}"), SV("\u2329"));
check(SV("*\u232a*"), SV("{:*^4}"), SV("\u232a"));
check(SV("*\u2e80*"), SV("{:*^4}"), SV("\u2e80"));
check(SV("*\u303e*"), SV("{:*^4}"), SV("\u303e"));
check(SV("*\u3041*"), SV("{:*^4}"), SV("\u3041"));
check(SV("*\ua4d0*"), SV("{:*^3}"), SV("\ua4d0"));
check(SV("*\uac00*"), SV("{:*^4}"), SV("\uac00"));
check(SV("*\ud7a3*"), SV("{:*^4}"), SV("\ud7a3"));
check(SV("*\uf900*"), SV("{:*^4}"), SV("\uf900"));
check(SV("*\ufaff*"), SV("{:*^4}"), SV("\ufaff"));
check(SV("*\ufe10*"), SV("{:*^4}"), SV("\ufe10"));
check(SV("*\ufe19*"), SV("{:*^4}"), SV("\ufe19"));
check(SV("*\ufe30*"), SV("{:*^4}"), SV("\ufe30"));
check(SV("*\ufe70*"), SV("{:*^3}"), SV("\ufe70"));
check(SV("*\uff01*"), SV("{:*^4}"), SV("\uff01"));
check(SV("*\uff60*"), SV("{:*^4}"), SV("\uff60"));
check(SV("*\uffe0*"), SV("{:*^4}"), SV("\uffe0"));
check(SV("*\uffe6*"), SV("{:*^4}"), SV("\uffe6"));
check(SV("*\U00010000*"), SV("{:*^3}"), SV("\U00010000"));
check(SV("*\U0010FFFF*"), SV("{:*^3}"), SV("\U0010FFFF"));
check(SV("*\U0001f300*"), SV("{:*^4}"), SV("\U0001f300"));
check(SV("*\U0001f64f*"), SV("{:*^4}"), SV("\U0001f64f"));
check(SV("*\U0001f900*"), SV("{:*^4}"), SV("\U0001f900"));
check(SV("*\U0001f9ff*"), SV("{:*^4}"), SV("\U0001f9ff"));
check(SV("*\U00020000*"), SV("{:*^4}"), SV("\U00020000"));
check(SV("*\U0002fffd*"), SV("{:*^4}"), SV("\U0002fffd"));
check(SV("*\U00030000*"), SV("{:*^4}"), SV("\U00030000"));
check(SV("*\U0003fffd*"), SV("{:*^4}"), SV("\U0003fffd"));
}
template <class CharT>
static void test_single_code_point_truncate() {
check(SV("* *"), SV("{:*^3.1}"), SV(" "));
check(SV("*~*"), SV("{:*^3.1}"), SV("~"));
check(SV("*\u00a1*"), SV("{:*^3.1}"), SV("\u00a1"));
check(SV("*\u07ff*"), SV("{:*^3.1}"), SV("\u07ff"));
check(SV("*\u0800*"), SV("{:*^3.1}"), SV("\u0800"));
check(SV("*\ufffd*"), SV("{:*^3.1}"), SV("\ufffd"));
check(SV("***"), SV("{:*^3.1}"), SV("\u1100"));
check(SV("***"), SV("{:*^3.1}"), SV("\u115f"));
check(SV("***"), SV("{:*^3.1}"), SV("\u2329"));
check(SV("***"), SV("{:*^3.1}"), SV("\u232a"));
check(SV("***"), SV("{:*^3.1}"), SV("\u2e80"));
check(SV("***"), SV("{:*^3.1}"), SV("\u303e"));
check(SV("***"), SV("{:*^3.1}"), SV("\u3041"));
check(SV("*\ua4d0*"), SV("{:*^3.1}"), SV("\ua4d0"));
check(SV("***"), SV("{:*^3.1}"), SV("\uac00"));
check(SV("***"), SV("{:*^3.1}"), SV("\ud7a3"));
check(SV("***"), SV("{:*^3.1}"), SV("\uf900"));
check(SV("***"), SV("{:*^3.1}"), SV("\ufaff"));
check(SV("***"), SV("{:*^3.1}"), SV("\ufe10"));
check(SV("***"), SV("{:*^3.1}"), SV("\ufe19"));
check(SV("***"), SV("{:*^3.1}"), SV("\ufe30"));
check(SV("*\ufe70*"), SV("{:*^3.1}"), SV("\ufe70"));
check(SV("***"), SV("{:*^3.1}"), SV("\uff01"));
check(SV("***"), SV("{:*^3.1}"), SV("\uff60"));
check(SV("***"), SV("{:*^3.1}"), SV("\uffe0"));
check(SV("***"), SV("{:*^3.1}"), SV("\uffe6"));
check(SV("*\U00010000*"), SV("{:*^3.1}"), SV("\U00010000"));
check(SV("*\U0010FFFF*"), SV("{:*^3.1}"), SV("\U0010FFFF"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0001f300"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0001f64f"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0001f900"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0001f9ff"));
check(SV("***"), SV("{:*^3.1}"), SV("\U00020000"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0002fffd"));
check(SV("***"), SV("{:*^3.1}"), SV("\U00030000"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0003fffd"));
}
template <class CharT>
static void test_P1868() {
check(SV("*\u0041*"), SV("{:*^3}"), SV("\u0041"));
check(SV("*\u00c1*"), SV("{:*^3}"), SV("\u00c1"));
check(SV("*\u0041\u0301*"),
SV("{:*^3}"),
SV("\u0041\u0301"));
check(SV("*\u0132*"), SV("{:*^3}"), SV("\u0132"));
check(SV("*\u0394*"), SV("{:*^3}"), SV("\u0394"));
check(SV("*\u0429*"), SV("{:*^3}"), SV("\u0429"));
check(SV("*\u05d0*"), SV("{:*^3}"), SV("\u05d0"));
check(SV("*\u0634*"), SV("{:*^3}"), SV("\u0634"));
check(SV("*\u3009*"), SV("{:*^4}"), SV("\u3009"));
check(SV("*\u754c*"), SV("{:*^4}"), SV("\u754c"));
check(SV("*\U0001f921*"), SV("{:*^4}"), SV("\U0001f921"));
check(SV("*\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466*"),
SV("{:*^4}"),
SV("\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466"));
check(SV("*\u0041*"), SV("{:*^3.1}"), SV("\u0041"));
check(SV("*\u00c1*"), SV("{:*^3.1}"), SV("\u00c1"));
check(SV("*\u0041\u0301*"),
SV("{:*^3.1}"),
SV("\u0041\u0301"));
check(SV("*\u0132*"), SV("{:*^3.1}"), SV("\u0132"));
check(SV("*\u0394*"), SV("{:*^3.1}"), SV("\u0394"));
check(SV("*\u0429*"), SV("{:*^3.1}"), SV("\u0429"));
check(SV("*\u05d0*"), SV("{:*^3.1}"), SV("\u05d0"));
check(SV("*\u0634*"), SV("{:*^3.1}"), SV("\u0634"));
check(SV("***"), SV("{:*^3.1}"), SV("\u3009"));
check(SV("***"), SV("{:*^3.1}"), SV("\u754c"));
check(SV("***"), SV("{:*^3.1}"), SV("\U0001f921"));
check(SV("***"),
SV("{:*^3.1}"),
SV("\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466"));
check(SV("*\u0041*"), SV("{:*^3.2}"), SV("\u0041"));
check(SV("*\u00c1*"), SV("{:*^3.2}"), SV("\u00c1"));
check(SV("*\u0041\u0301*"),
SV("{:*^3.2}"),
SV("\u0041\u0301"));
check(SV("*\u0132*"), SV("{:*^3.2}"), SV("\u0132"));
check(SV("*\u0394*"), SV("{:*^3.2}"), SV("\u0394"));
check(SV("*\u0429*"), SV("{:*^3.2}"), SV("\u0429"));
check(SV("*\u05d0*"), SV("{:*^3.2}"), SV("\u05d0"));
check(SV("*\u0634*"), SV("{:*^3.2}"), SV("\u0634"));
check(SV("*\u3009*"), SV("{:*^4.2}"), SV("\u3009"));
check(SV("*\u754c*"), SV("{:*^4.2}"), SV("\u754c"));
check(SV("*\U0001f921*"), SV("{:*^4.2}"), SV("\U0001f921"));
check(SV("*\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466*"),
SV("{:*^4.2}"),
SV("\U0001f468\u200d\U0001F469\u200d\U0001F467\u200d\U0001F466"));
}
#ifdef _LIBCPP_VERSION
template <class CharT>
static void test_malformed_code_point() {
if constexpr (sizeof(CharT) == 1) {
check(SV("*ZZZZ\x8f*"), SV("{:*^7}"), SV("ZZZZ\x8f"));
check(SV("*ZZZZ\xcf*"), SV("{:*^7}"), SV("ZZZZ\xcf"));
check(SV("*ZZZZ\xef*"), SV("{:*^7}"), SV("ZZZZ\xef"));
check(SV("*ZZZZ\xff*"), SV("{:*^7}"), SV("ZZZZ\xff"));
check(SV("*ZZZZ\x8fZ*"), SV("{:*^8}"), SV("ZZZZ\x8fZ"));
check(SV("*ZZZZ\xcfZ*"), SV("{:*^8}"), SV("ZZZZ\xcfZ"));
check(SV("*ZZZZ\xefZ*"), SV("{:*^8}"), SV("ZZZZ\xefZ"));
check(SV("*ZZZZ\xffZ*"), SV("{:*^8}"), SV("ZZZZ\xffZ"));
check(SV("*ZZZZ\x8fZZ*"), SV("{:*^9}"), SV("ZZZZ\x8fZZ"));
check(SV("*ZZZZ\xcfZZ*"), SV("{:*^9}"), SV("ZZZZ\xcfZZ"));
check(SV("*ZZZZ\xefZZ*"), SV("{:*^9}"), SV("ZZZZ\xefZZ"));
check(SV("*ZZZZ\xffZZ*"), SV("{:*^9}"), SV("ZZZZ\xffZZ"));
check(SV("*ZZZZ\x8fZZZ*"), SV("{:*^10}"), SV("ZZZZ\x8fZZZ"));
check(SV("*ZZZZ\xcfZZZ*"), SV("{:*^10}"), SV("ZZZZ\xcfZZZ"));
check(SV("*ZZZZ\xefZZZ*"), SV("{:*^10}"), SV("ZZZZ\xefZZZ"));
check(SV("*ZZZZ\xffZZZ*"), SV("{:*^10}"), SV("ZZZZ\xffZZZ"));
check(SV("*ZZZZ\x8fZZZZ*"), SV("{:*^11}"), SV("ZZZZ\x8fZZZZ"));
check(SV("*ZZZZ\xcfZZZZ*"), SV("{:*^11}"), SV("ZZZZ\xcfZZZZ"));
check(SV("*ZZZZ\xefZZZZ*"), SV("{:*^11}"), SV("ZZZZ\xefZZZZ"));
check(SV("*ZZZZ\xffZZZZ*"), SV("{:*^11}"), SV("ZZZZ\xffZZZZ"));
check(SV("\xc2\x00"), SV("{}"), SV("\xc2\x00"));
check(SV("\xc2\x40"), SV("{}"), SV("\xc2\x40"));
check(SV("\xc2\xc0"), SV("{}"), SV("\xc2\xc0"));
check(SV("\xe0\x00\x80"), SV("{}"), SV("\xe0\x00\x80"));
check(SV("\xe0\x40\x80"), SV("{}"), SV("\xe0\x40\x80"));
check(SV("\xe0\xc0\x80"), SV("{}"), SV("\xe0\xc0\x80"));
check(SV("\xe0\x80\x00"), SV("{}"), SV("\xe0\x80\x00"));
check(SV("\xe0\x80\x40"), SV("{}"), SV("\xe0\x80\x40"));
check(SV("\xe0\x80\xc0"), SV("{}"), SV("\xe0\x80\xc0"));
check(SV("\xf0\x80\x80\x00"), SV("{}"), SV("\xf0\x80\x80\x00"));
check(SV("\xf0\x80\x80\x40"), SV("{}"), SV("\xf0\x80\x80\x40"));
check(SV("\xf0\x80\x80\xc0"), SV("{}"), SV("\xf0\x80\x80\xc0"));
check(SV("\xf0\x80\x00\x80"), SV("{}"), SV("\xf0\x80\x00\x80"));
check(SV("\xf0\x80\x40\x80"), SV("{}"), SV("\xf0\x80\x40\x80"));
check(SV("\xf0\x80\xc0\x80"), SV("{}"), SV("\xf0\x80\xc0\x80"));
check(SV("\xf0\x00\x80\x80"), SV("{}"), SV("\xf0\x00\x80\x80"));
check(SV("\xf0\x40\x80\x80"), SV("{}"), SV("\xf0\x40\x80\x80"));
check(SV("\xf0\xc0\x80\x80"), SV("{}"), SV("\xf0\xc0\x80\x80"));
check(SV("*ZZZZ\xef\xf5*"), SV("{:*^8}"), SV("ZZZZ\xef\xf5"));
check(SV("*ZZZZ\xef\xf5ZZZZ*"), SV("{:*^12}"), SV("ZZZZ\xef\xf5ZZZZ"));
check(SV("*ZZZZ\xff\xf5\xf5*"), SV("{:*^9}"), SV("ZZZZ\xff\xf5\xf5"));
check(SV("*ZZZZ\xff\xf5\xf5ZZZZ*"), SV("{:*^13}"), SV("ZZZZ\xff\xf5\xf5ZZZZ"));
} else if constexpr (sizeof(CharT) == 2) {
}
}
#endif
template <class CharT>
static void test() {
test_single_code_point_fill<CharT>();
test_single_code_point_truncate<CharT>();
test_P1868<CharT>();
#ifdef _LIBCPP_VERSION
test_malformed_code_point<CharT>();
#endif
}
int main(int, char**) {
test<char>();
#ifndef TEST_HAS_NO_WIDE_CHARACTERS
test<wchar_t>();
#endif
return 0;
}