Monthly Archives: July 2019

Unit test frameworks

I’ve never understood why people use GTest, CppUnit, Catch, oh wait, Catch2 now.
Here is my C++ unit test framework:

// test object constructor calls function
struct test {
    test(const std::function& f)
        try {
        catch (const std::exception& ex) {
            fputs(ex.what(), stderr);

Just write a int main() { return 0; } file and add .cpp files that call your tests.
They look something like this:

test my_function([]()
    assert (my_function_works());

Just make sure my_function_works throws an exception with an informative message. I use ensure for that.
More examples can be found here.
Why make things complicated?


What is a type? You see that word used all the time in computer languages but it never seems to be precisely defined. Here is my definition:

type is a set of bits together with the operations that can be performed on those bits along with the rules the operations satisfy.

This is really just math 101 but the computer science world is finally catching up. Math doesn’t mention the set of bits because everything is a set in math.

An int is a set of bits (usually 32 or 64 these days) that can be added, subtracted, multiplied, divided, complemented, etc. These operations satisfy certain rules. For example, x + y = y + x. They actually satisfy a long list of rules that most people can’t be bothered to learn until something blows up. What about 2’s complement or unsigned ints or overflow? Ain’t nobody got time for that.

As my friend Candy Chiu first pointed out to me, C++ concepts (at the time called Concepts Lite) provide a language mechanism to do precisely this. More precisely, they allow compile time checks on boolean constraints on template parameters. And template parameters are types.