testing overloaded assignment operator within a class

Posted on 2005-05-11
Medium Priority
Last Modified: 2010-04-17

I am trying to create a test class to test my overloaded functions and can't seem to call the operator functions correctly.

Here is my function for the assignment operator test for my complex number program:

void ComplexTest::test_AssignmentOp(const Complex& ct)
      cout << "Testing complex assignemnt operator..." << endl;
      if (_complex.operator =(ct.operator =)
            cout << "Passes assignment operator test!" << endl;
      else cout << "Failed assignment operator test!" << endl;
How do I test that opertaor-what needs to go on the right of it.  The program works fine I am just trying to add this test class to it.  Any ideas?  Thanks!
Question by:dauler
  • 2

Accepted Solution

Beer7Child earned 1000 total points
ID: 13984498

There are many thing you can test with regard to assignment operator.
You need to first decide what exactly do you want to test.
A reasonable thing to test is that after you assign an A to a B (i.e. B=A)
then they are equal (i.e. A==B).

So this uggest that the test function will assign one complex to the other,
and then compare them, If they differ, the test failed.

Here is the suggested code :

void ComplexTest::test_AssignmentOp(const Complex& ct)
/* Purpose : verify that the assignment operator works as expected.
 * Method : Assign a given complex object to a local object and verify
 * that after the assignment, both have the exact same value

  cout << "Testing complex assignemnt operator..." << endl;
  Complex lhs; // create a complex on stack, using default Ctor.
  lhs.operator=(ct); // assign the Complex given as parameter
  // verify that both complex are the same :
  if (lhs == ct ) // using operator == to compare
    cout << "Passes assignment operator test!" << endl;
    cout << "Failed assignment operator test!" << endl;

Of ourse, a shorter notation for the actuall assignment is :
  lhs = ct;

Hope this help,

Author Comment

ID: 13988332
Thanks beer7child(great moniker!)

That works-thank you.  However, testing the other operators such as +, - , * gives me an error message with the conditional statement.  The compiler says that conditional satements are not allowed with Complex type-any ideas?


Expert Comment

ID: 13996265

I'm glad you find my suggestion helpfull, and thanks for the points.

If you can post the actuall source code that is causing the compilation error
and the exact compiler message, I might be able to assist.


Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
The SignAloud Glove is capable of translating American Sign Language signs into text and audio.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Starting up a Project

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question