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 <limits> |
10 #include <string> | 10 #include <string> |
(...skipping 672 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
683 HighResClockTest(&TimeTicks::Now); | 683 HighResClockTest(&TimeTicks::Now); |
684 } | 684 } |
685 | 685 |
686 // Fails frequently on Android http://crbug.com/352633 with: | 686 // Fails frequently on Android http://crbug.com/352633 with: |
687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 | 687 // Expected: (delta_thread.InMicroseconds()) > (0), actual: 0 vs 0 |
688 #if defined(OS_ANDROID) | 688 #if defined(OS_ANDROID) |
689 #define MAYBE_ThreadNow DISABLED_ThreadNow | 689 #define MAYBE_ThreadNow DISABLED_ThreadNow |
690 #else | 690 #else |
691 #define MAYBE_ThreadNow ThreadNow | 691 #define MAYBE_ThreadNow ThreadNow |
692 #endif | 692 #endif |
693 TEST(TimeTicks, MAYBE_ThreadNow) { | 693 TEST(ThreadTicks, MAYBE_ThreadNow) { |
694 if (TimeTicks::IsThreadNowSupported()) { | 694 if (ThreadTicks::IsSupported()) { |
695 TimeTicks begin = TimeTicks::Now(); | 695 TimeTicks begin = TimeTicks::Now(); |
696 TimeTicks begin_thread = TimeTicks::ThreadNow(); | 696 ThreadTicks begin_thread = ThreadTicks::Now(); |
697 // Make sure that ThreadNow value is non-zero. | 697 // Make sure that ThreadNow value is non-zero. |
698 EXPECT_GT(begin_thread, TimeTicks()); | 698 EXPECT_GT(begin_thread, ThreadTicks()); |
699 // Sleep for 10 milliseconds to get the thread de-scheduled. | 699 // Sleep for 10 milliseconds to get the thread de-scheduled. |
700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); | 700 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); |
701 TimeTicks end_thread = TimeTicks::ThreadNow(); | 701 ThreadTicks end_thread = ThreadTicks::Now(); |
702 TimeTicks end = TimeTicks::Now(); | 702 TimeTicks end = TimeTicks::Now(); |
703 TimeDelta delta = end - begin; | 703 TimeDelta delta = end - begin; |
704 TimeDelta delta_thread = end_thread - begin_thread; | 704 TimeDelta delta_thread = end_thread - begin_thread; |
705 // Make sure that some thread time have elapsed. | 705 // Make sure that some thread time have elapsed. |
706 EXPECT_GT(delta_thread.InMicroseconds(), 0); | 706 EXPECT_GT(delta_thread.InMicroseconds(), 0); |
707 // But the thread time is at least 9ms less than clock time. | 707 // But the thread time is at least 9ms less than clock time. |
708 TimeDelta difference = delta - delta_thread; | 708 TimeDelta difference = delta - delta_thread; |
709 EXPECT_GE(difference.InMicroseconds(), 9000); | 709 EXPECT_GE(difference.InMicroseconds(), 9000); |
710 } | 710 } |
711 } | 711 } |
712 | 712 |
713 TEST(TimeTicks, NowFromSystemTraceTime) { | 713 TEST(TraceTicks, NowFromSystemTraceTime) { |
714 // Re-use HighRes test for now since clock properties are identical. | 714 // Re-use HighRes test for now since clock properties are identical. |
715 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); | 715 using NowFunction = TimeTicks (*)(void); |
| 716 HighResClockTest(reinterpret_cast<NowFunction>(&TraceTicks::Now)); |
716 } | 717 } |
717 | 718 |
718 TEST(TimeTicks, SnappedToNextTickBasic) { | 719 TEST(TimeTicks, SnappedToNextTickBasic) { |
719 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); | 720 base::TimeTicks phase = base::TimeTicks::FromInternalValue(4000); |
720 base::TimeDelta interval = base::TimeDelta::FromInternalValue(1000); | 721 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(1000); |
721 base::TimeTicks timestamp; | 722 base::TimeTicks timestamp; |
722 | 723 |
723 // Timestamp in previous interval. | 724 // Timestamp in previous interval. |
724 timestamp = base::TimeTicks::FromInternalValue(3500); | 725 timestamp = base::TimeTicks::FromInternalValue(3500); |
725 EXPECT_EQ(4000, | 726 EXPECT_EQ(4000, |
726 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); | 727 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); |
727 | 728 |
728 // Timestamp in next interval. | 729 // Timestamp in next interval. |
729 timestamp = base::TimeTicks::FromInternalValue(4500); | 730 timestamp = base::TimeTicks::FromInternalValue(4500); |
730 EXPECT_EQ(5000, | 731 EXPECT_EQ(5000, |
(...skipping 22 matching lines...) Expand all Loading... |
753 // Timestamp equal to phase. | 754 // Timestamp equal to phase. |
754 timestamp = base::TimeTicks::FromInternalValue(4000); | 755 timestamp = base::TimeTicks::FromInternalValue(4000); |
755 EXPECT_EQ(4000, | 756 EXPECT_EQ(4000, |
756 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); | 757 timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); |
757 } | 758 } |
758 | 759 |
759 TEST(TimeTicks, SnappedToNextTickOverflow) { | 760 TEST(TimeTicks, SnappedToNextTickOverflow) { |
760 // int(big_timestamp / interval) < 0, so this causes a crash if the number of | 761 // int(big_timestamp / interval) < 0, so this causes a crash if the number of |
761 // intervals elapsed is attempted to be stored in an int. | 762 // intervals elapsed is attempted to be stored in an int. |
762 base::TimeTicks phase = base::TimeTicks::FromInternalValue(0); | 763 base::TimeTicks phase = base::TimeTicks::FromInternalValue(0); |
763 base::TimeDelta interval = base::TimeDelta::FromInternalValue(4000); | 764 base::TimeDelta interval = base::TimeDelta::FromMicroseconds(4000); |
764 base::TimeTicks big_timestamp = | 765 base::TimeTicks big_timestamp = |
765 base::TimeTicks::FromInternalValue(8635916564000); | 766 base::TimeTicks::FromInternalValue(8635916564000); |
766 | 767 |
767 EXPECT_EQ(8635916564000, | 768 EXPECT_EQ(8635916564000, |
768 big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); | 769 big_timestamp.SnappedToNextTick(phase, interval).ToInternalValue()); |
769 EXPECT_EQ(8635916564000, | 770 EXPECT_EQ(8635916564000, |
770 big_timestamp.SnappedToNextTick(big_timestamp, interval) | 771 big_timestamp.SnappedToNextTick(big_timestamp, interval) |
771 .ToInternalValue()); | 772 .ToInternalValue()); |
772 } | 773 } |
773 | 774 |
(...skipping 319 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1093 | 1094 |
1094 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1095 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1095 std::ostringstream oss; | 1096 std::ostringstream oss; |
1096 oss << TimeTicks(); | 1097 oss << TimeTicks(); |
1097 EXPECT_TRUE(oss.good()); | 1098 EXPECT_TRUE(oss.good()); |
1098 } | 1099 } |
1099 | 1100 |
1100 } // namespace | 1101 } // namespace |
1101 | 1102 |
1102 } // namespace base | 1103 } // namespace base |
OLD | NEW |