📄 mfile_test.cpp
字号:
#include "mfile_test.h"
#include "../ENV_test.h"
#include "MCRT/mfile.h"
#include <sys/types.h>
#include <sys/stat.h> //file size
//for unlink
#ifdef WIN32
# include <stdio.h>
#else //! WIN32
# include <unistd.h>
#endif //end WIN32
// register the fixture into registry
CPPUNIT_TEST_SUITE_REGISTRATION( MFileTest );//register test class in cppunit
void MFileTest::setUp()
{
}
void MFileTest::tearDown()
{
}
void MFileTest::testOpenNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_HelloWorld.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.m_fp != NULL );
mfile.Close();
}
void MFileTest::testOpenNotExist()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/NotExist.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.m_fp == NULL );
mfile.Close();
}
void MFileTest::testCloseNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_HelloWorld.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
mfile.Close();
CPPUNIT_ASSERT( mfile.m_fp == NULL );
}
void MFileTest::testCloseNotOpen()
{
MFile mfile;
mfile.Close();
CPPUNIT_ASSERT( mfile.m_fp == NULL );
}
void MFileTest::testLengthNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_HelloWorld.txt" );
struct stat fileStat;
if ( ::stat( strFilePath.c_str(), &fileStat ) != 0 )
{
fileStat.st_size = 0;
}
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.GetLength() == fileStat.st_size );
mfile.Close();
MFile mfileNotOpen;
CPPUNIT_ASSERT( mfileNotOpen.GetLength() == 0 );
}
void MFileTest::testLengthNotOpen()
{
MFile mfileNotOpen;
CPPUNIT_ASSERT( mfileNotOpen.GetLength() == 0 );
}
void MFileTest::testSeekNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_seek.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.Seek(4, MFile::MPOS_BEGIN) == 4 );
mfile.Close();
}
void MFileTest::testSeekExceed()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_seek.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.Seek(128, MFile::MPOS_BEGIN) == 0 );
mfile.Close();
}
void MFileTest::testSeekNotOpen()
{
MFile mfile;
CPPUNIT_ASSERT( mfile.Seek(4, MFile::MPOS_BEGIN) == 0 );
}
void MFileTest::testTellNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_tell.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
mfile.Seek( 5, MFile::MPOS_BEGIN );
CPPUNIT_ASSERT( mfile.Tell() == 5 );
mfile.Close();
}
void MFileTest::testTellNotOpen()
{
MFile mfile;
CPPUNIT_ASSERT( mfile.Tell() == 0 );
}
void MFileTest::testReadNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_read.txt" );
MFile mfile;
char buf[32];
unsigned long bytesRead = 0;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
bytesRead = mfile.Read( buf, 32 );
buf[bytesRead] = '\0';
mfile.Close();
CPPUNIT_ASSERT( ::strcmp( buf, "Hello, World!" ) == 0 );
}
void MFileTest::testReadEnd()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_read.txt" );
MFile mfile;
char buf[32];
unsigned long bytesRead = 0;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
mfile.SeekToEnd();
bytesRead = mfile.Read( buf, 32 );
mfile.Close();
CPPUNIT_ASSERT( bytesRead == 0 );
}
void MFileTest::testReadNotOpen()
{
MFile mfile;
char buf[32];
unsigned long bytesRead = mfile.Read( buf, 32 );
CPPUNIT_ASSERT( bytesRead == 0 );
}
void MFileTest::testWriteNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_write.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_WRONLY );
char* pBufSrc = "Hello, World!";
unsigned long bytesWrite = mfile.Write( pBufSrc, ::strlen( pBufSrc ) );
mfile.Close();
CPPUNIT_ASSERT( bytesWrite == ::strlen( pBufSrc ) );
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
char buf[32];
unsigned long bytesRead = mfile.Read( buf, 32 );
buf[bytesRead] = '\0';
mfile.Close();
CPPUNIT_ASSERT( ::strcmp( buf, pBufSrc ) == 0 );
::unlink( strFilePath.c_str() );
}
void MFileTest::testWriteNotOpen()
{
MFile mfile;
char* pBuf = "Hello, World!";
unsigned long bytesWrite = mfile.Write( pBuf, ::strlen(pBuf) );
CPPUNIT_ASSERT( bytesWrite == 0 );
}
void MFileTest::testResizeNormal()
{
}
void MFileTest::testResizeNotOpen()
{
MFile mfile;
CPPUNIT_ASSERT( mfile.Resize(1024) == E_FILE_OPEN );
}
void MFileTest::testResizeLess()
{
}
void MFileTest::testIs_openNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_HelloWorld.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.IsOpen() );
mfile.Close();
}
void MFileTest::testIs_openNotOpen()
{
MFile mfile;
CPPUNIT_ASSERT( ! mfile.IsOpen() );
}
void MFileTest::testClearNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_clear.txt" );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_APPEND );
mfile.Write( "Hello, World!", ::strlen( "Hello, World!" ) );
mfile.Close();
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.GetLength() != 0 );
mfile.Close();
MFile::Clear( strFilePath.c_str() );
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.GetLength() == 0 );
mfile.Close();
::unlink( strFilePath.c_str() );
}
void MFileTest::testClearNotExist()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_NotExist.txt" );
CPPUNIT_ASSERT( MFile::Clear( strFilePath.c_str() ) != E_SUCCESS );
}
void MFileTest::testCreate()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_create.txt" );
MFile::Create( strFilePath.c_str(), 512 );
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_RDONLY );
CPPUNIT_ASSERT( mfile.GetLength() == 512 );
mfile.Close();
::unlink( strFilePath.c_str() );
}
void MFileTest::testCompareNormal()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_compare.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/MFile_compare_equal.txt" );
CPPUNIT_ASSERT( MFile::Compare( strSrcFilePath.c_str(), strDestFilePath.c_str() ) == E_SUCCESS );
}
void MFileTest::testCompareDiff()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_compare.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/MFile_compare_diff.txt" );
CPPUNIT_ASSERT( MFile::Compare( strSrcFilePath.c_str(), strDestFilePath.c_str() ) != E_SUCCESS );
}
void MFileTest::testCompareNotExist()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_compare.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/MFile_compare_NotExist.txt" );
CPPUNIT_ASSERT( MFile::Compare( strSrcFilePath.c_str(), strDestFilePath.c_str() ) != E_SUCCESS );
}
void MFileTest::testCopyNormal()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_copy_src.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/MFile_copy_dest.txt" );
MFile::Copy( strDestFilePath.c_str(), strSrcFilePath.c_str() );
CPPUNIT_ASSERT( MFile::Compare( strDestFilePath.c_str(), strSrcFilePath.c_str() ) == E_SUCCESS );
::unlink( strDestFilePath.c_str() );
}
void MFileTest::testCopySrcNotExist()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_copy_NotExist.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/MFile_copy_dest.txt" );
MFile::Copy( strDestFilePath.c_str(), strSrcFilePath.c_str() );
CPPUNIT_ASSERT( MFile::Compare( strDestFilePath.c_str(), strSrcFilePath.c_str() ) != E_SUCCESS );
}
void MFileTest::testCopyDestCannotCreate()
{
std::string strSrcFilePath = g_ENVTest.getProjectRoot();
strSrcFilePath.append( "/test_data/MCRT/MFile_copy_NotExist.txt" );
std::string strDestFilePath = g_ENVTest.getProjectRoot();
strDestFilePath.append( "/test_data/MCRT/InvalidDir/MFile_copy_dest.txt" );
MFile::Copy( strDestFilePath.c_str(), strSrcFilePath.c_str() );
CPPUNIT_ASSERT( MFile::Compare( strDestFilePath.c_str(), strSrcFilePath.c_str() ) != E_SUCCESS );
}
void MFileTest::testStaticReadNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_read.txt" );
char buf[32];
unsigned long bytesRead = MFile::Read( strFilePath.c_str(), ::strlen("Hello, "), buf, 32 );
buf[bytesRead] = '\0';
CPPUNIT_ASSERT( ::strcmp( buf, "World!" ) == 0 );
}
void MFileTest::testStaticReadExceed()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_read.txt" );
char buf[32];
CPPUNIT_ASSERT( MFile::Read( strFilePath.c_str(), 32, buf, 32 ) == 0 );
}
void MFileTest::testStaticReadNotExist()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_read_NotExist.txt" );
char buf[32];
CPPUNIT_ASSERT( MFile::Read( strFilePath.c_str(), 32, buf, 32 ) == 0 );
}
void MFileTest::testStaticWriteNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_write_static.txt" );
char* pHead = "AAA";
//create empty file first
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_WRONLY );
mfile.Write( pHead, ::strlen(pHead) );
mfile.Close();
char* pBufSrc = "Hello, World!";
CPPUNIT_ASSERT( MFile::Write( strFilePath.c_str(), ::strlen(pHead), pBufSrc, ::strlen( pBufSrc ) ) == ::strlen(pBufSrc) );
char buf[32];
unsigned long bytesRead = MFile::Read( strFilePath.c_str(), ::strlen(pHead), buf, 32 );
buf[bytesRead] = '\0';
CPPUNIT_ASSERT( ::strcmp( buf, pBufSrc ) == 0 );
::unlink( strFilePath.c_str() );
}
void MFileTest::testStaticWriteExceed()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_write_static.txt" );
char* pHead = "AAA";
//create empty file first
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_WRONLY );
mfile.Write( pHead, ::strlen(pHead) );
mfile.Close();
char* pBufSrc = "Hello, World!";
CPPUNIT_ASSERT( MFile::Write( strFilePath.c_str(), 32, pBufSrc, ::strlen( pBufSrc ) ) != ::strlen(pBufSrc) );
::unlink( strFilePath.c_str() );
}
void MFileTest::testStaticWriteNotExist()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_write_static.txt" );
char pBuf[] = "Hello, World!";
CPPUNIT_ASSERT( MFile::Write( strFilePath.c_str(), 0, pBuf, ::strlen(pBuf) ) != ::strlen("Hello, World!") );
}
void MFileTest::testStaticAppendNormal()
{
std::string strFilePath = g_ENVTest.getProjectRoot();
strFilePath.append( "/test_data/MCRT/MFile_append_static.txt" );
char* pHead = "AAA";
//create empty file first
MFile mfile;
mfile.Open( strFilePath.c_str(), MFile::MO_WRONLY );
mfile.Write( pHead, ::strlen(pHead) );
mfile.Close();
char* pBufSrc = "Hello, World!";
CPPUNIT_ASSERT( MFile::Append( strFilePath.c_str(), pBufSrc, ::strlen( pBufSrc ) ) == E_SUCCESS );
char buf[32];
unsigned long bytesRead = MFile::Read( strFilePath.c_str(), ::strlen(pHead), buf, 32 );
buf[bytesRead] = '\0';
CPPUNIT_ASSERT( ::strcmp( buf, pBufSrc ) == 0 );
::unlink( strFilePath.c_str() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -