Synchro Test Suite

Posted on
C github lychrel number unit test

I use this mainly for unit testing my C programs. Since most of the programs were targetted for embedded device, it’s a bit tricky to test, but at least this small suite forced me to separate the modules and make sure they’re all decoupled

Most of the main features were actually macros. Stored at test_fwx.h, the source is self-documented, just take a read to figure out how to use the assertions. And then I was inspired by Python doctest, a tool to execute unit test written at docstring in the source code. It was actually came from Peter Norvig’s So I decide to create similar tool, written in C, I used bison and flex to generate the parser, which I call chouchou.

Here be example on how to use chouchou:
First, make sure you already have bison and flex installed, then compile chouchou with gcc, just type make and you’ll set.

I’ll start with this C source file

``` c lychrel.c //this file will be named lychrel.c //It’s not actually finding lychrel number //but you’ve got the idea. //

#include #include #include #include “lychrel.h”

int do_reverse(int number) { char m[11]; int len, i, j;

if (!sprintf(m, "%d", number))
    return 0;

len = strlen(m);

for(i = 0, j = len - 1; i < j; i++, j--) {
    char c = m[i];
    m[i] = m[j];
    m[j] = c;

return atoi(m);


int is_palindrom(int num) { int test = do_reverse(num); if (test == num) return 1; return 0; }

long do_addition(int num) { return num + do_reverse(num); }

Then I will use this header file

``` c lychrel.h  
#ifndef _H_LYCHREL
#define _H_LYCHREL

#include "test_fwx.h"

 * Reverse the number
 * #! do_reverse(12345) .== 54321
int do_reverse(int number);

 * Check whether input is palindrom
 * #! is_palindrom(12345) .== 0 
 * #! is_palindrom(43034) .== 1
int is_palindrom(int number);

 * Perform addition between current input
 * and its reverse
 * #= fprintf(stderr, "do_addition(12345) = ");
 * #= fprintf(stderr, "12345 + 54321= ");
 * #= fprintf(stderr, "%ld\n", do_addition(12345));
long do_addition(int number);


From the sources above, I can generate test case by calling chouchou like this

I’ll presume chouchou’s location was at the same directory with the rest of source codes
$ ./chouchou lychrel.h > lychrel_test.c
then compile the test file
$ gcc -o lychrel_test lychrel.c lychrel_test.c
and run it

$ ./lychrel_test 
TEST0001 : do_reverse(12345) == 54321 : ...PASSED  
TEST0002 : is_palindrom(12345) == 0 : ...PASSED  
TEST0003 : is_palindrom(43034) == 1 : ...PASSED  
do_addition(12345) = 12345 + 54321 = 66666  
3 Tests, 3 success, 0 failed.

While I don’t think it will useful for typical small one file application, I’m pretty sure this will help in prototyping bigger application. And better, the source code will be much more understandable, and easier to maintain.