SlideShare a Scribd company logo
1 of 42
Download to read offline
#include // Provides cout.
#include // Provides size_t.
#include "sequence3.h" // Provides the sequence class with double items.
using namespace std;
using namespace main_savitch_5;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 6;
const int POINTS[MANY_TESTS+1] = {
18, // 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
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for sequence Class with a linked sequence",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current",
"Testing the copy constructor",
"Testing the assignment operator",
"Testing insert/attach for somewhat larger sequences"
};
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[])
{
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 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( )
{
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( )
{
const size_t TESTSIZE = 30;
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 << TESTSIZE*10 << " at the sequence's end." << endl;
for (i = 4; i <= TESTSIZE; 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 << TESTSIZE*10 << "." << endl;
test.start( );
for (i = 1; i <= TESTSIZE; 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( )
{
const size_t TESTSIZE = 30;
sequence test;
// 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
// 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 < TESTSIZE; i++)
test.insert(0);
for (i = 0; i < TESTSIZE; i++)
test.remove_current( );
// All tests passed
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
// **************************************************************************
int test4( )
{
const size_t TESTSIZE = 30;
sequence original; // A sequence that we'll copy.
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change 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*TESTSIZE; i++)
original.attach(i);
sequence copy2(original);
original.remove_current( ); // Delete tail from original, but not the copy
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).
sequence copy3(original);
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor at front." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4(original);
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy4, 2*TESTSIZE-1, 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( ); // Delete 2 from original, but not the copy.
if (!correct
(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
)
return 0;
// All tests passed
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
int test5( )
{
const size_t TESTSIZE = 30;
sequence original; // A sequence that we'll copy.
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change 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: cursor at tail." << endl;
for (i=2; i <= 2*TESTSIZE; i++)
original.attach(i);
sequence copy2;
copy2 = original;
original.remove_current( ); // Delete tail from original, but not the copy
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Assignment operator test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).
sequence copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Assignment operator test: with cursor at front." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy4, 2*TESTSIZE-1, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Assignment operator test: 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( ); // Deletes 2 from the original, but not copy.
if (!correct
(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
)
return 0;
cout << "Checking correctness of a self-assignment x = x;" << endl;
original.insert(2);
original = original;
if (!correct
(original, 2*TESTSIZE-1, 1, items)
)
return 0;
// All tests passed
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
int test6( )
{
const size_t TESTSIZE = 30;
sequence testa, testi;
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; 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*TESTSIZE; i++)
testa.attach(i);
if (!correct
(testa, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
cout << "Testing to see that insert works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 2*TESTSIZE; i >= 1; i--)
testi.insert(i);
if (!correct
(testi, 2*TESTSIZE, 0, items)
)
return 0;
// All tests passed
cout << "All tests of this sixth function have been passed." << endl;
return POINTS[6];
}
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( )
{
int sum = 0;
cout << "Running " << DESCRIPTION[0] << endl;
sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); cout << sum << endl;
sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); cout << sum << endl;
sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); cout << sum << endl;
sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); cout << sum << endl;
sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); cout << sum << endl;
sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); cout << sum << endl;
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;
}
sequence3.h
#ifndef MAIN_SAVITCH_SEQUENCE3_H
#define MAIN_SAVITCH_SEQUENCE3_H
#include // Provides size_t
#include "node1.h" // Provides node class
namespace main_savitch_5
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
// CONSTRUCTORS and DESTRUCTOR
sequence();
sequence(const sequence& source);
~sequence();
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void operator =(const sequence& source);
void remove_current( );
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return many_nodes; }
bool is_item( ) const { return (cursor != NULL); }
value_type current( ) const;
private:
node *head_ptr;
node *tail_ptr;
node *cursor;
node *precursor;
size_type many_nodes;
};
}
#endif
node1.cpp
#include "node1.h"
#include // Provides assert
#include // Provides NULL and size_t
using namespace std;
namespace main_savitch_5
{
size_t list_length(const node* head_ptr)
// Library facilities used: cstdlib
{
const node *cursor;
size_t answer;
answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
++answer;
return answer;
}
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
head_ptr = new node(entry, head_ptr);
}
void list_insert(node* previous_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, previous_ptr->link( ));
previous_ptr->set_link(insert_ptr);
}
node* list_search(node* head_ptr, const node::value_type& target)
// Library facilities used: cstdlib
{
node *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
if (target == cursor->data( ))
return cursor;
return NULL;
}
const node* list_search(const node* head_ptr, const node::value_type& target)
// Library facilities used: cstdlib
{
const node *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
if (target == cursor->data( ))
return cursor;
return NULL;
}
node* list_locate(node* head_ptr, size_t position)
// Library facilities used: cassert, cstdlib
{
node *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link( );
return cursor;
}
const node* list_locate(const node* head_ptr, size_t position)
// Library facilities used: cassert, cstdlib
{
const node *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link( );
return cursor;
}
void list_head_remove(node*& head_ptr)
{
node *remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link( );
delete remove_ptr;
}
void list_remove(node* previous_ptr)
{
node *remove_ptr;
remove_ptr = previous_ptr->link( );
previous_ptr->set_link( remove_ptr->link( ) );
delete remove_ptr;
}
void list_clear(node*& head_ptr)
// Library facilities used: cstdlib
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
// Library facilities used: cstdlib
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list.
if (source_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it.
list_head_insert(head_ptr, source_ptr->data( ));
tail_ptr = head_ptr;
// Copy the rest of the nodes one at a time, adding at the tail of new list.
source_ptr = source_ptr->link( );
while (source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->data( ));
tail_ptr = tail_ptr->link( );
source_ptr = source_ptr->link( );
}
}
}
node1.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include // Provides size_t and NULL
namespace main_savitch_5
{
class node
{
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR
node(
const value_type& init_data = value_type( ),
node* init_link = NULL
)
{ data_field = init_data; link_field = init_link; }
// Member functions to set the data and link fields:
void set_data(const value_type& new_data) { data_field = new_data; }
void set_link(node* new_link) { link_field = new_link; }
// Constant member function to retrieve the current data:
value_type data( ) const { return data_field; }
// Two slightly different member functions to retreive
// the current link:
const node* link( ) const { return link_field; }
node* link( ) { return link_field; }
private:
value_type data_field;
node* link_field;
};
// FUNCTIONS for the linked list toolkit
std::size_t list_length(const node* head_ptr);
void list_head_insert(node*& head_ptr, const node::value_type& entry);
void list_insert(node* previous_ptr, const node::value_type& entry);
node* list_search(node* head_ptr, const node::value_type& target);
const node* list_search
(const node* head_ptr, const node::value_type& target);
node* list_locate(node* head_ptr, std::size_t position);
const node* list_locate(const node* head_ptr, std::size_t position);
void list_head_remove(node*& head_ptr);
void list_remove(node* previous_ptr);
void list_clear(node*& head_ptr);
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);
}
#endif
Solution
#include // Provides cout.
#include // Provides size_t.
#include "sequence3.h" // Provides the sequence class with double items.
using namespace std;
using namespace main_savitch_5;
// Descriptions and points for each of the tests:
const size_t MANY_TESTS = 6;
const int POINTS[MANY_TESTS+1] = {
18, // 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
};
const char DESCRIPTION[MANY_TESTS+1][256] = {
"tests for sequence Class with a linked sequence",
"Testing insert, attach, and the constant member functions",
"Testing situations where the cursor goes off the sequence",
"Testing remove_current",
"Testing the copy constructor",
"Testing the assignment operator",
"Testing insert/attach for somewhat larger sequences"
};
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[])
{
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 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( )
{
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( )
{
const size_t TESTSIZE = 30;
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 << TESTSIZE*10 << " at the sequence's end." << endl;
for (i = 4; i <= TESTSIZE; 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 << TESTSIZE*10 << "." << endl;
test.start( );
for (i = 1; i <= TESTSIZE; 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( )
{
const size_t TESTSIZE = 30;
sequence test;
// 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
// 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 < TESTSIZE; i++)
test.insert(0);
for (i = 0; i < TESTSIZE; i++)
test.remove_current( );
// All tests passed
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
// **************************************************************************
int test4( )
{
const size_t TESTSIZE = 30;
sequence original; // A sequence that we'll copy.
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change 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*TESTSIZE; i++)
original.attach(i);
sequence copy2(original);
original.remove_current( ); // Delete tail from original, but not the copy
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Copy constructor test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).
sequence copy3(original);
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Copy constructor test: for a sequence with cursor at front." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4(original);
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy4, 2*TESTSIZE-1, 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( ); // Delete 2 from original, but not the copy.
if (!correct
(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
)
return 0;
// All tests passed
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
int test5( )
{
const size_t TESTSIZE = 30;
sequence original; // A sequence that we'll copy.
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
// Test copying of an empty sequence. After the copying, we change 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: cursor at tail." << endl;
for (i=2; i <= 2*TESTSIZE; i++)
original.attach(i);
sequence copy2;
copy2 = original;
original.remove_current( ); // Delete tail from original, but not the copy
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
// Test copying of a sequence with cursor near the middle.
cout << "Assignment operator test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
// Cursor is now at location [TESTSIZE] (counting [0] as the first spot).
sequence copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
// Test copying of a sequence with cursor at the front.
cout << "Assignment operator test: with cursor at front." << endl;
original.insert(2);
original.start( );
// Cursor is now at the front.
sequence copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( ); // Delete 2 from original, but not the copy.
if (!correct
(copy4, 2*TESTSIZE-1, 0, items)
)
return 0;
// Test copying of a sequence with no current item.
cout << "Assignment operator test: 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( ); // Deletes 2 from the original, but not copy.
if (!correct
(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
)
return 0;
cout << "Checking correctness of a self-assignment x = x;" << endl;
original.insert(2);
original = original;
if (!correct
(original, 2*TESTSIZE-1, 1, items)
)
return 0;
// All tests passed
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
int test6( )
{
const size_t TESTSIZE = 30;
sequence testa, testi;
double items[2*TESTSIZE];
size_t i;
// Set up the items array to conatin 1...2*TESTSIZE.
for (i = 1; i <= 2*TESTSIZE; 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*TESTSIZE; i++)
testa.attach(i);
if (!correct
(testa, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
cout << "Testing to see that insert works correctly when the ";
cout << "current capacity is exceeded." << endl;
for (i = 2*TESTSIZE; i >= 1; i--)
testi.insert(i);
if (!correct
(testi, 2*TESTSIZE, 0, items)
)
return 0;
// All tests passed
cout << "All tests of this sixth function have been passed." << endl;
return POINTS[6];
}
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( )
{
int sum = 0;
cout << "Running " << DESCRIPTION[0] << endl;
sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); cout << sum << endl;
sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); cout << sum << endl;
sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); cout << sum << endl;
sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); cout << sum << endl;
sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); cout << sum << endl;
sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); cout << sum << endl;
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;
}
sequence3.h
#ifndef MAIN_SAVITCH_SEQUENCE3_H
#define MAIN_SAVITCH_SEQUENCE3_H
#include // Provides size_t
#include "node1.h" // Provides node class
namespace main_savitch_5
{
class sequence
{
public:
// TYPEDEFS and MEMBER CONSTANTS
typedef double value_type;
typedef std::size_t size_type;
// CONSTRUCTORS and DESTRUCTOR
sequence();
sequence(const sequence& source);
~sequence();
// MODIFICATION MEMBER FUNCTIONS
void start( );
void advance( );
void insert(const value_type& entry);
void attach(const value_type& entry);
void operator =(const sequence& source);
void remove_current( );
// CONSTANT MEMBER FUNCTIONS
size_type size( ) const { return many_nodes; }
bool is_item( ) const { return (cursor != NULL); }
value_type current( ) const;
private:
node *head_ptr;
node *tail_ptr;
node *cursor;
node *precursor;
size_type many_nodes;
};
}
#endif
node1.cpp
#include "node1.h"
#include // Provides assert
#include // Provides NULL and size_t
using namespace std;
namespace main_savitch_5
{
size_t list_length(const node* head_ptr)
// Library facilities used: cstdlib
{
const node *cursor;
size_t answer;
answer = 0;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
++answer;
return answer;
}
void list_head_insert(node*& head_ptr, const node::value_type& entry)
{
head_ptr = new node(entry, head_ptr);
}
void list_insert(node* previous_ptr, const node::value_type& entry)
{
node *insert_ptr;
insert_ptr = new node(entry, previous_ptr->link( ));
previous_ptr->set_link(insert_ptr);
}
node* list_search(node* head_ptr, const node::value_type& target)
// Library facilities used: cstdlib
{
node *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
if (target == cursor->data( ))
return cursor;
return NULL;
}
const node* list_search(const node* head_ptr, const node::value_type& target)
// Library facilities used: cstdlib
{
const node *cursor;
for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( ))
if (target == cursor->data( ))
return cursor;
return NULL;
}
node* list_locate(node* head_ptr, size_t position)
// Library facilities used: cassert, cstdlib
{
node *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link( );
return cursor;
}
const node* list_locate(const node* head_ptr, size_t position)
// Library facilities used: cassert, cstdlib
{
const node *cursor;
size_t i;
assert (0 < position);
cursor = head_ptr;
for (i = 1; (i < position) && (cursor != NULL); i++)
cursor = cursor->link( );
return cursor;
}
void list_head_remove(node*& head_ptr)
{
node *remove_ptr;
remove_ptr = head_ptr;
head_ptr = head_ptr->link( );
delete remove_ptr;
}
void list_remove(node* previous_ptr)
{
node *remove_ptr;
remove_ptr = previous_ptr->link( );
previous_ptr->set_link( remove_ptr->link( ) );
delete remove_ptr;
}
void list_clear(node*& head_ptr)
// Library facilities used: cstdlib
{
while (head_ptr != NULL)
list_head_remove(head_ptr);
}
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr)
// Library facilities used: cstdlib
{
head_ptr = NULL;
tail_ptr = NULL;
// Handle the case of the empty list.
if (source_ptr == NULL)
return;
// Make the head node for the newly created list, and put data in it.
list_head_insert(head_ptr, source_ptr->data( ));
tail_ptr = head_ptr;
// Copy the rest of the nodes one at a time, adding at the tail of new list.
source_ptr = source_ptr->link( );
while (source_ptr != NULL)
{
list_insert(tail_ptr, source_ptr->data( ));
tail_ptr = tail_ptr->link( );
source_ptr = source_ptr->link( );
}
}
}
node1.h
#ifndef MAIN_SAVITCH_NODE1_H
#define MAIN_SAVITCH_NODE1_H
#include // Provides size_t and NULL
namespace main_savitch_5
{
class node
{
public:
// TYPEDEF
typedef double value_type;
// CONSTRUCTOR
node(
const value_type& init_data = value_type( ),
node* init_link = NULL
)
{ data_field = init_data; link_field = init_link; }
// Member functions to set the data and link fields:
void set_data(const value_type& new_data) { data_field = new_data; }
void set_link(node* new_link) { link_field = new_link; }
// Constant member function to retrieve the current data:
value_type data( ) const { return data_field; }
// Two slightly different member functions to retreive
// the current link:
const node* link( ) const { return link_field; }
node* link( ) { return link_field; }
private:
value_type data_field;
node* link_field;
};
// FUNCTIONS for the linked list toolkit
std::size_t list_length(const node* head_ptr);
void list_head_insert(node*& head_ptr, const node::value_type& entry);
void list_insert(node* previous_ptr, const node::value_type& entry);
node* list_search(node* head_ptr, const node::value_type& target);
const node* list_search
(const node* head_ptr, const node::value_type& target);
node* list_locate(node* head_ptr, std::size_t position);
const node* list_locate(const node* head_ptr, std::size_t position);
void list_head_remove(node*& head_ptr);
void list_remove(node* previous_ptr);
void list_clear(node*& head_ptr);
void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);
}
#endif

More Related Content

Similar to #include iostream     Provides cout. #include cstdlib   .pdf

#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdfsrinivas9922
 
#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdfsrinivas9922
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdffashiongallery1
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean testsDanylenko Max
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docxgertrudebellgrove
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docxgertrudebellgrove
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsStephen Chin
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfamittripathi2002
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfamittripathi2002
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docxjoyjonna282
 
Test string and array
Test string and arrayTest string and array
Test string and arrayNabeel Ahmed
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docxjoyjonna282
 
Test string and array
Test string and arrayTest string and array
Test string and arrayNabeel Ahmed
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good TestsTomek Kaczanowski
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfIan0J2Bondo
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfIan0J2Bondo
 

Similar to #include iostream     Provides cout. #include cstdlib   .pdf (20)

#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf
 
#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf#includeiostream#includestack#includestring #include .pdf
#includeiostream#includestack#includestring #include .pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdfUsing c++Im also using a the ide editor called CodeLiteThe hea.pdf
Using c++Im also using a the ide editor called CodeLiteThe hea.pdf
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
How to write clean tests
How to write clean testsHow to write clean tests
How to write clean tests
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docx
 
@author Jane Programmer @cwid 123 45 678 @class.docx
@author Jane Programmer  @cwid   123 45 678  @class.docx@author Jane Programmer  @cwid   123 45 678  @class.docx
@author Jane Programmer @cwid 123 45 678 @class.docx
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
Pro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise ApplicationsPro Java Fx – Developing Enterprise Applications
Pro Java Fx – Developing Enterprise Applications
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
 
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdfHello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
Hello Everyone!!!I’m writing a c++ program that presents a menu to.pdf
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docx
 
Test string and array
Test string and arrayTest string and array
Test string and array
 
PQTimer.java A simple driver program to run timing t.docx
  PQTimer.java     A simple driver program to run timing t.docx  PQTimer.java     A simple driver program to run timing t.docx
PQTimer.java A simple driver program to run timing t.docx
 
Test string and array
Test string and arrayTest string and array
Test string and array
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
 
Hello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdfHello need help on this lab- what you need to do is add a code to Arra.pdf
Hello need help on this lab- what you need to do is add a code to Arra.pdf
 

More from aptind

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdfaptind
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdfaptind
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdfaptind
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfaptind
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfaptind
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfaptind
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdfaptind
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfaptind
 
Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfaptind
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfaptind
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfaptind
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfaptind
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfaptind
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfaptind
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfaptind
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfaptind
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfaptind
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfaptind
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfaptind
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdfaptind
 

More from aptind (20)

ssian chemist, Dmitri Mendeleev is often consider.pdf
                     ssian chemist, Dmitri Mendeleev is often consider.pdf                     ssian chemist, Dmitri Mendeleev is often consider.pdf
ssian chemist, Dmitri Mendeleev is often consider.pdf
 
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf                     moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
moles of HCl = 0.1106 x 10 millimoles = 1.106 mil.pdf
 
               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf               CLOUD COMPUTING -----------------------------------.pdf
               CLOUD COMPUTING -----------------------------------.pdf
 
You cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdfYou cannot.SolutionYou cannot..pdf
You cannot.SolutionYou cannot..pdf
 
ViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdfViVi is universally available on Unix systems. It has been around.pdf
ViVi is universally available on Unix systems. It has been around.pdf
 
Waterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdfWaterfall methodThe model consists of various phases based on the.pdf
Waterfall methodThe model consists of various phases based on the.pdf
 
Hi, I am unable to understand the terminology in .pdf
                     Hi, I am unable to understand the terminology in .pdf                     Hi, I am unable to understand the terminology in .pdf
Hi, I am unable to understand the terminology in .pdf
 
The main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdfThe main function of cerebellum is to control the motor movements. H.pdf
The main function of cerebellum is to control the motor movements. H.pdf
 
Starting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdfStarting with Main.java, where I tested everythingimport College..pdf
Starting with Main.java, where I tested everythingimport College..pdf
 
solution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdfsolution of question no.6inputPresent stateNext stateoutput.pdf
solution of question no.6inputPresent stateNext stateoutput.pdf
 
Sexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdfSexual reproduction has played the most crucial role in evolution of.pdf
Sexual reproduction has played the most crucial role in evolution of.pdf
 
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdfpackage com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
package com.java2novice.ds.linkedlist;import java.util.NoSuchEleme.pdf
 
And is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdfAnd is option DIf variable interest rate decrease , asset value wi.pdf
And is option DIf variable interest rate decrease , asset value wi.pdf
 
import java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdfimport java.util.Scanner;public class Factorial { method usi.pdf
import java.util.Scanner;public class Factorial { method usi.pdf
 
Hi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdfHi please find my code.import java.util.HashMap;import java.util.pdf
Hi please find my code.import java.util.HashMap;import java.util.pdf
 
Given below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdfGiven below is the code for the question. Since the test files (ment.pdf
Given below is the code for the question. Since the test files (ment.pdf
 
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdfCisco Systems, Inc Acquisition Integration for manufacturing at.pdf
Cisco Systems, Inc Acquisition Integration for manufacturing at.pdf
 
As we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdfAs we understand, when soil particles binds to each other more stron.pdf
As we understand, when soil particles binds to each other more stron.pdf
 
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdfAmount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
Amount deposited (base amount) = 2000Rate of interest = 5Amount.pdf
 
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
24. Accomodation - n. Ability of lens to chhange shape diminishes as.pdf
 

Recently uploaded

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 

Recently uploaded (20)

Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 

#include iostream     Provides cout. #include cstdlib   .pdf

  • 1. #include // Provides cout. #include // Provides size_t. #include "sequence3.h" // Provides the sequence class with double items. using namespace std; using namespace main_savitch_5; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 6; const int POINTS[MANY_TESTS+1] = { 18, // 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 }; const char DESCRIPTION[MANY_TESTS+1][256] = { "tests for sequence Class with a linked sequence", "Testing insert, attach, and the constant member functions", "Testing situations where the cursor goes off the sequence", "Testing remove_current", "Testing the copy constructor", "Testing the assignment operator", "Testing insert/attach for somewhat larger sequences" }; 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)
  • 2. { 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[]) { 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,";
  • 3. cout << " but is_item still returns true." << endl; answer = false; } cout << (answer ? "Passed." : "Failed.") << endl; return answer; } 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( ) {
  • 4. 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);
  • 5. 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( ) { const size_t TESTSIZE = 30; 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;
  • 6. } 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 << TESTSIZE*10 << " at the sequence's end." << endl; for (i = 4; i <= TESTSIZE; 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 << TESTSIZE*10 << "." << endl; test.start( ); for (i = 1; i <= TESTSIZE; i++) { if ((!test.is_item( )) || test.current( ) != i*10) { cout << " Test failed to find " << i*10 << endl; return 0; } test.advance( ); }
  • 7. 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( ) { const size_t TESTSIZE = 30; sequence test; // 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 // 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.";
  • 8. 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);
  • 9. 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 < TESTSIZE; i++) test.insert(0); for (i = 0; i < TESTSIZE; i++) test.remove_current( ); // All tests passed cout << "All tests of this third function have been passed." << endl; return POINTS[3]; } // ************************************************************************** int test4( ) { const size_t TESTSIZE = 30; sequence original; // A sequence that we'll copy. double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change 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;
  • 10. // 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*TESTSIZE; i++) original.attach(i); sequence copy2(original); original.remove_current( ); // Delete tail from original, but not the copy original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy2, 2*TESTSIZE, 2*TESTSIZE-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Copy constructor test: with cursor near middle." << endl; original.insert(2); for (i = 1; i < TESTSIZE; i++) original.advance( ); // Cursor is now at location [TESTSIZE] (counting [0] as the first spot). sequence copy3(original); original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy3, 2*TESTSIZE-1, TESTSIZE, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Copy constructor test: for a sequence with cursor at front." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4(original); original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy.
  • 11. if (!correct (copy4, 2*TESTSIZE-1, 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( ); // Delete 2 from original, but not the copy. if (!correct (copy5, 2*TESTSIZE-1, 2*TESTSIZE, items) ) return 0; // All tests passed cout << "All tests of this fourth function have been passed." << endl; return POINTS[4]; } int test5( ) { const size_t TESTSIZE = 30; sequence original; // A sequence that we'll copy. double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change original. cout << "Assignment operator test: for an empty sequence." << endl; sequence copy1; copy1 = original;
  • 12. 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: cursor at tail." << endl; for (i=2; i <= 2*TESTSIZE; i++) original.attach(i); sequence copy2; copy2 = original; original.remove_current( ); // Delete tail from original, but not the copy original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy2, 2*TESTSIZE, 2*TESTSIZE-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Assignment operator test: with cursor near middle." << endl; original.insert(2); for (i = 1; i < TESTSIZE; i++) original.advance( ); // Cursor is now at location [TESTSIZE] (counting [0] as the first spot). sequence copy3; copy3 = original; original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy3, 2*TESTSIZE-1, TESTSIZE, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Assignment operator test: with cursor at front." << endl; original.insert(2); original.start( ); // Cursor is now at the front.
  • 13. sequence copy4; copy4 = original; original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy4, 2*TESTSIZE-1, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Assignment operator test: 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( ); // Deletes 2 from the original, but not copy. if (!correct (copy5, 2*TESTSIZE-1, 2*TESTSIZE, items) ) return 0; cout << "Checking correctness of a self-assignment x = x;" << endl; original.insert(2); original = original; if (!correct (original, 2*TESTSIZE-1, 1, items) ) return 0; // All tests passed cout << "All tests of this fifth function have been passed." << endl; return POINTS[5]; } int test6( )
  • 14. { const size_t TESTSIZE = 30; sequence testa, testi; double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; 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*TESTSIZE; i++) testa.attach(i); if (!correct (testa, 2*TESTSIZE, 2*TESTSIZE-1, items) ) return 0; cout << "Testing to see that insert works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 2*TESTSIZE; i >= 1; i--) testi.insert(i); if (!correct (testi, 2*TESTSIZE, 0, items) ) return 0; // All tests passed cout << "All tests of this sixth function have been passed." << endl; return POINTS[6]; } 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;
  • 15. 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( ) { int sum = 0; cout << "Running " << DESCRIPTION[0] << endl; sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); cout << sum << endl; sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); cout << sum << endl; sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); cout << sum << endl; sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); cout << sum << endl; sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); cout << sum << endl; sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); cout << sum << endl; 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; } sequence3.h #ifndef MAIN_SAVITCH_SEQUENCE3_H #define MAIN_SAVITCH_SEQUENCE3_H #include // Provides size_t #include "node1.h" // Provides node class
  • 16. namespace main_savitch_5 { class sequence { public: // TYPEDEFS and MEMBER CONSTANTS typedef double value_type; typedef std::size_t size_type; // CONSTRUCTORS and DESTRUCTOR sequence(); sequence(const sequence& source); ~sequence(); // MODIFICATION MEMBER FUNCTIONS void start( ); void advance( ); void insert(const value_type& entry); void attach(const value_type& entry); void operator =(const sequence& source); void remove_current( ); // CONSTANT MEMBER FUNCTIONS size_type size( ) const { return many_nodes; } bool is_item( ) const { return (cursor != NULL); } value_type current( ) const; private: node *head_ptr; node *tail_ptr; node *cursor; node *precursor; size_type many_nodes; }; } #endif node1.cpp #include "node1.h" #include // Provides assert
  • 17. #include // Provides NULL and size_t using namespace std; namespace main_savitch_5 { size_t list_length(const node* head_ptr) // Library facilities used: cstdlib { const node *cursor; size_t answer; answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) ++answer; return answer; } void list_head_insert(node*& head_ptr, const node::value_type& entry) { head_ptr = new node(entry, head_ptr); } void list_insert(node* previous_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, previous_ptr->link( )); previous_ptr->set_link(insert_ptr); } node* list_search(node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL;
  • 18. } const node* list_search(const node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { const node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL; } node* list_locate(node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { node *cursor; size_t i; assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; } const node* list_locate(const node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { const node *cursor; size_t i; assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; } void list_head_remove(node*& head_ptr)
  • 19. { node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr->link( ); delete remove_ptr; } void list_remove(node* previous_ptr) { node *remove_ptr; remove_ptr = previous_ptr->link( ); previous_ptr->set_link( remove_ptr->link( ) ); delete remove_ptr; } void list_clear(node*& head_ptr) // Library facilities used: cstdlib { while (head_ptr != NULL) list_head_remove(head_ptr); } void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) // Library facilities used: cstdlib { head_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. if (source_ptr == NULL) return; // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data( )); tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link( ); while (source_ptr != NULL) {
  • 20. list_insert(tail_ptr, source_ptr->data( )); tail_ptr = tail_ptr->link( ); source_ptr = source_ptr->link( ); } } } node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include // Provides size_t and NULL namespace main_savitch_5 { class node { public: // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data_field = init_data; link_field = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data_field; } // Two slightly different member functions to retreive // the current link: const node* link( ) const { return link_field; } node* link( ) { return link_field; } private:
  • 21. value_type data_field; node* link_field; }; // FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_ptr, const node::value_type& entry); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const node* head_ptr, const node::value_type& target); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const node* head_ptr, std::size_t position); void list_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr); } #endif Solution #include // Provides cout. #include // Provides size_t. #include "sequence3.h" // Provides the sequence class with double items. using namespace std; using namespace main_savitch_5; // Descriptions and points for each of the tests: const size_t MANY_TESTS = 6; const int POINTS[MANY_TESTS+1] = { 18, // 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
  • 22. 2 // Test 6 points }; const char DESCRIPTION[MANY_TESTS+1][256] = { "tests for sequence Class with a linked sequence", "Testing insert, attach, and the constant member functions", "Testing situations where the cursor goes off the sequence", "Testing remove_current", "Testing the copy constructor", "Testing the assignment operator", "Testing insert/attach for somewhat larger sequences" }; 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[]) { 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...";
  • 23. 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 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; }
  • 24. // 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( ) { 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);
  • 25. 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;
  • 26. // All tests have been passed cout << "All tests of this first function have been passed." << endl; return POINTS[1]; } int test2( ) { const size_t TESTSIZE = 30; 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;
  • 27. // 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 << TESTSIZE*10 << " at the sequence's end." << endl; for (i = 4; i <= TESTSIZE; 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 << TESTSIZE*10 << "." << endl; test.start( ); for (i = 1; i <= TESTSIZE; 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( ) { const size_t TESTSIZE = 30; sequence test; // Within this function, I create several different sequences using the
  • 28. // 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 // 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);
  • 29. 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 < TESTSIZE; i++) test.insert(0); for (i = 0; i < TESTSIZE; i++) test.remove_current( ); // All tests passed
  • 30. cout << "All tests of this third function have been passed." << endl; return POINTS[3]; } // ************************************************************************** int test4( ) { const size_t TESTSIZE = 30; sequence original; // A sequence that we'll copy. double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change 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*TESTSIZE; i++) original.attach(i); sequence copy2(original); original.remove_current( ); // Delete tail from original, but not the copy original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy2, 2*TESTSIZE, 2*TESTSIZE-1, items) ) return 0; // Test copying of a sequence with cursor near the middle. cout << "Copy constructor test: with cursor near middle." << endl; original.insert(2);
  • 31. for (i = 1; i < TESTSIZE; i++) original.advance( ); // Cursor is now at location [TESTSIZE] (counting [0] as the first spot). sequence copy3(original); original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy3, 2*TESTSIZE-1, TESTSIZE, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Copy constructor test: for a sequence with cursor at front." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4(original); original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy4, 2*TESTSIZE-1, 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( ); // Delete 2 from original, but not the copy. if (!correct (copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
  • 32. ) return 0; // All tests passed cout << "All tests of this fourth function have been passed." << endl; return POINTS[4]; } int test5( ) { const size_t TESTSIZE = 30; sequence original; // A sequence that we'll copy. double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; i++) items[i-1] = i; // Test copying of an empty sequence. After the copying, we change 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: cursor at tail." << endl; for (i=2; i <= 2*TESTSIZE; i++) original.attach(i); sequence copy2; copy2 = original; original.remove_current( ); // Delete tail from original, but not the copy original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy2, 2*TESTSIZE, 2*TESTSIZE-1, items) ) return 0;
  • 33. // Test copying of a sequence with cursor near the middle. cout << "Assignment operator test: with cursor near middle." << endl; original.insert(2); for (i = 1; i < TESTSIZE; i++) original.advance( ); // Cursor is now at location [TESTSIZE] (counting [0] as the first spot). sequence copy3; copy3 = original; original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy3, 2*TESTSIZE-1, TESTSIZE, items) ) return 0; // Test copying of a sequence with cursor at the front. cout << "Assignment operator test: with cursor at front." << endl; original.insert(2); original.start( ); // Cursor is now at the front. sequence copy4; copy4 = original; original.start( ); original.advance( ); original.remove_current( ); // Delete 2 from original, but not the copy. if (!correct (copy4, 2*TESTSIZE-1, 0, items) ) return 0; // Test copying of a sequence with no current item. cout << "Assignment operator test: with no current item." << endl; original.insert(2); while (original.is_item( )) original.advance( ); // There is now no current item. sequence copy5;
  • 34. copy5 = original; original.start( ); original.advance( ); original.remove_current( ); // Deletes 2 from the original, but not copy. if (!correct (copy5, 2*TESTSIZE-1, 2*TESTSIZE, items) ) return 0; cout << "Checking correctness of a self-assignment x = x;" << endl; original.insert(2); original = original; if (!correct (original, 2*TESTSIZE-1, 1, items) ) return 0; // All tests passed cout << "All tests of this fifth function have been passed." << endl; return POINTS[5]; } int test6( ) { const size_t TESTSIZE = 30; sequence testa, testi; double items[2*TESTSIZE]; size_t i; // Set up the items array to conatin 1...2*TESTSIZE. for (i = 1; i <= 2*TESTSIZE; 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*TESTSIZE; i++) testa.attach(i); if (!correct (testa, 2*TESTSIZE, 2*TESTSIZE-1, items) )
  • 35. return 0; cout << "Testing to see that insert works correctly when the "; cout << "current capacity is exceeded." << endl; for (i = 2*TESTSIZE; i >= 1; i--) testi.insert(i); if (!correct (testi, 2*TESTSIZE, 0, items) ) return 0; // All tests passed cout << "All tests of this sixth function have been passed." << endl; return POINTS[6]; } 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( ) { int sum = 0;
  • 36. cout << "Running " << DESCRIPTION[0] << endl; sum += run_a_test(1, DESCRIPTION[1], test1, POINTS[1]); cout << sum << endl; sum += run_a_test(2, DESCRIPTION[2], test2, POINTS[2]); cout << sum << endl; sum += run_a_test(3, DESCRIPTION[3], test3, POINTS[3]); cout << sum << endl; sum += run_a_test(4, DESCRIPTION[4], test4, POINTS[4]); cout << sum << endl; sum += run_a_test(5, DESCRIPTION[5], test5, POINTS[5]); cout << sum << endl; sum += run_a_test(6, DESCRIPTION[6], test6, POINTS[6]); cout << sum << endl; 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; } sequence3.h #ifndef MAIN_SAVITCH_SEQUENCE3_H #define MAIN_SAVITCH_SEQUENCE3_H #include // Provides size_t #include "node1.h" // Provides node class namespace main_savitch_5 { class sequence { public: // TYPEDEFS and MEMBER CONSTANTS typedef double value_type; typedef std::size_t size_type; // CONSTRUCTORS and DESTRUCTOR sequence(); sequence(const sequence& source); ~sequence(); // MODIFICATION MEMBER FUNCTIONS void start( ); void advance( ); void insert(const value_type& entry);
  • 37. void attach(const value_type& entry); void operator =(const sequence& source); void remove_current( ); // CONSTANT MEMBER FUNCTIONS size_type size( ) const { return many_nodes; } bool is_item( ) const { return (cursor != NULL); } value_type current( ) const; private: node *head_ptr; node *tail_ptr; node *cursor; node *precursor; size_type many_nodes; }; } #endif node1.cpp #include "node1.h" #include // Provides assert #include // Provides NULL and size_t using namespace std; namespace main_savitch_5 { size_t list_length(const node* head_ptr) // Library facilities used: cstdlib { const node *cursor; size_t answer; answer = 0; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) ++answer; return answer; }
  • 38. void list_head_insert(node*& head_ptr, const node::value_type& entry) { head_ptr = new node(entry, head_ptr); } void list_insert(node* previous_ptr, const node::value_type& entry) { node *insert_ptr; insert_ptr = new node(entry, previous_ptr->link( )); previous_ptr->set_link(insert_ptr); } node* list_search(node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL; } const node* list_search(const node* head_ptr, const node::value_type& target) // Library facilities used: cstdlib { const node *cursor; for (cursor = head_ptr; cursor != NULL; cursor = cursor->link( )) if (target == cursor->data( )) return cursor; return NULL; } node* list_locate(node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { node *cursor; size_t i;
  • 39. assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; } const node* list_locate(const node* head_ptr, size_t position) // Library facilities used: cassert, cstdlib { const node *cursor; size_t i; assert (0 < position); cursor = head_ptr; for (i = 1; (i < position) && (cursor != NULL); i++) cursor = cursor->link( ); return cursor; } void list_head_remove(node*& head_ptr) { node *remove_ptr; remove_ptr = head_ptr; head_ptr = head_ptr->link( ); delete remove_ptr; } void list_remove(node* previous_ptr) { node *remove_ptr; remove_ptr = previous_ptr->link( ); previous_ptr->set_link( remove_ptr->link( ) ); delete remove_ptr; } void list_clear(node*& head_ptr) // Library facilities used: cstdlib {
  • 40. while (head_ptr != NULL) list_head_remove(head_ptr); } void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr) // Library facilities used: cstdlib { head_ptr = NULL; tail_ptr = NULL; // Handle the case of the empty list. if (source_ptr == NULL) return; // Make the head node for the newly created list, and put data in it. list_head_insert(head_ptr, source_ptr->data( )); tail_ptr = head_ptr; // Copy the rest of the nodes one at a time, adding at the tail of new list. source_ptr = source_ptr->link( ); while (source_ptr != NULL) { list_insert(tail_ptr, source_ptr->data( )); tail_ptr = tail_ptr->link( ); source_ptr = source_ptr->link( ); } } } node1.h #ifndef MAIN_SAVITCH_NODE1_H #define MAIN_SAVITCH_NODE1_H #include // Provides size_t and NULL namespace main_savitch_5 { class node { public:
  • 41. // TYPEDEF typedef double value_type; // CONSTRUCTOR node( const value_type& init_data = value_type( ), node* init_link = NULL ) { data_field = init_data; link_field = init_link; } // Member functions to set the data and link fields: void set_data(const value_type& new_data) { data_field = new_data; } void set_link(node* new_link) { link_field = new_link; } // Constant member function to retrieve the current data: value_type data( ) const { return data_field; } // Two slightly different member functions to retreive // the current link: const node* link( ) const { return link_field; } node* link( ) { return link_field; } private: value_type data_field; node* link_field; }; // FUNCTIONS for the linked list toolkit std::size_t list_length(const node* head_ptr); void list_head_insert(node*& head_ptr, const node::value_type& entry); void list_insert(node* previous_ptr, const node::value_type& entry); node* list_search(node* head_ptr, const node::value_type& target); const node* list_search (const node* head_ptr, const node::value_type& target); node* list_locate(node* head_ptr, std::size_t position); const node* list_locate(const node* head_ptr, std::size_t position); void list_head_remove(node*& head_ptr); void list_remove(node* previous_ptr); void list_clear(node*& head_ptr); void list_copy(const node* source_ptr, node*& head_ptr, node*& tail_ptr);