运算符重载测试失败

时间:2015-12-02 23:18:53

标签: c++ visual-c++ overloading operator-keyword

构建成功,但测试用例#2仍然失败。我不知道要改变或修复什么。测试程序说

测试用例#2运算符< =()FAILED和运算符>()失败。

这是我的标题

#ifndef MYSTRING_H

#define MYSTRING_H

#include <iostream>

#include <cstring> //library functions 

#include <cstdlib> //exit() function 
using namespace std;


// MyString class
class MyString
{
private:
char *str;
int len;

public:
//constructor
MyString()
{
    *str = 0;
    len = 0;
}

// convert and copy constructors. 
MyString(char *);
MyString(MyString &);


// Destructor. 
~MyString()
{
    if (len != 0)
        delete[]  str;
    str = 0;
    len = 0;
}
// operators.   
int length() { return len; }
char *getValue() { return str; };

//==
bool operator==(MyString &);

//!=
bool operator!=(MyString &);

//>
bool operator>(MyString &);

//<
bool operator<(MyString &);

//>=
bool operator>=(MyString &);

//<=
bool operator<=(MyString &);

// insertion and extraction operators.
friend ostream &operator<<(ostream &, const MyString &);
};
// NOTE: Below is the implementation for the functions that are declared   above

//constructor
MyString::MyString(char *sptr)
{
len = strlen(sptr);

str = new char[len + 1];

strcpy(str, sptr);
}
//Copy Constructor
MyString::MyString(MyString &right)
{
str = new char[right.length() + 1];

strcpy(str, right.getValue());

len = right.length();
}

//==
bool MyString::operator==(MyString &right) 
{
return !strcmp(str, right.getValue());
}

//!=
bool MyString::operator!=(MyString &right)
{
return strcmp(str, right.getValue());
}
//>
bool MyString::operator>(MyString &right)
{
if (strcmp(str, right.getValue()) > 0)
    return true;
else
    return false;
}

//<
bool MyString::operator<(MyString &right)
{
if (strcmp(str, right.getValue()) < 0)
    return true;
else
    return false;
}

//>=
bool MyString::operator>=(MyString &right)
{
if (strcmp(str, right.getValue()) >= 0)
    return true;
else
    return false;

}

//<=
bool MyString::operator<=(MyString &right)
{
if (strcmp(str, right.getValue()) <= 0)
    return true;
else
    return false;

}

//stream operators
ostream &operator<<(ostream &strm, const MyString &obj)
{
strm << obj.str;
 return strm;
}

#endif

这是测试程序。

#include "stdafx.h"

#include <iostream>

#include "MyString.h"

using namespace std;

// Declare a new datatype that defines requires test case parameters
typedef struct {
char str1[128];
char str2[128];
bool equalNotEqual;
bool lessThan;
bool lessThanEqual;
bool greaterThan;
bool greaterThanEqual;
bool testPassed;
} TEST_CASE;

// Declare various test cases used to test the MyString object                          
TEST_CASE testCases[] = {
/* str1           str2         ==       <       <=       >       >=     P/F flag                                                        */
{ "test",        "test",      true,   false,   true,   false,   true,     true },
{ "test",        "Test",      false,  false,   true,   false,   true,     true },
{ "test",        "test1",     false,  true,    true,   false,   false,    true },
{ "test ",       "test",      false,  false,   false,  true,    true,        true }
};


// Main programentry point
int main()
{

// Flag used to determine if any test case failed
bool failed = false;

// Loop through all test cases
for (int i = 0; i < sizeof(testCases) / sizeof(TEST_CASE); ++i)
{

    // Instantiate two MyString objects that will be used to test overloaded operators
    MyString myStrObj1(testCases[i].str1);
    MyString myStrObj2(testCases[i].str2);

    cout << "Test Case #" << i + 1 << endl;

    //-------------------------
    // Test the operator==()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 == myStrObj2) != testCases[i].equalNotEqual)
    {
        cout << "\t***** operator==() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    //-------------------------
    // Test the operator!=()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 != myStrObj2) == testCases[i].equalNotEqual)
    {
        cout << "\t***** operator!=() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    //-------------------------
    // Test the operator<()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 < myStrObj2) != testCases[i].lessThan)
    {
        cout << "\t***** operator<() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    //-------------------------
    // Test the operator<=()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 <= myStrObj2) != testCases[i].lessThanEqual)
    {
        cout << "\t***** operator<=() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    //-------------------------
    // Test the operator>()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 > myStrObj2) != testCases[i].greaterThan)
    {
        cout << "\t***** operator>() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    //-------------------------
    // Test the operator>=()
    //vvvvvvvvvvvvvvvvvvvvvvvvv
    if ((myStrObj1 >= myStrObj2) != testCases[i].greaterThanEqual)
    {
        cout << "\t***** operator>=() FAILED" << endl;
        testCases[i].testPassed = false;
    }

    // Use the ostream operator to display the string stored in the MyString operator 
    cout << "The string should be \'" << testCases[i].str1 << "\' string   returned from MyClass \'" << myStrObj1 << "\'" << endl << endl;

    // Did this test case passed?
    //vvvvvvvvvvvvvvvvvvvvvvvvvvvvv
    if (testCases[i].testPassed)
    {
        // yes!!!
        cout << "Test Case #" << i + 1 << " PASSED!!!!" << endl << endl;
    }
    else
    {
        // Nope, set failed flag
        failed = true;
    }
} /* end of for loop */

  //-------------------------
  // Display Overall Status
  //vvvvvvvvvvvvvvvvvvvvvvvvv
if (!failed)
{
    cout << "**************************" << endl;
    cout << "***** OVERALL PASSED *****" << endl;
    cout << "**************************" << endl;
}
else
{
    cout << "**************************" << endl;
    cout << "***** OVERALL FAILED *****" << endl;
    cout << "**************************" << endl;
}

return 0;
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

“[strcmp]函数开始比较每个字符串的第一个字符。如果它们彼此相等,则继续使用以下对,直到字符不同或者到达终止空字符。函数执行字符的二进制比较。“

由于此<img src="//i.imgur.com/7xYQq8P.jpg" />大于't',因此'T'大于"test"。也许你期望"Test"做一些比实际更多的事情,比如实施单词比较的英语语言规则。看看像strcmp这样的东西。