Home Assignments Lecture Blog Resources Discussion Group

Guitar Hero: RingBuffer implementation with unit tests and exceptions (Part A)

In Part A, we implement the ring buffer that will hold the guitar string position data, and write test functions and exception handling.

See for the full assignment


Write a class named RingBuffer that implements the following API:

public class RingBuffer
          RingBuffer(int capacity)  // create an empty ring buffer, with given max capacity
      int size()                    // return number of items currently in the buffer
     bool isEmpty()                 // is the buffer empty (size equals zero)?
     bool isFull()                  // is the buffer full  (size equals capacity)?
     void enqueue(sf::Int16 x)      // add item x to the end
sf::Int16 dequeue()                 // delete and return item from the front
sf::Int16 peek()                    // return (but do not delete) item from the front

Important notes:

  1. The code should be in a pair of files named RingBuffer.cpp and RingBuffer.hpp.
  2. You must #include <SFML/System.hpp> to have access to the sf::Int16 data type (needed to play back an audio buffer).
  3. Attempts to instantiate with a capacity less than 1 should result in a std::invalid_argument exception, and the error message RB constructor: capacity must be greater than zero.
  4. Attempts to enqueue to a full buffer should result in a std::runtime_error exception, and the error message enqueue: can't enqueue to a full ring.
  5. Attempts to dequeue or peek from an empty buffer should result in a std::runtime_error exception, and an appropriate error message.


You should write a main.cpp file that drives around your RingBuffer class, and demonstrates that it works correctly.

Then, you should write a test.cpp file that uses the Boost functions BOOST_REQUIRE_THROW and BOOST_REQUIRE_NO_THROW to verify that your code properly throws the specified exceptions when appropriate (and does not throw an exception when it shouldn't).

The test file should also exercise all methods of the class.

See the Attach:ps5a-test.cpp file that we went over in class to get started.

Additional Files

Produce and turn in a Makefile for building your class, your main routine, and a test executable.

Produce and turn in a plain-text readme.txt file that explains what you have done.

In particular, describe:

  • how you implemented the ring buffer (e.g. per the Princeton guidance, or some other way)
  • exactly what works or doesn't work


You should be submitting at least six files:

  1. RingBuffer.cpp
  2. RingBuffer.hpp
  3. main.cpp
  4. test.cpp
  5. Makefile
  6. readme.txt

Submit the files:

submit fredm 204-ps5a files

Grading rubric

core implementation4full & correct implementation=4 pts; nearly complete=3pts; part way=2 pts; started=1 pt
Makefile2Makefile or explicit build/link instructions included
your own test.cpp4should test that you:
  generate std::invalid_argument exception on bad constructor;
  don't generate exception on good constructor;
  enqueue, dequeue, and peek work;
  generate std::runtime_error when calling enqueue on full buffer;
  generate std::runtime_error when calling dequeue or peek on empty buffer.
readme.txt2Readme should say something meaningful about what you accomplished