Please send questions to
st10@humboldt.edu .
/*-----
Signature: main: void -> int
Purpose: testing program for the string_list class
Examples: when this is run, it should:
* create an empty string_list
* confirm that its length is currently 0
* add 5 strings
* confirm that its length is currently 5
* confirm that its 5 elements are what we think
* remove a 1-instance string that IS there; confirm it returns true,
* ...and that the string is NO longer there;
* remove 1st instance of a 2-instance string; confirm it rets true,
* ...and that 1st instance is gone, 1nd instance is still there,
* try to remove a string that is NOT there; confirm it returns false,
* ...and that the 3 strings are STILL there;
* try to (gulp!) copy a string_list to another string_list,
add to one, and show the other is unchanged?
* does adding > 10 strings work...?
by: Sharon M. Tuttle
last modified: 12-09-10
-----*/
#include <iostream>
#include "string_list.h"
using namespace std;
int main()
{
bool remove_result; // holds the result of the latest remove call
// create an empty string_list
string_list first_names;
cout << boolalpha << endl;
cout << "testing class string_list: " << endl;
cout << "---------------------------------------" << endl;
// confirm that its length is currently 0
cout << "first_names.get_length() == 0: "
<< (first_names.get_length() == 0) << endl;
// add 5 strings
first_names.add("David");
first_names.add("Katherine");
first_names.add("Thomas");
first_names.add("David");
first_names.add("Sandra");
// confirm that its length is currently 5
cout << endl;
cout << "AFTER 5 add's: " << endl;
cout << " first_names.get_length() == 5: "
<< (first_names.get_length() == 5) << endl;
// confirm that its 5 elements are what we think
cout << " are the 5 names added there? ";
cout << ((first_names.get_element(0) == "David") &&
(first_names.get_element(1) == "Katherine") &&
(first_names.get_element(2) == "Thomas") &&
(first_names.get_element(3) == "David") &&
(first_names.get_element(4) == "Sandra")) << endl;
// remove a string that IS there; confirm it returns true,
// and that only 4 names remain
remove_result = first_names.remove("Katherine");
cout << endl;
cout << "AFTER a successful remove: " << endl;
cout << " remove call returned: " << remove_result << endl;
cout << " first_names.get_length() == 4: "
<< (first_names.get_length() == 4) << endl;
cout << " the 4 names left are what we expect: ";
cout << ((first_names.get_element(0) == "David") &&
(first_names.get_element(1) == "Thomas") &&
(first_names.get_element(2) == "David") &&
(first_names.get_element(3) == "Sandra")) << endl;
// try to remove the first instance of a 2-instance string;
// confirm it returns true, and that only the first instance
// is removed, and that 3 strings are still there
remove_result = first_names.remove("David");
cout << endl;
cout << "AFTER a successful remove of a 2-instance string: " << endl;
cout << " remove call returned: " << remove_result << endl;
cout << " first_names.get_length() == 3: "
<< (first_names.get_length() == 3) << endl;
cout << " the 3 names left are what we expect: ";
cout << ((first_names.get_element(0) == "Thomas") &&
(first_names.get_element(1) == "David") &&
(first_names.get_element(2) == "Sandra")) << endl;
// try to remove a string that is NOT there; confirm it returns false,
// and that the 3 strings are still there
remove_result = first_names.remove("Greg");
cout << endl;
cout << "AFTER an properly-unsuccessful remove: " << endl;
cout << " remove call returned false? "
<< (remove_result == false) << endl;
cout << " first_names.get_length() == 3: "
<< (first_names.get_length() == 3) << endl;
cout << " the 3 names are still what we expect: ";
cout << ((first_names.get_element(0) == "Thomas") &&
(first_names.get_element(1) == "David") &&
(first_names.get_element(2) == "Sandra")) << endl;
// try to to (gulp!) copy a string_list to another string_list,
// add to one, and show the other is unchanged?
string_list list_copy;
list_copy = first_names;
// why not add > 10 strings to list_copy, and see if first_names changes?
for (int i=0; i < 12; i++)
{
list_copy.add("Moo");
}
cout << endl;
cout << "NOW..." << endl;
cout << "after list_copy = first_names and " << endl;
cout << " THEN adding 12 Moo's to list_copy:" << endl;
cout << endl;
cout << " first_names contains: " << endl;
for (int i=0; i< first_names.get_length(); i++)
{
cout << "first_names.get_element(" << i << "): "
<< first_names.get_element(i) << endl;
}
cout << endl;
cout << " list_copy contains: " << endl;
for (int i=0; i< list_copy.get_length(); i++)
{
cout << "list_copy.get_element(" << i << "): "
<< list_copy.get_element(i) << endl;
}
cout << endl;
return EXIT_SUCCESS;
}