// strtest.cpp - Test program for simple string class
//
// CSE374 example, 5/08-11/15 hp

#include <iostream>
using namespace std;

#include "str.h"

// function prototypes
str id(str);
void printstr(str&);

// print message to show progress
void here(int n) {
  cout << "main: " << n << endl;
}

// id(x) == x (tests copy construtor)
str id(str s) {
  return s;
}

// print the string s
// (s is a reference parameter to avoid making copies of it)
void printstr(str &s) {
  char * str = s.c_str();
  cout << str << endl;
  delete [] str;
}

// create some strings and play with them
int main() {
  // local variables in main's stack frame
  // (destructor is called when main exits)
  str s1, s2;            // null constructors
  str hello("hello");    // c-string constructor
  str howdy = hello;     // copy constructor

  here(1);

  // get c-string from str
  char *hi = howdy.c_str();
  cout << hi << endl;
  delete [] hi;

  here(2);

  // test append (constructs a str and destructs it)
  hello.append(" there");
  printstr(hello);
  cout << "  length is " << hello.length() << endl;

  here(3);

  // assignment operator and str(char *) constructor
  s1 = "howdy";
  printstr(s1);

  here(4);

  // copy constructors
  s2 = id(hello);
  printstr(s2);

  here(5);

  // heap allocated str object
  // (uses copy constructor)
  // (destructor called when object deleted)
  // (same general idea as a Java String, but in C++)
  str * h = new str(hello);
  h->append(" y'all");
  printstr(*h);
  cout << "  length is " << h->length() << endl;
  delete h;

  here(6);

  // Enough already!!
  return 0;
}