Chapter 18 Exercise Set 0: Chapter ReviewΒΆ

  1. Using what you did in Chapter 17 Exercise Set 0: Chapter Review as a model, set up automated unit tests for stacks, including a Makefile, a src subdirectory, Stack.h, and test_stacks.cpp files.

  2. Add the following tests to test_stacks.cpp:

    TEST_CASE("Test basic stack operations on stack of ints") {
        Stack<int> stack;
        stack.push(9);
        CHECK(stack.top() == 9);
        stack.push(11);
        CHECK(stack.top() == 11);
        stack.push(42);
        CHECK(stack.top() == 42);
        CHECK(stack.pop() == 42);
        CHECK(stack.top() == 11);
        CHECK(stack.empty() == false);
        stack.pop();
        CHECK(stack.pop() == 9);
        CHECK(stack.empty() == true);
    }
    
  3. Add the following tests to test_stacks.cpp:

    TEST_CASE("Test basic stack operations on stack of strings") {
        Stack<string> stack;
        stack.push("cheese");
        CHECK(stack.top() == "cheese");
        stack.push("anchovies");
        CHECK(stack.top() == "anchovies");
        stack.push("onions");
        CHECK(stack.top() == "onions");
        CHECK(stack.pop() == "onions");
        CHECK(stack.top() == "anchovies");
        CHECK(stack.empty() == false);
        stack.pop();
        CHECK(stack.pop() == "cheese");
        CHECK(stack.empty() == true);
    }
    
  4. Write your own tests for a stack of user defined objects (Card perhaps?).

  5. Run your test suite with all three implementations of the Stack ADT that were introduced in the chapter.