Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Unit testing of C source code

by Satish@Stag (Novice)
on Sep 28, 2007 at 10:46 UTC ( #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

Comment on Unit testing of C source code
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.

    -Paul

Re: Unit testing of C source code
by syphilis (Canon) on Sep 28, 2007 at 10:53 UTC
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/foo.pl 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 (http://jc.ngo.org.uk/trac-bin/trac.cgi/wiki/LibTap). 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/Makefile.am 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 Makefile.am:
      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 Corion (Pope) 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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2014-10-21 07:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (98 votes), past polls