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

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

Issue 1988663002: Add: check exploded time is properly converted (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mmenke comments Created 4 years, 7 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
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 <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) {
mmenke 2016/05/24 16:08:33 I don't think this is used?
maksims (do not use this acc) 2016/05/26 04:38:18 it is automatically invoked by gtest, when using T
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, FromExplodedOutOfBoundsTime) {
76 // FromUTCExploded must set time to Time(0) and failure, if the day is set to
77 // 31 on a 28-30 day month. Test |exploded| returns Time(0) on 31st of
78 // February and 31st of April. New implementation handles this.
79 EXPECT_EQ(test_data_.explode.HasValidValues(), test_data_.is_valid);
80
81 base::Time result;
82 EXPECT_FALSE(base::Time::FromUTCExploded(test_data_.explode, &result));
83 EXPECT_TRUE(result.is_null());
84 EXPECT_FALSE(base::Time::FromLocalExploded(test_data_.explode, &result));
85 EXPECT_TRUE(result.is_null());
86 }
87
88 INSTANTIATE_TEST_CASE_P(,
89 TimeTestOutOfBounds,
90 testing::ValuesIn(kDateTestData));
mmenke 2016/05/24 16:08:33 Rather than use "INSTANTIATE_TEST_CASE_P", may jus
maksims (do not use this acc) 2016/05/26 04:38:18 Done.
91
24 // Specialized test fixture allowing time strings without timezones to be 92 // Specialized test fixture allowing time strings without timezones to be
25 // tested by comparing them to a known time in the local zone. 93 // tested by comparing them to a known time in the local zone.
26 // See also pr_time_unittests.cc 94 // See also pr_time_unittests.cc
27 class TimeTest : public testing::Test { 95 class TimeTest : public testing::Test {
28 protected: 96 protected:
29 void SetUp() override { 97 void SetUp() override {
30 // Use mktime to get a time_t, and turn it into a PRTime by converting 98 // 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 99 // 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 100 // must be a time guaranteed to be outside of a DST fallback hour in
33 // any timezone. 101 // any timezone.
(...skipping 1087 matching lines...) Expand 10 before | Expand all | Expand 10 after
1121 1189
1122 TEST(TimeTicksLogging, DoesNotMakeStreamBad) { 1190 TEST(TimeTicksLogging, DoesNotMakeStreamBad) {
1123 std::ostringstream oss; 1191 std::ostringstream oss;
1124 oss << TimeTicks(); 1192 oss << TimeTicks();
1125 EXPECT_TRUE(oss.good()); 1193 EXPECT_TRUE(oss.good());
1126 } 1194 }
1127 1195
1128 } // namespace 1196 } // namespace
1129 1197
1130 } // namespace base 1198 } // namespace base
OLDNEW
« base/time/time_posix.cc ('K') | « base/time/time_posix.cc ('k') | base/time/time_win.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698