SlideShare a Scribd company logo
1 of 52
Download to read offline
(C++) Change the following program so that it uses a dynamic array instead of a static one. Test
it with sequence_exam2.cpp.
sequence1.cpp
#include "sequence1.h"
#include
#include
namespace sequencelab
{
sequence::sequence()
{
used = 0;
current_index = 0;
}
void sequence::start()
{
if (used > 0)
{
current_index = 0;
}
else
{
current_index = used;
}
}
void sequence::advance()
{
if (is_item())
{
current_index++;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
void sequence::insert(const value_type& figure)
{
if (size() < CAPACITY)
{
if (!is_item())
{
start();
}
for (size_t i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = figure;
used++;
}
else
{
std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::attach(const value_type& figure)
{
if (size() < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > current_index + 1; i--)
{
data[i] = data[i-1];
}
data[current_index + 1] = figure;
current_index++;
used++;
}
else
{
data[used] = figure;
current_index = used;
used++;
}
}
else
{
std::cout << "Did not attach, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::remove_current()
{
if (is_item())
{
for (size_t i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return ((current_index >= 0) && (current_index < used) && (used != 0));
}
sequence::value_type sequence::current() const
{
if (is_item())
{
return data[current_index];
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
}
----------------------------------------------------------------------------------
sequence1.h
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include
namespace sequencelab
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
static const size_type CAPACITY = 30;
sequence( );
void insert(const value_type& figure);
void attach(const value_type& figure);
void start( );
void advance( );
void remove_current( );
value_type current( ) const;
size_type size( ) const;
bool is_item( ) const;
private:
value_type data[CAPACITY];
size_type used;
size_type current_index;
};
}
#endif
------------------------------------------------------------------------------------
sequence_exam2.cpp
#include // Provides cout.
#include // Provides memcpy.
#include // Provides size_t.
#include "sequence2.h" // Provides the Sequence class with double items.
using namespace std;
using namespace main_savitch_4;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 7;
const int POINTS[MANY_TESTS+1] = {
21, // Total points for all tests.
4, // Test 1 points
4, // Test 2 points
4, // Test 3 points
2, // Test 4 points
2, // Test 5 points
2, // Test 6 points
3 // Test 7 points
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for sequence class with a dynamic array",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current",
"Testing the resize member function",
"Testing the copy constructor",
"Testing the assignment operator",
"Testing insert/attach when current DEFAULT_CAPACITY exceeded"
};
// **************************************************************************
// bool test_basic(const sequence& test, size_t 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, size_t 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, size_t s, size_t 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, size_t s, size_t i, double items[])
{
bool answer = true;
cout << "The cursor should be at item [" << i << "]" << " of the sequence ";
cout << "(counting the first item as [0]). I will advance the cursor ";
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 << " Cursor fell off the sequence too soon." << endl;
answer = false;
}
else if (i != s)
{ // The test.current( ) function returned a wrong value.
cout << " The item [" << i << "] should be " << items[i] << ", ";
cout << " but it was " << test.current( ) << " instead. ";
answer = false;
}
else if (test.is_item( ))
{ // The test.is_item( ) function returns true after moving off the sequence.
cout << " 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, size_t s, size_t 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
// items[0] ... items[s-1]
// c. if cursor_spot < s, then test's cursor must be at
// the location given by cursor_spot.
// d. if cursor_spot >= s, then test must not have a cursor.
// NOTE: The function also moves the cursor off the sequence.
// **************************************************************************
bool correct(sequence& test, size_t size, size_t 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. ";
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. ";
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. ";
cout << "Then I move the cursor to the start and attach 15 ";
cout << "after the 10." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.attach(15);
if (!correct(test, 4, 1, items2)) return 0;
// All tests have been passed
cout << "All tests of this first function have been passed." << endl;
return POINTS[1];
}
// **************************************************************************
// int test2( )
// Performs a test to ensure that the cursor can correctly be run off the end
// of the sequence. Also tests that attach/insert work correctly when there is
// no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test2( )
{
sequence test;
size_t i;
// Put three items in the sequence
cout << "Using attach to put 20 and 30 in the sequence, and then calling ";
cout << "advance, so that is_item should return false ... ";
cout.flush( );
test.attach(20);
test.attach(30);
test.advance( );
if (test.is_item( ))
{
cout << "failed." << endl;
return 0;
}
cout << "passed." << endl;
// Insert 10 at the front and run the cursor off the end again
cout << "Inserting 10, which should go at the sequence's front." << endl;
cout << "Then calling advance three times to run cursor off the sequence ...";
cout.flush( );
test.insert(10);
test.advance( ); // advance to the 20
test.advance( ); // advance to the 30
test.advance( ); // advance right off the sequence
if (test.is_item( ))
{
cout << " failed." << endl;
return false;
}
cout << " passed." << endl;
// Attach more items until the sequence becomes full.
// Note that the first attach should attach to the end of the sequence.
cout << "Calling attach to put the numbers 40, 50, 60 ...";
cout << test.DEFAULT_CAPACITY*10 << " at the sequence's end." << endl;
for (i = 4; i <= test.DEFAULT_CAPACITY; i++)
test.attach(i*10);
// Test that the sequence is correctly filled.
cout << "Now I will test that the sequence has 10, 20, 30, ...";
cout << test.DEFAULT_CAPACITY*10 << "." << endl;
test.start( );
for (i = 1; i <= test.DEFAULT_CAPACITY; i++)
{
if ((!test.is_item( )) || test.current( ) != i*10)
{
cout << " Test failed to find " << i*10 << endl;
return 0;
}
test.advance( );
}
if (test.is_item( ))
{
cout << " There are too many items on the sequence." << endl;
return false;
}
// All tests passed
cout << "All tests of this second function have been passed." << endl;
return POINTS[2];
}
// **************************************************************************
// int test3( )
// Performs basic tests for the remove_current function.
// Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost
// all the tests are passed. Otherwise returns 0.
// **************************************************************************
int test3( )
{
// In the next declarations, I am declaring a sequence called test.
// Both before and after the sequence, I declare a small array of characters,
// and I put the character 'x' into each spot of these arrays.
// Later, if I notice that one of the x's has been changed, or if
// I notice an 'x' inside of the sequence, then the most
// likely reason was that one of the sequence's member functions accessed
// the sequence's array outside of its legal indexes.
char prefix[4] = {'x', 'x', 'x', 'x'};
sequence test;
char suffix[4] = {'x', 'x', 'x', 'x'};
// Within this function, I create several different sequences using the
// items in these arrays:
double items1[1] = { 30 };
double items2[2] = { 10, 30 };
double items3[3] = { 10, 20, 30 };
size_t i; // for-loop control variable
char *char_ptr; // Variable to loop at each character in a sequence's memory
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and last and then first.
cout << "Using attach to build a sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Remove the 30, so entire sequence is now just 10 with no cursor.";
cout << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 1, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items2)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and then first and then last.
cout << "Using attach to build another sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10," << endl;
cout << "so the sequence should now contain just 30." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 1, 0, items1)) return 0;
cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items1)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the first.
cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence ";
cout << "is 20, then 10, then 30). Then remove the 20." << endl;
test.insert(30);
test.insert(10);
test.insert(20);
test.remove_current( );
if (!correct(test, 2, 0, items2)) return 0;
test.start( );
test.remove_current( );
test.remove_current( );
// Just for fun, fill up the sequence, and empty it!
cout << "Just for fun, I'll empty the sequence then fill it up, then ";
cout << "empty it again. During this process, I'll try to determine ";
cout << "whether any of the sequence's member functions access the ";
cout << "array outside of its legal indexes." << endl;
for (i = 0; i < test.DEFAULT_CAPACITY; i++)
test.insert(0);
for (i = 0; i < test.DEFAULT_CAPACITY; i++)
test.remove_current( );
// Make sure that the character 'x' didn't somehow get into the sequence,
// as that would indicate that the sequence member functions are
// copying data from before or after the sequence into the sequence.
char_ptr = (char *) &test;
for (i = 0; i < sizeof(sequence); i++)
if (char_ptr[i] == 'x')
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// Make sure that the prefix and suffix arrays still have four
// x's each. Otherwise one of the sequence operations wrote outside of
// the legal boundaries of its array.
for (i = 0; i < 4; i++)
if ((suffix[i] != 'x') || (prefix[i] != 'x'))
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// All tests passed
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
// **************************************************************************
// int test4( )
// Performs some tests of resize.
// Returns POINTS[4] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test4( )
{
sequence test;
size_t i;
char bytes[sizeof(sequence)];
char newbytes[sizeof(sequence)];
size_t mismatches;
cout << "I will now resize a sequence to a larger capacity, and then ";
cout << "attach that many items. The sequence should NOT need to ";
cout << "resize itself under this situation." << endl;
test.resize(2*test.DEFAULT_CAPACITY);
test.attach(0);
memcpy(bytes, (char *) &test, sizeof(sequence));
// At this point, I should be able to insert 2*DEFAULT_CAPACITY-1
// more items without calling resize again. Therefore, at most 1 byte
// of the object will change (the least significant byte of used).
for (i = 1; i < 2*test.DEFAULT_CAPACITY; i++)
test.attach(i);
test.start( );
memcpy(newbytes, (char *) &test, sizeof(sequence));
for (i = 0; i < 2*test.DEFAULT_CAPACITY; i++)
{
if (test.current( ) != i)
{
cout << " sequence does not contain correct items." << endl;
return 0;
}
test.advance( );
}
test.start( );
mismatches = 0;
for (i = 0; i < sizeof(sequence); i++)
if (bytes[i] != newbytes[i])
mismatches++;
if (mismatches > 1)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
cout << "Now I will call resize(1) for the sequence, but the actual ";
cout << "sequence should not change because the sequence already has  ";
cout << test.DEFAULT_CAPACITY*2 << " items." << endl;
memcpy(bytes, (char *) &test, sizeof(sequence));
test.resize(1);
mismatches = 0;
for (i = 0; i < sizeof(sequence); i++)
if (bytes[i] != newbytes[i])
mismatches++;
if (mismatches > 0)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
// All tests passed
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
// **************************************************************************
// int test5( )
// Performs some tests of the copy constructor.
// Returns POINTS[5] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test5( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Copy constructor test: for an empty sequence." << endl;
sequence copy1(original);
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Copy constructor test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++)
original.attach(i);
sequence copy2(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.DEFAULT_CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.DEFAULT_CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Copy constructor test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items)
)
return 0;
// All tests passed
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
// **************************************************************************
// int test6( )
// Performs some tests of the assignment operator.
// Returns POINTS[6] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test6( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Assignment operator test: for an empty sequence." << endl;
sequence copy1;
copy1 = original;
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Assignment operator test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++)
original.attach(i);
sequence copy2;
copy2 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.DEFAULT_CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.DEFAULT_CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Assignment operator test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5;
copy5 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items)
)
return 0;
cout << "Checking correctness of a self-assignment x = x;" << endl;
original.insert(2);
original = original;
if (!correct
(original, 2*original.DEFAULT_CAPACITY, 1, items)
)
return 0;
// All tests passed
cout << "All tests of this sixth function have been passed." << endl;
return POINTS[6];
}
// **************************************************************************
// int test7( )
// Performs some basic tests of insert and attach for the case where the
// current capacity has been reached.
// Returns POINTS[7] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test7( )
{
sequence testa, testi;
double items[2*testa.DEFAULT_CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++)
items[i-1] = i;
cout << "Testing to see that attach works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++)
testa.attach(i);
if (!correct
(testa, 2*testa.DEFAULT_CAPACITY, 2*testa.DEFAULT_CAPACITY-1, items)
)
return 0;
cout << "Testing to see that insert works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 2*testi.DEFAULT_CAPACITY; i >= 1; i--)
testi.insert(i);
if (!correct
(testi, 2*testi.DEFAULT_CAPACITY, 0, items)
)
return 0;
// All tests passed
cout << "All tests of this seventh function have been passed." << endl;
return POINTS[7];
}
int run_a_test(int number, const char message[], int test_function( ), int max)
{
int result;
cout << endl << "START OF TEST " << number << ":" << endl;
cout << message << " (" << max << " points)." << endl;
result = test_function( );
if (result > 0)
{
cout << "Test " << number << " got " << result << " points";
cout << " out of a possible " << max << "." << endl;
}
else
cout << "Test " << number << " failed." << endl;
cout << "END OF TEST " << number << "." << endl << endl;
return result;
}
// **************************************************************************
// int main( )
// The main program calls all tests and prints the sum of all points
// earned from the tests.
// **************************************************************************
int main( )
{
int sum = 0;
cout << "Running " << DESCRIPTION[0] << endl;
sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]);
sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]);
sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]);
sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]);
sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]);
sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]);
sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]);
cout << "If you submit this sequence to Dora now, you will have ";
cout << sum << " points out of the " << POINTS[0];
cout << " points from this test program. ";
return EXIT_SUCCESS;
}
Solution
//change to dynamic array is working fine..But already some of the functions were not available.
I did not take that into consideration since it is out of scope for this question.
PROGRAM CODE:
sequence1.h
/*
* sequence1.h
*
* Created on: 24-Feb-2017
* Author: kasturi
*/
#ifndef MAIN_SAVITCH_SEQUENCE_H
#define MAIN_SAVITCH_SEQUENCE_H
#include
namespace sequencelab
{
class sequence
{
public:
typedef double value_type;
typedef std::size_t size_type;
size_type CAPACITY;
sequence( );
void insert(const value_type& figure);
void attach(const value_type& figure);
void start( );
void advance( );
void remove_current( );
value_type current( ) const;
size_type size( ) const;
bool is_item( ) const;
private:
value_type *data;
size_type used;
size_type current_index;
};
}
#endif
sequence1.cpp
/*
* sequence1.cpp
*
* Created on: 24-Feb-2017
* Author: kasturi
*/
#include "sequence1.h"
#include
#include
namespace sequencelab
{
sequence::sequence()
{
used = 0;
current_index = 0;
CAPACITY = 30;
data = new value_type[CAPACITY];
}
void sequence::start()
{
if (used > 0)
{
current_index = 0;
}
else
{
current_index = used;
}
}
void sequence::advance()
{
if (is_item())
{
current_index++;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
void sequence::insert(const value_type& figure)
{
if (size() < CAPACITY)
{
if (!is_item())
{
start();
}
for (size_t i = used; i > current_index; i--)
{
data[i] = data[i-1];
}
data[current_index] = figure;
used++;
}
else
{
std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::attach(const value_type& figure)
{
if (used < CAPACITY)
{
if (is_item())
{
for (size_t i = used; i > current_index + 1; i--)
{
data[i] = data[i-1];
}
data[current_index + 1] = figure;
current_index++;
used++;
}
else
{
data[used] = figure;
current_index = used;
used++;
}
}
else
{
std::cout << "Did not attach, size is larger/equal to the capacity." << std::endl;
}
}
void sequence::remove_current()
{
if (is_item())
{
for (size_t i = current_index; i < used; i++)
{
data[i] = data[i+1];
}
used--;
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
sequence::size_type sequence::size() const
{
return used;
}
bool sequence::is_item() const
{
return ((current_index >= 0) && (current_index < used) && (used != 0));
}
sequence::value_type sequence::current() const
{
if (is_item())
{
return data[current_index];
}
else
{
std::cout << "Nothing here!" << std::endl;
}
}
}
sequence_exam2.cpp
/*
* sequence_exam2.cpp
*
* Created on: 24-Feb-2017
* Author: kasturi
*/
#include // Provides cout.
#include // Provides memcpy.
#include // Provides size_t.
#include "sequence1.h" // Provides the Sequence class with double items.
using namespace std;
using namespace sequencelab;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 7;
const int POINTS[MANY_TESTS+1] = {
21, // Total points for all tests.
4, // Test 1 points
4, // Test 2 points
4, // Test 3 points
2, // Test 4 points
2, // Test 5 points
2, // Test 6 points
3 // Test 7 points
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for sequence class with a dynamic array",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current",
"Testing the resize member function",
"Testing the copy constructor",
"Testing the assignment operator",
"Testing insert/attach when current DEFAULT_CAPACITY exceeded"
};
// **************************************************************************
// bool test_basic(const sequence& test, size_t 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, size_t 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, size_t s, size_t 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, size_t s, size_t i, double items[])
{
bool answer = true;
cout << "The cursor should be at item [" << i << "]" << " of the sequence ";
cout << "(counting the first item as [0]). I will advance the cursor ";
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 << " Cursor fell off the sequence too soon." << endl;
answer = false;
}
else if (i != s)
{ // The test.current( ) function returned a wrong value.
cout << " The item [" << i << "] should be " << items[i] << ", ";
cout << " but it was " << test.current( ) << " instead. ";
answer = false;
}
else if (test.is_item( ))
{ // The test.is_item( ) function returns true after moving off the sequence.
cout << " 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, size_t s, size_t 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
// items[0] ... items[s-1]
// c. if cursor_spot < s, then test's cursor must be at
// the location given by cursor_spot.
// d. if cursor_spot >= s, then test must not have a cursor.
// NOTE: The function also moves the cursor off the sequence.
// **************************************************************************
bool correct(sequence& test, size_t size, size_t 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. ";
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. ";
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. ";
cout << "Then I move the cursor to the start and attach 15 ";
cout << "after the 10." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.attach(15);
if (!correct(test, 4, 1, items2)) return 0;
// All tests have been passed
cout << "All tests of this first function have been passed." << endl;
return POINTS[1];
}
// **************************************************************************
// int test2( )
// Performs a test to ensure that the cursor can correctly be run off the end
// of the sequence. Also tests that attach/insert work correctly when there is
// no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test2( )
{
sequence test;
size_t i;
// Put three items in the sequence
cout << "Using attach to put 20 and 30 in the sequence, and then calling ";
cout << "advance, so that is_item should return false ... ";
cout.flush( );
test.attach(20);
test.attach(30);
test.advance( );
if (test.is_item( ))
{
cout << "failed." << endl;
return 0;
}
cout << "passed." << endl;
// Insert 10 at the front and run the cursor off the end again
cout << "Inserting 10, which should go at the sequence's front." << endl;
cout << "Then calling advance three times to run cursor off the sequence ...";
cout.flush( );
test.insert(10);
test.advance( ); // advance to the 20
test.advance( ); // advance to the 30
test.advance( ); // advance right off the sequence
if (test.is_item( ))
{
cout << " failed." << endl;
return false;
}
cout << " passed." << endl;
// Attach more items until the sequence becomes full.
// Note that the first attach should attach to the end of the sequence.
cout << "Calling attach to put the numbers 40, 50, 60 ...";
cout << test.CAPACITY*10 << " at the sequence's end." << endl;
for (i = 4; i <= test.CAPACITY; i++)
test.attach(i*10);
// Test that the sequence is correctly filled.
cout << "Now I will test that the sequence has 10, 20, 30, ...";
cout << test.CAPACITY*10 << "." << endl;
test.start( );
for (i = 1; i <= test.CAPACITY; i++)
{
if ((!test.is_item( )) || test.current( ) != i*10)
{
cout << " Test failed to find " << i*10 << endl;
return 0;
}
test.advance( );
}
if (test.is_item( ))
{
cout << " There are too many items on the sequence." << endl;
return false;
}
// All tests passed
cout << "All tests of this second function have been passed." << endl;
return POINTS[2];
}
// **************************************************************************
// int test3( )
// Performs basic tests for the remove_current function.
// Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost
// all the tests are passed. Otherwise returns 0.
// **************************************************************************
int test3( )
{
// In the next declarations, I am declaring a sequence called test.
// Both before and after the sequence, I declare a small array of characters,
// and I put the character 'x' into each spot of these arrays.
// Later, if I notice that one of the x's has been changed, or if
// I notice an 'x' inside of the sequence, then the most
// likely reason was that one of the sequence's member functions accessed
// the sequence's array outside of its legal indexes.
char prefix[4] = {'x', 'x', 'x', 'x'};
sequence test;
char suffix[4] = {'x', 'x', 'x', 'x'};
// Within this function, I create several different sequences using the
// items in these arrays:
double items1[1] = { 30 };
double items2[2] = { 10, 30 };
double items3[3] = { 10, 20, 30 };
size_t i; // for-loop control variable
char *char_ptr; // Variable to loop at each character in a sequence's memory
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and last and then first.
cout << "Using attach to build a sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Remove the 30, so entire sequence is now just 10 with no cursor.";
cout << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 1, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items2)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the middle,
// and then first and then last.
cout << "Using attach to build another sequence with 10,30." << endl;
test.attach(10);
test.attach(30);
cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl;
test.insert(20);
if (!correct(test, 3, 1, items3)) return 0;
cout << "Remove the 20, so entire sequence is now 10,30." << endl;
test.start( );
test.advance( );
test.remove_current( );
if (!correct(test, 2, 1, items2)) return 0;
cout << "Set the cursor to the start and remove the 10," << endl;
cout << "so the sequence should now contain just 30." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 1, 0, items1)) return 0;
cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl;
test.start( );
test.remove_current( );
if (!correct(test, 0, 0, items1)) return 0;
// Build a sequence with three items 10, 20, 30, and remove the first.
cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence ";
cout << "is 20, then 10, then 30). Then remove the 20." << endl;
test.insert(30);
test.insert(10);
test.insert(20);
test.remove_current( );
if (!correct(test, 2, 0, items2)) return 0;
test.start( );
test.remove_current( );
test.remove_current( );
// Just for fun, fill up the sequence, and empty it!
cout << "Just for fun, I'll empty the sequence then fill it up, then ";
cout << "empty it again. During this process, I'll try to determine ";
cout << "whether any of the sequence's member functions access the ";
cout << "array outside of its legal indexes." << endl;
for (i = 0; i < test.CAPACITY; i++)
test.insert(0);
for (i = 0; i < test.CAPACITY; i++)
test.remove_current( );
// Make sure that the character 'x' didn't somehow get into the sequence,
// as that would indicate that the sequence member functions are
// copying data from before or after the sequence into the sequence.
char_ptr = (char *) &test;
for (i = 0; i < sizeof(sequence); i++)
if (char_ptr[i] == 'x')
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// Make sure that the prefix and suffix arrays still have four
// x's each. Otherwise one of the sequence operations wrote outside of
// the legal boundaries of its array.
for (i = 0; i < 4; i++)
if ((suffix[i] != 'x') || (prefix[i] != 'x'))
{
cout << "Illegal array access detected." << endl;
return POINTS[3] / 4;
}
// All tests passed
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
// **************************************************************************
// int test4( )
// Performs some tests of resize.
// Returns POINTS[4] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test4( )
{
sequence test;
size_t i;
char bytes[sizeof(sequence)];
char newbytes[sizeof(sequence)];
size_t mismatches;
cout << "I will now resize a sequence to a larger capacity, and then ";
cout << "attach that many items. The sequence should NOT need to ";
cout << "resize itself under this situation." << endl;
//test.resize(2*test.CAPACITY);
cout<<"Current size: "< 1)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
cout << "Now I will call resize(1) for the sequence, but the actual ";
cout << "sequence should not change because the sequence already has  ";
cout << test.CAPACITY*2 << " items." << endl;
memcpy(bytes, (char *) &test, sizeof(sequence));
// test.resize(1);
mismatches = 0;
for (i = 0; i < sizeof(sequence); i++)
if (bytes[i] != newbytes[i])
mismatches++;
if (mismatches > 0)
{
cout << " sequence was resized when it should not be." << endl;
return 0;
}
else
cout << " Test passed." << endl;
// All tests passed
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
// **************************************************************************
// int test5( )
// Performs some tests of the copy constructor.
// Returns POINTS[5] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test5( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Copy constructor test: for an empty sequence." << endl;
sequence copy1(original);
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Copy constructor test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.CAPACITY; i++)
original.attach(i);
sequence copy2(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct(copy2, 2*original.CAPACITY, 2*original.CAPACITY-1, items))
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.CAPACITY, original.CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Copy constructor test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5(original);
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.CAPACITY, 2*original.CAPACITY, items)
)
return 0;
// All tests passed
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
// **************************************************************************
// int test6( )
// Performs some tests of the assignment operator.
// Returns POINTS[6] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test6( )
{
sequence original; // A sequence that we'll copy.
double items[2*original.CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*original.CAPACITY; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change the original.
cout << "Assignment operator test: for an empty sequence." << endl;
sequence copy1;
copy1 = original;
original.attach(1); // Changes the original sequence, but not the copy.
if (!correct(copy1, 0, 0, items)) return 0;
// Test copying of a sequence with current item at the tail.
cout << "Assignment operator test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*original.CAPACITY; i++)
original.attach(i);
sequence copy2;
copy2 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy2, 2*original.CAPACITY, 2*original.CAPACITY-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < original.CAPACITY; i++)
original.advance( );
// Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
sequence copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy3, 2*original.CAPACITY, original.CAPACITY, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy4, 2*original.CAPACITY, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Assignment operator test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
// There is now no current item.
sequence copy5;
copy5 = original;
original.start( );
original.advance( );
original.remove_current( ); // Removes 2 from the original, but not the copy.
if (!correct
(copy5, 2*original.CAPACITY, 2*original.CAPACITY, items)
)
return 0;
cout << "Checking correctness of a self-assignment x = x;" << endl;
original.insert(2);
//original = original;
if (!correct
(original, 2*original.CAPACITY, 1, items)
)
return 0;
// All tests passed
cout << "All tests of this sixth function have been passed." << endl;
return POINTS[6];
}
// **************************************************************************
// int test7( )
// Performs some basic tests of insert and attach for the case where the
// current capacity has been reached.
// Returns POINTS[7] if the tests are passed. Otherwise returns 0.
// **************************************************************************
int test7( )
{
sequence testa, testi;
double items[2*testa.CAPACITY];
size_t i;
// Set up the items array to conatin 1...2*DEFAULT_CAPACITY.
for (i = 1; i <= 2*testa.CAPACITY; i++)
items[i-1] = i;
cout << "Testing to see that attach works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 1; i <= 2*testa.CAPACITY; i++)
testa.attach(i);
if (!correct
(testa, 2*testa.CAPACITY, 2*testa.CAPACITY-1, items)
)
return 0;
cout << "Testing to see that insert works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 2*testi.CAPACITY; i >= 1; i--)
testi.insert(i);
if (!correct
(testi, 2*testi.CAPACITY, 0, items)
)
return 0;
// All tests passed
cout << "All tests of this seventh function have been passed." << endl;
return POINTS[7];
}
int run_a_test(int number, const char message[], int test_function( ), int max)
{
int result;
cout << endl << "START OF TEST " << number << ":" << endl;
cout << message << " (" << max << " points)." << endl;
result = test_function( );
if (result > 0)
{
cout << "Test " << number << " got " << result << " points";
cout << " out of a possible " << max << "." << endl;
}
else
cout << "Test " << number << " failed." << endl;
cout << "END OF TEST " << number << "." << endl << endl;
return result;
}
// **************************************************************************
// int main( )
// The main program calls all tests and prints the sum of all points
// earned from the tests.
// **************************************************************************
int main( )
{
int sum = 0;
cout << "Running " << DESCRIPTION[0] << endl;
sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]);
sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]);
sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]);
sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]);
sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]);
sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]);
sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]);
cout << "If you submit this sequence to Dora now, you will have ";
cout << sum << " points out of the " << POINTS[0];
cout << " points from this test program. ";
return EXIT_SUCCESS;
}
OUTPUT:
Running tests for sequence class with a dynamic array
START OF TEST 1:
Testing insert, attach, and the constant member functions (4 points).
Starting with an empty sequence.
Testing that size() returns 0 ... Passed.
Testing that is_item() returns false ... Passed.
I'll call start() and look at the items one more time...
All tests passed for this sequence.
I am now using attach to put 10 into an empty sequence.
Testing that size() returns 1 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
I am now using insert to put 10 into an empty sequence.
Testing that size() returns 1 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
I am now using attach to put 10,20,30 in an empty sequence.
Then I move the cursor to the start and insert 5.
Testing that size() returns 4 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
I am now using attach to put 10,20,30 in an empty sequence.
Then I move the cursor to the start, advance once, and insert 15.
Testing that size() returns 4 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
I am now using attach to put 10,20,30 in an empty sequence.
Then I move the cursor to the start and attach 15 after the 10.
Testing that size() returns 4 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
All tests of this first function have been passed.
Test 1 got 4 points out of a possible 4.
END OF TEST 1.
START OF TEST 2:
Testing situations where the cursor goes off the sequence (4 points).
Using attach to put 20 and 30 in the sequence, and then calling
advance, so that is_item should return false ... passed.
Inserting 10, which should go at the sequence's front.
Then calling advance three times to run cursor off the sequence ... passed.
Calling attach to put the numbers 40, 50, 60 ...300 at the sequence's end.
Now I will test that the sequence has 10, 20, 30, ...300.
All tests of this second function have been passed.
Test 2 got 4 points out of a possible 4.
END OF TEST 2.
START OF TEST 3:
Testing remove_current (4 points).
Using attach to build a sequence with 10,30.
Insert a 20 before the 30, so entire sequence is 10,20,30.
Testing that size() returns 3 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Remove the 20, so entire sequence is now 10,30.
Testing that size() returns 2 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Remove the 30, so entire sequence is now just 10 with no cursor.
Testing that size() returns 1 ... Passed.
Testing that is_item() returns false ... Passed.
I'll call start() and look at the items one more time...
All tests passed for this sequence.
Set the cursor to the start and remove the 10.
Testing that size() returns 0 ... Passed.
Testing that is_item() returns false ... Passed.
I'll call start() and look at the items one more time...
All tests passed for this sequence.
Using attach to build another sequence with 10,30.
Insert a 20 before the 30, so entire sequence is 10,20,30.
Testing that size() returns 3 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Remove the 20, so entire sequence is now 10,30.
Testing that size() returns 2 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [1] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Set the cursor to the start and remove the 10,
so the sequence should now contain just 30.
Testing that size() returns 1 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Remove the 30 from the sequence, resulting in an empty sequence.
Testing that size() returns 0 ... Passed.
Testing that is_item() returns false ... Passed.
I'll call start() and look at the items one more time...
All tests passed for this sequence.
Build a new sequence by inserting 30, 10, 20 (so the sequence
is 20, then 10, then 30). Then remove the 20.
Testing that size() returns 2 ... Passed.
Testing that is_item() returns true ... Passed.
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
I'll call start() and look at the items one more time...
The cursor should be at item [0] of the sequence
(counting the first item as [0]). I will advance the cursor
to the end of the sequence, checking that each item is correct...Passed.
All tests passed for this sequence.
Just for fun, I'll empty the sequence then fill it up, then
empty it again. During this process, I'll try to determine
whether any of the sequence's member functions access the
array outside of its legal indexes.
All tests of this third function have been passed.
Test 3 got 4 points out of a possible 4.
END OF TEST 3.

