jeudi 24 septembre 2015

How to unit test code with cin?

The main reason for this question is that I want to test my operator overloader without having to do user input during my unit tests. How would I best achieve this?

std :: istream & operator>>( istream &in, Fruit & f )
{
   char temp[31];

   in >> temp;
   f.name = new char[strlen(temp) + 1];
   strcpy(f.name, temp);
   for( int i = 0; i < CODE_LEN; i++ ) 
      in >> f.code[i];
   return in;
}


std :: ostream & operator<<( ostream &out, const Fruit & f )
{ 
   out << setiosflags(ios::left) << setw(MAX_NAME_LEN) << f.name 
      << " ";
   for( int i = 0; i < CODE_LEN; i++ ) // why is this necessary?
      out << f.code[i];
   return out;
}

Aucun commentaire:

Enregistrer un commentaire