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