Add BufferWithExtendablebufferTest
Bug: 13754552 Change-Id: Ib9c330dc85bdb5d4d3d5bc15cc3ce5f28faa0b12
This commit is contained in:
parent
ff6ca09905
commit
d4c9d50ef0
3 changed files with 98 additions and 2 deletions
|
@ -124,4 +124,5 @@ LATIN_IME_CORE_TEST_FILES := \
|
|||
defines_test.cpp \
|
||||
suggest/core/layout/normal_distribution_2d_test.cpp \
|
||||
suggest/core/dictionary/bloom_filter_test.cpp \
|
||||
suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer_test.cpp \
|
||||
utils/autocorrection_threshold_utils_test.cpp
|
||||
|
|
|
@ -102,8 +102,9 @@ bool BufferWithExtendableBuffer::writeCodePointsAndAdvancePosition(const int *co
|
|||
|
||||
bool BufferWithExtendableBuffer::extendBuffer(const size_t size) {
|
||||
const size_t extendSize = std::max(EXTEND_ADDITIONAL_BUFFER_SIZE_STEP, size);
|
||||
const size_t sizeAfterExtending = mAdditionalBuffer.size() + extendSize;
|
||||
if (sizeAfterExtending > mMaxAdditionalBufferSize) {
|
||||
const size_t sizeAfterExtending =
|
||||
std::min(mAdditionalBuffer.size() + extendSize, mMaxAdditionalBufferSize);
|
||||
if (sizeAfterExtending < mAdditionalBuffer.size() + size) {
|
||||
return false;
|
||||
}
|
||||
mAdditionalBuffer.resize(sizeAfterExtending);
|
||||
|
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright (C) 2014 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "suggest/policyimpl/dictionary/utils/buffer_with_extendable_buffer.h"
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
namespace latinime {
|
||||
namespace {
|
||||
|
||||
const int DEFAULT_MAX_BUFFER_SIZE = 1024;
|
||||
|
||||
TEST(BufferWithExtendablebufferTest, TestWriteAndRead) {
|
||||
BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
|
||||
int pos = 0;
|
||||
// 1 byte
|
||||
const uint32_t data_1 = 0xFF;
|
||||
EXPECT_TRUE(buffer.writeUint(data_1, 1 /* size */, pos));
|
||||
EXPECT_EQ(data_1, buffer.readUint(1, pos));
|
||||
pos += 1;
|
||||
// 2 byte
|
||||
const uint32_t data_2 = 0xFFFF;
|
||||
EXPECT_TRUE(buffer.writeUint(data_2, 2 /* size */, pos));
|
||||
EXPECT_EQ(data_2, buffer.readUint(2, pos));
|
||||
pos += 2;
|
||||
// 3 byte
|
||||
const uint32_t data_3 = 0xFFFFFF;
|
||||
EXPECT_TRUE(buffer.writeUint(data_3, 3 /* size */, pos));
|
||||
EXPECT_EQ(data_3, buffer.readUint(3, pos));
|
||||
pos += 3;
|
||||
// 4 byte
|
||||
const uint32_t data_4 = 0xFFFFFFFF;
|
||||
EXPECT_TRUE(buffer.writeUint(data_4, 4 /* size */, pos));
|
||||
EXPECT_EQ(data_4, buffer.readUint(4, pos));
|
||||
}
|
||||
|
||||
TEST(BufferWithExtendablebufferTest, TestExtend) {
|
||||
BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
|
||||
EXPECT_EQ(0, buffer.getTailPosition());
|
||||
EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
|
||||
EXPECT_EQ(4, buffer.getTailPosition());
|
||||
EXPECT_TRUE(buffer.extend(8 /* size */));
|
||||
EXPECT_EQ(12, buffer.getTailPosition());
|
||||
EXPECT_TRUE(buffer.writeUint(0xFFFF /* data */, 4 /* size */, 8 /* pos */));
|
||||
EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
|
||||
}
|
||||
|
||||
TEST(BufferWithExtendablebufferTest, TestCopy) {
|
||||
BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
|
||||
EXPECT_TRUE(buffer.writeUint(0xFF /* data */, 4 /* size */, 0 /* pos */));
|
||||
EXPECT_TRUE(buffer.writeUint(0xFFFF /* data */, 4 /* size */, 4 /* pos */));
|
||||
BufferWithExtendableBuffer targetBuffer(DEFAULT_MAX_BUFFER_SIZE);
|
||||
EXPECT_TRUE(targetBuffer.copy(&buffer));
|
||||
EXPECT_EQ(0xFFu, targetBuffer.readUint(4 /* size */, 0 /* pos */));
|
||||
EXPECT_EQ(0xFFFFu, targetBuffer.readUint(4 /* size */, 4 /* pos */));
|
||||
}
|
||||
|
||||
TEST(BufferWithExtendablebufferTest, TestSizeLimit) {
|
||||
BufferWithExtendableBuffer emptyBuffer(0 /* maxAdditionalBufferSize */);
|
||||
EXPECT_FALSE(emptyBuffer.writeUint(0 /* data */, 1 /* size */, 0 /* pos */));
|
||||
EXPECT_FALSE(emptyBuffer.extend(1 /* size */));
|
||||
|
||||
BufferWithExtendableBuffer smallBuffer(4 /* maxAdditionalBufferSize */);
|
||||
EXPECT_TRUE(smallBuffer.writeUint(0 /* data */, 4 /* size */, 0 /* pos */));
|
||||
EXPECT_FALSE(smallBuffer.writeUint(0 /* data */, 1 /* size */, 4 /* pos */));
|
||||
|
||||
EXPECT_TRUE(smallBuffer.copy(&emptyBuffer));
|
||||
EXPECT_FALSE(emptyBuffer.copy(&smallBuffer));
|
||||
|
||||
BufferWithExtendableBuffer buffer(DEFAULT_MAX_BUFFER_SIZE);
|
||||
EXPECT_FALSE(buffer.isNearSizeLimit());
|
||||
int pos = 0;
|
||||
while (!buffer.isNearSizeLimit()) {
|
||||
EXPECT_TRUE(buffer.writeUintAndAdvancePosition(0 /* data */, 4 /* size */, &pos));
|
||||
}
|
||||
EXPECT_GT(pos, 0);
|
||||
EXPECT_LE(pos, DEFAULT_MAX_BUFFER_SIZE);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace latinime
|
Loading…
Reference in a new issue