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> |
11 #include <type_traits> | |
10 | 12 |
11 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
13 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
14 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
15 #include "build/build_config.h" | 17 #include "build/build_config.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
17 | 19 |
18 namespace base { | 20 namespace base { |
19 | 21 |
(...skipping 615 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
635 // Unfortunately, our InMilliseconds() function truncates | 637 // Unfortunately, our InMilliseconds() function truncates |
636 // rather than rounds. We should consider fixing this | 638 // rather than rounds. We should consider fixing this |
637 // so that our averages come out better. | 639 // so that our averages come out better. |
638 EXPECT_GE(delta.InMilliseconds(), 9); | 640 EXPECT_GE(delta.InMilliseconds(), 9); |
639 EXPECT_GE(delta.InMicroseconds(), 9000); | 641 EXPECT_GE(delta.InMicroseconds(), 9000); |
640 EXPECT_EQ(delta.InSeconds(), 0); | 642 EXPECT_EQ(delta.InSeconds(), 0); |
641 } | 643 } |
642 } | 644 } |
643 | 645 |
644 static void HighResClockTest(TimeTicks (*GetTicks)()) { | 646 static void HighResClockTest(TimeTicks (*GetTicks)()) { |
645 #if defined(OS_WIN) | |
646 // HighResNow doesn't work on some systems. Since the product still works | 647 // 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. | 648 // even if it doesn't work, it makes this entire test questionable. |
648 if (!TimeTicks::IsHighResClockWorking()) | 649 if (!TimeTicks::IsHighResolution()) |
649 return; | 650 return; |
650 #endif | |
651 | 651 |
652 // Why do we loop here? | 652 // Why do we loop here? |
653 // We're trying to measure that intervals increment in a VERY small amount | 653 // 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 | 654 // 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 | 655 // 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 | 656 // 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 | 657 // able to detect the fine-granularity timers at least once, then the test |
658 // has succeeded. | 658 // has succeeded. |
659 | 659 |
660 const int kTargetGranularityUs = 15000; // 15ms | 660 const int kTargetGranularityUs = 15000; // 15ms |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
785 | 785 |
786 // We could define this separately for Time, TimeTicks and TimeDelta but the | 786 // We could define this separately for Time, TimeTicks and TimeDelta but the |
787 // definitions would be identical anyway. | 787 // definitions would be identical anyway. |
788 template <class Any> | 788 template <class Any> |
789 std::string AnyToString(Any any) { | 789 std::string AnyToString(Any any) { |
790 std::ostringstream oss; | 790 std::ostringstream oss; |
791 oss << any; | 791 oss << any; |
792 return oss.str(); | 792 return oss.str(); |
793 } | 793 } |
794 | 794 |
795 // Paranoia test: Since std::abs(int64) is new to C++11, make sure incomplete | |
796 // toolchains don't silently pull in an alternate overload when the needed | |
797 // function is missing. | |
798 TEST(TimeDelta, ParanoidAboutAbsFunction) { | |
brianderson
2015/01/07 00:53:44
Do you plan to remove this test before landing?
miu
2015/01/07 05:22:45
The trybots on PS1 forced me to change tactics. S
| |
799 int64 max_int64_minus_one = std::numeric_limits<int64>::max() - 1; | |
800 int64 min_int64_plus_two = std::numeric_limits<int64>::min() + 2; | |
801 static_assert(std::is_same<decltype(max_int64_minus_one), | |
802 decltype(std::abs(max_int64_minus_one))>::value, | |
803 "compiler is choosing incorrect std::abs() overload"); | |
804 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), | |
805 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); | |
806 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), | |
807 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); | |
808 } | |
809 | |
795 TEST(TimeDeltaLogging, DCheckEqCompiles) { | 810 TEST(TimeDeltaLogging, DCheckEqCompiles) { |
796 DCHECK_EQ(TimeDelta(), TimeDelta()); | 811 DCHECK_EQ(TimeDelta(), TimeDelta()); |
797 } | 812 } |
798 | 813 |
799 TEST(TimeDeltaLogging, EmptyIsZero) { | 814 TEST(TimeDeltaLogging, EmptyIsZero) { |
800 TimeDelta zero; | 815 TimeDelta zero; |
801 EXPECT_EQ("0s", AnyToString(zero)); | 816 EXPECT_EQ("0s", AnyToString(zero)); |
802 } | 817 } |
803 | 818 |
804 TEST(TimeDeltaLogging, FiveHundredMs) { | 819 TEST(TimeDeltaLogging, FiveHundredMs) { |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
872 | 887 |
873 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 888 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
874 std::ostringstream oss; | 889 std::ostringstream oss; |
875 oss << TimeTicks(); | 890 oss << TimeTicks(); |
876 EXPECT_TRUE(oss.good()); | 891 EXPECT_TRUE(oss.good()); |
877 } | 892 } |
878 | 893 |
879 } // namespace | 894 } // namespace |
880 | 895 |
881 } // namespace base | 896 } // namespace base |
OLD | NEW |