Testing

Here is your testing assignment. Follow these steps to fulfill the goals of the assignment.

  1. Read the handout on Testing Programs.
  2. Extract the source dode from this message, and compile it.
  3. Run the program a few times to get a feel for what it does, and how it does it. You will need to read the code to do this.
  4. Prepare a set of files to be used as input to the program that will test its behavior according to the principles in the handout. Try to test any extremes of the program, if any; its behavior with special cases; its behavior with unexpected input and its behavior in typical cases. Print the results of each test along with the input that produced the test. Your aim is really to show the program failings, not just its good points.
  5. Analyze the test output and draw conclusions as to whether the program works correctly or not. Notice that correctness is really determined by the intended behavior, not necessarily the actual behavior. The description in the source code gives the intended behavior. Write your conclusions in a brief report (just a few sentences).
  6. Hand in your test with their results, and the report, by 5pm on Friday 28th. March.

(Note that this source code will also be sent by e-mail)

///////////////////////////////////////////////////
// Library holdings                              //
// Roger Hartley, Fall 1996                      //
// (modified for vectors, Spring 1997            //
// This program reads a number of lines of       //
// input, each of the form "action title author" //
// where action can be "add," when the action    //
// updates an array of books by adding the book  //
// (if it is not already present), or "delete"   //
// when the book is removed from the array (if it//
// is present). When the input is exhausted, the //
// array is printed.                             //
///////////////////////////////////////////////////

#include <string>
#include <vector>
#include <iostream.h>

// a book consists of a title and author
class Book {
private:
  string title;
  string author;
public:
  Book() { title = author = ""; }
  Book(string t, string a) { title = t; author = a; }
  bool operator==(Book b) { return title == b.title && author == b.author; }
  string getTitle() { return title; }
  string getAuthor() { return author; }
};

// a library is a number of books, stored in a vector
class Library {
private:
  vector<Book> collection;
  int number;
public:
  Library() : collection(10) { number = 0; }
  void Add(Book b);
  void Delete(Book b);
  void PrintHoldings();
};

// collection is the application class, it contains
// a library
class Collection {
private:
  Library lib;
public:
  void run();
};

// adding a book to the collection increases the number held by one
void Library::Add(Book b) {
  collection[number++] = b;
}

// deleting a book is a search through the vector
// if the book is found, it is deleted and the number held
// decreased by one
void Library::Delete(Book b) {
  for (int n = 0; n < number; n++)
    if (collection[n] == b) {
      for (int nn = n + 1; nn < number; nn++)
	collection[nn - 1] = collection[nn];
      cout << b.getTitle() << " by " << b.getAuthor() << " deleted" << endl;
      --number;
      return;
    }
  cout << b.getTitle() << " not found" << endl;
}

// printing the library is a single pass through
// the elements of the vector
void Library::PrintHoldings() {
  cout << endl << "There are " << number << " books" << endl;
  for (int n = 0; n < number; n++)
    cout << collection[n].getTitle() << " by " <<
            collection[n].getAuthor() << endl;
}

// books are added to the library from title and author
// typed in by the user
void Collection::run() {
  Library lib;
  string title, author, action;
  string prompt("Type action, title and author, separated by spaces: ");
  while (!cin.eof()) {
    cout << prompt;
    cin >> action >> title >> author;
    if (action == "add")
      lib.Add(Book(title, author));
    else
      lib.Delete(Book(title, author));
  }
  lib.PrintHoldings();  // print the library holdings
}

// the main function just calls the run function of the application
int main() {
  Collection c;
  c.run();
}