xref: /aosp_15_r20/external/fmtlib/test/enforce-checks-test.cc (revision 5c90c05cd622c0a81b57953a4d343e0e489f2e08)
1 // Formatting library for C++ - formatting library tests
2 //
3 // Copyright (c) 2012 - present, Victor Zverovich
4 // All rights reserved.
5 //
6 // For the license information refer to format.h.
7 
8 #include <iterator>
9 #include <vector>
10 
11 #define I 42  // simulate https://en.cppreference.com/w/c/numeric/complex/I
12 #include "fmt/chrono.h"
13 #include "fmt/color.h"
14 #include "fmt/format.h"
15 #include "fmt/ostream.h"
16 #include "fmt/ranges.h"
17 #include "fmt/xchar.h"
18 #undef I
19 
20 // Exercise the API to verify that everything we expect to can compile.
test_format_api()21 void test_format_api() {
22   (void)fmt::format(FMT_STRING("{}"), 42);
23   (void)fmt::format(FMT_STRING(L"{}"), 42);
24   (void)fmt::format(FMT_STRING("noop"));
25 
26   (void)fmt::to_string(42);
27   (void)fmt::to_wstring(42);
28 
29   std::vector<char> out;
30   fmt::format_to(std::back_inserter(out), FMT_STRING("{}"), 42);
31 
32   char buffer[4];
33   fmt::format_to_n(buffer, 3, FMT_STRING("{}"), 12345);
34 
35   wchar_t wbuffer[4];
36   fmt::format_to_n(wbuffer, 3, FMT_STRING(L"{}"), 12345);
37 }
38 
test_chrono()39 void test_chrono() {
40   (void)fmt::format(FMT_STRING("{}"), std::chrono::seconds(42));
41   (void)fmt::format(FMT_STRING(L"{}"), std::chrono::seconds(42));
42 }
43 
test_text_style()44 void test_text_style() {
45   fmt::print(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)");
46   (void)fmt::format(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"),
47                     "rgb(255,20,30)");
48 
49   fmt::text_style ts = fg(fmt::rgb(255, 20, 30));
50   std::string out;
51   fmt::format_to(std::back_inserter(out), ts,
52                  FMT_STRING("rgb(255,20,30){}{}{}"), 1, 2, 3);
53 }
54 
test_range()55 void test_range() {
56   std::vector<char> hello = {'h', 'e', 'l', 'l', 'o'};
57   (void)fmt::format(FMT_STRING("{}"), hello);
58 }
59 
main()60 int main() {
61   test_format_api();
62   test_chrono();
63   test_text_style();
64   test_range();
65 }
66