C++ Boost::Test::BOOST_level_EQUAL














































C++ Boost::Test::BOOST_level_EQUAL



Description : 

  Boost test is a unit test framework, It is more neat approach to test individual pieces of codes. The Boost.Test library provides both an easy to use and flexible set of interfaces for writing test programs, organizing tests into simple test cases and test suites, and controlling their runtime execution.


These tools are used to perform equality checks. It provides three levels of check.

  • BOOST_WARN_EQUAL(left, right) - Warns about the inequal test results along with the data
  • BOOST_REQUIRE_EQUAL(left, right) - If equality check fails stops check with fatal error
  • BOOST_CHECK_EQUAL(left, right) - Throws errors about the inequal test results along with the data

Note: Don't use BOOST_<level>_EQUAL to compare floating-point values. It's better to use BOOST_<level>_CLOSE or BOOST_<level>_CLOSE_FRACTION


Header : 

  #include <boost/test/included/unit_test.hpp>



CODE :

#define BOOST_TEST_MODULE boost level equal // Defines the name of the program which is used in the messages
#include <boost/test/included/unit_test.hpp>

int sum(int firstNumber, int secondNumber)
{
    return firstNumber + secondNumber;
}

int sumCheck = 10;
int firstNumber[] = {1, 2, 3, 4, 5};
int secondNumber[] = {5, 6, 7, 8, 9};

BOOST_AUTO_TEST_CASE(sum_equal_warn)// Declares a test case with name sum_check which will be automatically created and registered
{
    
    for(int i=0; i< sizeof(firstNumber)/sizeof(int) ; i++)
    {
        BOOST_WARN_EQUAL(sum(firstNumber[i], secondNumber[i]), sumCheck ); // WARN - Warns about the inequal test results along with the data
    }

}
BOOST_AUTO_TEST_CASE(sum_equal_check)// Declares a test case with name sum_check which will be automatically created and registered
{
    
    for(int i=0; i< sizeof(firstNumber)/sizeof(int) ; i++)
    {
        BOOST_CHECK_EQUAL(sum(firstNumber[i], secondNumber[i]), sumCheck ); // CHECK - Throws errors about the inequal test results along with the data
    }

}
BOOST_AUTO_TEST_CASE(sum_equal_require)// Declares a test case with name sum_check which will be automatically created and registered
{
    
    for(int i=0; i< sizeof(firstNumber)/sizeof(int) ; i++)
    {
        BOOST_REQUIRE_EQUAL(sum(firstNumber[i], secondNumber[i]), sumCheck ); // REQUIRE - If equality fails stops check with fatal error
    }

}

//  g++ boostEqual.cpp   - to compile
// ./a.out -l all          - to execute
// ./a.out --help          - to view other available execution options



Compile and execute :


g++ boostEqual.cpp     - to compile
./a.out -l all         - to execute



OUTPUT :


Running 3 test cases...
Entering test module "boost level equal"
boostEqual.cpp(13): Entering test case "sum_equal_warn"
boostEqual.cpp(18): warning: in "sum_equal_warn": condition sum(firstNumber[i], secondNumber[i]) == sumCheck is not satisfied [6 != 10]
boostEqual.cpp(18): warning: in "sum_equal_warn": condition sum(firstNumber[i], secondNumber[i]) == sumCheck is not satisfied [8 != 10]
boostEqual.cpp(18): info: check sum(firstNumber[i], secondNumber[i]) == sumCheck has passed
boostEqual.cpp(18): warning: in "sum_equal_warn": condition sum(firstNumber[i], secondNumber[i]) == sumCheck is not satisfied [12 != 10]
boostEqual.cpp(18): warning: in "sum_equal_warn": condition sum(firstNumber[i], secondNumber[i]) == sumCheck is not satisfied [14 != 10]
boostEqual.cpp(13): Leaving test case "sum_equal_warn"; testing time: 195us
boostEqual.cpp(22): Entering test case "sum_equal_check"
boostEqual.cpp(27): error: in "sum_equal_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [6 != 10]
boostEqual.cpp(27): error: in "sum_equal_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [8 != 10]
boostEqual.cpp(27): info: check sum(firstNumber[i], secondNumber[i]) == sumCheck has passed
boostEqual.cpp(27): error: in "sum_equal_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [12 != 10]
boostEqual.cpp(27): error: in "sum_equal_check": check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [14 != 10]
boostEqual.cpp(22): Leaving test case "sum_equal_check"; testing time: 180us
boostEqual.cpp(31): Entering test case "sum_equal_require"
boostEqual.cpp(36): fatal error: in "sum_equal_require": critical check sum(firstNumber[i], secondNumber[i]) == sumCheck has failed [6 != 10]
boostEqual.cpp(31): Leaving test case "sum_equal_require"; testing time: 192us
Leaving test module "boost level equal"; testing time: 633us

*** 5 failures are detected in the test module "boost level equal"





Comments