OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/time/time.h" | 5 #include "base/time/time.h" |
6 | 6 |
7 #include <time.h> | 7 #include <time.h> |
8 | 8 |
| 9 #include <limits> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 13 #include "base/logging.h" |
13 #include "base/strings/stringprintf.h" | 14 #include "base/strings/stringprintf.h" |
14 #include "base/threading/platform_thread.h" | 15 #include "base/threading/platform_thread.h" |
15 #include "build/build_config.h" | 16 #include "build/build_config.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
(...skipping 616 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
635 // Unfortunately, our InMilliseconds() function truncates | 636 // Unfortunately, our InMilliseconds() function truncates |
636 // rather than rounds. We should consider fixing this | 637 // rather than rounds. We should consider fixing this |
637 // so that our averages come out better. | 638 // so that our averages come out better. |
638 EXPECT_GE(delta.InMilliseconds(), 9); | 639 EXPECT_GE(delta.InMilliseconds(), 9); |
639 EXPECT_GE(delta.InMicroseconds(), 9000); | 640 EXPECT_GE(delta.InMicroseconds(), 9000); |
640 EXPECT_EQ(delta.InSeconds(), 0); | 641 EXPECT_EQ(delta.InSeconds(), 0); |
641 } | 642 } |
642 } | 643 } |
643 | 644 |
644 static void HighResClockTest(TimeTicks (*GetTicks)()) { | 645 static void HighResClockTest(TimeTicks (*GetTicks)()) { |
645 #if defined(OS_WIN) | |
646 // HighResNow doesn't work on some systems. Since the product still works | 646 // HighResNow doesn't work on some systems. Since the product still works |
647 // even if it doesn't work, it makes this entire test questionable. | 647 // even if it doesn't work, it makes this entire test questionable. |
648 if (!TimeTicks::IsHighResClockWorking()) | 648 if (!TimeTicks::IsHighResolution()) |
649 return; | 649 return; |
650 #endif | |
651 | 650 |
652 // Why do we loop here? | 651 // Why do we loop here? |
653 // We're trying to measure that intervals increment in a VERY small amount | 652 // We're trying to measure that intervals increment in a VERY small amount |
654 // of time -- less than 15ms. Unfortunately, if we happen to have a | 653 // of time -- less than 15ms. Unfortunately, if we happen to have a |
655 // context switch in the middle of our test, the context switch could easily | 654 // context switch in the middle of our test, the context switch could easily |
656 // exceed our limit. So, we iterate on this several times. As long as we're | 655 // exceed our limit. So, we iterate on this several times. As long as we're |
657 // able to detect the fine-granularity timers at least once, then the test | 656 // able to detect the fine-granularity timers at least once, then the test |
658 // has succeeded. | 657 // has succeeded. |
659 | 658 |
660 const int kTargetGranularityUs = 15000; // 15ms | 659 const int kTargetGranularityUs = 15000; // 15ms |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
785 | 784 |
786 // We could define this separately for Time, TimeTicks and TimeDelta but the | 785 // We could define this separately for Time, TimeTicks and TimeDelta but the |
787 // definitions would be identical anyway. | 786 // definitions would be identical anyway. |
788 template <class Any> | 787 template <class Any> |
789 std::string AnyToString(Any any) { | 788 std::string AnyToString(Any any) { |
790 std::ostringstream oss; | 789 std::ostringstream oss; |
791 oss << any; | 790 oss << any; |
792 return oss.str(); | 791 return oss.str(); |
793 } | 792 } |
794 | 793 |
| 794 TEST(TimeDelta, Magnitude) { |
| 795 const int64 zero = 0; |
| 796 EXPECT_EQ(TimeDelta::FromMicroseconds(zero), |
| 797 TimeDelta::FromMicroseconds(zero).magnitude()); |
| 798 |
| 799 const int64 one = 1; |
| 800 const int64 negative_one = -1; |
| 801 EXPECT_EQ(TimeDelta::FromMicroseconds(one), |
| 802 TimeDelta::FromMicroseconds(one).magnitude()); |
| 803 EXPECT_EQ(TimeDelta::FromMicroseconds(one), |
| 804 TimeDelta::FromMicroseconds(negative_one).magnitude()); |
| 805 |
| 806 const int64 max_int64_minus_one = std::numeric_limits<int64>::max() - 1; |
| 807 const int64 min_int64_plus_two = std::numeric_limits<int64>::min() + 2; |
| 808 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), |
| 809 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); |
| 810 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), |
| 811 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); |
| 812 } |
| 813 |
795 TEST(TimeDeltaLogging, DCheckEqCompiles) { | 814 TEST(TimeDeltaLogging, DCheckEqCompiles) { |
796 DCHECK_EQ(TimeDelta(), TimeDelta()); | 815 DCHECK_EQ(TimeDelta(), TimeDelta()); |
797 } | 816 } |
798 | 817 |
799 TEST(TimeDeltaLogging, EmptyIsZero) { | 818 TEST(TimeDeltaLogging, EmptyIsZero) { |
800 TimeDelta zero; | 819 TimeDelta zero; |
801 EXPECT_EQ("0s", AnyToString(zero)); | 820 EXPECT_EQ("0s", AnyToString(zero)); |
802 } | 821 } |
803 | 822 |
804 TEST(TimeDeltaLogging, FiveHundredMs) { | 823 TEST(TimeDeltaLogging, FiveHundredMs) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
872 | 891 |
873 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 892 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
874 std::ostringstream oss; | 893 std::ostringstream oss; |
875 oss << TimeTicks(); | 894 oss << TimeTicks(); |
876 EXPECT_TRUE(oss.good()); | 895 EXPECT_TRUE(oss.good()); |
877 } | 896 } |
878 | 897 |
879 } // namespace | 898 } // namespace |
880 | 899 |
881 } // namespace base | 900 } // namespace base |
OLD | NEW |