Question: Write test cases in C++ for the following code using #using to test every method and function implemented within the String class. These should be

Write test cases in C++ for the following code using #using to test every method and function implemented within the String class. These should be adequate, well tested cases for each function/method. And if there are any mistakes in the code comment beside it the correct code! (This is important!)

--------------------------------------------------------------------------------------------------------------------------

String.cpp

--------------------------------------------------------------------------------------------------------------------------

#include

#include "String.hpp"

String::String(){

str[0] = 0;

}

String::String(char ch){

str[0] = ch;

str[1] = 0;

}

String::String(const char* s){

int pos = 0;

while(s[pos] != 0){

str[pos] = s[pos];

++pos;

if(pos >= capacity()) break;

}

str[pos] = 0;

}

int String::length() const{

int size = 0;

while(str[size] != 0)

++size;

return size;

}

int String::capacity() const{

return STRING_SIZE -1;

}

char& String::operator[](int i){

assert(i >= 0);

assert(i < length());

return str[i];

}

char String::operator[](int i) const{

assert(i >= 0);

assert(i < length());

return str[i];

}

bool String::operator==(const String& rhs) const{

int pos = 0;

while(str[pos] != 0 && str[pos] == rhs.str[pos]){

++pos;

}

return str[pos] == rhs.str[pos];

}

std::istream& operator>>(std::istream& in, String& rhs){

in >> rhs.str;

return in;

}

std::ostream& operator<<(std::ostream& out, const String& rhs){

out << rhs.str;

return out;

}

bool String::operator<(const String& rhs) const{

int pos = 0;

while(str[pos] != 0 && rhs.str[pos] != 0 && str[pos] == rhs.str[pos]){

++pos;

}

return str[pos] < rhs.str[pos];

}

String String::operator+(const String& rhs) const{

String result(str);

int offset = length();

int pos = 0;

while(rhs.str[pos] != 0){

result.str[offset + pos] = rhs.str[pos];

++pos;

}

result.str[offset + pos] = 0;

return result;

}

String& String::operator+=(String rhs){

int offset = length();

int pos = 0;

while(rhs.str[pos] != 0){

if((offset + pos) >= capacity())

break;

str[offset + pos] = rhs.str[pos];

++pos;

}

str[offset + pos] = 0;

return rhs;

}

String operator+(const char charArray[], const String& rhs){

return rhs + charArray;

}

String operator+(char s, const String& rhs){

return s + rhs;

}

bool operator==(const char charArray[], const String& rhs){

if(charArray == rhs){

return true;

}

else{

return false;

}

}

bool operator==(char s, const String& rhs){

if(s == rhs){

return true;

}

else{

return false;

}

}

bool operator<(const char charArray[], const String& rhs){

if(charArray < rhs){

return true;

}

else{

return false;

}

}

bool operator<(char s, const String& rhs){

if(s < rhs){

return true;

}

else{

return false;

}

}

bool operator<=(const String& lhs, const String& rhs){

if(lhs <= rhs){

return true;

}

else{

return false;

}

}

bool operator!=(const String& lhs, const String& rhs){

if(lhs != rhs){

return true;

}

else{

return false;

}

}

bool operator>=(const String& lhs, const String& rhs){

if(lhs >= rhs) {

return true;

}

else{

return false;

}

}

bool operator>(const String& lhs, const String& rhs){

if(lhs > rhs){

return true;

}

else{

return false;

}

}

--------------------------------------------------------------------------------------------------------------------------

String.hpp

--------------------------------------------------------------------------------------------------------------------------

#ifndef String_hpp

#define String_hpp

#include

#include

// size of the array.

const int STRING_SIZE = 256;

/**

* @invariant str[length()] == 0

* && 0 <= length() <= capacity()

* && capacity() == STRING_SIZE - 1

*

*/

class String {

private:

char str[STRING_SIZE];

public:

// constructor: empty string, String('x'), and String("abcd")

String();

String(char);

String(const char[]);

// subscript: accessor/modifier and accessor

char & operator[](int);

char operator[](int) const;

// max chars that can be stored (not including null terminator)

int capacity() const;

// number of char in string

int length () const;

// concatenation

String operator+ (const String &) const;

String & operator+=(String);

// relational methods

bool operator==(const String &) const;

bool operator< (const String &) const;

// i/o

friend std::istream & operator>>(std::istream &, String &);

friend std::ostream & operator<<(std::ostream &, const String &);

};

// free functios for concatenation and relational

String operator+ (const char[], const String &);

String operator+ (char, const String &);

bool operator== (const char[], const String &);

bool operator== (char, const String &);

bool operator< (const char[], const String &);

bool operator< (char, const String &);

bool operator<= (const String &, const String &);

bool operator!= (const String &, const String &);

bool operator>= (const String &, const String &);

bool operator> (const String &, const String &);

#endif

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!