Write test cases in C for the following code using using to

Write test cases in C++ for the following code using #using <cassert> 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 <iostream>

#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 <iostream>

#include <cassert>

// 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

Solution

I didn\'t understand why you have declared free functions for concatenation and relational operators. You could have declared it inside the class itself apart from some functions like (operator+(char [], String str) etc. Its just my suggestion. That\'s fine.

Other suggestion is never use \"assert\" to test your code. Because it is not encouraged from c++11. Because it always ends up in Core Dumps which may lead to potential memory leaks.

I have created a strmain.cpp to test using \"assert\" for testing some of the basic functionalites in your code. The following changes are need to be done in every relational operator function in your code.

Everywhere i have seen you are directly comparing lhs==rhs (or) lhs>rhs or lhs>=rhs. This is wrong. It basically compares the address of lhs and rhs. Not their contents. You have to implement like the below code

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

{

int len1=lhs.length(), len2=lhs.length();

if(len1>len2) return true;

else if(len1<len2) return false;

else{

for(int i=0;i<len1;i++)

{

if(lhs[i]>rhs[i]) return true;

}

return false;

}

}

Like the above function you have to compare every character in the relational and comparision operators. The following is the sample strmain.cpp. Please use like it below to test your code.

strmain.cpp:

#include<iostream>
using namespace std;

#include \"String.hpp\"

int main(void)
{

String str(\"This is test\");
String str2(\"This is \");

cout<<str<<endl<<str2<<endl;

assert(str!=str2);


//str2=operator+(\"test\", str2);

assert(str==str2);

return 0;
}

String.cpp:

#include <iostream>
#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];
}

//I changed the code here
bool String::operator==(const String& rhs) const{
int pos = 0;
int rhslength=rhs.length();
int lhslength=this->length();

if(lhslength!=rhslength) return false;

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<<std::endl;
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;
}
}

//I changed the code here
bool operator!=(const String& lhs, const String& rhs){

if(lhs.length()!=rhs.length()) return true;

int pos=0;

while(lhs[pos] != rhs[pos])
{
pos++;
}

if(pos==lhs.length())
return true;

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;
}
}

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

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site