| 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 | #include "fmt/chrono.h" |
| 12 | #include "fmt/color.h" |
| 13 | #include "fmt/format.h" |
| 14 | #include "fmt/ostream.h" |
| 15 | #include "fmt/ranges.h" |
| 16 | #include "fmt/xchar.h" |
| 17 | |
| 18 | // Exercise the API to verify that everything we expect to can compile. |
| 19 | void test_format_api() { |
| 20 | (void)fmt::format(FMT_STRING("{}"), 42); |
| 21 | (void)fmt::format(FMT_STRING(L"{}"), 42); |
| 22 | (void)fmt::format(FMT_STRING("noop")); |
| 23 | |
| 24 | (void)fmt::to_string(42); |
| 25 | (void)fmt::to_wstring(42); |
| 26 | |
| 27 | std::vector<char> out; |
| 28 | fmt::format_to(std::back_inserter(out), FMT_STRING("{}"), 42); |
| 29 | |
| 30 | char buffer[4]; |
| 31 | fmt::format_to_n(buffer, 3, FMT_STRING("{}"), 12345); |
| 32 | |
| 33 | wchar_t wbuffer[4]; |
| 34 | fmt::format_to_n(wbuffer, 3, FMT_STRING(L"{}"), 12345); |
| 35 | } |
| 36 | |
| 37 | void test_chrono() { |
| 38 | (void)fmt::format(FMT_STRING("{}"), std::chrono::seconds(42)); |
| 39 | (void)fmt::format(FMT_STRING(L"{}"), std::chrono::seconds(42)); |
| 40 | } |
| 41 | |
| 42 | void test_text_style() { |
| 43 | fmt::print(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), "rgb(255,20,30)"); |
| 44 | (void)fmt::format(fg(fmt::rgb(255, 20, 30)), FMT_STRING("{}"), |
| 45 | "rgb(255,20,30)"); |
| 46 | |
| 47 | fmt::text_style ts = fg(fmt::rgb(255, 20, 30)); |
| 48 | std::string out; |
| 49 | fmt::format_to(std::back_inserter(out), ts, |
| 50 | FMT_STRING("rgb(255,20,30){}{}{}"), 1, 2, 3); |
| 51 | } |
| 52 | |
| 53 | void test_range() { |
| 54 | std::vector<char> hello = {'h', 'e', 'l', 'l', 'o'}; |
| 55 | (void)fmt::format(FMT_STRING("{}"), hello); |
| 56 | } |
| 57 | |
| 58 | int main() { |
| 59 | test_format_api(); |
| 60 | test_chrono(); |
| 61 | test_text_style(); |
| 62 | test_range(); |
| 63 | } |