1
这个问题的主要原因是,我想测试我的运营商overloader,而不必在我的单元测试期间做用户输入。我如何最好地实现这一目标?如何用cin单元测试代码?
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;
}