summaryrefslogtreecommitdiffstats
path: root/components/leveldb_proto/proto_database_impl_unittest.cc
diff options
context:
space:
mode:
authorpeter <peter@chromium.org>2015-07-30 07:03:14 -0700
committerCommit bot <commit-bot@chromium.org>2015-07-30 14:03:44 +0000
commit7cdc93ca18e82e9f3849edccbc2ac11856b5a3b5 (patch)
tree31bdce9e8631b869c88f3ac8f2c31a8ab1d0b34e /components/leveldb_proto/proto_database_impl_unittest.cc
parent0f04f123d77fb5ae1ad4c0296bf92a4e7965e93a (diff)
downloadchromium_src-7cdc93ca18e82e9f3849edccbc2ac11856b5a3b5.zip
chromium_src-7cdc93ca18e82e9f3849edccbc2ac11856b5a3b5.tar.gz
chromium_src-7cdc93ca18e82e9f3849edccbc2ac11856b5a3b5.tar.bz2
Clean-ups in the leveldb_proto component.
The main intention of this change is to trim down on reference churn by passing callbacks and scoped_refptrs as constant references. While in the area, however, I also updated a bunch of other old habits in favor of better C++11-y habits. R=cjhopman BUG= Review URL: https://codereview.chromium.org/1262713004 Cr-Commit-Position: refs/heads/master@{#341110}
Diffstat (limited to 'components/leveldb_proto/proto_database_impl_unittest.cc')
-rw-r--r--components/leveldb_proto/proto_database_impl_unittest.cc54
1 files changed, 29 insertions, 25 deletions
diff --git a/components/leveldb_proto/proto_database_impl_unittest.cc b/components/leveldb_proto/proto_database_impl_unittest.cc
index 7ac19a5..76b4ae5 100644
--- a/components/leveldb_proto/proto_database_impl_unittest.cc
+++ b/components/leveldb_proto/proto_database_impl_unittest.cc
@@ -112,7 +112,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBInitSuccess) {
EXPECT_CALL(caller, InitCallback(true));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
@@ -128,7 +128,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) {
EXPECT_CALL(caller, InitCallback(false));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
base::RunLoop().RunUntilIdle();
@@ -136,9 +136,9 @@ TEST_F(ProtoDatabaseImplTest, TestDBInitFailure) {
ACTION_P(AppendLoadEntries, model) {
std::vector<std::string>* output = arg0;
- for (EntryMap::const_iterator it = model.begin(); it != model.end(); ++it) {
- output->push_back(it->second.SerializeAsString());
- }
+ for (const auto& pair : model)
+ output->push_back(pair.second.SerializeAsString());
+
return true;
}
@@ -161,7 +161,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBLoadSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Load(_)).WillOnce(AppendLoadEntries(model));
@@ -182,7 +182,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBLoadFailure) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Load(_)).WillOnce(Return(false));
@@ -198,12 +198,16 @@ ACTION_P(VerifyUpdateEntries, expected) {
// Create a vector of TestProto from |actual| to reuse the comparison
// function.
std::vector<TestProto> extracted_entries;
- for (KeyValueVector::const_iterator it = actual.begin(); it != actual.end();
- ++it) {
+ for (const auto& pair : actual) {
TestProto entry;
- entry.ParseFromString(it->second);
+ if (!entry.ParseFromString(pair.second)) {
+ ADD_FAILURE() << "Unable to deserialize the protobuf";
+ return false;
+ }
+
extracted_entries.push_back(entry);
}
+
ExpectEntryPointersEquals(expected, extracted_entries);
return true;
}
@@ -221,14 +225,14 @@ TEST_F(ProtoDatabaseImplTest, TestDBSaveSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
new ProtoDatabase<TestProto>::KeyEntryVector());
- for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
- entries->push_back(std::make_pair(it->second.id(), it->second));
- }
+ for (const auto& pair : model)
+ entries->push_back(std::make_pair(pair.second.id(), pair.second));
+
scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(VerifyUpdateEntries(model));
@@ -252,7 +256,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBSaveFailure) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
@@ -277,15 +281,14 @@ TEST_F(ProtoDatabaseImplTest, TestDBRemoveSuccess) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
scoped_ptr<ProtoDatabase<TestProto>::KeyEntryVector> entries(
new ProtoDatabase<TestProto>::KeyEntryVector());
scoped_ptr<KeyVector> keys_to_remove(new KeyVector());
- for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
- keys_to_remove->push_back(it->second.id());
- }
+ for (const auto& pair : model)
+ keys_to_remove->push_back(pair.second.id());
KeyVector keys_copy(*keys_to_remove.get());
EXPECT_CALL(*mock_db, Save(_, keys_copy)).WillOnce(Return(true));
@@ -309,7 +312,7 @@ TEST_F(ProtoDatabaseImplTest, TestDBRemoveFailure) {
EXPECT_CALL(*mock_db, Init(_));
EXPECT_CALL(caller, InitCallback(_));
db_->InitWithDatabase(
- scoped_ptr<LevelDB>(mock_db), base::FilePath(path),
+ make_scoped_ptr(mock_db), path,
base::Bind(&MockDatabaseCaller::InitCallback, base::Unretained(&caller)));
EXPECT_CALL(*mock_db, Save(_, _)).WillOnce(Return(false));
@@ -361,9 +364,9 @@ void TestLevelDBSaveAndLoad(bool close_after_save) {
std::vector<std::string> load_entries;
KeyVector remove_keys;
- for (EntryMap::iterator it = model.begin(); it != model.end(); ++it) {
+ for (const auto& pair : model) {
save_entries.push_back(
- std::make_pair(it->second.id(), it->second.SerializeAsString()));
+ std::make_pair(pair.second.id(), pair.second.SerializeAsString()));
}
scoped_ptr<LevelDB> db(new LevelDB());
@@ -376,14 +379,15 @@ void TestLevelDBSaveAndLoad(bool close_after_save) {
}
EXPECT_TRUE(db->Load(&load_entries));
+
// Convert the strings back to TestProto.
std::vector<TestProto> loaded_protos;
- for (std::vector<std::string>::iterator it = load_entries.begin();
- it != load_entries.end(); ++it) {
+ for (const auto& serialized_entry : load_entries) {
TestProto entry;
- entry.ParseFromString(*it);
+ ASSERT_TRUE(entry.ParseFromString(serialized_entry));
loaded_protos.push_back(entry);
}
+
ExpectEntryPointersEquals(model, loaded_protos);
}