'Operator Overloading Test Failure
Build is successful, but the test case #2 keep failing. I don't know what to change or fix. The test program says that
Test Case #2 operator <=() FAILED and operator >() Failed.
Here is My header
#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
And here is the test program.
#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;
}
Thanks is advance for any help.
Solution 1:[1]
"[The strcmp] function starts comparing the first character of each string. If they are equal to each other, it continues with the following pairs until the characters differ or until a terminating null-character is reached. This function performs a binary comparison of the characters."
Since 't'
is greater than 'T'
, "test"
is greater than "Test"
by this rule. Perhaps you're expecting strcmp
to do something more than it actually does, such as implement English language rules for word comparisons. Have a look at things like std::collate
.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | David Schwartz |