1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
|
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "components/services/storage/indexed_db/scopes/varint_coding.h"
#include <string_view>
#include "base/dcheck_is_on.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace content::indexed_db {
namespace {
static std::string WrappedEncodeVarInt(int64_t value) {
std::string buffer;
EncodeVarInt(value, &buffer);
return buffer;
}
TEST(VarIntCoding, Encode) {
EXPECT_EQ(1u, WrappedEncodeVarInt(0).size());
EXPECT_EQ(1u, WrappedEncodeVarInt(1).size());
EXPECT_EQ(2u, WrappedEncodeVarInt(255).size());
EXPECT_EQ(2u, WrappedEncodeVarInt(256).size());
EXPECT_EQ(5u, WrappedEncodeVarInt(0xffffffff).size());
EXPECT_EQ(8u, WrappedEncodeVarInt(0xfffffffffffffll).size());
EXPECT_EQ(9u, WrappedEncodeVarInt(0x7fffffffffffffffll).size());
#if !DCHECK_IS_ON()
EXPECT_EQ(10u, WrappedEncodeVarInt(-100).size());
#endif
}
TEST(VarIntCoding, Decode) {
std::vector<int64_t> test_cases = {
0,
1,
255,
256,
65535,
655536,
7711192431755665792ll,
0x7fffffffffffffffll,
#if !DCHECK_IS_ON()
-3,
#endif
};
for (size_t i = 0; i < test_cases.size(); ++i) {
int64_t n = test_cases[i];
std::string v = WrappedEncodeVarInt(n);
ASSERT_GT(v.size(), 0u);
std::string_view slice(v);
int64_t res;
EXPECT_TRUE(DecodeVarInt(&slice, &res));
EXPECT_EQ(n, res);
EXPECT_TRUE(slice.empty());
slice = std::string_view(&*v.begin(), v.size() - 1);
EXPECT_FALSE(DecodeVarInt(&slice, &res));
slice = std::string_view(&*v.begin(), static_cast<size_t>(0));
EXPECT_FALSE(DecodeVarInt(&slice, &res));
// Verify decoding at an offset, to detect unaligned memory access.
v.insert(v.begin(), 1u, static_cast<char>(0));
slice = std::string_view(v).substr(1u);
EXPECT_TRUE(DecodeVarInt(&slice, &res));
EXPECT_EQ(n, res);
EXPECT_TRUE(slice.empty());
}
}
TEST(VarIntCoding, SingleByteCases) {
std::vector<unsigned char> test_cases = {0, 1, 127};
for (size_t i = 0; i < test_cases.size(); ++i) {
unsigned char n = test_cases[i];
std::string a = std::string(1, n);
std::string b = WrappedEncodeVarInt(static_cast<int64_t>(n));
EXPECT_EQ(a.size(), b.size());
EXPECT_EQ(*a.begin(), *b.begin());
}
}
} // namespace
} // namespace content::indexed_db
|