More Related Content

Similar to (C++) Change the following program so that it uses a dynamic array i.pdf

2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good TestsTomek Kaczanowski
 
Chaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscoreChaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscoreNicolas Carlo
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Given the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdfGiven the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdfNicholasflqStewartl
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceTobie Langel
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsTomek Kaczanowski
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOSKremizas Kostas
 
----------Evaluator-java---------------- package evaluator- import j.docx
----------Evaluator-java---------------- package evaluator-   import j.docx----------Evaluator-java---------------- package evaluator-   import j.docx
----------Evaluator-java---------------- package evaluator- import j.docxjanettjz6sfehrle
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good TestsTomek Kaczanowski
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreNicolas Carlo
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdfHans Jones
 
#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdfsrinivas9922
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxssuser454af01
 
Ember testing internals with ember cli
Ember testing internals with ember cliEmber testing internals with ember cli
Ember testing internals with ember cliCory Forsyth
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsDawid Rusnak
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxbradburgess22840
 
Ive posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfIve posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfdeepaarora22
 

Similar to (C++) Change the following program so that it uses a dynamic array i.pdf (20)

2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests2013 DevFest Vienna - Bad Tests, Good Tests
2013 DevFest Vienna - Bad Tests, Good Tests
 
Chaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscoreChaining et composition de fonctions avec lodash / underscore
Chaining et composition de fonctions avec lodash / underscore
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Given the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdfGiven the following errors and class in Java- How are these errors fix.pdf
Given the following errors and class in Java- How are these errors fix.pdf
 
