Skip to content

Instantly share code, notes, and snippets.

@MetroWind
Last active June 5, 2020 22:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MetroWind/903e8faa0737bfc14454ee94d2bdf97a to your computer and use it in GitHub Desktop.
Save MetroWind/903e8faa0737bfc14454ee94d2bdf97a to your computer and use it in GitHub Desktop.
Interactive matrix processing system

Interactive matrix processing system

The goal of this project is to construct a functional interactive matrix processing system with confidence, using test-driven methodology.

Preparation

Get familiar with unit testing

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

  1. Name this file test-plus1.c

  2. Download µnit and put it next it test-plus1.c

  3. Write your definition of plus1() in plus1.c and plus1.h

  4. Include plus1.h in test-plus1.c

  5. Compile the package: cc -o test-plus1 test-plus1.c plus1.c

  6. Run the test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment