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

Unified Diff: net/disk_cache/simple/simple_index_file_unittest.cc

Issue 14263005: Refactor our SimpleIndex file format and serialization to use Pickle instead of the previously bugg… (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 7 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 side-by-side diff with in-line comments
Download patch
Index: net/disk_cache/simple/simple_index_file_unittest.cc
diff --git a/net/disk_cache/simple/simple_index_file_unittest.cc b/net/disk_cache/simple/simple_index_file_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..32afc30acc3bddcb2c92ce3f5aabf981df459886
--- /dev/null
+++ b/net/disk_cache/simple/simple_index_file_unittest.cc
@@ -0,0 +1,92 @@
+// Copyright (c) 2011 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
Philippe 2013/04/16 11:58:47 base/basictypes.h for arrayzise()
+#include "base/hash.h"
+#include "base/logging.h"
+#include "base/memory/scoped_ptr.h"
+#include "base/pickle.h"
+#include "base/stringprintf.h"
+#include "base/time.h"
+#include "net/disk_cache/simple/simple_entry_format.h"
+#include "net/disk_cache/simple/simple_index.h"
+#include "net/disk_cache/simple/simple_index_file.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using base::Time;
+using disk_cache::SimpleIndexFile;
+using disk_cache::SimpleIndex;
+
+namespace disk_cache {
+
+class IndexMetadataTest : public testing::Test {};
+
+TEST_F(IndexMetadataTest, Basics) {
+ SimpleIndexFile::IndexMetadata index_metadata;
+
+ EXPECT_EQ(disk_cache::kSimpleIndexMagicNumber, index_metadata.magic_number_);
+ EXPECT_EQ(disk_cache::kSimpleVersion, index_metadata.version_);
+ EXPECT_EQ(0U, index_metadata.GetNumberOfEntries());
+ EXPECT_EQ(0U, index_metadata.cache_size_);
+
+ EXPECT_TRUE(index_metadata.CheckIndexMetadata());
+}
+
+TEST_F(IndexMetadataTest, Serialize) {
+ SimpleIndexFile::IndexMetadata index_metadata(123, 456);
+ Pickle pickle;
+ index_metadata.Serialize(&pickle);
+ PickleIterator it(pickle);
+ SimpleIndexFile::IndexMetadata new_index_metadata;
+ new_index_metadata.Deserialize(&it);
+
+ EXPECT_EQ(new_index_metadata.magic_number_, index_metadata.magic_number_);
+ EXPECT_EQ(new_index_metadata.version_, index_metadata.version_);
+ EXPECT_EQ(new_index_metadata.GetNumberOfEntries(),
+ index_metadata.GetNumberOfEntries());
+ EXPECT_EQ(new_index_metadata.cache_size_, index_metadata.cache_size_);
+
+ EXPECT_TRUE(new_index_metadata.CheckIndexMetadata());
+}
+
+class SimpleIndexFileTest : public testing::Test {
+ public:
+ bool CompareTwoEntryMetadata(const EntryMetadata& a, const EntryMetadata& b) {
Philippe 2013/04/16 11:58:47 Nit: this should be a static method (or a const me
+ return a.hash_key_ == b.hash_key_ &&
+ a.last_used_time_ == b.last_used_time_ &&
+ a.entry_size_ == b.entry_size_;
+ }
+
Philippe 2013/04/16 11:58:47 Nit: extra blank line.
+};
+
+TEST_F(SimpleIndexFileTest, Serialize) {
+ SimpleIndex::EntrySet entries;
+ EntryMetadata entries_array[] = {
+ EntryMetadata(11, Time::FromInternalValue(22), 33),
+ EntryMetadata(22, Time::FromInternalValue(33), 44),
+ EntryMetadata(33, Time::FromInternalValue(44), 55)
+ };
+ SimpleIndexFile::IndexMetadata index_metadata(arraysize(entries_array), 456);
+ for (uint32 i = 0; i < arraysize(entries_array); ++i) {
+ SimpleIndex::InsertInEntrySet(entries_array[i], &entries);
+ }
+
+ scoped_ptr<Pickle> pickle = SimpleIndexFile::Serialize(
+ index_metadata, entries);
+ EXPECT_TRUE(pickle.get() != NULL);
+
+ scoped_ptr<SimpleIndex::EntrySet> new_entries = SimpleIndexFile::Deserialize(
+ reinterpret_cast<const char*>(pickle->data()),
+ pickle->size());
+ EXPECT_TRUE(new_entries.get() != NULL);
+ EXPECT_EQ(entries.size(), new_entries->size());
+
+ for (uint32 i = 0; i < arraysize(entries_array); ++i) {
+ SimpleIndex::EntrySet::iterator it =
+ new_entries->find(entries_array[i].GetHashKey());
+ EXPECT_TRUE(new_entries->end() != it);
+ EXPECT_TRUE(CompareTwoEntryMetadata(it->second, entries_array[i]));
+ }
+}
+
+}

Powered by Google App Engine
This is Rietveld 408576698