Unittesting JavaScript with Evidence
Unittesting JavaScript with EvidenceUnittesting JavaScript with Evidence
Unittesting JavaScript with Evidence
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
 
Testing the waters of iOS
Testing the waters of iOSTesting the waters of iOS
Testing the waters of iOS
 
----------Evaluator-java---------------- package evaluator- import j.docx
----------Evaluator-java---------------- package evaluator-   import j.docx----------Evaluator-java---------------- package evaluator-   import j.docx
----------Evaluator-java---------------- package evaluator- import j.docx
 
33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests33rd Degree 2013, Bad Tests, Good Tests
33rd Degree 2013, Bad Tests, Good Tests
 
Google guava
Google guavaGoogle guava
Google guava
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
Chaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscoreChaining and function composition with lodash / underscore
Chaining and function composition with lodash / underscore
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf
 
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docxfilesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
filesHeap.h#ifndef HEAP_H#define HEAP_H#includ.docx
 
Ember testing internals with ember cli
Ember testing internals with ember cliEmber testing internals with ember cli
Ember testing internals with ember cli
 
Node.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizationsNode.js behind: V8 and its optimizations
Node.js behind: V8 and its optimizations
 
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docxIn Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
In Class AssignmetzCST280W13a-1.pdfCST 280 In-Class Pract.docx
 
