|
Boost Users : |
Subject: [Boost-users] [Boost.Test] BOOST_CHECK_THROW behaviour
From: Ghyslain Leclerc (ghleclerc_at_[hidden])
Date: 2014-06-19 09:03:38
Hello,
Sorry for the long post!
I have a simple class, which I am posting a fraction of here for discussion sake:
------------------------------------------------------
class AsciiTableRow
{
public:
  void setCol( std::size_t colIdx, std::string const& newContent );
  void setCol( std::size_t colIdx, const char* newContent );
private:
  void checkIdxBound_( std::size_t colIdx, std::string const& msg );
  std::vector< std::string > data_;
  bool maxColWidthIsValid_;
}
inline  void AsciiTableRow::setCol( std::size_t colIdx, std::string const& newVal )
{
  checkIdxBound_( colIdx, "setCol" );
  maxColWidthIsValid_ = false;
  data_[colIdx] = newVal;
}
inline  void AsciiTableRow::setCol( std::size_t colIdx, const char* newContent )
{
  setCol( colIdx, std::string( newContent ) );
}
inline void AsciiTableRow::checkIdxBound_( std::size_t colIdx, std::string const& msg )
{
  if( colIdx > data_.size() )
  {
   throw std::out_of_range( "AsciiTableRow::" + msg + " : colIdx out of range" );
  }
}
------------------------------------------------------
The code above should be sufficient for the discussion and I donât think any other code is relevant. Â Should you need to see my constructor and destructor, nothing is virtual so I just copy every value and set some defaults. Â Nothing fancy, but I will gladly post the code if need be.
What I do then is try and devise a test to see if the exception is indeed thrown and if it is the correct one. Â Here is the code I use:
AsciiTableRow testRow; Â // empty, so any index will throw
BOOST_CHECK_THROW( testRow.setCol( 30, 12 ), std::out_of_range );
BOOST_CHECK_THROW( testRow.setCol( 30, 12l ), std::out_of_range );
This works fine. Â Then, I had the idea to check what the behaviour would be if the wrong exception is thrown. Â I simply changed the first call like this :
BOOST_CHECK_THROW( testRow.setCol( 30, 12 ), std::runtime_error );
BOOST_CHECK_THROW( testRow.setCol( 30, 12l ), std::out_of_range );
I was under the impression (and I might be wrong here, sorry  :-(  ) that the Boost.Test library would (the numbers are not to mean that order, simply there for enumeration):
1- output a message saying the test has failed, that an exception was indeed thrown, but it was not a runtime_error
2- continue the rest of the test (single test currently)
3- terminate
but I actually never get the message of step 1, the execution of the rest of the test seems never occur (the second BOOST_CHECK_THROW) and the program terminate because of my exception and not very gracefully.
I guess my questions to the other users here are:
 - Was my understanding of what BOOST_CHECK_THROW does flawed ?
 - Is the behaviour normal ?
 - Is there an obvious problem I am missing with the design of my simple class ?
In case it is useful, I am on OSX 10.9. Â I have compiled boost myself (sources of version 1.55 downloaded from the Boost site). Â I compile my work with C++11 enabled. Â I am using GCC 4.7 installed via Macports.
Anyhow, any insight will be appreciated.
As everyone is busy, thanks in advance to anybody for any time spent on this.
Ghyslain
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net