c++ - 运算符过载测试失败

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

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

测试用例 #2 operator <=() FAILED 和 operator >() 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;
}

提前感谢您的帮助。

最佳答案

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

因为 '' 大于 'T',所以 "test" 大于 "Test" 按此规则。也许您希望 strcmp 做一些比它实际做的更多的事情,例如实现单词比较的英语语言规则。看看像 std::collat​​e 这样的东西。

关于c++ - 运算符过载测试失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34054982/

相关文章:

c++ - 如何为一类数值数组同时重载所有数学函数

c++ - 在 cpp 中重载 << 运算符的正确方法是什么

c++ - 使用不同方法的置换函数的运行速度导致意外结果

c++ - 这个 std::decay 的实现是正确的吗

c++ - 为什么 fstream 方法返回对 "*this"的引用?

c++ - 当代码具有从 char 到 wchar_t 的显式转换或反之亦然时,要求 vc++ 编译器显示编译器警告

c++ - 身份验证后从 curl 获取安全 cookie (c++)

c++ - 使用外部函数作为成员函数指针

c++ - 在 Visual C++ 上使用 msxml6.h 时出错

c++ - 模板类表达式参数重载