Ive posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdfIve posted 3 classes after the instruction that were given at star.pdf
Ive posted 3 classes after the instruction that were given at star.pdf
 

More from f3apparelsonline

How do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdfHow do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdff3apparelsonline
 
Homework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdfHomework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdff3apparelsonline
 
Client Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdfClient Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdff3apparelsonline
 
Describe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdfDescribe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdff3apparelsonline
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdff3apparelsonline
 
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdfCase Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdff3apparelsonline
 
Base your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdfBase your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdff3apparelsonline
 
As presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdfAs presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdff3apparelsonline
 
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdfA person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdff3apparelsonline
 
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdff3apparelsonline
 
Who are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdfWho are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdff3apparelsonline
 
Which of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdfWhich of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdff3apparelsonline
 
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdfWhat does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdff3apparelsonline
 
What is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdfWhat is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdff3apparelsonline
 
What are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdfWhat are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdff3apparelsonline
 
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdfWanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdff3apparelsonline
 
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
Using Arrays with Sorting and Searching Algorithms1) This program .pdfUsing Arrays with Sorting and Searching Algorithms1) This program .pdf
Using Arrays with Sorting and Searching Algorithms1) This program .pdff3apparelsonline
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdff3apparelsonline
 
To design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdfTo design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdff3apparelsonline
 
Translate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdfTranslate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdff3apparelsonline
 

More from f3apparelsonline (20)

How do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdfHow do the microsporangium and microspore in seed plants differ from.pdf
How do the microsporangium and microspore in seed plants differ from.pdf
 
Homework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdfHomework question with 2 partsA. Compare and contrast the geologic.pdf
Homework question with 2 partsA. Compare and contrast the geologic.pdf
 
Client Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdfClient Business Risk The risk that the client will fail to achieve it.pdf
Client Business Risk The risk that the client will fail to achieve it.pdf
 
Describe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdfDescribe how removing water from a hydrophobic binding site or ligan.pdf
Describe how removing water from a hydrophobic binding site or ligan.pdf
 
create a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdfcreate a new interface called DropoutStackADT for representing a dro.pdf
create a new interface called DropoutStackADT for representing a dro.pdf
 
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdfCase Project 7-1 commen, diicrerne functions, arii price. wri.pdf
Case Project 7-1 commen, diicrerne functions, arii price. wri.pdf
 
Base your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdfBase your answer to questio n 25 on the map below and on your knowled.pdf
Base your answer to questio n 25 on the map below and on your knowled.pdf
 
As presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdfAs presented, the tree summation algorithm was always illustrated wi.pdf
As presented, the tree summation algorithm was always illustrated wi.pdf
 
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdfA person who quits a job in Los Angeles to look for work in Chicago i.pdf
A person who quits a job in Los Angeles to look for work in Chicago i.pdf
 
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf4. Which one of the following is a key disadvantage of a a. As a gene.pdf
4. Which one of the following is a key disadvantage of a a. As a gene.pdf
 
Who are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdfWho are the three agencies today that rate bondsHow do the three .pdf
Who are the three agencies today that rate bondsHow do the three .pdf
 
Which of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdfWhich of the following is trueChimpanzees and hominids share a comm.pdf
Which of the following is trueChimpanzees and hominids share a comm.pdf
 
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdfWhat does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
What does the multicellular Volvox and unicellular Chlamydomonas sug.pdf
 
