1 //===- llvm/unittest/Support/Chrono.cpp - Time utilities tests ------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "llvm/Support/Chrono.h" 11 #include "llvm/ADT/SmallVector.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, StringConversion) { 34 std::string S; 35 raw_string_ostream OS(S); 36 OS << system_clock::now(); 37 38 // Do a basic sanity check on the output. 39 // The format we expect is YYYY-MM-DD HH:MM:SS.MMMUUUNNN 40 StringRef Date, Time; 41 std::tie(Date, Time) = StringRef(OS.str()).split(' '); 42 43 SmallVector<StringRef, 3> Components; 44 Date.split(Components, '-'); 45 ASSERT_EQ(3u, Components.size()); 46 EXPECT_EQ(4u, Components[0].size()); 47 EXPECT_EQ(2u, Components[1].size()); 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 } 61 62 // Test that toTimePoint and toTimeT can be called with a arguments with varying 63 // precisions. 64 TEST(Chrono, ImplicitConversions) { 65 std::time_t TimeT = 47; 66 TimePoint<seconds> Sec = toTimePoint(TimeT); 67 TimePoint<milliseconds> Milli = toTimePoint(TimeT); 68 TimePoint<microseconds> Micro = toTimePoint(TimeT); 69 TimePoint<nanoseconds> Nano = toTimePoint(TimeT); 70 EXPECT_EQ(Sec, Milli); 71 EXPECT_EQ(Sec, Micro); 72 EXPECT_EQ(Sec, Nano); 73 EXPECT_EQ(TimeT, toTimeT(Sec)); 74 EXPECT_EQ(TimeT, toTimeT(Milli)); 75 EXPECT_EQ(TimeT, toTimeT(Micro)); 76 EXPECT_EQ(TimeT, toTimeT(Nano)); 77 } 78 79 } // anonymous namespace 80