| OLD | NEW |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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 <stddef.h> | 5 #include <stddef.h> |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <cstring> | 8 #include <cstring> |
| 9 #include <string> | 9 #include <string> |
| 10 | 10 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 99 int Compare(const base::StringPiece& a, const base::StringPiece& b) const { | 99 int Compare(const base::StringPiece& a, const base::StringPiece& b) const { |
| 100 return comparator_.Compare(a, b); | 100 return comparator_.Compare(a, b); |
| 101 } | 101 } |
| 102 | 102 |
| 103 LevelDBDatabase* db() { return leveldb_.get(); } | 103 LevelDBDatabase* db() { return leveldb_.get(); } |
| 104 | 104 |
| 105 scoped_refptr<LevelDBTransaction> CreateTransaction() { | 105 scoped_refptr<LevelDBTransaction> CreateTransaction() { |
| 106 return new LevelDBTransaction(db()); | 106 return new LevelDBTransaction(db()); |
| 107 } | 107 } |
| 108 | 108 |
| 109 static constexpr size_t SizeOfRecord() { |
| 110 return sizeof(LevelDBTransaction::Record); |
| 111 } |
| 112 |
| 109 private: | 113 private: |
| 110 base::ScopedTempDir temp_directory_; | 114 base::ScopedTempDir temp_directory_; |
| 111 SimpleComparator comparator_; | 115 SimpleComparator comparator_; |
| 112 std::unique_ptr<LevelDBDatabase> leveldb_; | 116 std::unique_ptr<LevelDBDatabase> leveldb_; |
| 113 | 117 |
| 114 DISALLOW_COPY_AND_ASSIGN(LevelDBTransactionTest); | 118 DISALLOW_COPY_AND_ASSIGN(LevelDBTransactionTest); |
| 115 }; | 119 }; |
| 116 | 120 |
| 117 TEST_F(LevelDBTransactionTest, GetAndPut) { | 121 TEST_F(LevelDBTransactionTest, GetPutDelete) { |
| 118 leveldb::Status status; | 122 leveldb::Status status; |
| 119 | 123 |
| 120 const std::string key("key"); | 124 const std::string key("key"); |
| 121 std::string got_value; | 125 std::string got_value; |
| 122 | 126 |
| 123 const std::string old_value("value"); | 127 const std::string old_value("value"); |
| 124 Put(key, old_value); | 128 Put(key, old_value); |
| 125 | 129 |
| 126 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); | 130 scoped_refptr<LevelDBTransaction> transaction = new LevelDBTransaction(db()); |
| 127 | 131 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 143 Put(added_key, added_value); | 147 Put(added_key, added_value); |
| 144 | 148 |
| 145 Get(added_key, &got_value, &found); | 149 Get(added_key, &got_value, &found); |
| 146 EXPECT_TRUE(found); | 150 EXPECT_TRUE(found); |
| 147 EXPECT_EQ(Compare(got_value, added_value), 0); | 151 EXPECT_EQ(Compare(got_value, added_value), 0); |
| 148 | 152 |
| 149 EXPECT_FALSE(TransactionHas(transaction.get(), added_key)); | 153 EXPECT_FALSE(TransactionHas(transaction.get(), added_key)); |
| 150 | 154 |
| 151 const std::string another_key("another key"); | 155 const std::string another_key("another key"); |
| 152 const std::string another_value("another value"); | 156 const std::string another_value("another value"); |
| 157 EXPECT_EQ(0ull, transaction->GetTransactionSize()); |
| 153 TransactionPut(transaction.get(), another_key, another_value); | 158 TransactionPut(transaction.get(), another_key, another_value); |
| 159 EXPECT_EQ(SizeOfRecord() + another_key.size() * 2 + another_value.size(), |
| 160 transaction->GetTransactionSize()); |
| 154 | 161 |
| 155 status = transaction->Get(another_key, &got_value, &found); | 162 status = transaction->Get(another_key, &got_value, &found); |
| 156 EXPECT_TRUE(status.ok()); | 163 EXPECT_TRUE(status.ok()); |
| 157 EXPECT_TRUE(found); | 164 EXPECT_TRUE(found); |
| 158 EXPECT_EQ(Compare(got_value, another_value), 0); | 165 EXPECT_EQ(Compare(got_value, another_value), 0); |
| 166 |
| 167 transaction->Remove(another_key); |
| 168 EXPECT_EQ(SizeOfRecord() + another_key.size() * 2, |
| 169 transaction->GetTransactionSize()); |
| 159 } | 170 } |
| 160 | 171 |
| 161 TEST_F(LevelDBTransactionTest, Iterator) { | 172 TEST_F(LevelDBTransactionTest, Iterator) { |
| 162 const std::string key1("key1"); | 173 const std::string key1("key1"); |
| 163 const std::string value1("value1"); | 174 const std::string value1("value1"); |
| 164 const std::string key2("key2"); | 175 const std::string key2("key2"); |
| 165 const std::string value2("value2"); | 176 const std::string value2("value2"); |
| 166 | 177 |
| 167 Put(key1, value1); | 178 Put(key1, value1); |
| 168 Put(key2, value2); | 179 Put(key2, value2); |
| (...skipping 254 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 423 EXPECT_EQ(Compare(key_after_range_, it->Key()), 0); | 434 EXPECT_EQ(Compare(key_after_range_, it->Key()), 0); |
| 424 } | 435 } |
| 425 | 436 |
| 426 INSTANTIATE_TEST_CASE_P(LevelDBTransactionRangeTests, | 437 INSTANTIATE_TEST_CASE_P(LevelDBTransactionRangeTests, |
| 427 LevelDBTransactionRangeTest, | 438 LevelDBTransactionRangeTest, |
| 428 ::testing::Values(DataInMemory, | 439 ::testing::Values(DataInMemory, |
| 429 DataInDatabase, | 440 DataInDatabase, |
| 430 DataMixed)); | 441 DataMixed)); |
| 431 | 442 |
| 432 } // namespace content | 443 } // namespace content |
| OLD | NEW |