Grex Jellyware Conference

Item 37: The Testing item

Entered by cross on Sun Sep 17 00:17:46 2006:

1 new of 4 responses total.


#3 of 4 by cross on Tue Sep 19 03:42:20 2006:

You could do that.  It needn't be so elaborate.  I found and modified
a minimal testing suite for C once that was about six lines of preprocessor
macros, and I started using it for grexsoft.  Here's my current "myunit.h"
that I use for testing C code under Unix:

/*
 * My own simple unit-testing framework.  A simplified version
 * of the minunit used on grex.
 */

#include <stdio.h>

extern int myu_ntests;
extern int myu_nfailed;

#define myuinit() \
        do { \
                myu_ntests = 0; \
                myu_nfailed = 0; \
        } while (0)

#define myuassert(test, ...) \
        do { \
                int r = (test); \
                if (!r) {  \
                        (void)fprintf(stderr, "ERROR: "); \
                        (void)fprintf(stderr, __VA_ARGS__); \
                        (void)fprintf(stderr, "\n"); \
                        return (!r); \
                } \
        } while (0)

#define myuruntest(test, ...) \
        do { \
                int r = test(__VA_ARGS__); \
                myu_ntests++; \
                if (r) myu_nfailed++; \
        } while (0)

#define myurunsuite(test) \
        do { \
                test(); \
        } while (0)

#define myureport() \
        do { \
                (void)printf("Tests run: %d, failed: %d (%2.2f%%).\n", \
                    myu_ntests, myu_nfailed, \
                   (float)myu_nfailed / (float)myu_ntests * 100.0); \
        } while (0)


Here's an example of its usage:

/*
 *  Test bit vector code.
 *
 *      $Id: bitvec_test.c,v 1.2 2005/06/03 19:22:46 cross Exp $
 *
 *  Dan Cross <cross@math.psu.edu>
 */

#include "bitvec.h"
#include "myunit.h"
int myu_ntests, myu_nfailed;

int
test_bv_get(BITVEC_T *bp, int pos, int expected)
{
        myuassert(bv_get(bp, pos) == expected, "bv_get(bp, %d) != %d", pos,
expe
cted);
        return 0;
}

int
main(void)
{
        BITVEC_T bv;

        myuinit();

        bv_init(&bv, 12);
        bv_free(&bv);

        bv_init(&bv, 33);
        bv_free(&bv);

        bv_init(&bv, 32);

        bv_set(&bv, 0);
        bv_set(&bv, 1);
        bv_set(&bv, 2);

        myuruntest(test_bv_get, &bv, 0, 1);
        myuruntest(test_bv_get, &bv, 1, 1);
        myuruntest(test_bv_get, &bv, 2, 1);
        myuruntest(test_bv_get, &bv, 7, 0);
        myuruntest(test_bv_get, &bv, 8, 0);

        bv_clr(&bv, 2);
        myuruntest(test_bv_get, &bv, 0, 1);
        myuruntest(test_bv_get, &bv, 1, 1);
        myuruntest(test_bv_get, &bv, 2, 0);
        myuruntest(test_bv_get, &bv, 64, -1);

        bv_free(&bv);

        myureport();

        return(0);
}

I used to do all of this via printf() statements combined with grep or
just eyesight, but I find this much better.  I've also used CppUnit,
check, jUnit, and a few others to good effect.

The extreme programming people have a lot of experience with this sort
of thing:

http://www.xprogramming.com/


There are no more items selected.

You have several choices: