Reputation: 35980
Using the CUTE unit test framework I have the following classes:
const std::size_t COARSE_MODEL_COUNT = 4;
class ModelType
{
public:
explicit ModelType(std::size_t aModelNumber) : theModelNumber(aModelNumber){}
virtual operator size_t(){ return theModelNumber; }
protected:
std::size_t theModelNumber;
};
class CoarseModelType : public ModelType
{
public:
explicit CoarseModelType(std::size_t aModelNumber) : ModelType(aModelNumber)
{
if (COARSE_MODEL_COUNT <= aModelNumber)
{
throw std::invalid_argument("Invalid model number selected for \
this model type.");
}
}
};
with the following unit test:
void ModelTypeTest::testCoarseModelConstructor()
{
//greater than test
ASSERT_THROWS(CoarseModelType(COARSE_MODEL_COUNT+1), std::invalid_argument);
//equal to
ASSERT_THROWS(CoarseModelType(COARSE_MODEL_COUNT), std::invalid_argument);
}
If I comment out the second ASSERT_THROWS
the unit test passes correctly. If I leave it in it fails. I would think that using the <=
operator on a std::size_t
type would recognize the ==
component but it's not. The ASSERT_THROWS
is a macro.
We're on Linux using gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-46)
. Is there some sort of implicit conversion or something going on that I'm not aware of? We're all scratching our heads on this one.
Edit:
The following does throw an error within the unit test and is registered as an error rather than a fail. The exception is thrown before the second ASSERT_THROWS
statement has a chance to fail:
void ModelTypeTest::testCoarseModelConstructor()
{
//greater than test
ASSERT_THROWS(CoarseModelType(COARSE_MODEL_COUNT+1), std::invalid_argument);
if(COARSE_MODEL_COUNT <= COARSE_MODEL_COUNT){
throw std::invalid_argument("wtf");
}
//equal to
ASSERT_THROWS(CoarseModelType(COARSE_MODEL_COUNT), std::invalid_argument);
}
So this is an issue with CUTE. I just don't know the how or why in regards to the macro interaction with the constructor code.
Upvotes: 2
Views: 545
Reputation: 99094
(Ahem) I think the ASSERT_THROWS
macro is doing something you're not aware of. Instead of
CoarseModelType(COARSE_MODEL_COUNT)
try
CoarseModelType dummy(COARSE_MODEL_COUNT)
and see what happens. The first is an odd way to invoke the constructor, and a clumsy macro might turn it into something that the compiler interprets differently.
(Note: you can use gcc -E
to preprocess the source code without compiling it, expanding macros in place so that you can see what they're doing.)
Upvotes: 2