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 <stdint.h> | 7 #include <stdint.h> |
8 #include <time.h> | 8 #include <time.h> |
9 #include <limits> | 9 #include <limits> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/macros.h" |
14 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
15 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
16 #include "build/build_config.h" | 17 #include "build/build_config.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 // Specialized test fixture allowing time strings without timezones to be | 24 // Specialized test fixture allowing time strings without timezones to be |
(...skipping 463 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
487 TEST_F(TimeTest, Max) { | 488 TEST_F(TimeTest, Max) { |
488 Time max = Time::Max(); | 489 Time max = Time::Max(); |
489 EXPECT_TRUE(max.is_max()); | 490 EXPECT_TRUE(max.is_max()); |
490 EXPECT_EQ(max, Time::Max()); | 491 EXPECT_EQ(max, Time::Max()); |
491 EXPECT_GT(max, Time::Now()); | 492 EXPECT_GT(max, Time::Now()); |
492 EXPECT_GT(max, Time()); | 493 EXPECT_GT(max, Time()); |
493 } | 494 } |
494 | 495 |
495 TEST_F(TimeTest, MaxConversions) { | 496 TEST_F(TimeTest, MaxConversions) { |
496 Time t = Time::Max(); | 497 Time t = Time::Max(); |
497 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue()); | 498 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); |
498 | 499 |
499 t = Time::FromDoubleT(std::numeric_limits<double>::infinity()); | 500 t = Time::FromDoubleT(std::numeric_limits<double>::infinity()); |
500 EXPECT_TRUE(t.is_max()); | 501 EXPECT_TRUE(t.is_max()); |
501 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT()); | 502 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToDoubleT()); |
502 | 503 |
503 t = Time::FromJsTime(std::numeric_limits<double>::infinity()); | 504 t = Time::FromJsTime(std::numeric_limits<double>::infinity()); |
504 EXPECT_TRUE(t.is_max()); | 505 EXPECT_TRUE(t.is_max()); |
505 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime()); | 506 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.ToJsTime()); |
506 | 507 |
507 t = Time::FromTimeT(std::numeric_limits<time_t>::max()); | 508 t = Time::FromTimeT(std::numeric_limits<time_t>::max()); |
(...skipping 26 matching lines...) Expand all Loading... |
534 t = Time::FromFileTime(ftime); | 535 t = Time::FromFileTime(ftime); |
535 EXPECT_TRUE(t.is_max()); | 536 EXPECT_TRUE(t.is_max()); |
536 ftime = t.ToFileTime(); | 537 ftime = t.ToFileTime(); |
537 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); | 538 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwHighDateTime); |
538 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); | 539 EXPECT_EQ(std::numeric_limits<DWORD>::max(), ftime.dwLowDateTime); |
539 #endif | 540 #endif |
540 } | 541 } |
541 | 542 |
542 #if defined(OS_MACOSX) | 543 #if defined(OS_MACOSX) |
543 TEST_F(TimeTest, TimeTOverflow) { | 544 TEST_F(TimeTest, TimeTOverflow) { |
544 Time t = Time::FromInternalValue(std::numeric_limits<int64>::max() - 1); | 545 Time t = Time::FromInternalValue(std::numeric_limits<int64_t>::max() - 1); |
545 EXPECT_FALSE(t.is_max()); | 546 EXPECT_FALSE(t.is_max()); |
546 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); | 547 EXPECT_EQ(std::numeric_limits<time_t>::max(), t.ToTimeT()); |
547 } | 548 } |
548 #endif | 549 #endif |
549 | 550 |
550 #if defined(OS_ANDROID) | 551 #if defined(OS_ANDROID) |
551 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { | 552 TEST_F(TimeTest, FromLocalExplodedCrashOnAndroid) { |
552 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2. | 553 // This crashed inside Time:: FromLocalExploded() on Android 4.1.2. |
553 // See http://crbug.com/287821 | 554 // See http://crbug.com/287821 |
554 Time::Exploded midnight = {2013, // year | 555 Time::Exploded midnight = {2013, // year |
(...skipping 237 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
792 // We could define this separately for Time, TimeTicks and TimeDelta but the | 793 // We could define this separately for Time, TimeTicks and TimeDelta but the |
793 // definitions would be identical anyway. | 794 // definitions would be identical anyway. |
794 template <class Any> | 795 template <class Any> |
795 std::string AnyToString(Any any) { | 796 std::string AnyToString(Any any) { |
796 std::ostringstream oss; | 797 std::ostringstream oss; |
797 oss << any; | 798 oss << any; |
798 return oss.str(); | 799 return oss.str(); |
799 } | 800 } |
800 | 801 |
801 TEST(TimeDelta, Magnitude) { | 802 TEST(TimeDelta, Magnitude) { |
802 const int64 zero = 0; | 803 const int64_t zero = 0; |
803 EXPECT_EQ(TimeDelta::FromMicroseconds(zero), | 804 EXPECT_EQ(TimeDelta::FromMicroseconds(zero), |
804 TimeDelta::FromMicroseconds(zero).magnitude()); | 805 TimeDelta::FromMicroseconds(zero).magnitude()); |
805 | 806 |
806 const int64 one = 1; | 807 const int64_t one = 1; |
807 const int64 negative_one = -1; | 808 const int64_t negative_one = -1; |
808 EXPECT_EQ(TimeDelta::FromMicroseconds(one), | 809 EXPECT_EQ(TimeDelta::FromMicroseconds(one), |
809 TimeDelta::FromMicroseconds(one).magnitude()); | 810 TimeDelta::FromMicroseconds(one).magnitude()); |
810 EXPECT_EQ(TimeDelta::FromMicroseconds(one), | 811 EXPECT_EQ(TimeDelta::FromMicroseconds(one), |
811 TimeDelta::FromMicroseconds(negative_one).magnitude()); | 812 TimeDelta::FromMicroseconds(negative_one).magnitude()); |
812 | 813 |
813 const int64 max_int64_minus_one = std::numeric_limits<int64>::max() - 1; | 814 const int64_t max_int64_minus_one = std::numeric_limits<int64_t>::max() - 1; |
814 const int64 min_int64_plus_two = std::numeric_limits<int64>::min() + 2; | 815 const int64_t min_int64_plus_two = std::numeric_limits<int64_t>::min() + 2; |
815 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), | 816 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), |
816 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); | 817 TimeDelta::FromMicroseconds(max_int64_minus_one).magnitude()); |
817 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), | 818 EXPECT_EQ(TimeDelta::FromMicroseconds(max_int64_minus_one), |
818 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); | 819 TimeDelta::FromMicroseconds(min_int64_plus_two).magnitude()); |
819 } | 820 } |
820 | 821 |
821 TEST(TimeDelta, Max) { | 822 TEST(TimeDelta, Max) { |
822 TimeDelta max = TimeDelta::Max(); | 823 TimeDelta max = TimeDelta::Max(); |
823 EXPECT_TRUE(max.is_max()); | 824 EXPECT_TRUE(max.is_max()); |
824 EXPECT_EQ(max, TimeDelta::Max()); | 825 EXPECT_EQ(max, TimeDelta::Max()); |
825 EXPECT_GT(max, TimeDelta::FromDays(100 * 365)); | 826 EXPECT_GT(max, TimeDelta::FromDays(100 * 365)); |
826 EXPECT_GT(max, TimeDelta()); | 827 EXPECT_GT(max, TimeDelta()); |
827 } | 828 } |
828 | 829 |
829 bool IsMin(TimeDelta delta) { | 830 bool IsMin(TimeDelta delta) { |
830 return (-delta).is_max(); | 831 return (-delta).is_max(); |
831 } | 832 } |
832 | 833 |
833 TEST(TimeDelta, MaxConversions) { | 834 TEST(TimeDelta, MaxConversions) { |
834 TimeDelta t = TimeDelta::Max(); | 835 TimeDelta t = TimeDelta::Max(); |
835 EXPECT_EQ(std::numeric_limits<int64>::max(), t.ToInternalValue()); | 836 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.ToInternalValue()); |
836 | 837 |
837 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays()); | 838 EXPECT_EQ(std::numeric_limits<int>::max(), t.InDays()); |
838 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours()); | 839 EXPECT_EQ(std::numeric_limits<int>::max(), t.InHours()); |
839 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes()); | 840 EXPECT_EQ(std::numeric_limits<int>::max(), t.InMinutes()); |
840 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF()); | 841 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InSecondsF()); |
841 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InSeconds()); | 842 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InSeconds()); |
842 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF()); | 843 EXPECT_EQ(std::numeric_limits<double>::infinity(), t.InMillisecondsF()); |
843 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMilliseconds()); | 844 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMilliseconds()); |
844 EXPECT_EQ(std::numeric_limits<int64>::max(), t.InMillisecondsRoundedUp()); | 845 EXPECT_EQ(std::numeric_limits<int64_t>::max(), t.InMillisecondsRoundedUp()); |
845 | 846 |
846 t = TimeDelta::FromDays(std::numeric_limits<int>::max()); | 847 t = TimeDelta::FromDays(std::numeric_limits<int>::max()); |
847 EXPECT_TRUE(t.is_max()); | 848 EXPECT_TRUE(t.is_max()); |
848 | 849 |
849 t = TimeDelta::FromHours(std::numeric_limits<int>::max()); | 850 t = TimeDelta::FromHours(std::numeric_limits<int>::max()); |
850 EXPECT_TRUE(t.is_max()); | 851 EXPECT_TRUE(t.is_max()); |
851 | 852 |
852 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); | 853 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); |
853 EXPECT_TRUE(t.is_max()); | 854 EXPECT_TRUE(t.is_max()); |
854 | 855 |
855 int64 max_int = std::numeric_limits<int64>::max(); | 856 int64_t max_int = std::numeric_limits<int64_t>::max(); |
856 | 857 |
857 t = TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1); | 858 t = TimeDelta::FromSeconds(max_int / Time::kMicrosecondsPerSecond + 1); |
858 EXPECT_TRUE(t.is_max()); | 859 EXPECT_TRUE(t.is_max()); |
859 | 860 |
860 t = TimeDelta::FromMilliseconds(max_int / Time::kMillisecondsPerSecond + 1); | 861 t = TimeDelta::FromMilliseconds(max_int / Time::kMillisecondsPerSecond + 1); |
861 EXPECT_TRUE(t.is_max()); | 862 EXPECT_TRUE(t.is_max()); |
862 | 863 |
863 t = TimeDelta::FromMicroseconds(max_int); | 864 t = TimeDelta::FromMicroseconds(max_int); |
864 EXPECT_TRUE(t.is_max()); | 865 EXPECT_TRUE(t.is_max()); |
865 | 866 |
866 t = TimeDelta::FromSeconds(-max_int / Time::kMicrosecondsPerSecond - 1); | 867 t = TimeDelta::FromSeconds(-max_int / Time::kMicrosecondsPerSecond - 1); |
867 EXPECT_TRUE(IsMin(t)); | 868 EXPECT_TRUE(IsMin(t)); |
868 | 869 |
869 t = TimeDelta::FromMilliseconds(-max_int / Time::kMillisecondsPerSecond - 1); | 870 t = TimeDelta::FromMilliseconds(-max_int / Time::kMillisecondsPerSecond - 1); |
870 EXPECT_TRUE(IsMin(t)); | 871 EXPECT_TRUE(IsMin(t)); |
871 | 872 |
872 t = TimeDelta::FromMicroseconds(-max_int); | 873 t = TimeDelta::FromMicroseconds(-max_int); |
873 EXPECT_TRUE(IsMin(t)); | 874 EXPECT_TRUE(IsMin(t)); |
874 | 875 |
875 t = -TimeDelta::FromMicroseconds(std::numeric_limits<int64>::min()); | 876 t = -TimeDelta::FromMicroseconds(std::numeric_limits<int64_t>::min()); |
876 EXPECT_FALSE(IsMin(t)); | 877 EXPECT_FALSE(IsMin(t)); |
877 | 878 |
878 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity()); | 879 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity()); |
879 EXPECT_TRUE(t.is_max()); | 880 EXPECT_TRUE(t.is_max()); |
880 | 881 |
881 double max_d = max_int; | 882 double max_d = max_int; |
882 | 883 |
883 t = TimeDelta::FromSecondsD(max_d / Time::kMicrosecondsPerSecond + 1); | 884 t = TimeDelta::FromSecondsD(max_d / Time::kMicrosecondsPerSecond + 1); |
884 EXPECT_TRUE(t.is_max()); | 885 EXPECT_TRUE(t.is_max()); |
885 | 886 |
(...skipping 229 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1115 | 1116 |
1116 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1117 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1117 std::ostringstream oss; | 1118 std::ostringstream oss; |
1118 oss << TimeTicks(); | 1119 oss << TimeTicks(); |
1119 EXPECT_TRUE(oss.good()); | 1120 EXPECT_TRUE(oss.good()); |
1120 } | 1121 } |
1121 | 1122 |
1122 } // namespace | 1123 } // namespace |
1123 | 1124 |
1124 } // namespace base | 1125 } // namespace base |
OLD | NEW |