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/macros.h" |
15 #include "base/strings/stringprintf.h" | 15 #include "base/strings/stringprintf.h" |
16 #include "base/threading/platform_thread.h" | 16 #include "base/threading/platform_thread.h" |
17 #include "build/build_config.h" | 17 #include "build/build_config.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 | 19 |
20 namespace base { | 20 namespace base { |
21 | 21 |
22 namespace { | 22 namespace { |
23 | 23 |
| 24 const struct DateTestData { |
| 25 Time::Exploded explode; |
| 26 bool is_valid; |
| 27 } kDateTestData[] = { |
| 28 // 31st of February |
| 29 {{2016, 2, 0, 31, 12, 30, 0, 0}, true}, |
| 30 // 31st of April |
| 31 {{2016, 4, 0, 31, 8, 43, 0, 0}, true}, |
| 32 // Negative month |
| 33 {{2016, -5, 0, 2, 4, 10, 0, 0}, false}, |
| 34 // Negative date of month |
| 35 {{2016, 6, 0, -15, 2, 50, 0, 0}, false}, |
| 36 // Negative hours |
| 37 {{2016, 7, 0, 10, -11, 29, 0, 0}, false}, |
| 38 // Negative minutes |
| 39 {{2016, 3, 0, 14, 10, -29, 0, 0}, false}, |
| 40 // Negative seconds |
| 41 {{2016, 10, 0, 25, 7, 47, -30, 0}, false}, |
| 42 // Negative milliseconds |
| 43 {{2016, 10, 0, 25, 7, 47, 20, -500}, false}, |
| 44 // Hours are too large |
| 45 {{2016, 7, 0, 10, 26, 29, 0, 0}, false}, |
| 46 // Minutes are too large |
| 47 {{2016, 3, 0, 14, 10, 78, 0, 0}, false}, |
| 48 // Seconds are too large |
| 49 {{2016, 10, 0, 25, 7, 47, 234, 0}, false}, |
| 50 // Milliseconds are too large |
| 51 {{2016, 10, 0, 25, 6, 31, 23, 1643}, false}, |
| 52 }; |
| 53 |
| 54 void PrintTo(const DateTestData& test_data, std::ostream* os) { |
| 55 *os << " year: " << test_data.explode.year |
| 56 << "; month: " << test_data.explode.month |
| 57 << "; day_of_month: " << test_data.explode.day_of_month |
| 58 << "; hour: " << test_data.explode.hour |
| 59 << "; minute: " << test_data.explode.minute |
| 60 << "; second: " << test_data.explode.second |
| 61 << "; millisecond: " << test_data.explode.millisecond; |
| 62 } |
| 63 |
| 64 // Class for testing out of real bounds times. |
| 65 // For example, setting day 31 on 30 day month. |
| 66 class TimeTestOutOfBounds : public testing::TestWithParam<DateTestData> { |
| 67 public: |
| 68 virtual ~TimeTestOutOfBounds() {} |
| 69 void SetUp() override { test_data_ = GetParam(); } |
| 70 |
| 71 protected: |
| 72 DateTestData test_data_; |
| 73 }; |
| 74 |
| 75 TEST_P(TimeTestOutOfBounds, FromExplodedOutOfBoundsTimeOld) { |
| 76 // FromUTCExploded must return Time(0), if the day is set to 31 on a |
| 77 // 28-30 day month. Test if |exploded| returns Time(0) on 31st of February and |
| 78 // 31st of April. Test fails because old implementation does not handle this. |
| 79 // The test is to be deleted after we check which environments fail. |
| 80 EXPECT_EQ(test_data_.explode.HasValidValues(), test_data_.is_valid); |
| 81 |
| 82 base::Time t_time = base::Time::FromUTCExploded(test_data_.explode); |
| 83 EXPECT_TRUE(t_time.is_null()); // must fail |
| 84 t_time = base::Time::FromLocalExploded(test_data_.explode); |
| 85 EXPECT_TRUE(t_time.is_null()); // must fail |
| 86 } |
| 87 |
| 88 TEST_P(TimeTestOutOfBounds, FromExplodedOutOfBoundsTimeNew) { |
| 89 // FromUTCExploded must set time to Time(0) and failure, if the day is set to |
| 90 // 31 on a 28-30 day month. Test |exploded| returns Time(0) on 31st of |
| 91 // February and 31st of April. New implementation handles this. |
| 92 EXPECT_EQ(test_data_.explode.HasValidValues(), test_data_.is_valid); |
| 93 |
| 94 base::Time t_time; |
| 95 EXPECT_FALSE(base::Time::FromUTCExploded(test_data_.explode, t_time)); |
| 96 EXPECT_TRUE(t_time.is_null()); |
| 97 EXPECT_FALSE(base::Time::FromLocalExploded(test_data_.explode, t_time)); |
| 98 EXPECT_TRUE(t_time.is_null()); |
| 99 } |
| 100 |
| 101 INSTANTIATE_TEST_CASE_P(, |
| 102 TimeTestOutOfBounds, |
| 103 testing::ValuesIn(kDateTestData)); |
| 104 |
24 // Specialized test fixture allowing time strings without timezones to be | 105 // Specialized test fixture allowing time strings without timezones to be |
25 // tested by comparing them to a known time in the local zone. | 106 // tested by comparing them to a known time in the local zone. |
26 // See also pr_time_unittests.cc | 107 // See also pr_time_unittests.cc |
27 class TimeTest : public testing::Test { | 108 class TimeTest : public testing::Test { |
28 protected: | 109 protected: |
29 void SetUp() override { | 110 void SetUp() override { |
30 // Use mktime to get a time_t, and turn it into a PRTime by converting | 111 // Use mktime to get a time_t, and turn it into a PRTime by converting |
31 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This | 112 // seconds to microseconds. Use 15th Oct 2007 12:45:00 local. This |
32 // must be a time guaranteed to be outside of a DST fallback hour in | 113 // must be a time guaranteed to be outside of a DST fallback hour in |
33 // any timezone. | 114 // any timezone. |
(...skipping 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1116 | 1197 |
1117 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { | 1198 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { |
1118 std::ostringstream oss; | 1199 std::ostringstream oss; |
1119 oss << TimeTicks(); | 1200 oss << TimeTicks(); |
1120 EXPECT_TRUE(oss.good()); | 1201 EXPECT_TRUE(oss.good()); |
1121 } | 1202 } |
1122 | 1203 |
1123 } // namespace | 1204 } // namespace |
1124 | 1205 |
1125 } // namespace base | 1206 } // namespace base |
OLD | NEW |