Hi
I am tring to test some method of vector STL. I putting here entire code. Please some try it out. Please Tell me, why setUp is not failing?
**************************************************
#ifndef __TEST_H__
#define __TEST_H__
#include<vector>
#include "cppunit/extensions/HelperMacros.h"
using namespace std;
typedef vector <char>charvect;
class vect : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( vect );
CPPUNIT_TEST( SizePushBackTest );
CPPUNIT_TEST_SUITE_END();
private:
charvect v;
}
void vect::SizePushBackTest ()
{
CPPUNIT_ASSERT_EQUAL(char('f'),v[5]);
int siz=v.size();
CPPUNIT_ASSERT_EQUAL(5,siz);
for( int i=0; i<v.size();i++)
{
CPPUNIT_ASSERT_EQUAL(char(i+'a'),v[i]);
}
}
As initialy v size was zero. then i am resizing in the setup to 5. while assigning the value to v. It should fail setUp.
I have tried this thing without the CPPUNIT.
So here Cppunit is not fullfilling the purpose.
- Regards
Ritesh
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
setUp isn't intended to fail tests for one. Second, the code you're running is not guaranteed to cause an error condition because it's what's known as "undefined behavior". This means that you may or may not get a failure.
If you want vector accesses to throw an exception, you've got to use the at() member function rather than operator[]().
v.resize(5);
v[10] = value; // will not necessarily cause an error, but it *is* undefined behavior
v.at(10) = value; // will throw an exception.
CppUnit is doing exactly what it should do in this case. If you want to get error conditions when accessing a vector, use a call that will generate error conditions.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
[disregarding the IMHO incorrect usage of setUp for reporting errors]
You are assigning an non-existent element of the vector using the array
subscription operator:
v[i] = i+a;
When "i >= v.size()", this results in undefined behaviour - it _could_ throw
an exception in debug builds on some STL implementations, but I'd guess that
on many implementations this results in 'silent' memory corruption.
If you want to have exceptions thrown, try:
v.at(i) = i+a;
// Johan
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi
I am tring to test some method of vector STL. I putting here entire code. Please some try it out. Please Tell me, why setUp is not failing?
**************************************************
#ifndef __TEST_H__
#define __TEST_H__
#include<vector>
#include "cppunit/extensions/HelperMacros.h"
using namespace std;
typedef vector <char>charvect;
class vect : public CppUnit::TestFixture
{
CPPUNIT_TEST_SUITE( vect );
CPPUNIT_TEST( SizePushBackTest );
CPPUNIT_TEST_SUITE_END();
private:
charvect v;
public:
void setUp();
void SizePushBackTest ();
};
#endif
**************************************************
#include<cctype>
#include"text1.h"
void vect::setUp()
{
v.resize(5);
for(int i=0;i<10;i++)
{
//push_back(i+'a');
v[i]=i+'a';
}
}
void vect::SizePushBackTest ()
{
CPPUNIT_ASSERT_EQUAL(char('f'),v[5]);
int siz=v.size();
CPPUNIT_ASSERT_EQUAL(5,siz);
for( int i=0; i<v.size();i++)
{
CPPUNIT_ASSERT_EQUAL(char(i+'a'),v[i]);
}
}
As initialy v size was zero. then i am resizing in the setup to 5. while assigning the value to v. It should fail setUp.
I have tried this thing without the CPPUNIT.
So here Cppunit is not fullfilling the purpose.
- Regards
Ritesh
setUp isn't intended to fail tests for one. Second, the code you're running is not guaranteed to cause an error condition because it's what's known as "undefined behavior". This means that you may or may not get a failure.
If you want vector accesses to throw an exception, you've got to use the at() member function rather than operator[]().
v.resize(5);
v[10] = value; // will not necessarily cause an error, but it *is* undefined behavior
v.at(10) = value; // will throw an exception.
CppUnit is doing exactly what it should do in this case. If you want to get error conditions when accessing a vector, use a call that will generate error conditions.
[disregarding the IMHO incorrect usage of setUp for reporting errors]
You are assigning an non-existent element of the vector using the array
subscription operator:
v[i] = i+a;
When "i >= v.size()", this results in undefined behaviour - it _could_ throw
an exception in debug builds on some STL implementations, but I'd guess that
on many implementations this results in 'silent' memory corruption.
If you want to have exceptions thrown, try:
v.at(i) = i+a;
// Johan