0

I am using following example from here

Consider I have following class

#include <iostream>

class Distance {
private:
  int feet;             
  int inches;           

public:
  Distance()             : feet(), inches() {}
  Distance(int f, int i) : feet(f), inches(i) {}

  friend std::ostream &operator<<( std::ostream &output, const Distance &D )
  { 
     output << "F : " << D.feet << " I : " << D.inches;
     return output;            
  }

  friend std::istream &operator>>( std::istream  &input, Distance &D )
  { 
     input >> D.feet >> D.inches;
     return input;            
  }
};

I am using Gtest to test this class.

But I could not find better way to test it.

I can use the macro provided in gtest ASSERT_NO_THROW, but it will not validate the values. Is there any way I can use EXPECT_EQ instead?

Thanks

sbi
  • 219,715
  • 46
  • 258
  • 445
rovy
  • 2,481
  • 5
  • 18
  • 26
  • Just looking at this one example I can already tell you that this tutorial is bad and teaches C++ that professionals will frown upon. Get [a good book](http://stackoverflow.com/a/388282/140719) instead! – sbi Aug 04 '15 at 17:08
  • @sbi, I know, I just wanted to give simple example to explain my problem. – rovy Aug 04 '15 at 17:12
  • Could you please post your testing code also? I'd go for testing using a `std::istringstream`, `std::ostringstream`. – πάντα ῥεῖ Aug 04 '15 at 17:13
  • @Rohit: I have edited your question so that the class now uses [initialization lists](http://stackoverflow.com/q/1711990/140719). Feel free to disagree with my formatting, but without the initialization lists the code was just awful. And see [here](http://stackoverflow.com/a/1453605/140719) for why `using namespace std` is bad. – sbi Aug 04 '15 at 17:25
  • @sbi I agree code was awful. My apologies. – rovy Aug 04 '15 at 17:27

2 Answers2

5

Is there any way I can use EXPECT_EQ instead?

You can use a stringstream to print the results of operator<< to a string, then compare the string.

https://en.cppreference.com/w/cpp/io/basic_stringstream

TEST( Distance, Output )
{
    std::ostringstream out;
    Distance d;
    out << d;
    EXPECT_EQ( "F:0 I:0", out.str() );
}

Input test would be similar, just use std::istringtream instead.

Kevin Chen
  • 994
  • 8
  • 24
Slava
  • 43,454
  • 1
  • 47
  • 90
2

What do you want to test about the operators?

  • That the stream is in a good state after writing to or reading from it.
    You can check for that.

  • That the output operator writes a particular string for a particular distance.
    You can do this by writing into a std::ostringstream and comparing the result of calling its str() member with your expectations.

  • That the input iterator reads a particular distance from a particular string.
    You can do this employing a std::istringstream initialized with the string, comparing the distance read from it with what you expect.

  • That the class eats its own dog food.
    Use a std::stringstream to write into, then read from it, and compare what you read with what you wrote.
    Note: This will currently fail.

sbi
  • 219,715
  • 46
  • 258
  • 445