From 0571ee21fb97b3b509d999b0d35d10347fbae40b Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sun, 28 Aug 2022 20:58:52 +0200 Subject: [PATCH] Introduce unittest for BaseProcess. --- code/Common/AssertHandler.cpp | 20 +++--- code/Common/AssertHandler.h | 46 +++++++------ code/Common/BaseImporter.cpp | 14 ++-- code/Common/BaseProcess.cpp | 13 +++- code/Common/BaseProcess.h | 44 ++++++------ test/CMakeLists.txt | 3 +- test/unit/Common/utBaseProcess.cpp | 105 +++++++++++++++++++++++++++++ 7 files changed, 185 insertions(+), 60 deletions(-) create mode 100644 test/unit/Common/utBaseProcess.cpp diff --git a/code/Common/AssertHandler.cpp b/code/Common/AssertHandler.cpp index d0bd6ccad..469e7bec5 100644 --- a/code/Common/AssertHandler.cpp +++ b/code/Common/AssertHandler.cpp @@ -5,8 +5,6 @@ Open Asset Import Library (assimp) Copyright (c) 2006-2022, assimp team - - All rights reserved. Redistribution and use of this software in source and binary forms, @@ -50,23 +48,23 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -void Assimp::defaultAiAssertHandler(const char* failedExpression, const char* file, int line) -{ +void Assimp::defaultAiAssertHandler(const char* failedExpression, const char* file, int line) { std::cerr << "ai_assert failure in " << file << "(" << line << "): " << failedExpression << std::endl; std::abort(); } -namespace -{ +namespace { Assimp::AiAssertHandler s_handler = Assimp::defaultAiAssertHandler; } -void Assimp::setAiAssertHandler(AiAssertHandler handler) -{ - s_handler = handler; +void Assimp::setAiAssertHandler(AiAssertHandler handler) { + if (handler != nullptr) { + s_handler = handler; + } else { + s_handler = Assimp::defaultAiAssertHandler; + } } -void Assimp::aiAssertViolation(const char* failedExpression, const char* file, int line) -{ +void Assimp::aiAssertViolation(const char* failedExpression, const char* file, int line) { s_handler(failedExpression, file, line); } diff --git a/code/Common/AssertHandler.h b/code/Common/AssertHandler.h index 2515f0bf2..365a924b0 100644 --- a/code/Common/AssertHandler.h +++ b/code/Common/AssertHandler.h @@ -47,29 +47,33 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include -namespace Assimp -{ - // --------------------------------------------------------------------------- - /** Signature of functions which handle assert violations. - */ - using AiAssertHandler = void (*)(const char* failedExpression, const char* file, int line); +namespace Assimp { - // --------------------------------------------------------------------------- - /** Set the assert handler. - */ - ASSIMP_API void setAiAssertHandler(AiAssertHandler handler); +// --------------------------------------------------------------------------- +/** + * @brief Signature of functions which handle assert violations. + */ +using AiAssertHandler = void (*)(const char* failedExpression, const char* file, int line); - // --------------------------------------------------------------------------- - /** The assert handler which is set by default. - * - * This issues a message to stderr and calls abort. - */ - ASSIMP_API void defaultAiAssertHandler(const char* failedExpression, const char* file, int line); +// --------------------------------------------------------------------------- +/** + * @brief Set the assert handler. + */ +ASSIMP_API void setAiAssertHandler(AiAssertHandler handler); + +// --------------------------------------------------------------------------- +/** The assert handler which is set by default. + * + * @brief This issues a message to stderr and calls abort. + */ +ASSIMP_API void defaultAiAssertHandler(const char* failedExpression, const char* file, int line); + +// --------------------------------------------------------------------------- +/** + * @brief Dispatches an assert violation to the assert handler. + */ +ASSIMP_API void aiAssertViolation(const char* failedExpression, const char* file, int line); - // --------------------------------------------------------------------------- - /** Dispatches an assert violation to the assert handler. - */ - ASSIMP_API void aiAssertViolation(const char* failedExpression, const char* file, int line); } // end of namespace Assimp -#endif // INCLUDED_AI_ASSERTHANDLER_H \ No newline at end of file +#endif // INCLUDED_AI_ASSERTHANDLER_H diff --git a/code/Common/BaseImporter.cpp b/code/Common/BaseImporter.cpp index 383300ef1..1d0aea6a3 100644 --- a/code/Common/BaseImporter.cpp +++ b/code/Common/BaseImporter.cpp @@ -234,19 +234,23 @@ void BaseImporter::GetExtensionList(std::set &extensions) { std::string::size_type pos = pFile.find_last_of('.'); // no file extension - can't read - if (pos == std::string::npos) + if (pos == std::string::npos) { return false; + } const char *ext_real = &pFile[pos + 1]; - if (!ASSIMP_stricmp(ext_real, ext0)) + if (!ASSIMP_stricmp(ext_real, ext0)) { return true; + } // check for other, optional, file extensions - if (ext1 && !ASSIMP_stricmp(ext_real, ext1)) + if (ext1 && !ASSIMP_stricmp(ext_real, ext1)) { return true; + } - if (ext2 && !ASSIMP_stricmp(ext_real, ext2)) - return true; + if (ext2 && !ASSIMP_stricmp(ext_real, ext2)) { + return true; + } return false; } diff --git a/code/Common/BaseProcess.cpp b/code/Common/BaseProcess.cpp index 48895b9f3..32e9ad608 100644 --- a/code/Common/BaseProcess.cpp +++ b/code/Common/BaseProcess.cpp @@ -66,17 +66,26 @@ BaseProcess::~BaseProcess() { // ------------------------------------------------------------------------------------------------ void BaseProcess::ExecuteOnScene(Importer *pImp) { ai_assert( nullptr != pImp ); - ai_assert( nullptr != pImp->Pimpl()->mScene); + if (pImp == nullptr) { + return; + } + + ai_assert(nullptr != pImp->Pimpl()->mScene); + if (pImp->Pimpl()->mScene == nullptr) { + return; + } progress = pImp->GetProgressHandler(); ai_assert(nullptr != progress); + if (progress == nullptr) { + return; + } SetupProperties(pImp); // catch exceptions thrown inside the PostProcess-Step try { Execute(pImp->Pimpl()->mScene); - } catch (const std::exception &err) { // extract error description diff --git a/code/Common/BaseProcess.h b/code/Common/BaseProcess.h index 64403567a..cd9a43b6b 100644 --- a/code/Common/BaseProcess.h +++ b/code/Common/BaseProcess.h @@ -179,19 +179,20 @@ class ASSIMP_API_WINONLY BaseProcess { friend class Importer; public: - /** Constructor to be privately used by Importer */ + /** @brief onstructor to be privately used by Importer */ BaseProcess() AI_NO_EXCEPT; - /** Destructor, private as well */ + /** @brief Destructor, private as well */ virtual ~BaseProcess(); // ------------------------------------------------------------------- - /** Returns whether the processing step is present in the given flag. + /** + * @brief Returns whether the processing step is present in the given flag. * @param pFlags The processing flags the importer was called with. A * bitwise combination of #aiPostProcessSteps. * @return true if the process is present in this flag fields, * false if not. - */ + */ virtual bool IsActive(unsigned int pFlags) const = 0; // ------------------------------------------------------------------- @@ -200,33 +201,36 @@ public: virtual bool RequireVerboseFormat() const; // ------------------------------------------------------------------- - /** Executes the post processing step on the given imported data. - * The function deletes the scene if the postprocess step fails ( - * the object pointer will be set to nullptr). - * @param pImp Importer instance (pImp->mScene must be valid) - */ + /** + * @brief Executes the post processing step on the given imported data. + * The function deletes the scene if the post-process step fails ( + * the object pointer will be set to nullptr). + * @param pImp Importer instance (pImp->mScene must be valid) + */ void ExecuteOnScene(Importer *pImp); // ------------------------------------------------------------------- - /** Called prior to ExecuteOnScene(). - * The function is a request to the process to update its configuration - * basing on the Importer's configuration property list. - */ + /** + * @brief Called prior to ExecuteOnScene(). + * The function is a request to the process to update its configuration + * basing on the Importer's configuration property list. + */ virtual void SetupProperties(const Importer *pImp); // ------------------------------------------------------------------- - /** Executes the post processing step on the given imported data. - * A process should throw an ImportErrorException* if it fails. - * This method must be implemented by deriving classes. - * @param pScene The imported data to work at. - */ + /** + * @brief Executes the post processing step on the given imported data. + * A process should throw an ImportErrorException* if it fails. + * This method must be implemented by deriving classes. + * @param pScene The imported data to work at. + */ virtual void Execute(aiScene *pScene) = 0; // ------------------------------------------------------------------- /** Assign a new SharedPostProcessInfo to the step. This object - * allows multiple postprocess steps to share data. + * allows multiple post-process steps to share data. * @param sh May be nullptr - */ + */ inline void SetSharedData(SharedPostProcessInfo *sh) { shared = sh; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index cdf13d778..5c87dabbe 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -71,7 +71,6 @@ SET( COMMON unit/AssimpAPITest_aiQuaternion.cpp unit/AssimpAPITest_aiVector2D.cpp unit/AssimpAPITest_aiVector3D.cpp - unit/Common/utHash.cpp unit/MathTest.cpp unit/MathTest.h unit/RandomNumberGeneration.h @@ -98,6 +97,8 @@ SET( COMMON unit/Common/utAssertHandler.cpp unit/Common/utXmlParser.cpp unit/Common/utBase64.cpp + unit/Common/utHash.cpp + unit/Common/utBaseProcess.cpp ) SET( IMPORTERS diff --git a/test/unit/Common/utBaseProcess.cpp b/test/unit/Common/utBaseProcess.cpp new file mode 100644 index 000000000..2da5c18b0 --- /dev/null +++ b/test/unit/Common/utBaseProcess.cpp @@ -0,0 +1,105 @@ +/* +--------------------------------------------------------------------------- +Open Asset Import Library (assimp) +--------------------------------------------------------------------------- + +Copyright (c) 2006-2022, assimp team + +All rights reserved. + +Redistribution and use of this software in source and binary forms, +with or without modification, are permitted provided that the following +conditions are met: + +* Redistributions of source code must retain the above +copyright notice, this list of conditions and the +following disclaimer. + +* Redistributions in binary form must reproduce the above +copyright notice, this list of conditions and the +following disclaimer in the documentation and/or other +materials provided with the distribution. + +* Neither the name of the assimp team, nor the names of its +contributors may be used to endorse or promote products +derived from this software without specific prior +written permission of the assimp team. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +--------------------------------------------------------------------------- +*/ + +#include "TestIOSystem.h" +#include "UnitTestPCH.h" + +#include "Common/BaseProcess.h" +#include "Common/AssertHandler.h" + +using namespace Assimp; + +class BaseProcessTest : public ::testing::Test { +public: + static void test_handler( const char*, const char*, int ) { + HandlerWasCalled = true; + } + + void SetUp() override { + HandlerWasCalled = false; + setAiAssertHandler(test_handler); + } + + void TearDown() override { + setAiAssertHandler(nullptr); + } + + static bool handlerWasCalled() { + return HandlerWasCalled; + } + +private: + static bool HandlerWasCalled; +}; + +bool BaseProcessTest::HandlerWasCalled = false; + +class TestingBaseProcess : public BaseProcess { +public: + TestingBaseProcess() : BaseProcess() { + // empty + } + + ~TestingBaseProcess() override = default; + + bool IsActive( unsigned int ) const override { + return true; + } + + void Execute(aiScene*) override { + + } +}; +TEST_F( BaseProcessTest, constructTest ) { + bool ok = true; + try { + TestingBaseProcess process; + } catch (...) { + ok = false; + } + EXPECT_TRUE(ok); +} + +TEST_F( BaseProcessTest, executeOnSceneTest ) { + TestingBaseProcess process; + process.ExecuteOnScene(nullptr); + EXPECT_TRUE(BaseProcessTest::handlerWasCalled()); +}