Fixes ResizableBufferAdapter<Data>::resize doesn't consider the situation of new size which is lower than old size. (#18553)

This commit is contained in:
James Chen 2017-12-12 17:49:11 +08:00 committed by minggo
parent 269ea967a2
commit 89781a44df
3 changed files with 38 additions and 1 deletions

View File

@ -103,7 +103,8 @@ class ResizableBufferAdapter<Data> : public ResizableBuffer {
public:
explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {}
virtual void resize(size_t size) override {
if (static_cast<size_t>(_buffer->getSize()) < size) {
size_t oldSize = static_cast<size_t>(_buffer->getSize());
if (oldSize != size) {
auto old = _buffer->getBytes();
void* buffer = realloc(old, size);
if (buffer)

View File

@ -52,6 +52,7 @@ UnitTests::UnitTests()
ADD_TEST_CASE(UTFConversionTest);
ADD_TEST_CASE(UIHelperSubStringTest);
ADD_TEST_CASE(ParseUriTest);
ADD_TEST_CASE(ResizableBufferAdapterTest);
#ifdef UNIT_TEST_FOR_OPTIMIZED_MATH_UTIL
ADD_TEST_CASE(MathUtilTest);
#endif
@ -1664,3 +1665,29 @@ std::string MathUtilTest::subtitle() const
{
return "MathUtilTest";
}
// ResizableBufferAdapterTest
void ResizableBufferAdapterTest::onEnter()
{
UnitTestDemo::onEnter();
Data data;
ResizableBufferAdapter<Data> buffer(&data);
FileUtils::getInstance()->getContents("effect1.wav", &buffer);
EXPECT_EQ(data.getSize(), 10026);
FileUtils::getInstance()->getContents("effect2.ogg", &buffer);
EXPECT_EQ(data.getSize(), 4278);
FileUtils::getInstance()->getContents("effect1.wav", &buffer);
EXPECT_EQ(data.getSize(), 10026);
}
std::string ResizableBufferAdapterTest::subtitle() const
{
return "ResiziableBufferAdapter<Data> Test";
}

View File

@ -71,4 +71,13 @@ public:
virtual std::string subtitle() const override;
};
class ResizableBufferAdapterTest : public UnitTestDemo
{
public:
CREATE_FUNC(ResizableBufferAdapterTest);
virtual void onEnter() override;
virtual std::string subtitle() const override;
};
#endif /* __UNIT_TEST__ */