fairly new to c++, trying to get this program to work correctly. I was provided a .h and main test file and am expected to write the .template file. When I run the code, my test all fail because the is_item() function isn't working correctly. I can't figure out how to word it appropriately so that it works.
here's the three files:
/////////////////////////////////////////////////////////////////////.h file
#ifndef _STACK_SEQUENCE_H_
#define _STACK_SAVITCH_SEQUENCE_H_
#include <cstdlib>
#include <stack>
namespace stack_sequence_4
{
template <typename T>
class sequence
{
public:
typedef std::size_t size_type;
sequence( );
void start( );
void advance( );
void insert(const T& entry);
void attach(const T& entry);
void remove_current( );
size_type size( ) const;
bool is_item( ) const;
T current( ) const;
private:
std::stack<T> first;
std::stack<T> second;
};
}
#include "sequence4.template"
#endif
////////////////////////////////////////////////////////////////.template file
#include <stack>
#include <cassert>
namespace stack_sequence_4
{
template <class T>
sequence<T>::sequence( )
{
std::stack<T> first;
std::stack<T> second;
}
template <class T>
void sequence<T>::start( )
{
int k;
while(first.size()>1)
{
k=first.top();
first.pop();
second.push(k);
}
}
template <class T>
void sequence<T>::advance( )
{
assert(is_item());
if (second.empty())
{return;}
else
{
int k;
k=second.top();
second.pop();
first.push(k);
}
}
template <class T>
void sequence<T>::insert(const T& entry)
{
if (first.empty())
{first.push(entry);}
else
{
int k;
k=first.top();
first.pop();
second.push(k);
first.push(entry);
}
}
template <class T>
void sequence<T>::attach(const T& entry)
{
first.push(entry);
}
template <class T>
void sequence<T>::remove_current( )
{
assert(is_item());
first.pop();
if(second.size()>0)
{
int k;
k=second.top();
first.push(k);
second.pop();
}
}
template <class T>
typename sequence<T>::size_type sequence<T>::size( ) const
{
size_type totalSize;
totalSize=first.size()+second.size();
return totalSize;
}
template <class T>
bool sequence<T>::is_item( ) const
{
return (!first.empty());
}
template <class T>
T sequence<T>::current( ) const
{
assert(is_item());
return first.top();
}
}
////////////////////////////////////////////main.cpp file
#include <iostream>
#include <cstdlib>
#include "sequence4.h"
using namespace std;
using namespace stack_sequence_4;
const size_t MANY_TESTS = 6;
const int POINTS[MANY_TESTS+1] = {
18,
4,
4,
4,
2,
2,
2
};
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<double>& 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<double>& test, size_t s, size_t i, double items[])
{
bool answer = true;
cout << "The cursor should be at item [" << i << "]" << " of the sequence\n";
cout << "(counting the first item as [0]). I will advance the cursor\n";
cout << "to the end of the sequence, checking that each item is correct...";
cout.flush( );
while ((i < s) && test.is_item( ) && (test.current( ) == items[i]))
{
i++;
test.advance( );
}
if ((i != s) && !test.is_item( ))
{
cout << "\n Cursor fell off the sequence too soon." << endl;
answer = false;
}
else if (i != s)
{
cout << "\n The item [" << i << "] should be " << items[i] << ",\n";
cout << " but it was " << test.current( ) << " instead.\n";
answer = false;
}
else if (test.is_item( ))
{
cout << "\n The cursor was moved off the sequence,";
cout << " but is_item still returns true." << endl;
answer = false;
}
cout << (answer ? "Passed." : "Failed.") << endl;
return answer;
}
bool correct
(sequence<double>& test, size_t size, size_t cursor_spot, double items[])
{
bool has_cursor = (cursor_spot < size);
if (!test_basic(test, size, has_cursor))
{
cout << "Basic test of size() or is_item() failed." << endl << endl;
return false;
}
if (has_cursor && !test_items(test, size, cursor_spot, items))
{
cout << "Test of the sequence's items failed." << endl << endl;
return false;
}
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;
}
cout << "All tests passed for this sequence." << endl << endl;
return true;
}
int test1( )
{
sequence<double> empty;
sequence<double> test;
double items1[4] = { 5, 10, 20, 30 };
double items2[4] = { 10, 15, 20, 30 };
cout << "Starting with an empty sequence." << endl;
if (!correct(empty, 0, 0, items1)) return 0;
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;
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;
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start and insert 5." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.insert(5);
if (!correct(test, 4, 0, items1)) return 0;
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start, advance once, ";
cout << "and insert 15." << endl;
test = empty;
test.attach(10);
test.attach(20);
test.attach(30);
test.start( );
test.advance( );
test.insert(15);
if (!correct(test, 4, 1, items2)) return 0;
cout << "I am now using attach to put 10,20,30 in an empty sequence.\n";
cout << "Then I move the cursor to the start and attach 15 ";
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;
cout << "All tests of this first function have been passed." << endl;
return POINTS[1];
}
int test2( )
{
const size_t TESTSIZE = 30;
sequence<double> test;
size_t i;
cout << "Using attach to put 20 and 30 in the sequence, and then calling\n";
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;
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( );
test.advance( );
test.advance( );
if (test.is_item( ))
{
cout << " failed." << endl;
return false;
}
cout << " passed." << endl;
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);
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;
}
cout << "All tests of this second function have been passed." << endl;
return POINTS[2];
}
int test3( )
{
const size_t TESTSIZE = 30;
sequence<double> test;
double items1[1] = { 30 };
double items2[2] = { 10, 30 };
double items3[3] = { 10, 20, 30 };
size_t i;
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;
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;
cout << "Build a new sequence by inserting 30, 10, 20 (so the sequence\n";
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( );
cout << "Just for fun, I'll empty the sequence then fill it up, then\n";
cout << "empty it again. During this process, I'll try to determine\n";
cout << "whether any of the sequence's member functions access the\n";
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( );
cout << "All tests of this third function have been passed." << endl;
return POINTS[3];
}
int test4( )
{
const size_t TESTSIZE = 30;
sequence<double> original;
double items[2*TESTSIZE];
size_t i;
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
cout << "Copy constructor test: for an empty sequence." << endl;
sequence<double> copy1(original);
original.attach(1);
if (!correct(copy1, 0, 0, items)) return 0;
cout << "Copy constructor test: for a sequence with cursor at tail." << endl;
for (i=2; i <= 2*TESTSIZE; i++)
original.attach(i);
sequence<double> copy2(original);
original.remove_current( );
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
cout << "Copy constructor test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
sequence<double> copy3(original);
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
cout << "Copy constructor test: for a sequence with cursor at front." << endl;
original.insert(2);
original.start( );
sequence<double> copy4(original);
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy4, 2*TESTSIZE-1, 0, items)
)
return 0;
cout << "Copy constructor test: for a sequence with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
sequence<double> copy5(original);
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy5, 2*TESTSIZE-1, 2*TESTSIZE, items)
)
return 0;
cout << "All tests of this fourth function have been passed." << endl;
return POINTS[4];
}
int test5( )
{
const size_t TESTSIZE = 30;
sequence<double> original;
double items[2*TESTSIZE];
size_t i;
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
cout << "Assignment operator test: for an empty sequence." << endl;
sequence<double> copy1;
copy1 = original;
original.attach(1);
if (!correct(copy1, 0, 0, items)) return 0;
cout << "Assignment operator test: cursor at tail." << endl;
for (i=2; i <= 2*TESTSIZE; i++)
original.attach(i);
sequence<double> copy2;
copy2 = original;
original.remove_current( );
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy2, 2*TESTSIZE, 2*TESTSIZE-1, items)
)
return 0;
cout << "Assignment operator test: with cursor near middle." << endl;
original.insert(2);
for (i = 1; i < TESTSIZE; i++)
original.advance( );
sequence<double> copy3;
copy3 = original;
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy3, 2*TESTSIZE-1, TESTSIZE, items)
)
return 0;
cout << "Assignment operator test: with cursor at front." << endl;
original.insert(2);
original.start( );
sequence<double> copy4;
copy4 = original;
original.start( );
original.advance( );
original.remove_current( );
if (!correct
(copy4, 2*TESTSIZE-1, 0, items)
)
return 0;
cout << "Assignment operator test: with no current item." << endl;
original.insert(2);
while (original.is_item( ))
original.advance( );
sequence<double> copy5;
copy5 = original;
original.start( );
original.advance( );
original.remove_current( );
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;
cout << "All tests of this fifth function have been passed." << endl;
return POINTS[5];
}
int test6( )
{
const size_t TESTSIZE = 30;
sequence<double> testa, testi;
double items[2*TESTSIZE];
size_t i;
for (i = 1; i <= 2*TESTSIZE; i++)
items[i-1] = i;
cout << "Testing to see that attach works correctly when the\n";
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\n";
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;
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\n";
cout << sum << " points out of the " << POINTS[0];
cout << " points from this test program.\n";
return EXIT_SUCCESS;
}
What I have tried:
// return(size()>0);
return (!first.empty());
// return (cursor != NULL);
//
// return (current() != NULL);
// return first.top() !=NULL;
// return current() < size();
// if(first.empty())
// return false;
// else
// return true;