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(int16_t x)        // add item x to the end
int16_t dequeue()                 // delete and return item from the front
int16_t peek()                    // return (but do not delete) item from the front

Your code must #include <stdint.h> header that defines the standard 16-bit integer type int16_t.

Important notes:

  1. The code should be in a pair of files named RingBuffer.cpp and RingBuffer.hpp.
  2. 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.
  3. 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.
  4. Attempts to dequeue or peek from an empty buffer should result in a std::runtime_error exception, and an appropriate error message.

Debugging and testing

  • 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). As usual, use BOOST_REQUIRE to exercise all methods of the class.
  • Your test file must exercise all methods of your ring buffer, and must exercise all exceptions.
  • You can write a main.cpp file that drives around your RingBuffer class, and use that for additional testing.
  • See the Attach:ps5a-test.cpp file that we went over in class to get started.


Google's style guide is here:

The file can be retrieved from

Save the file on your machine, and then:

chmod +x
sudo mv /usr/local/bin

Now, you can style-check a file using as an executable: filename

Alternately, you could run it using Python:

python filename

Using cpplint

We've agreed to turn off certain warnings. At present, you may run with: --filter=-runtime/references,-build/header_guard --extensions=cpp,hpp

Additional Files

Produce and turn in a Makefile for building your class.

The executable that the Makefile builds must be called ps5a. This should be the result of linking your test.o and your RingBuffer.o.

Produce and turn in a plain-text ps5a-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

Submit your work

You should be submitting at least five files:

  1. RingBuffer.cpp
  2. RingBuffer.hpp
  3. test.cpp (this gets downloaded as ps5a-test.cpp, rename it to test.cpp)
  4. Makefile
  5. ps5a-readme.txt

If you create a main.cpp with printf-style tests, you may submit that as well.

Place the files in subdirectory called ps5a, and archive with:

tar czvf <archive-file-name>.tar.gz ps5a

Submit the archive via Bottlenose:

Martin section (201):
Grinberg section (202):

Grading rubric

core implementation4full & correct implementation=4 pts; nearly complete=3pts; part way=2 pts; started=1 pt
Makefile2Makefile 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.
cpplint2Your source files pass the style checks implemented in cpplint
readme.txt4Readme should say something meaningful about what you accomplished
  1 point for explaining how you tested your implementation
  1 point for explaining the exceptions you implemented
  2 points for correctly explaining the time and space performance of your RB implementation