Chrono.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- llvm/unittest/Support/Chrono.cpp - Time utilities tests ------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. #include "llvm/Support/Chrono.h"
  9. #include "llvm/ADT/SmallVector.h"
  10. #include "llvm/Support/FormatVariadic.h"
  11. #include "gtest/gtest.h"
  12. using namespace llvm;
  13. using namespace llvm::sys;
  14. using namespace std::chrono;
  15. namespace {
  16. TEST(Chrono, TimeTConversion) {
  17. EXPECT_EQ(time_t(0), toTimeT(toTimePoint(time_t(0))));
  18. EXPECT_EQ(time_t(1), toTimeT(toTimePoint(time_t(1))));
  19. EXPECT_EQ(time_t(47), toTimeT(toTimePoint(time_t(47))));
  20. TimePoint<> TP;
  21. EXPECT_EQ(TP, toTimePoint(toTimeT(TP)));
  22. TP += seconds(1);
  23. EXPECT_EQ(TP, toTimePoint(toTimeT(TP)));
  24. TP += hours(47);
  25. EXPECT_EQ(TP, toTimePoint(toTimeT(TP)));
  26. }
  27. TEST(Chrono, TimePointFormat) {
  28. using namespace std::chrono;
  29. struct tm TM {};
  30. TM.tm_year = 106;
  31. TM.tm_mon = 0;
  32. TM.tm_mday = 2;
  33. TM.tm_hour = 15;
  34. TM.tm_min = 4;
  35. TM.tm_sec = 5;
  36. TM.tm_isdst = -1;
  37. TimePoint<> T =
  38. system_clock::from_time_t(mktime(&TM)) + nanoseconds(123456789);
  39. // operator<< uses the format YYYY-MM-DD HH:MM:SS.NNNNNNNNN
  40. std::string S;
  41. raw_string_ostream OS(S);
  42. OS << T;
  43. EXPECT_EQ("2006-01-02 15:04:05.123456789", OS.str());
  44. // formatv default style matches operator<<.
  45. EXPECT_EQ("2006-01-02 15:04:05.123456789", formatv("{0}", T).str());
  46. // formatv supports strftime-style format strings.
  47. EXPECT_EQ("15:04:05", formatv("{0:%H:%M:%S}", T).str());
  48. // formatv supports our strftime extensions for sub-second precision.
  49. EXPECT_EQ("123", formatv("{0:%L}", T).str());
  50. EXPECT_EQ("123456", formatv("{0:%f}", T).str());
  51. EXPECT_EQ("123456789", formatv("{0:%N}", T).str());
  52. // our extensions don't interfere with %% escaping.
  53. EXPECT_EQ("%foo", formatv("{0:%%foo}", T).str());
  54. }
  55. // Test that toTimePoint and toTimeT can be called with a arguments with varying
  56. // precisions.
  57. TEST(Chrono, ImplicitConversions) {
  58. std::time_t TimeT = 47;
  59. TimePoint<seconds> Sec = toTimePoint(TimeT);
  60. TimePoint<milliseconds> Milli = toTimePoint(TimeT);
  61. TimePoint<microseconds> Micro = toTimePoint(TimeT);
  62. TimePoint<nanoseconds> Nano = toTimePoint(TimeT);
  63. EXPECT_EQ(Sec, Milli);
  64. EXPECT_EQ(Sec, Micro);
  65. EXPECT_EQ(Sec, Nano);
  66. EXPECT_EQ(TimeT, toTimeT(Sec));
  67. EXPECT_EQ(TimeT, toTimeT(Milli));
  68. EXPECT_EQ(TimeT, toTimeT(Micro));
  69. EXPECT_EQ(TimeT, toTimeT(Nano));
  70. }
  71. TEST(Chrono, DurationFormat) {
  72. EXPECT_EQ("1 h", formatv("{0}", hours(1)).str());
  73. EXPECT_EQ("1 m", formatv("{0}", minutes(1)).str());
  74. EXPECT_EQ("1 s", formatv("{0}", seconds(1)).str());
  75. EXPECT_EQ("1 ms", formatv("{0}", milliseconds(1)).str());
  76. EXPECT_EQ("1 us", formatv("{0}", microseconds(1)).str());
  77. EXPECT_EQ("1 ns", formatv("{0}", nanoseconds(1)).str());
  78. EXPECT_EQ("1 s", formatv("{0:+}", seconds(1)).str());
  79. EXPECT_EQ("1", formatv("{0:-}", seconds(1)).str());
  80. EXPECT_EQ("1000 ms", formatv("{0:ms}", seconds(1)).str());
  81. EXPECT_EQ("1000000 us", formatv("{0:us}", seconds(1)).str());
  82. EXPECT_EQ("1000", formatv("{0:ms-}", seconds(1)).str());
  83. EXPECT_EQ("1,000 ms", formatv("{0:+n}", milliseconds(1000)).str());
  84. EXPECT_EQ("0x3e8", formatv("{0:-x}", milliseconds(1000)).str());
  85. EXPECT_EQ("010", formatv("{0:-3}", milliseconds(10)).str());
  86. EXPECT_EQ("10,000", formatv("{0:ms-n}", seconds(10)).str());
  87. EXPECT_EQ("1.00 s", formatv("{0}", duration<float>(1)).str());
  88. EXPECT_EQ("0.123 s", formatv("{0:+3}", duration<float>(0.123f)).str());
  89. EXPECT_EQ("1.230e-01 s", formatv("{0:+e3}", duration<float>(0.123f)).str());
  90. typedef duration<float, std::ratio<60 * 60 * 24 * 14, 1000000>>
  91. microfortnights;
  92. EXPECT_EQ("1.00", formatv("{0:-}", microfortnights(1)).str());
  93. EXPECT_EQ("1209.60 ms", formatv("{0:ms}", microfortnights(1)).str());
  94. }
  95. } // anonymous namespace