PS5a
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 http://www.cs.princeton.edu/courses/archive/fall13/cos126/assignments/guitar.html for the full assignment
Implementation
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:
- The code should be in a pair of files named
RingBuffer.cpp
andRingBuffer.hpp
. - You must
#include <SFML/System.hpp>
to have access to thesf::Int16
data type (needed to play back an audio buffer). - Attempts to instantiate with a capacity less than 1 should result in a
std::invalid_argument
exception, and the error messageRB constructor: capacity must be greater than zero
. - Attempts to enqueue to a full buffer should result in a
std::runtime_error
exception, and the error messageenqueue: can't enqueue to a full ring
. - Attempts to dequeue or peek from an empty buffer should result in a
std::runtime_error
exception, and an appropriate error message.
Testing
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
Submit
You should be submitting at least six files:
RingBuffer.cpp
RingBuffer.hpp
main.cpp
test.cpp
Makefile
readme.txt
Submit the files:
submit fredm 204-ps5a files
Grading rubric
Feature | Value | Comment |
core implementation | 4 | full & correct implementation=4 pts; nearly complete=3pts; part way=2 pts; started=1 pt |
Makefile | 2 | Makefile or explicit build/link instructions included |
your own test.cpp | 4 | should 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.txt | 2 | Readme should say something meaningful about what you accomplished |
Total | 12 |