Solution/sequence_exam.cpp
Solution/sequence_exam.cpp
// FILE: sequence_exam.cpp
// Written by: Michael Main ([email protected]) - Oct 22, 1997
// Non-interactive test program for the sequence class from Section 3.2 of
// "Data Structures and Other Objects". Second Edition
//
// Each function of this program tests part of the sequence class, returning
// some number of points to indicate how much of the test was passed.
// A description and result of each test is printed to cout.
//
// Maximum number of points awarded by this program is determined by the
// constants POINTS[1], POINTS[2]...
#include // Provides cout and cin
#include // Provides EXIT_SUCCESS
#include "sequence1.h" // With value_type defined as double
using namespace std;
using namespace main_savitch_3;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 3;
const int POINTS[MANY_TESTS+1] = {
100, // Total points for all tests.
40, // Test 1 points
20, // Test 2 points
20 // Test 3 points
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for Chapter 3 sequence Class",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current"
};
// **************************************************************************
// bool test_basic(const sequence& test, sequence::size_type s, bool has_cursor)
// Postcondition: A return value of true indicates:
// a. test.size() is s, and
// b. test.is_item() is has_cursor.
// Otherwise the return value is false.
// In either case, a description of the test result is printed to cout.
// **************************************************************************
bool test_basic(const sequence& test, sequence::size_type s, bool has_cursor)
{
bool answer;
cout << "Testing that size() returns " << s << " ... ";
cout.flush( );
answer = (test.size( ) == s);
cout << (answer ? "Passed." : "Failed.") << endl;
if (answer)
{
cout << "Testing that is_item() returns ";
cout << (has_cursor ? "true" : "false") << " ... ";
cout.flush( );
answer = (test.is_item( ) == has_cursor);
cout << (answer ? "Passed." : "Failed.") << endl;
}
return answer;
}
// **************************************************************************
// bool test_items(sequence& test, sequence::size_type s, sequence::size_type i, double items[])
// The function determines if the test sequence has the correct items
// Precondition: The size of the items array is at least s.
// Postcondition: A return value of true indicates that test.current()
// is equal to items[i], and after test.advance() the result of
// test.current() is items[i+1], and so on through items[s-1].
// At this point, one more advance takes the cursor off the sequence.
// If any of this fails, the return value is false.
// NOTE: The test sequence has been changed by advancing its cursor.
// **************************************************************************
bool test_items(sequence& test, sequence::size_type s, sequence::size_type i, double items[])
{
bool answer = true;
cout << "The cursor should be at item [" << i << "]" << " of the sequence\n";
cout << "(counting the first item as [0]). I will advance the cursor\n";
cout << "to the end of the sequence, checking that each item is correct...";
cout.flush( );
while ((i < s) && test.is_item( ) && (test.current( ) == items[i]))
{
i++;
test.advance( );
}
if ((i != s) && !test.is_item( ))
{ // The test.is_item( ) function returns false too soon.
cout << "\n Cursor fell off the sequence too soon." << endl;
answer = false;
}
else if (i != s)
{ // The test.current( ) function returned a wrong value.
cout << "\n The item [" << i << "] should be " << items[i] << ",\n";
cout << " but it was " << test.current( ) << " instead.\n";
answer = false;
}
else if (test.is_item( ))
{ // The test.is_item( ) function returns true after moving off the sequence.
cout << "\n The cursor was moved off the sequence,";
cout << " but is_item still returns true." << endl;
answer = false;
}
cout << (answer ? "Passed." : "Failed.") << endl;
return answer;
}
// **************************************************************************
// bool correct(sequence test, sequence::size_type s, sequence::size_type cursor_spot, double items[])
// This function determines if the sequence (test) is "correct" according to
// these requirements:
// a. it has exactly s items.
// b. the items (starting at the front) are equal to
// double[0] ... double[size-1]
// c. if cursor_spot < size, then test's cursor must be at
// the location given by cursor_spot.
// d. if cursor_spot >= size, then test must not have a cursor.
// NOTE: The function also moves the cursor off the sequence.
// **************************************************************************
bool correct(sequence& test, sequence::size_type size, sequence::size_type cursor_spot, double items[])
{
bool has_cursor = (cursor_spot < size);
// Check the sequence's size and whether it has a cursor.
if (!test_basic(test, size, has_cursor))
{
cout << "Basic test of size() or is_item() failed." << endl << endl;
return false;
}
// If there is a cursor, check the items from cursor to end of the sequence.
if (has_cursor && !test_items(test, size, cursor_spot, items))
{
cout << "Test of the sequence's items failed." << endl << endl;
return false;
}
// Restart the cursor at the front of the sequence and test items again.
cout << "I'll call start() and look at the items one more time..." << endl;
test.start( );
if (has_cursor && !test_items(test, size, 0, items))
{
cout << "Test of the sequence's items failed." << endl << endl;
return false;
}
// If the code reaches here, then all tests have been passed.
cout << "All tests passed for this sequence." << endl << endl;
return true;
}
// **************************************************************************
// int test1( )
// Performs some basic tests of insert, attach, and the constant member
// functions. Returns POINTS[1] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test1( )
{
sequence empty; // An empty sequence
sequence test; // A sequence to add items to
double items1[4] = { 5, 10, 20, 30 }; // These 4 items are put in a sequence
double items2[4] = { 10, 15, 20, 30 }; // These are put in another sequence
// Test that the empty sequence is really empty
cout << "Starting with an empty sequence." << endl;
if (!correct(empty, 0, 0, items1)) return 0;
// Test the attach function to add something to an empty sequence
cout << "I am now using attach to put 10 into an empty sequence." << endl;
test.attach(10);
if (!correct(test, 1, 0, items2)) return 0;
// Test the insert function to add something to an empty sequence
cout << "I am now using insert to put 10 into an empty sequence." << endl;
test = empty;
test.insert(10);
if (!correct(test, 1, 0, items2)) return 0;
// Test the insert function to add an item at the front of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start and insert 5." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.insert(5);
if (!correct(test, 4, 0, items1)) return 0;
// Test the insert function to add an item in the middle of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start, advance once, ";
cout << "and insert 15." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.advance( );
test.insert(15);
if (!correct(test, 4, 1, items2)) return 0;
// Test the attach function to add an item in the middle of a sequence
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start and attach 15 ";...