How can I compare two ostream objects in C ++ for equality?

I overloaded the left shift operator in my class and the output works fine, for example, when I have a line that says cout <OBJ; I will post fields separated by commas.

 ostream& operator<<(ostream& output, const MyClass& obj)
 {
     output << obj.field1 << ", " << obj.field2;
     return output;
 }

I want to write a test case for this operation, but I have no idea how to compare the returned result with the expected result in cxxtest. I tried the following, but this did not work:

 TS_ASSERT_EQUALS(cout << "1, 50.0", cout << obj);

Should I use another cxxtest operation or change the mechanism for passing parameters to TS_ASSERT_EQUALS?

Note that when I output the following lines, I get the same results:

 cout << obj;
 cout << "1, 50.0";

Note. When I try to compile a program, I get a bunch of compilers because TS_ASSERT_EQUALS crashes.

+3
1

, cout ...

std::stringstream s, .

ostream, TS_ASSERT_RELATION.

+2

Source: https://habr.com/ru/post/1773300/


All Articles