The goal of this project is to construct a functional interactive matrix processing system with confidence, using test-driven methodology.
We will write unit tests through the whole project, and use it to guide our implementation (“test-driven”). The way it works is for each feature we want to implement, we will first writen down a set of unit tests that cover the desired behavior as complete as possible, and implement the feature after that. If the implementation passes the unit test, we will be confident that the it does what we want it to, and move on to the next feature.
We will use µnit as our unit test
framework. Let us try it on a toy project. Suppose we want to
implement a function plus1
that takes an int argument, and return
that argument plus 1. Here’s the unit test:
#include "munit/munit.h"
void testPlus1()
{
munit_assert_int(plus1(1), ==, 2);
munit_assert_int(plus1(100), ==, 101);
munit_assert_int(plus1(-123), ==, -122);
// Edge cases
munit_assert_int(plus1(0), ==, 1);
munit_assert_int(plus1(-1), ==, 0);
}
int main()
{
testPlus1()
return 0;
}
To use this
-
Name this file
test-plus1.c
-
Download µnit and put it next it
test-plus1.c
-
Write your definition of
plus1()
inplus1.c
andplus1.h
-
Include
plus1.h
intest-plus1.c
-
Compile the package:
cc -o test-plus1 test-plus1.c plus1.c
-
Run the test