1 //===----------------------------------------------------------------------===//
2 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
3 // See https://llvm.org/LICENSE.txt for license information.
4 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
5 //
6 //===----------------------------------------------------------------------===//
7
8 // UNSUPPORTED: c++03, c++11, c++14, c++17
9 // UNSUPPORTED: libcpp-has-no-incomplete-format
10
11 // <format>
12
13 // A user defined formatter using
14 // template<class Context>
15 // class basic_format_arg<Context>::handle
16
17 #include <format>
18
19 #include <array>
20 #include <cassert>
21 #include <cmath>
22 #include <charconv>
23 #include <concepts>
24 #include <iterator>
25 #include <string>
26 #include <type_traits>
27
28 #include "test_format_context.h"
29 #include "test_macros.h"
30
31 enum class color { black, red, gold };
32 const char* color_names[] = {"black", "red", "gold"};
33
34 template <>
35 struct std::formatter<color> : std::formatter<const char*> {
formatstd::formatter36 auto format(color c, auto& ctx) const {
37 return formatter<const char*>::format(color_names[static_cast<int>(c)], ctx);
38 }
39 };
40
test(std::string expected,std::string_view fmt,color arg,std::size_t offset)41 void test(std::string expected, std::string_view fmt, color arg, std::size_t offset) {
42 auto parse_ctx = std::format_parse_context(fmt);
43 std::formatter<color, char> formatter;
44 static_assert(std::semiregular<decltype(formatter)>);
45
46 auto it = formatter.parse(parse_ctx);
47 assert(it == fmt.end() - offset);
48
49 std::string result;
50 auto out = std::back_inserter(result);
51 using FormatCtxT = std::basic_format_context<decltype(out), char>;
52
53 FormatCtxT format_ctx =
54 test_format_context_create<decltype(out), char>(out, std::make_format_args<FormatCtxT>(arg));
55 formatter.format(arg, format_ctx);
56 assert(result == expected);
57 }
58
test_termination_condition(std::string expected,std::string f,color arg)59 void test_termination_condition(std::string expected, std::string f, color arg) {
60 // The format-spec is valid if completely consumed or terminates at a '}'.
61 // The valid inputs all end with a '}'. The test is executed twice:
62 // - first with the terminating '}',
63 // - second consuming the entire input.
64 std::string_view fmt{f};
65 assert(fmt.back() == '}' && "Pre-condition failure");
66
67 test(expected, fmt, arg, 1);
68 fmt.remove_suffix(1);
69 test(expected, fmt, arg, 0);
70 }
71
main(int,char **)72 int main(int, char**) {
73 test_termination_condition("black", "}", color::black);
74 test_termination_condition("red", "}", color::red);
75 test_termination_condition("gold", "}", color::gold);
76
77 return 0;
78 }
79