Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Unit testing of C source code

by Satish@Stag (Novice)
on Sep 28, 2007 at 10:46 UTC ( [id://641503]=perlquestion: print w/replies, xml ) Need Help??

Satish@Stag has asked for the wisdom of the Perl Monks concerning the following question:

I would like to test a module (collection of functions performing a task) of a protocol stack written in C. Can anyone plz let me know how can I test this C code using Perl scripts?. Also I would like to know whether any interface tool is required in between C code and Perl scripts. Thanks

Replies are listed 'Best First'.
Re: Unit testing of C source code
by Corion (Patriarch) on Sep 28, 2007 at 11:04 UTC

    I would approach the problem by creating an external C program that does little more than wrap your API. It would take the parameters from the command line or STDIN, call the API function and output the result(s) to STDOUT.

    If you have this program, make sure that it can be built automatically.

    Then, testing the API is very simple - you compile the program and then run tests using Test::Simple or Test::Base that call the external program and verify its output against the known output.

    This approach is the easiest approach because you don't need to learn new tools besides the Perl test tools. If you have time/resources to learn new tools, wrapping the library using Inline::C or XS and then calling it directly from Perl would give you faster tests and more interactivity with the library - for example if that library has a concept of "handles", you might need this.

Re: Unit testing of C source code
by theorbtwo (Prior) on Sep 28, 2007 at 11:04 UTC

    Perl's testing framework doesn't actually require in any way that the thing being tested is perl. There's a reasonably well-defined protocol that governs what the test outputs, all you need is some code to output that, and it doesn't matter the code that outputs it is perl, C, python, or brainf*ck. There's a fairly good article on the Test Anything Protocol on WP, or just run any existing test with perl -Mblib t/ and observe what it outputs.

    Warning: Do not use without understanding. All copyrights are relinquished into the public domain unless otherwise stated. I am not an angel. I am capable of error, and err on a fairly regular basis. If I made a mistake, please let me know (such as by replying to this node).

      Yeah, but it is also quite easy to implement a Test::More like testsuite in C with libtab ( This library is pretty small, so you can ship it with your program. I just added a 't' directory in my autoconf based project and put the libtab header and sources there. Tests look pretty much the same as in Perl:
      # t/simple_tests.c #include <stdio.h> #include "tap.h" #include "something.h" int main(void) { plan_tests(1); /* * A simple example of a test. */ ok(1 + 1 == 2, "Basic arithmetic works"); return exit_status(); /* Return the correct exit code */ }
      A simple t/ could look like this:
      # binaries check_PROGRAMS = simple_tests simple_tests_SOURCES = simple_tests.c tap.c ../src/something.c\ tap.h ../src/something.h simple_tests_CFLAGS = -Wall --pedantic -std=c99 -I ../src TESTS = simple_tests
      Then add 't' in your main
      SUBDIRS = src t
      Then you can run the tests with 'make check'.
      After learning the wonders of creating a CPAN module package, I'd highly consider using it to manage the building and testing of even non-Perl projects. I have not had to do this yet, but when I do it will be the first thing I try for sure.
Re: Unit testing of C source code
by jettero (Monsignor) on Sep 28, 2007 at 10:52 UTC

    Inline::C, perlxs, perlguts, perlapi...

    I think if your code is mainly C there are probably better tools for testing it. But if you're mixing perl and C then you can use any of the test modules. There's a lot of choices. I usually use regular old Test, but a lot of people like Test::More and there are a million other things.


Re: Unit testing of C source code
by syphilis (Archbishop) on Sep 28, 2007 at 10:53 UTC

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://641503]
Approved by andreas1234567
Front-paged by bart
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (5)
As of 2024-05-22 05:18 GMT
Find Nodes?
    Voting Booth?

    No recent polls found