Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit

Saint Louis University

Computer Science 2100
Data Structures

Michael Goldwasser

Fall 2015

Dept. of Math & Computer Science

Programming Assignment 02
Leaky Stack

Due: Monday, 28 September 2015, 11:59pm

Please see the general programming webpage for details about the programming environment for this course, guidelines for programming style, and details on electronic submission of assignments.

The files you may need for this assignment can be downloaded here.

Collaboration Policy

For this assignment, you must work individually in regard to the design and implementation of your project.

Please make sure you adhere to the policies on academic integrity in this regard.


Contents:


Overview

Some common uses for stacks in software is to support moving back through the history of a web browser, or undoing editing steps in a word processor. Theoretically, such software could support arbitrary levels of such history, if a stack were to have unbounded capacity, but in pratice, there may be a fixed limit on the size of such a stack.

The issue arises as to what should happen when the capacity is exhausted and a new item is pushed onto the stack. One possibility is to throw an exception. But this is not how modern software behaves. For example, if a web browser were only willing to save 50 pages in its history, and you visit more pages, it will make room in the history for a new page by throwing away the least recently visited page (i.e., the bottom of the stack). The formal Stack interface does not provide any means for accessing or removing the object on the bottom of the stack.

In this assignment, we define a new ADT that we call a LeakyStack. The interface for a LeakyStack is very similar to the interface given for a Stack. However in the case when the capacity is exhausted, a call to push will successfully accept the new element, but at the expense of the loss of the least recently added element. We formalize this interface as an abstract class definition, provided online in file LeakyStack.h.

/**
 * Interface for a leaky stack: A collection of objects that are inserted
 * and removed according to the last-in first-out principle, but with
 * overflow handled by the removal of the least-recently accessed item.
 */
class LeakyStack {
public:
    /**
     * Return the number of objects in the stack.
     * \return number of elements
     */
    int size() const;

    /**
     * Determine if the stack is currently empty.
     * \return true if empty, false otherwise.
     */
    bool empty() const;

    /**
     * Return a const reference to the top object in the stack.
     * \return const reference to top element
     * \throw runtime_error if the stack is empty
     */
    const string& top() const;

    /**
     * Insert an object at the top of the stack. If the stack
     * is already at capacity, the oldest element will be lost.
     * \param the new element
     */
    void push(const string& e);

    /**
     *  Remove the top object from the stack.
     *  \throw runtime_error if the stack is empty.
     */
    void pop();
};

Your Tasks

For this assignment, you will be required to give two different implementations for the LeakyStack interface, as described below. We will provide you with initial files that stub these classes.

Once you have completed your implementations, you are to perform some timing experiments described below. Also, you are to develop some unit tests for the project, also described below.

Simplifications

To make things a bit easier for this assignment,


Files We Are Providing

All such files can be downloaded here. You should only modify the first four of those.

The remaining files are necessary for building the executables, but you should not need to modify (or even examine) their contents.

Use of the Main Driver

Driver is a text-based, menu-driven program for testing your LeakyStack implementations. The menu allows you to call any combination of LeakyStack methods, including reconstructing new instances of either variant with a desired capacity.

By default, the program takes input from the keyboard. However the driver has an optional feature which allows it to read input from a file rather than from a keyboard. The file should have the identical characters which you would use if you were typing them yourself on the keyboard. This feature simply allows you to test and retest your program on a particular input without having to retype the input each time you want to run the program. It is also convenient at times when running with a debugger to have the input read from a file rather than from the keyboard. If the program reaches the end of the file without exiting, then it will revert back to reading input from the keyboard for the remainder of the program.

To use this feature, you must specify the exact filename (including any suffix), as a single argument at runtime, using a syntax such as

./Driver inputfile


Black-Box, Unit Testing

Part of developing good software is also knowing how to perform meaningful testing of your software. For example, suppose this class was used as part of a complete web browser. One approach to testing would be to wait until the entire browser project were completed and then to test the final result. But if something failed with the browser history, we would have to wonder whether the problem was due to an errant implementation of the data structure, to an errant use of a well-written data structure, or some other problem. Rather than relying on a test of the entire end product, a useful approach is to make sure that each individual piece works in isolation. This is called unit testing. For this assignment, you will develop a test plan for evaluating the correctness of the LeakyStack implementations without actually considering the larger application (in fact, we will not actually be writing any such hypothetical web browser). To perform unit testing of this data structure, you will use the provided driver which allows you to individually call any of the public methods supported by the data structure.

There is another issue we wish for you to consider when developing test plans. Often, testing is performed in close coordination with an examination of source code. For example, we may write some code or view some code, and then try to test that particular chunk of code. There is an inherent danger in relying only on such tests. In particular, if the tests are based on the same flow of thoughts as (errant) code, we are more likely to forget to test for a particular combination of events that were not considered by the code. The practice of "black box" testing is to design a set of tests based purely on the formal specifications for a data structure, but without actually seeing the source code.

On this assignment, we will have you develop a black-box unit test plan for LeakyStack implementations. Specifically, you must submit a plain text file named "inputfile" which will be used to test other students' implementations. The goal is to create test input which causes as many as possible of the other students' programs to fail in some way. Because our execution of student tests will be automated, you must make sure that your "inputfile" strictly adheres to the file format which is expected by the text-based driver, Driver, described earlier. Your file may use at most 100 commands. If your input file contains more than the prescribed number of commands, we will simply truncate the end of the file. In order to make sure that your inputfile follows the proper format, we strongly recommend that you run your program on your inputfile to make sure it is accepted by the driver.

Here is a sample input file, appropriately formatted.


Testing Efficiency

TestEfficiency is a second program that will be used to test the efficiency of your two implementations. It does not allow for user interaction. Instead, it performs an experiment in which it repeatedly pushes elements onto a leaky stack, well beyond the stated capacity. Specifically, you may specify a value N that is the capacity of the stack, and then it measures the amount of time that it takes to perform 5N pushes followed by N pops (although it reports its results in terms of the average time per operation).

This program should be invoked using a syntax such as

./TestEfficiency B 10000
to test the efficiency of LeakyStackB with capacity 10000.

Please test both versions of your completed leaky stacks on a reasonable sample of values for N, and record the results. We recommend that you look at what happens each time N doubles, for example, perhaps testing 1000, 2000, 4000, 8000 (although you may need to adjust the scale depending upon the speed of the machine on which you test). Please make sure you pick big enough tests so as to avoid neglible running times.


Files to Submit


Grading Standards

The assignment is worth 20 points. Sixteen points will be awarded based on our own evaluation of your assignment and the readme file. Two additional points will be awarded fractionally based on how well your program performs on other students' test inputs. The final two points will be awarded fractionally based on how well your test input fools other students' flawed programs.


Hints Regarding Modular Arithmetic

When dealing with an array in a circular fashion, there are many possible ways for adjusting indicies as they "wrap around" the ends of the array. The text chooses to use modular arithmetic (the "%" operator in C++). Please note that its precedence is equivalent to multiplication and division, and therefore the expression a+b%c+d is evaluated as a+(b%c)+d by default. Also, the results of a%b is system dependent if one of the operands is negative, so we recommend that you avoid such computations.


Michael Goldwasser
CSCI 2100, Fall 2015
Last modified: Monday, 28 September 2015
Assignments | Course Home | Documentation | Lab Hours/Tutoring | Schedule | Submit