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

Side by Side Diff: base/time/time_unittest.cc

Issue 235233003: Added support to convert double to timedelta's without loss of data during type conversion from dou… (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added unit tests Created 6 years, 8 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
« no previous file with comments | « base/time/time.h ('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/time.h" 5 #include "base/time/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/logging.h" 10 #include "base/logging.h"
(...skipping 499 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 510
511 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max()); 511 t = TimeDelta::FromMinutes(std::numeric_limits<int>::max());
512 EXPECT_TRUE(t.is_max()); 512 EXPECT_TRUE(t.is_max());
513 513
514 t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max()); 514 t = TimeDelta::FromSeconds(std::numeric_limits<int64>::max());
515 EXPECT_TRUE(t.is_max()); 515 EXPECT_TRUE(t.is_max());
516 516
517 t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max()); 517 t = TimeDelta::FromMilliseconds(std::numeric_limits<int64>::max());
518 EXPECT_TRUE(t.is_max()); 518 EXPECT_TRUE(t.is_max());
519 519
520 t = TimeDelta::FromSecondsD(std::numeric_limits<double>::infinity());
521 EXPECT_TRUE(t.is_max());
522
523 t = TimeDelta::FromMillisecondsD(std::numeric_limits<double>::infinity());
524 EXPECT_TRUE(t.is_max());
525
520 t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max()); 526 t = TimeDelta::FromMicroseconds(std::numeric_limits<int64>::max());
521 EXPECT_TRUE(t.is_max()); 527 EXPECT_TRUE(t.is_max());
522 } 528 }
523 529
524 TEST_F(TimeTest, Max) { 530 TEST_F(TimeTest, Max) {
525 Time max = Time::Max(); 531 Time max = Time::Max();
526 EXPECT_TRUE(max.is_max()); 532 EXPECT_TRUE(max.is_max());
527 EXPECT_EQ(max, Time::Max()); 533 EXPECT_EQ(max, Time::Max());
528 EXPECT_GT(max, Time::Now()); 534 EXPECT_GT(max, Time::Now());
529 EXPECT_GT(max, Time()); 535 EXPECT_GT(max, Time());
(...skipping 178 matching lines...) Expand 10 before | Expand all | Expand 10 after
708 HighResClockTest(&TimeTicks::NowFromSystemTraceTime); 714 HighResClockTest(&TimeTicks::NowFromSystemTraceTime);
709 } 715 }
710 716
711 TEST(TimeDelta, FromAndIn) { 717 TEST(TimeDelta, FromAndIn) {
712 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48)); 718 EXPECT_TRUE(TimeDelta::FromDays(2) == TimeDelta::FromHours(48));
713 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180)); 719 EXPECT_TRUE(TimeDelta::FromHours(3) == TimeDelta::FromMinutes(180));
714 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120)); 720 EXPECT_TRUE(TimeDelta::FromMinutes(2) == TimeDelta::FromSeconds(120));
715 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000)); 721 EXPECT_TRUE(TimeDelta::FromSeconds(2) == TimeDelta::FromMilliseconds(2000));
716 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) == 722 EXPECT_TRUE(TimeDelta::FromMilliseconds(2) ==
717 TimeDelta::FromMicroseconds(2000)); 723 TimeDelta::FromMicroseconds(2000));
724 EXPECT_TRUE(TimeDelta::FromSecondsD(2.3) ==
725 TimeDelta::FromMilliseconds(2300));
726 EXPECT_TRUE(TimeDelta::FromMillisecondsD(2.5) ==
727 TimeDelta::FromMicroseconds(2500));
718 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays()); 728 EXPECT_EQ(13, TimeDelta::FromDays(13).InDays());
719 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours()); 729 EXPECT_EQ(13, TimeDelta::FromHours(13).InHours());
720 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes()); 730 EXPECT_EQ(13, TimeDelta::FromMinutes(13).InMinutes());
721 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds()); 731 EXPECT_EQ(13, TimeDelta::FromSeconds(13).InSeconds());
722 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF()); 732 EXPECT_EQ(13.0, TimeDelta::FromSeconds(13).InSecondsF());
723 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds()); 733 EXPECT_EQ(13, TimeDelta::FromMilliseconds(13).InMilliseconds());
724 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF()); 734 EXPECT_EQ(13.0, TimeDelta::FromMilliseconds(13).InMillisecondsF());
735 EXPECT_EQ(13, TimeDelta::FromSecondsD(13.1).InSeconds());
736 EXPECT_EQ(13.1, TimeDelta::FromSecondsD(13.1).InSecondsF());
737 EXPECT_EQ(13, TimeDelta::FromMillisecondsD(13.3).InMilliseconds());
738 EXPECT_EQ(13.3, TimeDelta::FromMillisecondsD(13.3).InMillisecondsF());
mithro-old 2014/04/22 07:05:12 Technically anything EXPECT_EQ with floating point
725 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds()); 739 EXPECT_EQ(13, TimeDelta::FromMicroseconds(13).InMicroseconds());
726 } 740 }
727 741
728 #if defined(OS_POSIX) 742 #if defined(OS_POSIX)
729 TEST(TimeDelta, TimeSpecConversion) { 743 TEST(TimeDelta, TimeSpecConversion) {
730 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec(); 744 struct timespec result = TimeDelta::FromSeconds(0).ToTimeSpec();
731 EXPECT_EQ(result.tv_sec, 0); 745 EXPECT_EQ(result.tv_sec, 0);
732 EXPECT_EQ(result.tv_nsec, 0); 746 EXPECT_EQ(result.tv_nsec, 0);
733 747
734 result = TimeDelta::FromSeconds(1).ToTimeSpec(); 748 result = TimeDelta::FromSeconds(1).ToTimeSpec();
(...skipping 24 matching lines...) Expand all
759 exploded.minute = 0; 773 exploded.minute = 0;
760 exploded.second = 0; 774 exploded.second = 0;
761 exploded.millisecond = 0; 775 exploded.millisecond = 0;
762 Time t = Time::FromUTCExploded(exploded); 776 Time t = Time::FromUTCExploded(exploded);
763 // Unix 1970 epoch. 777 // Unix 1970 epoch.
764 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue()); 778 EXPECT_EQ(GG_INT64_C(11644473600000000), t.ToInternalValue());
765 779
766 // We can't test 1601 epoch, since the system time functions on Linux 780 // We can't test 1601 epoch, since the system time functions on Linux
767 // only compute years starting from 1900. 781 // only compute years starting from 1900.
768 } 782 }
OLDNEW
« no previous file with comments | « base/time/time.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698