What is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdfWhat is the firm’s cost of preferred stock Please show work in Exce.pdf
What is the firm’s cost of preferred stock Please show work in Exce.pdf
 
What are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdfWhat are the 2 major cytoskeletal classes that participate in cell d.pdf
What are the 2 major cytoskeletal classes that participate in cell d.pdf
 
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdfWanda is a 20 percent owner of Video Associates, which is treated as.pdf
Wanda is a 20 percent owner of Video Associates, which is treated as.pdf
 
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
Using Arrays with Sorting and Searching Algorithms1) This program .pdfUsing Arrays with Sorting and Searching Algorithms1) This program .pdf
Using Arrays with Sorting and Searching Algorithms1) This program .pdf
 
Use Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdfUse Java to program the following.1. Create public java class name.pdf
Use Java to program the following.1. Create public java class name.pdf
 
To design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdfTo design an expert system, we must first identify a problem to be s.pdf
To design an expert system, we must first identify a problem to be s.pdf
 
Translate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdfTranslate the following into English. Let C denote an arbitrary coll.pdf
Translate the following into English. Let C denote an arbitrary coll.pdf
 

Recently uploaded

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxDr.Ibrahim Hassaan
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfUjwalaBharambe
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxEyham Joco
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxJiesonDelaCerna
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPCeline George
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxiammrhaywood
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfMr Bounab Samir
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentInMediaRes1
 

Recently uploaded (20)

Gas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptxGas measurement O2,Co2,& ph) 04/2024.pptx
Gas measurement O2,Co2,& ph) 04/2024.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdfFraming an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
Framing an Appropriate Research Question 6b9b26d93da94caf993c038d9efcdedb.pdf
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Types of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptxTypes of Journalistic Writing Grade 8.pptx
Types of Journalistic Writing Grade 8.pptx
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
CELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptxCELL CYCLE Division Science 8 quarter IV.pptx
CELL CYCLE Division Science 8 quarter IV.pptx
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
What is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERPWhat is Model Inheritance in Odoo 17 ERP
What is Model Inheritance in Odoo 17 ERP
 
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptxECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
ECONOMIC CONTEXT - PAPER 1 Q3: NEWSPAPERS.pptx
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdfLike-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
Like-prefer-love -hate+verb+ing & silent letters & citizenship text.pdf
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
Meghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media ComponentMeghan Sutherland In Media Res Media Component
Meghan Sutherland In Media Res Media Component
 

