Menu

setUp problem pls check it out!!!

Help
ritesh
2002-12-27
2003-01-29
  • ritesh

    ritesh - 2002-12-27

    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

     
    • Tom Plunket

      Tom Plunket - 2003-01-10

      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.

       
    • Johan Nilsson

      Johan Nilsson - 2003-01-29

      [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&#039;d guess that
      on many implementations this results in &#039;silent&#039; memory corruption.

      If you want to have exceptions thrown, try:

      v.at(i) = i+a;

      // Johan

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.