Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: base/time_unittest.cc

Issue 10916089: Fixing Time::Max()'s behavior with Time::ToTimeT() and friends. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« base/time.h ('K') | « base/time.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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.h" 5 #include "base/time.h"
6 6
7 #include <time.h> 7 #include <time.h>
8 8
9 #include "base/compiler_specific.h" 9 #include "base/compiler_specific.h"
10 #include "base/threading/platform_thread.h" 10 #include "base/threading/platform_thread.h"
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 79
80 time_t now_t_2 = our_time_2.ToTimeT(); 80 time_t now_t_2 = our_time_2.ToTimeT();
81 EXPECT_EQ(now_t_1, now_t_2); 81 EXPECT_EQ(now_t_1, now_t_2);
82 82
83 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT()); 83 EXPECT_EQ(10, Time().FromTimeT(10).ToTimeT());
84 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT()); 84 EXPECT_EQ(10.0, Time().FromTimeT(10).ToDoubleT());
85 85
86 // Conversions of 0 should stay 0. 86 // Conversions of 0 should stay 0.
87 EXPECT_EQ(0, Time().ToTimeT()); 87 EXPECT_EQ(0, Time().ToTimeT());
88 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue()); 88 EXPECT_EQ(0, Time::FromTimeT(0).ToInternalValue());
89
90 // Conversions of Max() should stay max.
91 EXPECT_EQ(std::numeric_limits<time_t>::max(), Time::Max().ToTimeT());
92 EXPECT_EQ(std::numeric_limits<time_t>::max(),
93 Time().FromTimeT(std::numeric_limits<time_t>::max()).ToTimeT());
94 EXPECT_EQ(std::numeric_limits<int64>::max(),
95 Time::FromTimeT(std::numeric_limits<time_t>::max()).ToInternalValue());
89 } 96 }
90 97
91 // Test conversions to/from javascript time. 98 // Test conversions to/from javascript time.
92 TEST_F(TimeTest, JsTime) { 99 TEST_F(TimeTest, JsTime) {
93 Time epoch = Time::FromJsTime(0.0); 100 Time epoch = Time::FromJsTime(0.0);
94 EXPECT_EQ(epoch, Time::UnixEpoch()); 101 EXPECT_EQ(epoch, Time::UnixEpoch());
95 Time t = Time::FromJsTime(700000.3); 102 Time t = Time::FromJsTime(700000.3);
96 EXPECT_EQ(700.0003, t.ToDoubleT()); 103 EXPECT_EQ(700.0003, t.ToDoubleT());
97 t = Time::FromDoubleT(800.73); 104 t = Time::FromDoubleT(800.73);
98 EXPECT_EQ(800730.0, t.ToJsTime()); 105 EXPECT_EQ(800730.0, t.ToJsTime());
106
107 // Conversions of Max() should stay max.
108 t = Time::FromJsTime(std::numeric_limits<double>::max());
109 EXPECT_TRUE(t.is_max());
110 t = Time::Max();
111 EXPECT_EQ(std::numeric_limits<double>::max(), t.ToDoubleT());
99 } 112 }
100 113
101 #if defined(OS_POSIX) 114 #if defined(OS_POSIX)
102 TEST_F(TimeTest, FromTimeVal) { 115 TEST_F(TimeTest, FromTimeVal) {
103 Time now = Time::Now(); 116 Time now = Time::Now();
104 Time also_now = Time::FromTimeVal(now.ToTimeVal()); 117 Time also_now = Time::FromTimeVal(now.ToTimeVal());
105 EXPECT_EQ(now, also_now); 118 EXPECT_EQ(now, also_now);
106 } 119 }
107 #endif // OS_POSIX 120 #endif // OS_POSIX
108 121
(...skipping 364 matching lines...) Expand 10 before | Expand all | Expand 10 after
473 EXPECT_EQ(kUnixEpochYear, exploded.year); 486 EXPECT_EQ(kUnixEpochYear, exploded.year);
474 EXPECT_EQ(1, exploded.month); 487 EXPECT_EQ(1, exploded.month);
475 EXPECT_EQ(1, exploded.day_of_month); 488 EXPECT_EQ(1, exploded.day_of_month);
476 EXPECT_EQ(0, exploded.hour); 489 EXPECT_EQ(0, exploded.hour);
477 EXPECT_EQ(0, exploded.minute); 490 EXPECT_EQ(0, exploded.minute);
478 EXPECT_EQ(1, exploded.second); 491 EXPECT_EQ(1, exploded.second);
479 EXPECT_EQ(1, exploded.millisecond); 492 EXPECT_EQ(1, exploded.millisecond);
480 } 493 }
481 494
482 TEST_F(TimeTest, Max) { 495 TEST_F(TimeTest, Max) {
483 EXPECT_EQ(base::Time::Max(), base::Time::Max()); 496 Time max = Time::Max();
484 EXPECT_GT(base::Time::Max(), base::Time::Now()); 497 EXPECT_TRUE(max.is_max());
485 EXPECT_GT(base::Time::Max(), base::Time()); 498 EXPECT_EQ(max, Time::Max());
499 EXPECT_GT(max, Time::Now());
500 EXPECT_GT(max, Time());
486 } 501 }
487 502
488 TEST(TimeTicks, Deltas) { 503 TEST(TimeTicks, Deltas) {
489 for (int index = 0; index < 50; index++) { 504 for (int index = 0; index < 50; index++) {
490 TimeTicks ticks_start = TimeTicks::Now(); 505 TimeTicks ticks_start = TimeTicks::Now();
491 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10)); 506 base::PlatformThread::Sleep(base::TimeDelta::FromMilliseconds(10));
492 TimeTicks ticks_stop = TimeTicks::Now(); 507 TimeTicks ticks_stop = TimeTicks::Now();
493 TimeDelta delta = ticks_stop - ticks_start; 508 TimeDelta delta = ticks_stop - ticks_start;
494 // Note: Although we asked for a 10ms sleep, if the 509 // Note: Although we asked for a 10ms sleep, if the
495 // time clock has a finer granularity than the Sleep() 510 // time clock has a finer granularity than the Sleep()
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after
611 exploded.minute = 0; 626 exploded.minute = 0;
612 exploded.second = 0; 627 exploded.second = 0;
613 exploded.millisecond = 0; 628 exploded.millisecond = 0;
614 Time t = Time::FromUTCExploded(exploded); 629 Time t = Time::FromUTCExploded(exploded);
615 // Unix 1970 epoch. 630 // Unix 1970 epoch.
616 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); 631 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
617 632
618 // We can't test 1601 epoch, since the system time functions on Linux 633 // We can't test 1601 epoch, since the system time functions on Linux
619 // only compute years starting from 1900. 634 // only compute years starting from 1900.
620 } 635 }
OLDNEW
« base/time.h ('K') | « base/time.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698