Improved the naming of temporary file generator function. Replaced use of tmpnam in utIOStreamBuffer with this facility to addresssafety warning.

pull/1462/head
Jared Mulconry 2017-09-26 22:41:20 +10:00
parent 980d2b0eee
commit e77e89c8b7
3 changed files with 41 additions and 21 deletions

View File

@ -46,7 +46,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#if defined(__GNUC__) || defined(__clang__)
#define TMP_PATH "/tmp/"
inline FILE* MakeTmpFilePath(char* tmplate)
inline FILE* MakeTmpFile(char* tmplate)
{
auto fd = mkstemp(tmplate);
EXPECT_NE(-1, fd);
@ -61,7 +61,7 @@ inline FILE* MakeTmpFilePath(char* tmplate)
#elif defined(_MSC_VER)
#include <io.h>
#define TMP_PATH "./"
inline FILE* MakeTmpFilePath(char* tmplate)
inline FILE* MakeTmpFile(char* tmplate)
{
auto pathtemplate = _mktemp(tmplate);
EXPECT_NE(pathtemplate, nullptr);

View File

@ -60,7 +60,7 @@ TEST_F( utDefaultIOStream, FileSizeTest ) {
const auto dataCount = dataSize / sizeof(*data);
char fpath[] = { TMP_PATH"rndfp.XXXXXX" };
auto* fs = MakeTmpFilePath(fpath);
auto* fs = MakeTmpFile(fpath);
ASSERT_NE(nullptr, fs);
{
auto written = std::fwrite(data, sizeof(*data), dataCount, fs );

View File

@ -43,6 +43,7 @@ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "UnitTestPCH.h"
#include "IOStreamBuffer.h"
#include "TestIOStream.h"
#include "UnitTestFileGenerator.h"
class IOStreamBufferTest : public ::testing::Test {
// empty
@ -68,40 +69,59 @@ TEST_F( IOStreamBufferTest, accessCacheSizeTest ) {
EXPECT_EQ( 100U, myBuffer2.cacheSize() );
}
const char data[]{"Lorem ipsum dolor sit amet, consectetur adipiscing elit. Qui\
sque luctus sem diam, ut eleifend arcu auctor eu. Vestibulum id est vel nulla l\
obortis malesuada ut sed turpis. Nulla a volutpat tortor. Nunc vestibulum portt\
itor sapien ornare sagittis volutpat."};
TEST_F( IOStreamBufferTest, open_close_Test ) {
IOStreamBuffer<char> 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 ) );
const auto dataSize = sizeof(data);
const auto dataCount = dataSize / sizeof(*data);
char fname[]={ "octest.XXXXXX" };
auto* fs = MakeTmpFile(fname);
ASSERT_NE(nullptr, fs);
auto written = std::fwrite( data, sizeof(*data), dataCount, fs );
EXPECT_NE( 0U, written );
std::fflush( fs );
{
TestDefaultIOStream myStream( fs, fname );
TestDefaultIOStream myStream( fs, buffer );
EXPECT_TRUE( myBuffer.open( &myStream ) );
EXPECT_FALSE( myBuffer.open( &myStream ) );
EXPECT_TRUE( myBuffer.close() );
EXPECT_TRUE( myBuffer.open( &myStream ) );
EXPECT_FALSE( myBuffer.open( &myStream ) );
EXPECT_TRUE( myBuffer.close() );
}
remove(fname);
}
TEST_F( IOStreamBufferTest, readlineTest ) {
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 ) );
const auto dataSize = sizeof(data);
const auto dataCount = dataSize / sizeof(*data);
char fname[]={ "readlinetest.XXXXXX" };
auto* fs = MakeTmpFile(fname);
ASSERT_NE(nullptr, fs);
auto written = std::fwrite( data, sizeof(*data), dataCount, fs );
EXPECT_NE( 0U, written );
std::fflush( fs );
IOStreamBuffer<char> myBuffer( 26 );
EXPECT_EQ( 26U, myBuffer.cacheSize() );
const auto tCacheSize = 26u;
TestDefaultIOStream myStream( fs, buffer );
size_t size( myStream.FileSize() );
size_t numBlocks( size / myBuffer.cacheSize() );
IOStreamBuffer<char> myBuffer( tCacheSize );
EXPECT_EQ(tCacheSize, myBuffer.cacheSize() );
TestDefaultIOStream myStream( fs, fname );
auto size = myStream.FileSize();
auto numBlocks = size / myBuffer.cacheSize();
if ( size % myBuffer.cacheSize() > 0 ) {
numBlocks++;
}