Homework 5: A simple key-value store

Homework is important practice for you, but it’s not graded so you don’t have to submit it.


The fifth homework assignment deals, for the first time, with modules in C. Unlike in previous assignments, you’re not writing a complete C program for this one. Instead, you are implementing an interface for a simple data structure and you’ll write test cases for that implementation as described below. The resulting library module is general enough to be used in a variety of applications.

The Key-Value Store

A key-value store is a simplistic database. It can store associations between keys (think social security number or last name or URL) and values (think address or phone number or popularity score). For this assignment, the data is not even stored persistently on disk, it just exists in memory for as long as a client program runs. (If you’re familiar with Java, think of a key-value store as a Map; if you’re familiar with Python, think of it as a dict.)

On Piazza you’ll find a file kvs.h that describes the interface to our key-value store. Make sure that you read the interface carefully and understand what each operation is supposed to do before you start hacking! Your job is to write kvs.c to implement all the operations from kvs.h. You’ll have to decide how to store the data which will in turn determine the code you’ll have to write to access/update it.

Please do not introduce additional modules, put all the code for the key-value store into kvs.c!


Just writing a module without having any indication of whether it works or not is a really bad idea. So concurrently with your code for kvs.c you should develop a simple unit test driver called test_kvs.c using assert as we did in lecture. Remember that this kind of test driver stops running on a failed assertion, so essentially all your assertions must pass. In writing test_kvs.c make sure that you exercise all of the code in kvs.c!


In addition to kvs.h you need kvs.c and test_kvs.c as well as a Makefile with the usual clean and test targets. Since there is no designated “main program” the Makefile should probably also run the tests by default. You won’t need a shell script for unit testing, you’ll run the test_kvs program instead. However, you probably still want to use valgrind as part of testing.