D
D
Dmitry2012-06-10 22:53:14
Programming
Dmitry, 2012-06-10 22:53:14

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

1 answer(s)
P
prograholic, 2012-06-12
@prograholic

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.
Unit test code is also code, so it’s better not to use magic constants in the test: 0xE9, etc. Move
them to a separate namespace:
namespace consts
{
  const int ConstantName = 0xE9;
  const char OutputFileName [] = "Correct32.bin";
}
Naturally, at the same time give meaningful names to the constants.
Also, I would not use files in a unit test, instead I would use std::streambuf to read / write data in memory - but this is possible if your program has a mechanism for I / O not only through fstream

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question