Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2015 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 "time_utils.h"
     18 
     19 #include "gtest/gtest.h"
     20 
     21 namespace art {
     22 
     23 TEST(TimeUtilsTest, PrettyDuration) {
     24   const uint64_t one_sec = 1000000000;
     25   const uint64_t one_ms  = 1000000;
     26   const uint64_t one_us  = 1000;
     27 
     28   EXPECT_EQ("1s", PrettyDuration(1 * one_sec));
     29   EXPECT_EQ("10s", PrettyDuration(10 * one_sec));
     30   EXPECT_EQ("100s", PrettyDuration(100 * one_sec));
     31   EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms));
     32   EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us, 6));
     33   EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1, 9));
     34   EXPECT_EQ("1.000s", PrettyDuration(1 * one_sec + one_us, 3));
     35 
     36   EXPECT_EQ("1ms", PrettyDuration(1 * one_ms));
     37   EXPECT_EQ("10ms", PrettyDuration(10 * one_ms));
     38   EXPECT_EQ("100ms", PrettyDuration(100 * one_ms));
     39   EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us));
     40   EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1, 6));
     41 
     42   EXPECT_EQ("1us", PrettyDuration(1 * one_us));
     43   EXPECT_EQ("10us", PrettyDuration(10 * one_us));
     44   EXPECT_EQ("100us", PrettyDuration(100 * one_us));
     45   EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1));
     46 
     47   EXPECT_EQ("1ns", PrettyDuration(1));
     48   EXPECT_EQ("10ns", PrettyDuration(10));
     49   EXPECT_EQ("100ns", PrettyDuration(100));
     50 }
     51 
     52 TEST(TimeUtilsTest, TestSleep) {
     53   auto start = NanoTime();
     54   NanoSleep(MsToNs(1500));
     55   EXPECT_GT(NanoTime() - start, MsToNs(1000));
     56 }
     57 
     58 }  // namespace art
     59