From b13c30f50e7685dcbc32d358d80ff8975f2396c8 Mon Sep 17 00:00:00 2001 From: Kim Kulling Date: Sat, 29 Oct 2016 19:07:30 +0200 Subject: [PATCH] Obj: Fix some small issues --- code/IOStreamBuffer.h | 52 +++++++++++++++++++++++++++++++++++------ test/CMakeLists.txt | 1 + test/unit/TestIOStream.h | 23 ++++++++++++++++++ test/unit/utDefaultIOStream.cpp | 19 +-------------- test/unit/utIOStreamBuffer.cpp | 28 ++++++++++++++++++++++ 5 files changed, 98 insertions(+), 25 deletions(-) create mode 100644 test/unit/TestIOStream.h diff --git a/code/IOStreamBuffer.h b/code/IOStreamBuffer.h index 5b00ef5..e822073 100644 --- a/code/IOStreamBuffer.h +++ b/code/IOStreamBuffer.h @@ -47,15 +47,43 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. namespace Assimp { +// --------------------------------------------------------------------------- +/** + * Implementation of a cached stream buffer. + */ template class IOStreamBuffer { public: + /// @brief The class constructor. IOStreamBuffer( size_t cache = 4096 * 4096 ); + + /// @brief The class destructor. ~IOStreamBuffer(); + + /// @brief Will open the cached access for a given stream. + /// @param stream The stream to cache. + /// @return true if successful. bool open( IOStream *stream ); + + /// @brief Will close the cached access. + /// @return true if successful. bool close(); + + /// @brief Returns the filesize. + /// @return The filesize. size_t size() const; + + /// @brief Returns the cache size. + /// @return The cache size. + size_t cacheSize() const; + + /// @brief Will read the next block. + /// @return true if successful. bool readNextBlock(); + + /// @brief Will read the next line. + /// @param buffer The buffer for the next line. + /// @return true if successful. bool getNextLine( std::vector &buffer ); private: @@ -69,14 +97,14 @@ private: template inline -IOStreamBuffer::IOStreamBuffer( size_t cache = 4096 * 4096 ) - : m_stream( nullptr ) - , m_filesize( 0 ) - , m_cacheSize( cache ) - , m_cachePos( 0 ) - , m_filePos( 0 ) { +IOStreamBuffer::IOStreamBuffer( size_t cache ) +: m_stream( nullptr ) +, m_filesize( 0 ) +, m_cacheSize( cache ) +, m_cachePos( 0 ) +, m_filePos( 0 ) { m_cache.resize( cache ); - std::fill( m_cache.begin(), m_cache.end(), '\0' ); + std::fill( m_cache.begin(), m_cache.end(), '\n' ); } template @@ -88,6 +116,10 @@ IOStreamBuffer::~IOStreamBuffer() { template inline bool IOStreamBuffer::open( IOStream *stream ) { + if ( nullptr != m_stream ) { + return false; + } + if ( nullptr == stream ) { return false; } @@ -125,6 +157,12 @@ size_t IOStreamBuffer::size() const { template inline +size_t IOStreamBuffer::cacheSize() const { + return m_cacheSize; +} + +template +inline bool IOStreamBuffer::readNextBlock() { m_stream->Seek( m_filePos, aiOrigin_SET ); size_t readLen = m_stream->Read( &m_cache[ 0 ], sizeof( T ), m_cacheSize ); diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 7c14dff..c23ad80 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -70,6 +70,7 @@ SET( TEST_SRCS unit/utImporter.cpp unit/utImproveCacheLocality.cpp unit/utIOSystem.cpp + unit/utIOStreamBuffer.cpp unit/utIssues.cpp unit/utJoinVertices.cpp unit/utLimitBoneWeights.cpp diff --git a/test/unit/TestIOStream.h b/test/unit/TestIOStream.h new file mode 100644 index 0000000..1014a77 --- /dev/null +++ b/test/unit/TestIOStream.h @@ -0,0 +1,23 @@ +#pragma once + +#include "DefaultIOStream.h" + +using namespace ::Assimp; + +class TestDefaultIOStream : public DefaultIOStream { +public: + TestDefaultIOStream() + : DefaultIOStream() { + // empty + } + + TestDefaultIOStream( FILE* pFile, const std::string &strFilename ) + : DefaultIOStream( pFile, strFilename ) { + // empty + } + + virtual ~TestDefaultIOStream() { + // empty + } +}; + diff --git a/test/unit/utDefaultIOStream.cpp b/test/unit/utDefaultIOStream.cpp index 7d3c07f..50a1543 100644 --- a/test/unit/utDefaultIOStream.cpp +++ b/test/unit/utDefaultIOStream.cpp @@ -37,7 +37,7 @@ THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------*/ #include -#include "DefaultIOStream.h" +#include "TestIOStream.h" using namespace ::Assimp; @@ -45,23 +45,6 @@ class utDefaultIOStream : public ::testing::Test { // empty }; -class TestDefaultIOStream : public DefaultIOStream { -public: - TestDefaultIOStream() - : DefaultIOStream() { - // empty - } - - TestDefaultIOStream( FILE* pFile, const std::string &strFilename ) - : DefaultIOStream( pFile, strFilename ) { - // empty - } - - virtual ~TestDefaultIOStream() { - // empty - } -}; - TEST_F( utDefaultIOStream, FileSizeTest ) { char buffer[ L_tmpnam ]; tmpnam( buffer ); diff --git a/test/unit/utIOStreamBuffer.cpp b/test/unit/utIOStreamBuffer.cpp index 9d8d6ad..ec70f75 100644 --- a/test/unit/utIOStreamBuffer.cpp +++ b/test/unit/utIOStreamBuffer.cpp @@ -41,6 +41,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include "UnitTestPCH.h" #include "IOStreamBuffer.h" +#include "TestIOStream.h" class IOStreamBufferTest : public ::testing::Test { // empty @@ -58,6 +59,33 @@ TEST_F( IOStreamBufferTest, creationTest ) { EXPECT_TRUE( ok ); } +TEST_F( IOStreamBufferTest, accessCacheSizeTest ) { + IOStreamBuffer myBuffer1; + EXPECT_NE( 0, myBuffer1.cacheSize() ); + + IOStreamBuffer myBuffer2( 100 ); + EXPECT_EQ( 100, myBuffer2.cacheSize() ); +} + +TEST_F( IOStreamBufferTest, open_close_Test ) { + IOStreamBuffer myBuffer; + + EXPECT_FALSE( myBuffer.open( nullptr ) ); + EXPECT_FALSE( myBuffer.close() ); + + char buffer[ L_tmpnam ]; + tmpnam( buffer ); + std::FILE *fs( std::fopen( buffer, "w+" ) ); + size_t written( std::fwrite( buffer, 1, sizeof( char ) * L_tmpnam, fs ) ); + std::fflush( fs ); + + TestDefaultIOStream myStream( fs, buffer ); + + EXPECT_TRUE( myBuffer.open( &myStream ) ); + EXPECT_FALSE( myBuffer.open( &myStream ) ); + EXPECT_TRUE( myBuffer.close() ); +} + TEST_F( IOStreamBufferTest, readlineTest ) { } \ No newline at end of file -- 2.7.4