enforce-checks-test.cc 1.7 KB

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