From 89781a44df824196d6c1e655526365e958aabe31 Mon Sep 17 00:00:00 2001 From: James Chen Date: Tue, 12 Dec 2017 17:49:11 +0800 Subject: [PATCH] Fixes ResizableBufferAdapter::resize doesn't consider the situation of new size which is lower than old size. (#18553) --- cocos/platform/CCFileUtils.h | 3 ++- tests/cpp-tests/Classes/UnitTest/UnitTest.cpp | 27 +++++++++++++++++++ tests/cpp-tests/Classes/UnitTest/UnitTest.h | 9 +++++++ 3 files changed, 38 insertions(+), 1 deletion(-) diff --git a/cocos/platform/CCFileUtils.h b/cocos/platform/CCFileUtils.h index c3bb3da1e0..f9c4cef27a 100644 --- a/cocos/platform/CCFileUtils.h +++ b/cocos/platform/CCFileUtils.h @@ -103,7 +103,8 @@ class ResizableBufferAdapter : public ResizableBuffer { public: explicit ResizableBufferAdapter(BufferType* buffer) : _buffer(buffer) {} virtual void resize(size_t size) override { - if (static_cast(_buffer->getSize()) < size) { + size_t oldSize = static_cast(_buffer->getSize()); + if (oldSize != size) { auto old = _buffer->getBytes(); void* buffer = realloc(old, size); if (buffer) diff --git a/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp b/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp index c3cec558b3..3c97121cfe 100644 --- a/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp +++ b/tests/cpp-tests/Classes/UnitTest/UnitTest.cpp @@ -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 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 Test"; +} + + diff --git a/tests/cpp-tests/Classes/UnitTest/UnitTest.h b/tests/cpp-tests/Classes/UnitTest/UnitTest.h index 850e35a5ca..b1a45d074a 100644 --- a/tests/cpp-tests/Classes/UnitTest/UnitTest.h +++ b/tests/cpp-tests/Classes/UnitTest/UnitTest.h @@ -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__ */