Menu

Baffled: CPPUNIT_ASSERT_EQUAL for class

Lynn Allan
2002-04-19
2002-04-19
  • Lynn Allan

    Lynn Allan - 2002-04-19

    I'm trying to get a simple class able to use CPPUNIT_ASSERT_EQUAL with visual c++ 6.0.  I found a VERY helpful webpage from John Lam on setting up the vc project so that the link resolves and completes.

    His sample code for Employee used std::string.  I wanted to use char*, but can't figure out what the CPPUNIT_ASSERT_EQUAL macro actually invokes.  I can look at the output from the preprocessor, and figure that it becomes:

    ::CppUnit::TestAssert::assertEquals( _john,
        _John,
        ::CppUnit::SourceLine( "EmpVc6.cpp", 82 ) );

    I've defined _john and _John to have the same name, "John".  My code does a strcmp, and should consider them equal.

    Below is the interface for class Employee.  I've defined everything I can think of for the macro to invoke for the equality test.  I put a breakpoint within each member function, as well as a
    cout << "Reached xxx" << endl;
    to indicate whether the code was reached. 

    In every case, none of the member functions were reached.  I'm baffled what function to define so that CPPUNIT_ASSERT_EQUAL reaches my code.  As near as I can tell, it simply does the default equality check of whether the addresses are the same.

    class Employee
    {
    private:
      char  _name[20];

    public:
      Employee( const char *name );
      ~Employee() {}

      char* GetName() { return (_name); }

      bool operator==(const Employee& other);

      bool equal(const Employee& other);

      static bool equal(const Employee& first, const Employee& second);
      static bool operator==(const Employee& first, const Employee& second);

      friend bool equal(const Employee& first, const Employee& second);
      friend bool operator==(const Employee& first, const Employee& second);
    };

     
    • Baptiste Lepilleur

      I'm on a rush so I'm not sure I'm ansewering the right question.

      But if you are using ASSERT_EQUAL to compare const char *, basicly you are doing:

      const char *name1 = "john";
      const char *name2 = "john";

      if ( name1 == name2 )
        FAIL();

      => you are comparing pointer, not content.

      Baptiste.

       
    • Lynn Allan

      Lynn Allan - 2002-04-19

      Hello Baptiste,

      That's exactly the problem ... I want to be able to provide a member funtion of Employee that is invoked by CPPUNIT_ASSERT_EQUAL.  With my use of char*, Employee needs to use something like strcmp to determine equality.  It can't rely on std::string reference counting for default equality.

      Consider an Employee class with std::string's for both _first and _last.  The equality test has to accept Employee("Smith", "John") being equal to Employee("Smith, "John"), but reject both ("Smith", "James"), and ("Smithe", "John").  To do this, there has to have a signature member function that the cppunit framework invokes.  I have so far been baffled to figure this out.  I think it has something to do with providing a ToString or perhaps a different assertion_traits.

      Thanks for your help on this.  Sorry for my frustration and impatience coming through.

      Regards,
      Lynn Allan

       

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.