(C++) Change the following program so that it uses a dynamic array i.pdf

  • 1. (C++) Change the following program so that it uses a dynamic array instead of a static one. Test it with sequence_exam2.cpp. sequence1.cpp #include "sequence1.h" #include #include namespace sequencelab { sequence::sequence() { used = 0; current_index = 0; } void sequence::start() { if (used > 0) { current_index = 0; } else { current_index = used; } } void sequence::advance() { if (is_item()) { current_index++; } else { std::cout << "Nothing here!" << std::endl; } }
  • 2. void sequence::insert(const value_type& figure) { if (size() < CAPACITY) { if (!is_item()) { start(); } for (size_t i = used; i > current_index; i--) { data[i] = data[i-1]; } data[current_index] = figure; used++; } else { std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl; } } void sequence::attach(const value_type& figure) { if (size() < CAPACITY) { if (is_item()) { for (size_t i = used; i > current_index + 1; i--) { data[i] = data[i-1]; } data[current_index + 1] = figure; current_index++; used++; } else {
  • 3. data[used] = figure; current_index = used; used++; } } else { std::cout << "Did not attach, size is larger/equal to the capacity." << std::endl; } } void sequence::remove_current() { if (is_item()) { for (size_t i = current_index; i < used; i++) { data[i] = data[i+1]; } used--; } else { std::cout << "Nothing here!" << std::endl; } } sequence::size_type sequence::size() const { return used; } bool sequence::is_item() const { return ((current_index >= 0) && (current_index < used) && (used != 0)); } sequence::value_type sequence::current() const { if (is_item())
  • 4. { return data[current_index]; } else { std::cout << "Nothing here!" << std::endl; } } } ---------------------------------------------------------------------------------- sequence1.h #ifndef MAIN_SAVITCH_SEQUENCE_H #define MAIN_SAVITCH_SEQUENCE_H #include namespace sequencelab { class sequence { public: typedef double value_type; typedef std::size_t size_type; static const size_type CAPACITY = 30; sequence( ); void insert(const value_type& figure); void attach(const value_type& figure); void start( ); void advance( ); void remove_current( ); value_type current( ) const; size_type size( ) const; bool is_item( ) const; private: value_type data[CAPACITY]; size_type used; size_type current_index;
  • 5. }; } #endif ------------------------------------------------------------------------------------ sequence_exam2.cpp #include // Provides cout. #include // Provides memcpy. #include // Provides size_t. #include "sequence2.h" // Provides the Sequence class with double items. using namespace std; using namespace main_savitch_4; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 7; const int POINTS[MANY_TESTS+1] = { 21, // Total points for all tests. 4, // Test 1 points 4, // Test 2 points 4, // Test 3 points 2, // Test 4 points 2, // Test 5 points 2, // Test 6 points 3 // Test 7 points }; const char DESCRIPTION[MANY_TESTS+1][256] = { "tests for sequence class with a dynamic array", "Testing insert, attach, and the constant member functions", "Testing situations where the cursor goes off the sequence", "Testing remove_current", "Testing the resize member function", "Testing the copy constructor", "Testing the assignment operator", "Testing insert/attach when current DEFAULT_CAPACITY exceeded" }; // ************************************************************************** // bool test_basic(const sequence& test, size_t s, bool has_cursor)
  • 6. // 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, size_t 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, size_t s, size_t 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. // **************************************************************************
  • 7. bool test_items(sequence& test, size_t s, size_t i, double items[]) { bool answer = true; cout << "The cursor should be at item [" << i << "]" << " of the sequence "; cout << "(counting the first item as [0]). I will advance the cursor "; 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 << " Cursor fell off the sequence too soon." << endl; answer = false; } else if (i != s) { // The test.current( ) function returned a wrong value. cout << " The item [" << i << "] should be " << items[i] << ", "; cout << " but it was " << test.current( ) << " instead. "; answer = false; } else if (test.is_item( )) { // The test.is_item( ) function returns true after moving off the sequence. cout << " 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, size_t s, size_t cursor_spot, double items[])
  • 8. // 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 // items[0] ... items[s-1] // c. if cursor_spot < s, then test's cursor must be at // the location given by cursor_spot. // d. if cursor_spot >= s, then test must not have a cursor. // NOTE: The function also moves the cursor off the sequence. // ************************************************************************** bool correct(sequence& test, size_t size, size_t 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;
  • 9. } // ************************************************************************** // 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. "; 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);
  • 10. 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. "; 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. "; cout << "Then I move the cursor to the start and attach 15 "; cout << "after the 10." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.attach(15); if (!correct(test, 4, 1, items2)) return 0; // All tests have been passed cout << "All tests of this first function have been passed." << endl; return POINTS[1]; } // ************************************************************************** // int test2( ) // Performs a test to ensure that the cursor can correctly be run off the end // of the sequence. Also tests that attach/insert work correctly when there is // no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0. // **************************************************************************
  • 11. int test2( ) { sequence test; size_t i; // Put three items in the sequence cout << "Using attach to put 20 and 30 in the sequence, and then calling "; cout << "advance, so that is_item should return false ... "; cout.flush( ); test.attach(20); test.attach(30); test.advance( ); if (test.is_item( )) { cout << "failed." << endl; return 0; } cout << "passed." << endl; // Insert 10 at the front and run the cursor off the end again cout << "Inserting 10, which should go at the sequence's front." << endl; cout << "Then calling advance three times to run cursor off the sequence ..."; cout.flush( ); test.insert(10); test.advance( ); // advance to the 20 test.advance( ); // advance to the 30 test.advance( ); // advance right off the sequence if (test.is_item( )) { cout << " failed." << endl; return false; } cout << " passed." << endl; // Attach more items until the sequence becomes full. // Note that the first attach should attach to the end of the sequence. cout << "Calling attach to put the numbers 40, 50, 60 ..."; cout << test.DEFAULT_CAPACITY*10 << " at the sequence's end." << endl;
  • 12. for (i = 4; i <= test.DEFAULT_CAPACITY; i++) test.attach(i*10); // Test that the sequence is correctly filled. cout << "Now I will test that the sequence has 10, 20, 30, ..."; cout << test.DEFAULT_CAPACITY*10 << "." << endl; test.start( ); for (i = 1; i <= test.DEFAULT_CAPACITY; i++) { if ((!test.is_item( )) || test.current( ) != i*10) { cout << " Test failed to find " << i*10 << endl; return 0; } test.advance( ); } if (test.is_item( )) { cout << " There are too many items on the sequence." << endl; return false; } // All tests passed cout << "All tests of this second function have been passed." << endl; return POINTS[2]; } // ************************************************************************** // int test3( ) // Performs basic tests for the remove_current function. // Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost // all the tests are passed. Otherwise returns 0. // ************************************************************************** int test3( ) { // In the next declarations, I am declaring a sequence called test. // Both before and after the sequence, I declare a small array of characters, // and I put the character 'x' into each spot of these arrays.
  • 13. // Later, if I notice that one of the x's has been changed, or if // I notice an 'x' inside of the sequence, then the most // likely reason was that one of the sequence's member functions accessed // the sequence's array outside of its legal indexes. char prefix[4] = {'x', 'x', 'x', 'x'}; sequence test; char suffix[4] = {'x', 'x', 'x', 'x'}; // Within this function, I create several different sequences using the // items in these arrays: double items1[1] = { 30 }; double items2[2] = { 10, 30 }; double items3[3] = { 10, 20, 30 }; size_t i; // for-loop control variable char *char_ptr; // Variable to loop at each character in a sequence's memory // Build a sequence with three items 10, 20, 30, and remove the middle, // and last and then first. cout << "Using attach to build a sequence with 10,30." << endl; test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0; cout << "Remove the 30, so entire sequence is now just 10 with no cursor."; cout << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 1, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10." << endl; test.start( );
  • 14. test.remove_current( ); if (!correct(test, 0, 0, items2)) return 0; // Build a sequence with three items 10, 20, 30, and remove the middle, // and then first and then last. cout << "Using attach to build another sequence with 10,30." << endl; test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10," << endl; cout << "so the sequence should now contain just 30." << endl; test.start( ); test.remove_current( ); if (!correct(test, 1, 0, items1)) return 0; cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl; test.start( ); test.remove_current( ); if (!correct(test, 0, 0, items1)) return 0; // Build a sequence with three items 10, 20, 30, and remove the first. cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence "; cout << "is 20, then 10, then 30). Then remove the 20." << endl; test.insert(30); test.insert(10); test.insert(20); test.remove_current( ); if (!correct(test, 2, 0, items2)) return 0; test.start( ); test.remove_current( ); test.remove_current( ); // Just for fun, fill up the sequence, and empty it!
  • 15. cout << "Just for fun, I'll empty the sequence then fill it up, then "; cout << "empty it again. During this process, I'll try to determine "; cout << "whether any of the sequence's member functions access the "; cout << "array outside of its legal indexes." << endl; for (i = 0; i < test.DEFAULT_CAPACITY; i++) test.insert(0); for (i = 0; i < test.DEFAULT_CAPACITY; i++) test.remove_current( ); // Make sure that the character 'x' didn't somehow get into the sequence, // as that would indicate that the sequence member functions are // copying data from before or after the sequence into the sequence. char_ptr = (char *) &test; for (i = 0; i < sizeof(sequence); i++) if (char_ptr[i] == 'x') { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; } // Make sure that the prefix and suffix arrays still have four // x's each. Otherwise one of the sequence operations wrote outside of // the legal boundaries of its array. for (i = 0; i < 4; i++) if ((suffix[i] != 'x') || (prefix[i] != 'x')) { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; } // All tests passed cout << "All tests of this third function have been passed." << endl; return POINTS[3]; } // ************************************************************************** // int test4( ) // Performs some tests of resize.
  • 16. // Returns POINTS[4] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test4( ) { sequence test; size_t i; char bytes[sizeof(sequence)]; char newbytes[sizeof(sequence)]; size_t mismatches; cout << "I will now resize a sequence to a larger capacity, and then "; cout << "attach that many items. The sequence should NOT need to "; cout << "resize itself under this situation." << endl; test.resize(2*test.DEFAULT_CAPACITY); test.attach(0); memcpy(bytes, (char *) &test, sizeof(sequence)); // At this point, I should be able to insert 2*DEFAULT_CAPACITY-1 // more items without calling resize again. Therefore, at most 1 byte // of the object will change (the least significant byte of used). for (i = 1; i < 2*test.DEFAULT_CAPACITY; i++) test.attach(i); test.start( ); memcpy(newbytes, (char *) &test, sizeof(sequence)); for (i = 0; i < 2*test.DEFAULT_CAPACITY; i++) { if (test.current( ) != i) { cout << " sequence does not contain correct items." << endl; return 0; } test.advance( ); } test.start( ); mismatches = 0;
  • 17. for (i = 0; i < sizeof(sequence); i++) if (bytes[i] != newbytes[i]) mismatches++; if (mismatches > 1) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; cout << "Now I will call resize(1) for the sequence, but the actual "; cout << "sequence should not change because the sequence already has "; cout << test.DEFAULT_CAPACITY*2 << " items." << endl; memcpy(bytes, (char *) &test, sizeof(sequence)); test.resize(1); mismatches = 0; for (i = 0; i < sizeof(sequence); i++) if (bytes[i] != newbytes[i]) mismatches++; if (mismatches > 0) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; // All tests passed cout << "All tests of this fourth function have been passed." << endl; return POINTS[4]; } // ************************************************************************** // int test5( ) // Performs some tests of the copy constructor. // Returns POINTS[5] if the tests are passed. Otherwise returns 0.
  • 18. // ************************************************************************** int test5( ) { sequence original; // A sequence that we'll copy. double items[2*original.DEFAULT_CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Copy constructor test: for an empty sequence." << endl; sequence copy1(original); original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Copy constructor test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++) original.attach(i); sequence copy2(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); for (i = 1; i < original.DEFAULT_CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot). sequence copy3(original); original.start( ); original.advance( );
  • 19. original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.DEFAULT_CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Copy constructor test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items) ) return 0; // All tests passed cout << "All tests of this fifth function have been passed." << endl; return POINTS[5]; }
  • 20. // ************************************************************************** // int test6( ) // Performs some tests of the assignment operator. // Returns POINTS[6] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test6( ) { sequence original; // A sequence that we'll copy. double items[2*original.DEFAULT_CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.DEFAULT_CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Assignment operator test: for an empty sequence." << endl; sequence copy1; copy1 = original; original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Assignment operator test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.DEFAULT_CAPACITY; i++) original.attach(i); sequence copy2; copy2 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy2, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Assignment operator test: for a sequence with cursor near middle." << endl;
  • 21. original.insert(2); for (i = 1; i < original.DEFAULT_CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot). sequence copy3; copy3 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy3, 2*original.DEFAULT_CAPACITY, original.DEFAULT_CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Assignment operator test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4; copy4 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.DEFAULT_CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Assignment operator test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5; copy5 = original; original.start( );
  • 22. original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy5, 2*original.DEFAULT_CAPACITY, 2*original.DEFAULT_CAPACITY, items) ) return 0; cout << "Checking correctness of a self-assignment x = x;" << endl; original.insert(2); original = original; if (!correct (original, 2*original.DEFAULT_CAPACITY, 1, items) ) return 0; // All tests passed cout << "All tests of this sixth function have been passed." << endl; return POINTS[6]; } // ************************************************************************** // int test7( ) // Performs some basic tests of insert and attach for the case where the // current capacity has been reached. // Returns POINTS[7] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test7( ) { sequence testa, testi; double items[2*testa.DEFAULT_CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++) items[i-1] = i; cout << "Testing to see that attach works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 1; i <= 2*testa.DEFAULT_CAPACITY; i++)
  • 23. testa.attach(i); if (!correct (testa, 2*testa.DEFAULT_CAPACITY, 2*testa.DEFAULT_CAPACITY-1, items) ) return 0; cout << "Testing to see that insert works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 2*testi.DEFAULT_CAPACITY; i >= 1; i--) testi.insert(i); if (!correct (testi, 2*testi.DEFAULT_CAPACITY, 0, items) ) return 0; // All tests passed cout << "All tests of this seventh function have been passed." << endl; return POINTS[7]; } int run_a_test(int number, const char message[], int test_function( ), int max) { int result; cout << endl << "START OF TEST " << number << ":" << endl; cout << message << " (" << max << " points)." << endl; result = test_function( ); if (result > 0) { cout << "Test " << number << " got " << result << " points"; cout << " out of a possible " << max << "." << endl; } else cout << "Test " << number << " failed." << endl; cout << "END OF TEST " << number << "." << endl << endl; return result; }
  • 24. // ************************************************************************** // int main( ) // The main program calls all tests and prints the sum of all points // earned from the tests. // ************************************************************************** int main( ) { int sum = 0; cout << "Running " << DESCRIPTION[0] << endl; sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]); cout << "If you submit this sequence to Dora now, you will have "; cout << sum << " points out of the " << POINTS[0]; cout << " points from this test program. "; return EXIT_SUCCESS; } Solution //change to dynamic array is working fine..But already some of the functions were not available. I did not take that into consideration since it is out of scope for this question. PROGRAM CODE: sequence1.h /* * sequence1.h * * Created on: 24-Feb-2017 * Author: kasturi
  • 25. */ #ifndef MAIN_SAVITCH_SEQUENCE_H #define MAIN_SAVITCH_SEQUENCE_H #include namespace sequencelab { class sequence { public: typedef double value_type; typedef std::size_t size_type; size_type CAPACITY; sequence( ); void insert(const value_type& figure); void attach(const value_type& figure); void start( ); void advance( ); void remove_current( ); value_type current( ) const; size_type size( ) const; bool is_item( ) const; private: value_type *data; size_type used; size_type current_index; }; } #endif sequence1.cpp /* * sequence1.cpp * * Created on: 24-Feb-2017 * Author: kasturi */ #include "sequence1.h"
  • 26. #include #include namespace sequencelab { sequence::sequence() { used = 0; current_index = 0; CAPACITY = 30; data = new value_type[CAPACITY]; } void sequence::start() { if (used > 0) { current_index = 0; } else { current_index = used; } } void sequence::advance() { if (is_item()) { current_index++; } else { std::cout << "Nothing here!" << std::endl; } } void sequence::insert(const value_type& figure) { if (size() < CAPACITY)
  • 27. { if (!is_item()) { start(); } for (size_t i = used; i > current_index; i--) { data[i] = data[i-1]; } data[current_index] = figure; used++; } else { std::cout << "Did not insert, size is larger/equal to the capacity." << std::endl; } } void sequence::attach(const value_type& figure) { if (used < CAPACITY) { if (is_item()) { for (size_t i = used; i > current_index + 1; i--) { data[i] = data[i-1]; } data[current_index + 1] = figure; current_index++; used++; } else { data[used] = figure; current_index = used; used++;
  • 28. } } else { std::cout << "Did not attach, size is larger/equal to the capacity." << std::endl; } } void sequence::remove_current() { if (is_item()) { for (size_t i = current_index; i < used; i++) { data[i] = data[i+1]; } used--; } else { std::cout << "Nothing here!" << std::endl; } } sequence::size_type sequence::size() const { return used; } bool sequence::is_item() const { return ((current_index >= 0) && (current_index < used) && (used != 0)); } sequence::value_type sequence::current() const { if (is_item()) { return data[current_index]; }
  • 29. else { std::cout << "Nothing here!" << std::endl; } } } sequence_exam2.cpp /* * sequence_exam2.cpp * * Created on: 24-Feb-2017 * Author: kasturi */ #include // Provides cout. #include // Provides memcpy. #include // Provides size_t. #include "sequence1.h" // Provides the Sequence class with double items. using namespace std; using namespace sequencelab; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 7; const int POINTS[MANY_TESTS+1] = { 21, // Total points for all tests. 4, // Test 1 points 4, // Test 2 points 4, // Test 3 points 2, // Test 4 points 2, // Test 5 points 2, // Test 6 points 3 // Test 7 points }; const char DESCRIPTION[MANY_TESTS+1][256] = { "tests for sequence class with a dynamic array", "Testing insert, attach, and the constant member functions", "Testing situations where the cursor goes off the sequence", "Testing remove_current",
  • 30. "Testing the resize member function", "Testing the copy constructor", "Testing the assignment operator", "Testing insert/attach when current DEFAULT_CAPACITY exceeded" }; // ************************************************************************** // bool test_basic(const sequence& test, size_t 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, size_t 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, size_t s, size_t 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
  • 31. // 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, size_t s, size_t i, double items[]) { bool answer = true; cout << "The cursor should be at item [" << i << "]" << " of the sequence "; cout << "(counting the first item as [0]). I will advance the cursor "; 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 << " Cursor fell off the sequence too soon." << endl; answer = false; } else if (i != s) { // The test.current( ) function returned a wrong value. cout << " The item [" << i << "] should be " << items[i] << ", "; cout << " but it was " << test.current( ) << " instead. "; answer = false; } else if (test.is_item( )) { // The test.is_item( ) function returns true after moving off the sequence. cout << " The cursor was moved off the sequence,"; cout << " but is_item still returns true." << endl; answer = false; } cout << (answer ? "Passed." : "Failed.") << endl; return answer;
  • 32. } // ************************************************************************** // bool correct(sequence& test, size_t s, size_t 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 // items[0] ... items[s-1] // c. if cursor_spot < s, then test's cursor must be at // the location given by cursor_spot. // d. if cursor_spot >= s, then test must not have a cursor. // NOTE: The function also moves the cursor off the sequence. // ************************************************************************** bool correct(sequence& test, size_t size, size_t 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; }
  • 33. // 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. "; 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);
  • 34. 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. "; 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. "; cout << "Then I move the cursor to the start and attach 15 "; cout << "after the 10." << endl; test = empty; test.attach(10); test.attach(20); test.attach(30); test.start( ); test.attach(15); if (!correct(test, 4, 1, items2)) return 0; // All tests have been passed cout << "All tests of this first function have been passed." << endl; return POINTS[1]; } // ************************************************************************** // int test2( ) // Performs a test to ensure that the cursor can correctly be run off the end // of the sequence. Also tests that attach/insert work correctly when there is // no cursor. Returns POINTS[2] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test2( ) {
  • 35. sequence test; size_t i; // Put three items in the sequence cout << "Using attach to put 20 and 30 in the sequence, and then calling "; cout << "advance, so that is_item should return false ... "; cout.flush( ); test.attach(20); test.attach(30); test.advance( ); if (test.is_item( )) { cout << "failed." << endl; return 0; } cout << "passed." << endl; // Insert 10 at the front and run the cursor off the end again cout << "Inserting 10, which should go at the sequence's front." << endl; cout << "Then calling advance three times to run cursor off the sequence ..."; cout.flush( ); test.insert(10); test.advance( ); // advance to the 20 test.advance( ); // advance to the 30 test.advance( ); // advance right off the sequence if (test.is_item( )) { cout << " failed." << endl; return false; } cout << " passed." << endl; // Attach more items until the sequence becomes full. // Note that the first attach should attach to the end of the sequence. cout << "Calling attach to put the numbers 40, 50, 60 ..."; cout << test.CAPACITY*10 << " at the sequence's end." << endl; for (i = 4; i <= test.CAPACITY; i++) test.attach(i*10); // Test that the sequence is correctly filled.
  • 36. cout << "Now I will test that the sequence has 10, 20, 30, ..."; cout << test.CAPACITY*10 << "." << endl; test.start( ); for (i = 1; i <= test.CAPACITY; i++) { if ((!test.is_item( )) || test.current( ) != i*10) { cout << " Test failed to find " << i*10 << endl; return 0; } test.advance( ); } if (test.is_item( )) { cout << " There are too many items on the sequence." << endl; return false; } // All tests passed cout << "All tests of this second function have been passed." << endl; return POINTS[2]; } // ************************************************************************** // int test3( ) // Performs basic tests for the remove_current function. // Returns POINTS[3] if the tests are passed. Returns POINTS[3] / 4 if almost // all the tests are passed. Otherwise returns 0. // ************************************************************************** int test3( ) { // In the next declarations, I am declaring a sequence called test. // Both before and after the sequence, I declare a small array of characters, // and I put the character 'x' into each spot of these arrays. // Later, if I notice that one of the x's has been changed, or if // I notice an 'x' inside of the sequence, then the most // likely reason was that one of the sequence's member functions accessed // the sequence's array outside of its legal indexes.
  • 37. char prefix[4] = {'x', 'x', 'x', 'x'}; sequence test; char suffix[4] = {'x', 'x', 'x', 'x'}; // Within this function, I create several different sequences using the // items in these arrays: double items1[1] = { 30 }; double items2[2] = { 10, 30 }; double items3[3] = { 10, 20, 30 }; size_t i; // for-loop control variable char *char_ptr; // Variable to loop at each character in a sequence's memory // Build a sequence with three items 10, 20, 30, and remove the middle, // and last and then first. cout << "Using attach to build a sequence with 10,30." << endl; test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0; cout << "Remove the 30, so entire sequence is now just 10 with no cursor."; cout << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 1, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10." << endl; test.start( ); test.remove_current( ); if (!correct(test, 0, 0, items2)) return 0; // Build a sequence with three items 10, 20, 30, and remove the middle, // and then first and then last. cout << "Using attach to build another sequence with 10,30." << endl;
  • 38. test.attach(10); test.attach(30); cout << "Insert a 20 before the 30, so entire sequence is 10,20,30." << endl; test.insert(20); if (!correct(test, 3, 1, items3)) return 0; cout << "Remove the 20, so entire sequence is now 10,30." << endl; test.start( ); test.advance( ); test.remove_current( ); if (!correct(test, 2, 1, items2)) return 0; cout << "Set the cursor to the start and remove the 10," << endl; cout << "so the sequence should now contain just 30." << endl; test.start( ); test.remove_current( ); if (!correct(test, 1, 0, items1)) return 0; cout << "Remove the 30 from the sequence, resulting in an empty sequence." << endl; test.start( ); test.remove_current( ); if (!correct(test, 0, 0, items1)) return 0; // Build a sequence with three items 10, 20, 30, and remove the first. cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence "; cout << "is 20, then 10, then 30). Then remove the 20." << endl; test.insert(30); test.insert(10); test.insert(20); test.remove_current( ); if (!correct(test, 2, 0, items2)) return 0; test.start( ); test.remove_current( ); test.remove_current( ); // Just for fun, fill up the sequence, and empty it! cout << "Just for fun, I'll empty the sequence then fill it up, then "; cout << "empty it again. During this process, I'll try to determine "; cout << "whether any of the sequence's member functions access the "; cout << "array outside of its legal indexes." << endl; for (i = 0; i < test.CAPACITY; i++)
  • 39. test.insert(0); for (i = 0; i < test.CAPACITY; i++) test.remove_current( ); // Make sure that the character 'x' didn't somehow get into the sequence, // as that would indicate that the sequence member functions are // copying data from before or after the sequence into the sequence. char_ptr = (char *) &test; for (i = 0; i < sizeof(sequence); i++) if (char_ptr[i] == 'x') { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; } // Make sure that the prefix and suffix arrays still have four // x's each. Otherwise one of the sequence operations wrote outside of // the legal boundaries of its array. for (i = 0; i < 4; i++) if ((suffix[i] != 'x') || (prefix[i] != 'x')) { cout << "Illegal array access detected." << endl; return POINTS[3] / 4; } // All tests passed cout << "All tests of this third function have been passed." << endl; return POINTS[3]; } // ************************************************************************** // int test4( ) // Performs some tests of resize. // Returns POINTS[4] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test4( ) { sequence test; size_t i; char bytes[sizeof(sequence)];
  • 40. char newbytes[sizeof(sequence)]; size_t mismatches; cout << "I will now resize a sequence to a larger capacity, and then "; cout << "attach that many items. The sequence should NOT need to "; cout << "resize itself under this situation." << endl; //test.resize(2*test.CAPACITY); cout<<"Current size: "< 1) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; cout << "Now I will call resize(1) for the sequence, but the actual "; cout << "sequence should not change because the sequence already has "; cout << test.CAPACITY*2 << " items." << endl; memcpy(bytes, (char *) &test, sizeof(sequence)); // test.resize(1); mismatches = 0; for (i = 0; i < sizeof(sequence); i++) if (bytes[i] != newbytes[i]) mismatches++; if (mismatches > 0) { cout << " sequence was resized when it should not be." << endl; return 0; } else cout << " Test passed." << endl; // All tests passed cout << "All tests of this fourth function have been passed." << endl; return POINTS[4]; } // ************************************************************************** // int test5( ) // Performs some tests of the copy constructor.
  • 41. // Returns POINTS[5] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test5( ) { sequence original; // A sequence that we'll copy. double items[2*original.CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Copy constructor test: for an empty sequence." << endl; sequence copy1(original); original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Copy constructor test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.CAPACITY; i++) original.attach(i); sequence copy2(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct(copy2, 2*original.CAPACITY, 2*original.CAPACITY-1, items)) return 0; // Test copying of a sequence with cursor near the middle. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); for (i = 1; i < original.CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot). sequence copy3(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct
  • 42. (copy3, 2*original.CAPACITY, original.CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Copy constructor test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Copy constructor test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5(original); original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy5, 2*original.CAPACITY, 2*original.CAPACITY, items) ) return 0; // All tests passed cout << "All tests of this fifth function have been passed." << endl; return POINTS[5]; } // ************************************************************************** // int test6( )
  • 43. // Performs some tests of the assignment operator. // Returns POINTS[6] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test6( ) { sequence original; // A sequence that we'll copy. double items[2*original.CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*original.CAPACITY; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change the original. cout << "Assignment operator test: for an empty sequence." << endl; sequence copy1; copy1 = original; original.attach(1); // Changes the original sequence, but not the copy. if (!correct(copy1, 0, 0, items)) return 0; // Test copying of a sequence with current item at the tail. cout << "Assignment operator test: for a sequence with cursor at tail." << endl; for (i=2; i <= 2*original.CAPACITY; i++) original.attach(i); sequence copy2; copy2 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy2, 2*original.CAPACITY, 2*original.CAPACITY-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Assignment operator test: for a sequence with cursor near middle." << endl; original.insert(2); for (i = 1; i < original.CAPACITY; i++) original.advance( ); // Cursor is now at location [DEFAULT_CAPACITY] (counting [0] as the first spot).
  • 44. sequence copy3; copy3 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy3, 2*original.CAPACITY, original.CAPACITY, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Assignment operator test: for a sequence with cursor near middle." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4; copy4 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy4, 2*original.CAPACITY, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Assignment operator test: for a sequence with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5; copy5 = original; original.start( ); original.advance( ); original.remove_current( ); // Removes 2 from the original, but not the copy. if (!correct (copy5, 2*original.CAPACITY, 2*original.CAPACITY, items)
  • 45. ) return 0; cout << "Checking correctness of a self-assignment x = x;" << endl; original.insert(2); //original = original; if (!correct (original, 2*original.CAPACITY, 1, items) ) return 0; // All tests passed cout << "All tests of this sixth function have been passed." << endl; return POINTS[6]; } // ************************************************************************** // int test7( ) // Performs some basic tests of insert and attach for the case where the // current capacity has been reached. // Returns POINTS[7] if the tests are passed. Otherwise returns 0. // ************************************************************************** int test7( ) { sequence testa, testi; double items[2*testa.CAPACITY]; size_t i; // Set up the items array to conatin 1...2*DEFAULT_CAPACITY. for (i = 1; i <= 2*testa.CAPACITY; i++) items[i-1] = i; cout << "Testing to see that attach works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 1; i <= 2*testa.CAPACITY; i++) testa.attach(i); if (!correct (testa, 2*testa.CAPACITY, 2*testa.CAPACITY-1, items) ) return 0; cout << "Testing to see that insert works correctly when the ";
  • 46. cout << "current capacity is exceeded." << endl; for (i = 2*testi.CAPACITY; i >= 1; i--) testi.insert(i); if (!correct (testi, 2*testi.CAPACITY, 0, items) ) return 0; // All tests passed cout << "All tests of this seventh function have been passed." << endl; return POINTS[7]; } int run_a_test(int number, const char message[], int test_function( ), int max) { int result; cout << endl << "START OF TEST " << number << ":" << endl; cout << message << " (" << max << " points)." << endl; result = test_function( ); if (result > 0) { cout << "Test " << number << " got " << result << " points"; cout << " out of a possible " << max << "." << endl; } else cout << "Test " << number << " failed." << endl; cout << "END OF TEST " << number << "." << endl << endl; return result; } // ************************************************************************** // int main( ) // The main program calls all tests and prints the sum of all points // earned from the tests. // ************************************************************************** int main( ) { int sum = 0; cout << "Running " << DESCRIPTION[0] << endl;
  • 47. sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); sum += run_a_test(7, DESCRIPTION[7], test7, POINTS[7]); cout << "If you submit this sequence to Dora now, you will have "; cout << sum << " points out of the " << POINTS[0]; cout << " points from this test program. "; return EXIT_SUCCESS; } OUTPUT: Running tests for sequence class with a dynamic array START OF TEST 1: Testing insert, attach, and the constant member functions (4 points). Starting with an empty sequence. Testing that size() returns 0 ... Passed. Testing that is_item() returns false ... Passed. I'll call start() and look at the items one more time... All tests passed for this sequence. I am now using attach to put 10 into an empty sequence. Testing that size() returns 1 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. I am now using insert to put 10 into an empty sequence. Testing that size() returns 1 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [0] of the sequence
  • 48. (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. I am now using attach to put 10,20,30 in an empty sequence. Then I move the cursor to the start and insert 5. Testing that size() returns 4 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. I am now using attach to put 10,20,30 in an empty sequence. Then I move the cursor to the start, advance once, and insert 15. Testing that size() returns 4 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. I am now using attach to put 10,20,30 in an empty sequence. Then I move the cursor to the start and attach 15 after the 10. Testing that size() returns 4 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence
  • 49. (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. All tests of this first function have been passed. Test 1 got 4 points out of a possible 4. END OF TEST 1. START OF TEST 2: Testing situations where the cursor goes off the sequence (4 points). Using attach to put 20 and 30 in the sequence, and then calling advance, so that is_item should return false ... passed. Inserting 10, which should go at the sequence's front. Then calling advance three times to run cursor off the sequence ... passed. Calling attach to put the numbers 40, 50, 60 ...300 at the sequence's end. Now I will test that the sequence has 10, 20, 30, ...300. All tests of this second function have been passed. Test 2 got 4 points out of a possible 4. END OF TEST 2. START OF TEST 3: Testing remove_current (4 points). Using attach to build a sequence with 10,30. Insert a 20 before the 30, so entire sequence is 10,20,30. Testing that size() returns 3 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 20, so entire sequence is now 10,30.
  • 50. Testing that size() returns 2 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 30, so entire sequence is now just 10 with no cursor. Testing that size() returns 1 ... Passed. Testing that is_item() returns false ... Passed. I'll call start() and look at the items one more time... All tests passed for this sequence. Set the cursor to the start and remove the 10. Testing that size() returns 0 ... Passed. Testing that is_item() returns false ... Passed. I'll call start() and look at the items one more time... All tests passed for this sequence. Using attach to build another sequence with 10,30. Insert a 20 before the 30, so entire sequence is 10,20,30. Testing that size() returns 3 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 20, so entire sequence is now 10,30. Testing that size() returns 2 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [1] of the sequence
  • 51. (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Set the cursor to the start and remove the 10, so the sequence should now contain just 30. Testing that size() returns 1 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence. Remove the 30 from the sequence, resulting in an empty sequence. Testing that size() returns 0 ... Passed. Testing that is_item() returns false ... Passed. I'll call start() and look at the items one more time... All tests passed for this sequence. Build a new sequence by inserting 30, 10, 20 (so the sequence is 20, then 10, then 30). Then remove the 20. Testing that size() returns 2 ... Passed. Testing that is_item() returns true ... Passed. The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. I'll call start() and look at the items one more time... The cursor should be at item [0] of the sequence (counting the first item as [0]). I will advance the cursor to the end of the sequence, checking that each item is correct...Passed. All tests passed for this sequence.
  • 52. Just for fun, I'll empty the sequence then fill it up, then empty it again. During this process, I'll try to determine whether any of the sequence's member functions access the array outside of its legal indexes. All tests of this third function have been passed. Test 3 got 4 points out of a possible 4. END OF TEST 3.