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 <cmath> |
| 8 |
7 #include <time.h> | 9 #include <time.h> |
8 | 10 |
9 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
10 #include "base/logging.h" | 12 #include "base/logging.h" |
11 #include "base/strings/stringprintf.h" | 13 #include "base/strings/stringprintf.h" |
12 #include "base/threading/platform_thread.h" | 14 #include "base/threading/platform_thread.h" |
13 #include "build/build_config.h" | 15 #include "build/build_config.h" |
14 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
15 | 17 |
16 using base::Time; | 18 using base::Time; |
(...skipping 636 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
653 TimeDelta difference = delta - delta_thread; | 655 TimeDelta difference = delta - delta_thread; |
654 EXPECT_GE(difference.InMicroseconds(), 9000); | 656 EXPECT_GE(difference.InMicroseconds(), 9000); |
655 } | 657 } |
656 } | 658 } |
657 | 659 |
658 TEST(TimeTicks, NowFromSystemTraceTime) { | 660 TEST(TimeTicks, NowFromSystemTraceTime) { |
659 // Re-use HighResNow test for now since clock properties are identical. | 661 // Re-use HighResNow test for now since clock properties are identical. |
660 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); | 662 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); |
661 } | 663 } |
662 | 664 |
| 665 TEST(TimeTicks, WebKitConversion) { |
| 666 double double_null = 0; |
| 667 double double_max = std::numeric_limits<double>::max(); |
| 668 double double_min = std::numeric_limits<double>::lowest(); |
| 669 double double_positive_inf = std::numeric_limits<double>::infinity(); |
| 670 double double_negative_inf = -std::numeric_limits<double>::infinity(); |
| 671 |
| 672 TimeTicks ticks_null = TimeTicks(); |
| 673 TimeTicks ticks_min = |
| 674 TimeTicks::FromInternalValue(std::numeric_limits<int64>::min()); |
| 675 TimeTicks ticks_max = |
| 676 TimeTicks::FromInternalValue(std::numeric_limits<int64>::max()); |
| 677 |
| 678 EXPECT_EQ(ticks_null, TimeTicks::FromWebKit(double_null)); |
| 679 EXPECT_EQ(ticks_max, TimeTicks::FromWebKit(double_max)); |
| 680 EXPECT_EQ(ticks_min, TimeTicks::FromWebKit(double_min)); |
| 681 EXPECT_EQ(ticks_max, TimeTicks::FromWebKit(double_positive_inf)); |
| 682 EXPECT_EQ(ticks_min, TimeTicks::FromWebKit(double_negative_inf)); |
| 683 |
| 684 EXPECT_EQ(double_null, ticks_null.ToWebKit()); |
| 685 EXPECT_EQ(double_negative_inf, ticks_min.ToWebKit()); |
| 686 EXPECT_EQ(double_positive_inf, ticks_max.ToWebKit()); |
| 687 |
| 688 // Round trip some values |
| 689 // Outside of 2**53 the int64->double->int64 round trip isn't exact. |
| 690 for (int i = -53; i < 53; i++) { |
| 691 if (!i) |
| 692 continue; |
| 693 |
| 694 SCOPED_TRACE(i); |
| 695 double i_abs = abs(static_cast<double>(i)); |
| 696 |
| 697 TimeTicks ticks_value = TimeTicks::FromInternalValue( |
| 698 static_cast<int64>((i / i_abs) * pow(2.0, i_abs))); |
| 699 EXPECT_EQ(TimeTicks::FromWebKit(ticks_value.ToWebKit()), ticks_value); |
| 700 |
| 701 double double_value = (i / i_abs) * pow(2.0, i_abs) / 1e6; |
| 702 EXPECT_EQ(TimeTicks::FromWebKit(double_value).ToWebKit(), double_value); |
| 703 } |
| 704 } |
| 705 |
663 TEST(TimeDelta, FromAndIn) { | 706 TEST(TimeDelta, FromAndIn) { |
664 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); | 707 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); |
665 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); | 708 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); |
666 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); | 709 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); |
667 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); | 710 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); |
668 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == | 711 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == |
669 TimeDelta::FromMicroseconds(2000)); | 712 TimeDelta::FromMicroseconds(2000)); |
670 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); | 713 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); |
671 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); | 714 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); |
672 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); | 715 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
711 exploded.minute = 0; | 754 exploded.minute = 0; |
712 exploded.second = 0; | 755 exploded.second = 0; |
713 exploded.millisecond = 0; | 756 exploded.millisecond = 0; |
714 Time t = Time::FromUTCExploded(exploded); | 757 Time t = Time::FromUTCExploded(exploded); |
715 // Unix 1970 epoch. | 758 // Unix 1970 epoch. |
716 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); | 759 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); |
717 | 760 |
718 // We can't test 1601 epoch, since the system time functions on Linux | 761 // We can't test 1601 epoch, since the system time functions on Linux |
719 // only compute years starting from 1900. | 762 // only compute years starting from 1900. |
720 } | 763 } |
OLD | NEW |