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

Side by Side Diff: net/disk_cache/simple/simple_index_unittest.cc

Issue 2109503002: Use saturated_cast<> instead of checked_cast<> for converting (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: checkpoint Created 4 years, 5 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 | « net/disk_cache/simple/simple_index.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) 2013 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2013 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 "net/disk_cache/simple/simple_index.h" 5 #include "net/disk_cache/simple/simple_index.h"
6 6
7 #include <algorithm> 7 #include <algorithm>
8 #include <functional> 8 #include <functional>
9 #include <memory> 9 #include <memory>
10 #include <utility> 10 #include <utility>
(...skipping 16 matching lines...) Expand all
27 27
28 namespace disk_cache { 28 namespace disk_cache {
29 namespace { 29 namespace {
30 30
31 const base::Time kTestLastUsedTime = 31 const base::Time kTestLastUsedTime =
32 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20); 32 base::Time::UnixEpoch() + base::TimeDelta::FromDays(20);
33 const uint64_t kTestEntrySize = 789; 33 const uint64_t kTestEntrySize = 789;
34 34
35 } // namespace 35 } // namespace
36 36
37 37 class EntryMetadataTest : public testing::Test {
38 class EntryMetadataTest : public testing::Test {
39 public: 38 public:
40 EntryMetadata NewEntryMetadataWithValues() { 39 EntryMetadata NewEntryMetadataWithValues() {
41 return EntryMetadata(kTestLastUsedTime, kTestEntrySize); 40 return EntryMetadata(kTestLastUsedTime, kTestEntrySize);
42 } 41 }
43 42
44 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) { 43 void CheckEntryMetadataValues(const EntryMetadata& entry_metadata) {
45 EXPECT_LT(kTestLastUsedTime - base::TimeDelta::FromSeconds(2), 44 EXPECT_LT(kTestLastUsedTime - base::TimeDelta::FromSeconds(2),
46 entry_metadata.GetLastUsedTime()); 45 entry_metadata.GetLastUsedTime());
47 EXPECT_GT(kTestLastUsedTime + base::TimeDelta::FromSeconds(2), 46 EXPECT_GT(kTestLastUsedTime + base::TimeDelta::FromSeconds(2),
48 entry_metadata.GetLastUsedTime()); 47 entry_metadata.GetLastUsedTime());
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after
179 178
180 const base::Time new_time = base::Time::Now(); 179 const base::Time new_time = base::Time::Now();
181 entry_metadata.SetLastUsedTime(new_time); 180 entry_metadata.SetLastUsedTime(new_time);
182 181
183 EXPECT_LT(new_time - base::TimeDelta::FromSeconds(2), 182 EXPECT_LT(new_time - base::TimeDelta::FromSeconds(2),
184 entry_metadata.GetLastUsedTime()); 183 entry_metadata.GetLastUsedTime());
185 EXPECT_GT(new_time + base::TimeDelta::FromSeconds(2), 184 EXPECT_GT(new_time + base::TimeDelta::FromSeconds(2),
186 entry_metadata.GetLastUsedTime()); 185 entry_metadata.GetLastUsedTime());
187 } 186 }
188 187
188 // Tests that setting an unusually small/large last used time results in
189 // truncation (rather than crashing).
190 TEST_F(EntryMetadataTest, SaturatedLastUsedTime) {
191 EntryMetadata entry_metadata;
192
193 // Set a time that is too large to be represented internally as 32-bit unix
194 // timestamp. Will saturate to a large timestamp (in year 2106).
195 entry_metadata.SetLastUsedTime(base::Time::Max());
196 EXPECT_EQ(INT64_C(15939440895000000),
197 entry_metadata.GetLastUsedTime().ToInternalValue());
198
199 // Set a time that is too small to be represented by a unix timestamp (before
200 // 1970).
201 entry_metadata.SetLastUsedTime(
202 base::Time::FromInternalValue(7u)); // This is a date in 1601.
203 EXPECT_EQ(base::Time::UnixEpoch() + base::TimeDelta::FromSeconds(1),
204 entry_metadata.GetLastUsedTime());
205 }
206
189 TEST_F(EntryMetadataTest, Serialize) { 207 TEST_F(EntryMetadataTest, Serialize) {
190 EntryMetadata entry_metadata = NewEntryMetadataWithValues(); 208 EntryMetadata entry_metadata = NewEntryMetadataWithValues();
191 209
192 base::Pickle pickle; 210 base::Pickle pickle;
193 entry_metadata.Serialize(&pickle); 211 entry_metadata.Serialize(&pickle);
194 212
195 base::PickleIterator it(pickle); 213 base::PickleIterator it(pickle);
196 EntryMetadata new_entry_metadata; 214 EntryMetadata new_entry_metadata;
197 new_entry_metadata.Deserialize(&it); 215 new_entry_metadata.Deserialize(&it);
198 CheckEntryMetadataValues(new_entry_metadata); 216 CheckEntryMetadataValues(new_entry_metadata);
(...skipping 421 matching lines...) Expand 10 before | Expand all | Expand 10 after
620 WaitForTimeChange(); 638 WaitForTimeChange();
621 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); 639 EXPECT_EQ(expected_trigger, index()->write_to_disk_timer_.desired_run_time());
622 index()->Insert(hashes_.at<2>()); 640 index()->Insert(hashes_.at<2>());
623 index()->UpdateEntrySize(hashes_.at<2>(), 40); 641 index()->UpdateEntrySize(hashes_.at<2>(), 40);
624 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning()); 642 EXPECT_TRUE(index()->write_to_disk_timer_.IsRunning());
625 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time()); 643 EXPECT_LT(expected_trigger, index()->write_to_disk_timer_.desired_run_time());
626 index()->write_to_disk_timer_.Stop(); 644 index()->write_to_disk_timer_.Stop();
627 } 645 }
628 646
629 } // namespace disk_cache 647 } // namespace disk_cache
OLDNEW
« no previous file with comments | « net/disk_cache/simple/simple_index.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698