MY Test File for Lab 2

Important Note on this Test File:
  • This file will help you to verify that your List class is functioning properly. If your List.h file passes all of these tests, then it is *likely* that all functions are working properly (note that this file does not test ALL possible edge cases or situations).
  • It is meant to be used in conjunction with your own test file.
  • Please do not submit this file to Canvas
  • You must submit your own test file named ListTest.cpp where **YOU** test all of your functions by writing your own function calls.
  • This test file is not a substitute for doing your own tests. Do not submit this file as part of this assignment!
  • If you do not write your own test file and test all of your own functions, you will lose points on this assignment.
  • However, if you test your code using this test file, and all of the tests pass AND you submit your own test file in which you have thoroughly tested your code, then you are guaranteed a high score on the List.h part of Lab 2 (60% of your Lab 2 grade)!


How to Use this Test File

A Word About Assert and Preconditions

  • First, make sure that you have handled ALL preconditions using assert in both your access functions (such as getStart, getStop, getIterator, etc) , and manipulation procedures (such as removeStart, removeStop, moveIterPrevious, moveIterNext, insertIterator, and deleteIterator).
  • You should remove the cout statements used for the preconditions of removeStart and removeStop from Lab 1, and use assert instead.
  • This test file includes function calls that violate the preconditions for these functions.
  • Thus, your program will terminate when these function calls are made.
  • Verify that the assert is working for these function calls and then comment them out inside the test file.
  • For example:
sList.removeStop(); //should trigger assert
  • Call this function and then comment it out like this:

//sList.removeStop(); //commented out so will not trigger assert
  • Once it is commented out, you can proceed with the next test.

How to verify that your functions pass the tests

  • Use the cout statements in the test file as a guide. The cout will indicate what will be a correct result of the function call.
  • If anything is not working as the couts state that they should, go back and alter your code.
  • Let the instructor know if you have any questions.

A separate note on removeIterator

  • A couple of students have asked: after removeIterator is called, the iterator should become NULL, (i.e. post: iterator is NULL). This should also be evident in this test file.

A separate note on printing the list

  • If no elements are in the list, please print a blank line. Do not print a message that the list is empty.

A separate note on removeStop

  • removeStop should not longer have a while loop. A while loop is not needed as we now can utilize the stop->prev pointer to locate the second to last node in the List. Please remove this while loop to avoid loss of points.

A separate note on commenting the functions

  • To avoid loss of points, make sure that all of your function prototypes are commented, with any preconditions noted. This includes all of the new functions you added to your List.h file for this Lab 2. The comments should be evident from the descriptions of the functions given in the directions for Lab 2. However, it is up to you to identify the preconditions.


Test File

  • Create a new source file in your Eclipse project
  • Name this source file TestFile.cpp
  • Copy and paste the below code into this new file.
  • Comment out ListTest.cpp and Anagram.cpp if you have started it (you cannot have more than one main function in the same project at any given time)
  • Compile and run your code.
  • Let the instructor know if you have any questions.

/**
* @author Jennifer Parrish
* CIS 22C
* TestFile.cpp
*/

#include <iostream>
#include <string>
#include "List.h"
using namespace std;

int main() {
    List<int> iList;
    List<char> cList;
    List<double> dList;
    List<string> sList;

    cout << "***The next 6 function calls will fail the assertion. The program should halt. "
             <<"After you test each one, then comment out the function call***";
    sList.removeStop();
    sList.removeStart();
    sList.removeIterator();
    sList.getStart();
    sList.getStop();
    sList.getIterator();

    iList.insertStart(5);
    cout << "Should print 5: ";
    iList.displayList(cout);
    iList.insertStop(6);
    cout << "Should print 5 6:";
    iList.displayList(cout);
    cout <<"***The next function call will fail the assertion. After you test it, "
            << "then comment out the function call***";
    iList.getIterator();
    iList.startIterator();
    iList.insertIterator(7);
    cout << "Should print 5 7 6:"; //note these should all print on a single line separated by spaces
    iList.displayList(cout);

    List<int> iList2(iList); //copy constructor
    cout << "Should print 5 7 6:";
    iList2.displayList(cout);

    cout << "Should print lists are equal: " << endl;
    if (iList==iList2)
        cout << "Lists are equal" << endl;
    else
        cout << "Lists are not equal" << endl;

    iList.moveIterNext();
    iList.removeIterator();
    cout << "Should print 5 6:";
    iList.displayList(cout);

    cout << "Should print lists are not equal: " << endl;
    if (iList==iList2)
        cout << "Lists are equal" << endl;
    else
        cout << "Lists are not equal" << endl;

    cout << "Length of list should be 0: "<< cList.getLength() << endl;
    cList.insertStart('a');
    cList.removeStop();
    cout << "Length of list should be 0: "<< cList.getLength() << endl;
    cout << "List should be empty. Nothing printed to screen: "<< endl;
    //Should print nothing and move to a new line.
    //Should NOT print an error message or any other message
    cList.displayList(cout);
    cList.insertStop('Z');
    cList.startIterator();
    cList.insertIterator('z');
    cList.insertIterator('z');
    cList.moveIterNext();
    cList.insertIterator('z');
    cList.removeIterator();
    cout << "Should print Z z z:";
    cList.displayList(cout);
    cout << "The length of the list should be 3: " << cList.getLength() << endl;

    cout << "***Should fail the assertion as the iterator is off the end of the list. "
                << "Comment out when finished:*** \n";
    dList.moveIterNext();
    cout << "***Should fail the assertion as the iterator is off the end of the list. "
            << "Comment out when finished:*** \n";
    dList.insertIterator(-8.9); //should not insert this value

    dList.insertStop(8.8);
    dList.insertStop(9.9);
    cout << "Should print 8.8 9.9:";
    dList.displayList(cout);

    cout << "Should print iterator is off the end of the List:\n";
    if(dList.offEnd())
    {
        cout <<"Iterator is off end\n";
    }
    else
    {
        cout << "Iterator is not off end\n";
    }
    dList.startIterator();
    dList.moveIterNext();
    cout << "Should print 9.9: ";
    cout << dList.getIterator() << endl;
    dList.moveIterNext();
    cout << "***Should fail the assertion as the iterator is off the end of the list. "
                << "Comment out when finished:*** \n";
    cout << dList.getIterator() << endl;
    dList.removeStop();
    cout << "Should print 8.8: ";
    dList.displayList(cout);
}