Write Test cases for all of the functions and methods in C f

Write Test cases for all of the functions and methods in C++ for the class String.

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

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

Here is the testing code with asserts for you:

#include \"String.cpp\"
#include <cassert>
using namespace std;
int main()
{
    String str1;
    String str2 (\'H\');
    String str3 (\"Hello\");
    String str4 (\"World\");
    int temp = str1.capacity();
    cout<<\"The capacity of the initial String type variable is: \"<<temp<<endl;
    assert(temp == 255);
    cout<<\"The first element in str2 is: \"<<str2[0]<<endl;
    assert(str2[0]);
    temp = str3.length();
    cout<<\"The length of string str3 is: \"<<temp<<endl;
    assert(str3.length() == 5);
    str1 = str3 + str4;
    cout<<\"Now str1 is: \"<<str1<<endl;
    assert(str1 == \"HelloWorld\");
    str3 += str4;
    cout<<\"Now str3 is: \"<<str3<<endl;
    assert(str1 == str3);
    if(str1 < str3)
    cout<<\"Str1 is < Str3\"<<endl;
    else
    cout<<\"Str1 is !< Str3\"<<endl;
    assert(!(str1 < str3));
   
    if(str1 <= str3)
    cout<<\"Str1 is <= Str3\"<<endl;
    else
    cout<<\"Str1 is > Str3\"<<endl;
    assert(str1 <= str3);
   
    if(str2 <= str3)
    cout<<\"Str2 is <= Str3\"<<endl;
    else
    cout<<\"Str2 is > Str3\"<<endl;
    assert(str2 <= str3);
   
    if(str1 == str3)
    cout<<\"Str1 == Str3\"<<endl;
    else
    cout<<\"Str1 != Str3\"<<endl;
    assert(str1 == str3);
   
    if(str2 == str3)
    cout<<\"Str2 == Str3\"<<endl;
    else
    cout<<\"Str2 != Str3\"<<endl;
    assert(!(str2 == str3));
   
    if(str1 > str3)
    cout<<\"Str1 is > Str3\"<<endl;
    else
    cout<<\"Str1 is !> Str3\"<<endl;
    assert(!(str1 > str3));
   
    if(str1 >= str3)
    cout<<\"Str1 is >= Str3\"<<endl;
    else
    cout<<\"Str1 is < Str3\"<<endl;
    assert(str1 >= str3);
   
    if(str2 >= str3)
    cout<<\"Str2 is >= Str3\"<<endl;
    else
    cout<<\"Str2 is < Str3\"<<endl;
    assert(!(str2 >= str3));
   
    /*String s = \"the fastest test.\", t = \"st\";
    cout<<s<<endl;
    cout<<t<<endl;*/
   
}

Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------
Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------
Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------
Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------
Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------
Write Test cases for all of the functions and methods in C++ for the class String. --------------------------------------------------- String.cpp --------------

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site