Mercurial > hg > CbC > CbC_llvm
comparison unittests/Support/Chrono.cpp @ 121:803732b1fca8
LLVM 5.0
author | kono |
---|---|
date | Fri, 27 Oct 2017 17:07:41 +0900 |
parents | 1172e4bd9c6f |
children | c2174574ed3a |
comparison
equal
deleted
inserted
replaced
120:1172e4bd9c6f | 121:803732b1fca8 |
---|---|
7 // | 7 // |
8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
9 | 9 |
10 #include "llvm/Support/Chrono.h" | 10 #include "llvm/Support/Chrono.h" |
11 #include "llvm/ADT/SmallVector.h" | 11 #include "llvm/ADT/SmallVector.h" |
12 #include "llvm/Support/FormatVariadic.h" | |
12 #include "gtest/gtest.h" | 13 #include "gtest/gtest.h" |
13 | 14 |
14 using namespace llvm; | 15 using namespace llvm; |
15 using namespace llvm::sys; | 16 using namespace llvm::sys; |
16 using namespace std::chrono; | 17 using namespace std::chrono; |
28 EXPECT_EQ(TP, toTimePoint(toTimeT(TP))); | 29 EXPECT_EQ(TP, toTimePoint(toTimeT(TP))); |
29 TP += hours(47); | 30 TP += hours(47); |
30 EXPECT_EQ(TP, toTimePoint(toTimeT(TP))); | 31 EXPECT_EQ(TP, toTimePoint(toTimeT(TP))); |
31 } | 32 } |
32 | 33 |
33 TEST(Chrono, StringConversion) { | 34 TEST(Chrono, TimePointFormat) { |
35 using namespace std::chrono; | |
36 struct tm TM {}; | |
37 TM.tm_year = 106; | |
38 TM.tm_mon = 0; | |
39 TM.tm_mday = 2; | |
40 TM.tm_hour = 15; | |
41 TM.tm_min = 4; | |
42 TM.tm_sec = 5; | |
43 TM.tm_isdst = -1; | |
44 TimePoint<> T = | |
45 system_clock::from_time_t(mktime(&TM)) + nanoseconds(123456789); | |
46 | |
47 // operator<< uses the format YYYY-MM-DD HH:MM:SS.NNNNNNNNN | |
34 std::string S; | 48 std::string S; |
35 raw_string_ostream OS(S); | 49 raw_string_ostream OS(S); |
36 OS << system_clock::now(); | 50 OS << T; |
51 EXPECT_EQ("2006-01-02 15:04:05.123456789", OS.str()); | |
37 | 52 |
38 // Do a basic sanity check on the output. | 53 // formatv default style matches operator<<. |
39 // The format we expect is YYYY-MM-DD HH:MM:SS.MMMUUUNNN | 54 EXPECT_EQ("2006-01-02 15:04:05.123456789", formatv("{0}", T).str()); |
40 StringRef Date, Time; | 55 // formatv supports strftime-style format strings. |
41 std::tie(Date, Time) = StringRef(OS.str()).split(' '); | 56 EXPECT_EQ("15:04:05", formatv("{0:%H:%M:%S}", T).str()); |
42 | 57 // formatv supports our strftime extensions for sub-second precision. |
43 SmallVector<StringRef, 3> Components; | 58 EXPECT_EQ("123", formatv("{0:%L}", T).str()); |
44 Date.split(Components, '-'); | 59 EXPECT_EQ("123456", formatv("{0:%f}", T).str()); |
45 ASSERT_EQ(3u, Components.size()); | 60 EXPECT_EQ("123456789", formatv("{0:%N}", T).str()); |
46 EXPECT_EQ(4u, Components[0].size()); | 61 // our extensions don't interfere with %% escaping. |
47 EXPECT_EQ(2u, Components[1].size()); | 62 EXPECT_EQ("%foo", formatv("{0:%%foo}", T).str()); |
48 EXPECT_EQ(2u, Components[2].size()); | |
49 | |
50 StringRef Sec, Nano; | |
51 std::tie(Sec, Nano) = Time.split('.'); | |
52 | |
53 Components.clear(); | |
54 Sec.split(Components, ':'); | |
55 ASSERT_EQ(3u, Components.size()); | |
56 EXPECT_EQ(2u, Components[0].size()); | |
57 EXPECT_EQ(2u, Components[1].size()); | |
58 EXPECT_EQ(2u, Components[2].size()); | |
59 EXPECT_EQ(9u, Nano.size()); | |
60 } | 63 } |
61 | 64 |
62 // Test that toTimePoint and toTimeT can be called with a arguments with varying | 65 // Test that toTimePoint and toTimeT can be called with a arguments with varying |
63 // precisions. | 66 // precisions. |
64 TEST(Chrono, ImplicitConversions) { | 67 TEST(Chrono, ImplicitConversions) { |
74 EXPECT_EQ(TimeT, toTimeT(Milli)); | 77 EXPECT_EQ(TimeT, toTimeT(Milli)); |
75 EXPECT_EQ(TimeT, toTimeT(Micro)); | 78 EXPECT_EQ(TimeT, toTimeT(Micro)); |
76 EXPECT_EQ(TimeT, toTimeT(Nano)); | 79 EXPECT_EQ(TimeT, toTimeT(Nano)); |
77 } | 80 } |
78 | 81 |
82 TEST(Chrono, DurationFormat) { | |
83 EXPECT_EQ("1 h", formatv("{0}", hours(1)).str()); | |
84 EXPECT_EQ("1 m", formatv("{0}", minutes(1)).str()); | |
85 EXPECT_EQ("1 s", formatv("{0}", seconds(1)).str()); | |
86 EXPECT_EQ("1 ms", formatv("{0}", milliseconds(1)).str()); | |
87 EXPECT_EQ("1 us", formatv("{0}", microseconds(1)).str()); | |
88 EXPECT_EQ("1 ns", formatv("{0}", nanoseconds(1)).str()); | |
89 | |
90 EXPECT_EQ("1 s", formatv("{0:+}", seconds(1)).str()); | |
91 EXPECT_EQ("1", formatv("{0:-}", seconds(1)).str()); | |
92 | |
93 EXPECT_EQ("1000 ms", formatv("{0:ms}", seconds(1)).str()); | |
94 EXPECT_EQ("1000000 us", formatv("{0:us}", seconds(1)).str()); | |
95 EXPECT_EQ("1000", formatv("{0:ms-}", seconds(1)).str()); | |
96 | |
97 EXPECT_EQ("1,000 ms", formatv("{0:+n}", milliseconds(1000)).str()); | |
98 EXPECT_EQ("0x3e8", formatv("{0:-x}", milliseconds(1000)).str()); | |
99 EXPECT_EQ("010", formatv("{0:-3}", milliseconds(10)).str()); | |
100 EXPECT_EQ("10,000", formatv("{0:ms-n}", seconds(10)).str()); | |
101 | |
102 EXPECT_EQ("1.00 s", formatv("{0}", duration<float>(1)).str()); | |
103 EXPECT_EQ("0.123 s", formatv("{0:+3}", duration<float>(0.123f)).str()); | |
104 EXPECT_EQ("1.230e-01 s", formatv("{0:+e3}", duration<float>(0.123f)).str()); | |
105 | |
106 typedef duration<float, std::ratio<60 * 60 * 24 * 14, 1000000>> | |
107 microfortnights; | |
108 EXPECT_EQ("1.00", formatv("{0:-}", microfortnights(1)).str()); | |
109 EXPECT_EQ("1209.60 ms", formatv("{0:ms}", microfortnights(1)).str()); | |
110 } | |
111 | |
79 } // anonymous namespace | 112 } // anonymous namespace |