Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #include "perfetto/base/time.h"
     18 
     19 #include "gtest/gtest.h"
     20 
     21 namespace perfetto {
     22 namespace base {
     23 namespace {
     24 
     25 TEST(TimeTest, Conversions) {
     26   TimeMillis ms = GetWallTimeMs();
     27   TimeNanos ns = GetWallTimeNs();
     28   EXPECT_NEAR(ms.count(), ns.count() / 1000000, 1000);
     29 
     30   {
     31     struct timespec ts = ToPosixTimespec(TimeMillis(0));
     32     EXPECT_EQ(0, ts.tv_sec);
     33     EXPECT_EQ(0, ts.tv_nsec);
     34   }
     35   {
     36     struct timespec ts = ToPosixTimespec(TimeMillis(1));
     37     EXPECT_EQ(0, ts.tv_sec);
     38     EXPECT_EQ(1000000, ts.tv_nsec);
     39   }
     40   {
     41     struct timespec ts = ToPosixTimespec(TimeMillis(12345));
     42     EXPECT_EQ(12, ts.tv_sec);
     43     EXPECT_EQ(345000000, ts.tv_nsec);
     44   }
     45   {
     46     struct timespec ts = ToPosixTimespec(TimeMillis(1000000000001LL));
     47     EXPECT_EQ(1000000000, ts.tv_sec);
     48     EXPECT_EQ(1000000, ts.tv_nsec);
     49   }
     50 }
     51 
     52 }  // namespace
     53 }  // namespace base
     54 }  // namespace perfetto
     55