Answer the question
In order to leave comments, you need to log in
Can I improve my first unit test with boost::test?
Please make a code-review and advise something to improve.
The unit test code below checks whether the updated header is correctly written to the file by the CustomImage32 class.
The test algorithm is simple: If the values in the header are changed, written to a file, then after subsequent reading they must match those that were set in the header before writing to the file.
struct Correct32Fixture
{
Correct32Fixture()
: outputfile( string(OUTPUT_FILES_DIR) + string("Correct32.bin") )
{
string sourcefile( string(TEST_SAMPLES_DIR) + string("Correct32.bin") );
std::remove( outputfile.c_str() );
copy_file( path(sourcefile), path(outputfile) );
fileRaw = new fstream( outputfile.c_str(), ios::in | ios::out | ios::ate | ios::binary );
filePtr = InOutStreamPtr( fileRaw );
templateImg.setStream( filePtr );
templateImg.read();
}
~Correct32Fixture()
{
if(fileRaw->is_open() )
{
fileRaw->close();
}
std::remove( outputfile.c_str() );
}
string outputfile;
fstream* fileRaw;
InOutStreamPtr filePtr; // InOutStreamPtr это boost::shared_ptr<std::iostream>
CustomImage32 templateImg;
};
BOOST_FIXTURE_TEST_CASE( testSuperHeaderChanghing, Correct32Fixture )
{
BOOST_REQUIRE( templateImg.SuperHeader().boundary_field1 == 0x90 );
BOOST_REQUIRE( templateImg.SuperHeader().boundary_field2[9] == 0 );
// Arrange
templateImg.SuperHeader().boundary_field1 = 0xE9;
templateImg.SuperHeader().boundary_field2[9] = 0xE8E9;
templateImg.write();
// Act
fileRaw->close();
fstream* fileRaw2 = new fstream( outputfile.c_str(), ios::in | ios::ate | ios::binary );
InOutStreamPtr filePtr2( fileRaw2 );
CustomImage32 checkImg( filePtr2 );
//Assert
BOOST_CHECK( checkImg.SuperHeader().boundary_field = 0xE9 );
BOOST_CHECK( checkImg.SuperHeader().boundary_field[9] = 0xE8E9 );
}
Answer the question
In order to leave comments, you need to log in
A note about exception safety + RAII:
If you are using boost::shared_ptr, it makes sense to create objects with std::make_shared rather than using a raw pointer (fileRaw and fileRaw2) for this:
filePtr = boost::make_shared<std::fstream>( outputfile.c_str(), ios::in | ios::out | ios::ate | ios::binary );
. Well, throw out the use of fileRaw and fileRaw2. namespace consts
{
const int ConstantName = 0xE9;
const char OutputFileName [] = "Correct32.bin";
}
Naturally, at the same time give meaningful names to the constants. Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question