MinUnit: a minimal unit testing framework for C
Meh. Like, who the hell wants to have to restate their conditions twice? Not me, that&#x27;s for sure.<p>What you need is something more like this. (I&#x27;ll write out one case, and you can figure out the rest.) It&#x27;s more lines, but I don&#x27;t think you&#x27;ll feel like it&#x27;s bad value per LOC.<p>For the actual condition you write in your code, you want a macro that expands to something with __FILE__ and __LINE__, and uses the stringizing operator to get the LHS and RHS as strings. Here&#x27;s one that compares to ints (hence II) for equality (hence EQ).<p><pre><code>    #define TEST_EQ_II(A,B) (DoTestEqII((A),#A,(B),#B,__FILE__,__LINE__))
</code></pre>
The function that implements that. Note that because C is stupid, the int parameters basically have to be int64_t... \this isn&#x27;t really a problem in practice.<p><pre><code>    void DoTestEqII(int64_t a,const char *a_str,
                    int64_t b,const char *b_str,
                    const char *file,int64_t line)
    {
        DoTestII(a,a_str,b,b_str,file,line,a==b,&quot;==&quot;);
    }
</code></pre>
Clearly I&#x27;m all about the layering, and here&#x27;s the next layer.<p><pre><code>    void DoTestII(int64_t a,const char *a_str,
                  int64_t b,const char *b_str,
                  const char *file,int64_t line,
                  int result,const char *oper)
    {
        if(!result) {
            Print(&quot;%s%s%&quot; PRId64 &quot;%s: test failed\n&quot;,file,LINE_PREFIX,line,LINE_SUFFIX);
            Print(&quot;    Expected expression: %s\n&quot;,a_str);
            Print(&quot;               Operator: %s\n&quot;,oper);
            Print(&quot;      Actual expression: %s\n&quot;,b_str);
            Print(&quot;         Expected value: %&quot; PRId64 &quot; (0x%&quot; PRIx64 &quot;)\n&quot;,a,a);
            Print(&quot;           Actual value: %&quot; PRId64 &quot; (0x%&quot; PRIx64 &quot;)\n&quot;,b,b);
            Break();
        }
    }
</code></pre>
For VC++:<p><pre><code>    #define Break() __debugbreak()
    #define LINE_PREFIX &quot;(&quot;
    #define LINE_SUFFIX &quot;)&quot;
</code></pre>
Then make Print print via OutputDebugString (VS2015 does C99-style snprintf, so this is easy to arrange perfectly), so the output goes to the output window. Visual Studio treats output window lines beginning like &quot;FILE(LINE):&quot; as referring to source locations, so when you get a failing test you can just tap F8 - I think? I&#x27;m not in Windows right now - and you&#x27;ll be taken to the line and file with the error.<p>For POSIX-type stuff, e.g., Emacs:<p><pre><code>    #define Break() asm volatile (&quot;int $3\n&quot;) &#x2F;* if you get a build error here, your computer is shit. *&#x2F;\
                                              &#x2F;* Throw it away, and buy a proper one, with an x64 CPU. *&#x2F;
    #define LINE_PREFIX &quot;:&quot;
    #define LINE_SUFFIX &quot;&quot;
</code></pre>
And make Print print to stderr. I typically run my tests as part of the build process, invoked via M-x compile, and Emacs then picks up the FILE:LINE: markup when you use M-x next-error and M-x previous-error.<p>(If you use vim, or whatever... I&#x27;m sorry. But lesser editors often understand the standard FILE:LINE: markup, so even when you&#x27;re slumming it, you&#x27;re not, like, <i>totally</i> slumming it.)<p>You can probably figure out how this extends from int equals int to int not equals int, int less than int, blah blah... and from there to string vs string, and double vs double, and so on. It&#x27;s not really the operations that are the key thing, because of course they&#x27;re easy to add; for my money the benefit comes from auto-generating the output text from the condition, and from having it integrate tidily with the calling process - Visual Studio, Emacs, other (notepad, vim, etc.) - in such a way that you can jump immediately to the source location when a test fails.<p>Where the article and I <i>do</i> agree: the conclusion, all of it. Writing a unit test system isn&#x27;t hard, and writing your own isn&#x27;t crazy. It probably won&#x27;t take you even one hour.<p>P.S. if you like C++, you can go one better, and have the output text auto-generated without even having to say what sort of comparison it is: <a href="https:&#x2F;&#x2F;github.com&#x2F;philsquared&#x2F;Catch" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;philsquared&#x2F;Catch</a><p>Only downside is that while you&#x27;ll save one hour not writing your own, you&#x27;ll lose ten hours waiting for it to compile. Also, good luck trying to write test programs that have command line options. This is what I believe they call &quot;technical debt&quot;.