OLD | NEW |
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 "content/browser/indexed_db/indexed_db_leveldb_coding.h" | 5 #include "content/browser/indexed_db/indexed_db_leveldb_coding.h" |
6 | 6 |
| 7 #include <stddef.h> |
| 8 #include <stdint.h> |
| 9 |
7 #include <limits> | 10 #include <limits> |
8 #include <vector> | 11 #include <vector> |
9 | 12 |
10 #include "base/basictypes.h" | 13 #include "base/macros.h" |
11 #include "base/strings/string16.h" | 14 #include "base/strings/string16.h" |
12 #include "base/strings/string_piece.h" | 15 #include "base/strings/string_piece.h" |
13 #include "base/strings/utf_string_conversions.h" | 16 #include "base/strings/utf_string_conversions.h" |
14 #include "content/common/indexed_db/indexed_db_key.h" | 17 #include "content/common/indexed_db/indexed_db_key.h" |
15 #include "content/common/indexed_db/indexed_db_key_path.h" | 18 #include "content/common/indexed_db/indexed_db_key_path.h" |
16 #include "testing/gtest/include/gtest/gtest.h" | 19 #include "testing/gtest/include/gtest/gtest.h" |
17 | 20 |
18 using base::ASCIIToUTF16; | 21 using base::ASCIIToUTF16; |
19 using base::StringPiece; | 22 using base::StringPiece; |
20 using blink::WebIDBKeyTypeDate; | 23 using blink::WebIDBKeyTypeDate; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 EncodeIDBKey(IndexedDBKey(1000000, WebIDBKeyTypeDate), &date_key); | 165 EncodeIDBKey(IndexedDBKey(1000000, WebIDBKeyTypeDate), &date_key); |
163 | 166 |
164 EXPECT_LT(CompareKeys(min_key, max_key), 0); | 167 EXPECT_LT(CompareKeys(min_key, max_key), 0); |
165 EXPECT_LT(CompareKeys(min_key, array_key), 0); | 168 EXPECT_LT(CompareKeys(min_key, array_key), 0); |
166 EXPECT_LT(CompareKeys(min_key, binary_key), 0); | 169 EXPECT_LT(CompareKeys(min_key, binary_key), 0); |
167 EXPECT_LT(CompareKeys(min_key, string_key), 0); | 170 EXPECT_LT(CompareKeys(min_key, string_key), 0); |
168 EXPECT_LT(CompareKeys(min_key, number_key), 0); | 171 EXPECT_LT(CompareKeys(min_key, number_key), 0); |
169 EXPECT_LT(CompareKeys(min_key, date_key), 0); | 172 EXPECT_LT(CompareKeys(min_key, date_key), 0); |
170 } | 173 } |
171 | 174 |
172 static std::string WrappedEncodeInt(int64 value) { | 175 static std::string WrappedEncodeInt(int64_t value) { |
173 std::string buffer; | 176 std::string buffer; |
174 EncodeInt(value, &buffer); | 177 EncodeInt(value, &buffer); |
175 return buffer; | 178 return buffer; |
176 } | 179 } |
177 | 180 |
178 TEST(IndexedDBLevelDBCodingTest, EncodeInt) { | 181 TEST(IndexedDBLevelDBCodingTest, EncodeInt) { |
179 EXPECT_EQ(1u, WrappedEncodeInt(0).size()); | 182 EXPECT_EQ(1u, WrappedEncodeInt(0).size()); |
180 EXPECT_EQ(1u, WrappedEncodeInt(1).size()); | 183 EXPECT_EQ(1u, WrappedEncodeInt(1).size()); |
181 EXPECT_EQ(1u, WrappedEncodeInt(255).size()); | 184 EXPECT_EQ(1u, WrappedEncodeInt(255).size()); |
182 EXPECT_EQ(2u, WrappedEncodeInt(256).size()); | 185 EXPECT_EQ(2u, WrappedEncodeInt(256).size()); |
(...skipping 23 matching lines...) Expand all Loading... |
206 EXPECT_TRUE(slice.empty()); | 209 EXPECT_TRUE(slice.empty()); |
207 } | 210 } |
208 { | 211 { |
209 StringPiece slice; | 212 StringPiece slice; |
210 bool value; | 213 bool value; |
211 EXPECT_FALSE(DecodeBool(&slice, &value)); | 214 EXPECT_FALSE(DecodeBool(&slice, &value)); |
212 } | 215 } |
213 } | 216 } |
214 | 217 |
215 TEST(IndexedDBLevelDBCodingTest, DecodeInt) { | 218 TEST(IndexedDBLevelDBCodingTest, DecodeInt) { |
216 std::vector<int64> test_cases; | 219 std::vector<int64_t> test_cases; |
217 test_cases.push_back(0); | 220 test_cases.push_back(0); |
218 test_cases.push_back(1); | 221 test_cases.push_back(1); |
219 test_cases.push_back(255); | 222 test_cases.push_back(255); |
220 test_cases.push_back(256); | 223 test_cases.push_back(256); |
221 test_cases.push_back(65535); | 224 test_cases.push_back(65535); |
222 test_cases.push_back(655536); | 225 test_cases.push_back(655536); |
223 test_cases.push_back(7711192431755665792ll); | 226 test_cases.push_back(7711192431755665792ll); |
224 test_cases.push_back(0x7fffffffffffffffll); | 227 test_cases.push_back(0x7fffffffffffffffll); |
225 #ifdef NDEBUG | 228 #ifdef NDEBUG |
226 test_cases.push_back(-3); | 229 test_cases.push_back(-3); |
227 #endif | 230 #endif |
228 | 231 |
229 for (size_t i = 0; i < test_cases.size(); ++i) { | 232 for (size_t i = 0; i < test_cases.size(); ++i) { |
230 int64 n = test_cases[i]; | 233 int64_t n = test_cases[i]; |
231 std::string v = WrappedEncodeInt(n); | 234 std::string v = WrappedEncodeInt(n); |
232 ASSERT_GT(v.size(), 0u); | 235 ASSERT_GT(v.size(), 0u); |
233 StringPiece slice(v); | 236 StringPiece slice(v); |
234 int64 value; | 237 int64_t value; |
235 EXPECT_TRUE(DecodeInt(&slice, &value)); | 238 EXPECT_TRUE(DecodeInt(&slice, &value)); |
236 EXPECT_EQ(n, value); | 239 EXPECT_EQ(n, value); |
237 EXPECT_TRUE(slice.empty()); | 240 EXPECT_TRUE(slice.empty()); |
238 | 241 |
239 // Verify decoding at an offset, to detect unaligned memory access. | 242 // Verify decoding at an offset, to detect unaligned memory access. |
240 v.insert(v.begin(), 1u, static_cast<char>(0)); | 243 v.insert(v.begin(), 1u, static_cast<char>(0)); |
241 slice = StringPiece(&*v.begin() + 1, v.size() - 1); | 244 slice = StringPiece(&*v.begin() + 1, v.size() - 1); |
242 EXPECT_TRUE(DecodeInt(&slice, &value)); | 245 EXPECT_TRUE(DecodeInt(&slice, &value)); |
243 EXPECT_EQ(n, value); | 246 EXPECT_EQ(n, value); |
244 EXPECT_TRUE(slice.empty()); | 247 EXPECT_TRUE(slice.empty()); |
245 } | 248 } |
246 { | 249 { |
247 StringPiece slice; | 250 StringPiece slice; |
248 int64 value; | 251 int64_t value; |
249 EXPECT_FALSE(DecodeInt(&slice, &value)); | 252 EXPECT_FALSE(DecodeInt(&slice, &value)); |
250 } | 253 } |
251 } | 254 } |
252 | 255 |
253 static std::string WrappedEncodeVarInt(int64 value) { | 256 static std::string WrappedEncodeVarInt(int64_t value) { |
254 std::string buffer; | 257 std::string buffer; |
255 EncodeVarInt(value, &buffer); | 258 EncodeVarInt(value, &buffer); |
256 return buffer; | 259 return buffer; |
257 } | 260 } |
258 | 261 |
259 TEST(IndexedDBLevelDBCodingTest, EncodeVarInt) { | 262 TEST(IndexedDBLevelDBCodingTest, EncodeVarInt) { |
260 EXPECT_EQ(1u, WrappedEncodeVarInt(0).size()); | 263 EXPECT_EQ(1u, WrappedEncodeVarInt(0).size()); |
261 EXPECT_EQ(1u, WrappedEncodeVarInt(1).size()); | 264 EXPECT_EQ(1u, WrappedEncodeVarInt(1).size()); |
262 EXPECT_EQ(2u, WrappedEncodeVarInt(255).size()); | 265 EXPECT_EQ(2u, WrappedEncodeVarInt(255).size()); |
263 EXPECT_EQ(2u, WrappedEncodeVarInt(256).size()); | 266 EXPECT_EQ(2u, WrappedEncodeVarInt(256).size()); |
264 EXPECT_EQ(5u, WrappedEncodeVarInt(0xffffffff).size()); | 267 EXPECT_EQ(5u, WrappedEncodeVarInt(0xffffffff).size()); |
265 EXPECT_EQ(8u, WrappedEncodeVarInt(0xfffffffffffffLL).size()); | 268 EXPECT_EQ(8u, WrappedEncodeVarInt(0xfffffffffffffLL).size()); |
266 EXPECT_EQ(9u, WrappedEncodeVarInt(0x7fffffffffffffffLL).size()); | 269 EXPECT_EQ(9u, WrappedEncodeVarInt(0x7fffffffffffffffLL).size()); |
267 #ifdef NDEBUG | 270 #ifdef NDEBUG |
268 EXPECT_EQ(10u, WrappedEncodeVarInt(-100).size()); | 271 EXPECT_EQ(10u, WrappedEncodeVarInt(-100).size()); |
269 #endif | 272 #endif |
270 } | 273 } |
271 | 274 |
272 TEST(IndexedDBLevelDBCodingTest, DecodeVarInt) { | 275 TEST(IndexedDBLevelDBCodingTest, DecodeVarInt) { |
273 std::vector<int64> test_cases; | 276 std::vector<int64_t> test_cases; |
274 test_cases.push_back(0); | 277 test_cases.push_back(0); |
275 test_cases.push_back(1); | 278 test_cases.push_back(1); |
276 test_cases.push_back(255); | 279 test_cases.push_back(255); |
277 test_cases.push_back(256); | 280 test_cases.push_back(256); |
278 test_cases.push_back(65535); | 281 test_cases.push_back(65535); |
279 test_cases.push_back(655536); | 282 test_cases.push_back(655536); |
280 test_cases.push_back(7711192431755665792ll); | 283 test_cases.push_back(7711192431755665792ll); |
281 test_cases.push_back(0x7fffffffffffffffll); | 284 test_cases.push_back(0x7fffffffffffffffll); |
282 #ifdef NDEBUG | 285 #ifdef NDEBUG |
283 test_cases.push_back(-3); | 286 test_cases.push_back(-3); |
284 #endif | 287 #endif |
285 | 288 |
286 for (size_t i = 0; i < test_cases.size(); ++i) { | 289 for (size_t i = 0; i < test_cases.size(); ++i) { |
287 int64 n = test_cases[i]; | 290 int64_t n = test_cases[i]; |
288 std::string v = WrappedEncodeVarInt(n); | 291 std::string v = WrappedEncodeVarInt(n); |
289 ASSERT_GT(v.size(), 0u); | 292 ASSERT_GT(v.size(), 0u); |
290 StringPiece slice(v); | 293 StringPiece slice(v); |
291 int64 res; | 294 int64_t res; |
292 EXPECT_TRUE(DecodeVarInt(&slice, &res)); | 295 EXPECT_TRUE(DecodeVarInt(&slice, &res)); |
293 EXPECT_EQ(n, res); | 296 EXPECT_EQ(n, res); |
294 EXPECT_TRUE(slice.empty()); | 297 EXPECT_TRUE(slice.empty()); |
295 | 298 |
296 slice = StringPiece(&*v.begin(), v.size() - 1); | 299 slice = StringPiece(&*v.begin(), v.size() - 1); |
297 EXPECT_FALSE(DecodeVarInt(&slice, &res)); | 300 EXPECT_FALSE(DecodeVarInt(&slice, &res)); |
298 | 301 |
299 slice = StringPiece(&*v.begin(), static_cast<size_t>(0)); | 302 slice = StringPiece(&*v.begin(), static_cast<size_t>(0)); |
300 EXPECT_FALSE(DecodeVarInt(&slice, &res)); | 303 EXPECT_FALSE(DecodeVarInt(&slice, &res)); |
301 | 304 |
(...skipping 640 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
942 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 0)); | 945 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 0)); |
943 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 1)); | 946 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MinIDBKey(), 1)); |
944 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 0)); | 947 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 0)); |
945 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 1)); | 948 keys.push_back(IndexDataKey::Encode(1, 1, 30, MinIDBKey(), MaxIDBKey(), 1)); |
946 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 0)); | 949 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 0)); |
947 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 1)); | 950 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MinIDBKey(), 1)); |
948 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 0)); | 951 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 0)); |
949 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 1)); | 952 keys.push_back(IndexDataKey::Encode(1, 1, 30, MaxIDBKey(), MaxIDBKey(), 1)); |
950 keys.push_back(IndexDataKey::Encode(1, 1, 31, MinIDBKey(), MinIDBKey(), 0)); | 953 keys.push_back(IndexDataKey::Encode(1, 1, 31, MinIDBKey(), MinIDBKey(), 0)); |
951 keys.push_back(IndexDataKey::Encode(1, 2, 30, MinIDBKey(), MinIDBKey(), 0)); | 954 keys.push_back(IndexDataKey::Encode(1, 2, 30, MinIDBKey(), MinIDBKey(), 0)); |
952 keys.push_back( | 955 keys.push_back(IndexDataKey::EncodeMaxKey( |
953 IndexDataKey::EncodeMaxKey(1, 2, std::numeric_limits<int32>::max() - 1)); | 956 1, 2, std::numeric_limits<int32_t>::max() - 1)); |
954 | 957 |
955 for (size_t i = 0; i < keys.size(); ++i) { | 958 for (size_t i = 0; i < keys.size(); ++i) { |
956 EXPECT_EQ(Compare(keys[i], keys[i], false), 0); | 959 EXPECT_EQ(Compare(keys[i], keys[i], false), 0); |
957 | 960 |
958 for (size_t j = i + 1; j < keys.size(); ++j) { | 961 for (size_t j = i + 1; j < keys.size(); ++j) { |
959 EXPECT_LT(Compare(keys[i], keys[j], false), 0); | 962 EXPECT_LT(Compare(keys[i], keys[j], false), 0); |
960 EXPECT_GT(Compare(keys[j], keys[i], false), 0); | 963 EXPECT_GT(Compare(keys[j], keys[i], false), 0); |
961 } | 964 } |
962 } | 965 } |
963 } | 966 } |
964 | 967 |
965 TEST(IndexedDBLevelDBCodingTest, EncodeVarIntVSEncodeByteTest) { | 968 TEST(IndexedDBLevelDBCodingTest, EncodeVarIntVSEncodeByteTest) { |
966 std::vector<unsigned char> test_cases; | 969 std::vector<unsigned char> test_cases; |
967 test_cases.push_back(0); | 970 test_cases.push_back(0); |
968 test_cases.push_back(1); | 971 test_cases.push_back(1); |
969 test_cases.push_back(127); | 972 test_cases.push_back(127); |
970 | 973 |
971 for (size_t i = 0; i < test_cases.size(); ++i) { | 974 for (size_t i = 0; i < test_cases.size(); ++i) { |
972 unsigned char n = test_cases[i]; | 975 unsigned char n = test_cases[i]; |
973 | 976 |
974 std::string a = WrappedEncodeByte(n); | 977 std::string a = WrappedEncodeByte(n); |
975 std::string b = WrappedEncodeVarInt(static_cast<int64>(n)); | 978 std::string b = WrappedEncodeVarInt(static_cast<int64_t>(n)); |
976 | 979 |
977 EXPECT_EQ(a.size(), b.size()); | 980 EXPECT_EQ(a.size(), b.size()); |
978 EXPECT_EQ(*a.begin(), *b.begin()); | 981 EXPECT_EQ(*a.begin(), *b.begin()); |
979 } | 982 } |
980 } | 983 } |
981 | 984 |
982 } // namespace | 985 } // namespace |
983 | 986 |
984 } // namespace content | 987 } // namespace content |
OLD | NEW |