n4nature has asked for the wisdom of the Perl Monks concerning the following question:

Hello friends, I have been learning Perl for past week and a half and have written quite a few snippets from "Learning Perl - by Randal Schwartz". I know C/C++ languages and I have started learning Perl, so that Perl would help me test my C/C++ programs. I would like to have an insight into how I would test a C program for eg. Sorting. If a C program sorts a list, and I want to test that program, how would Perl help me automate the test task? I just want ideas, to head in the right direction. Thank you very much. Nick

Replies are listed 'Best First'.
Re: Unit testing with Perl
by arc_of_descent (Hermit) on May 25, 2009 at 13:38 UTC

    You should start by using the Test::Simple module in your Perl test scripts. This will give you a good idea on what testing in Perl is about. Its simply just evaluating a condition (in your case, whether the list processed by the C program is sorted), and then declaring a pass/fail. To automate the test script, you have to first write the routines which run the C program (via system possibly), redirect its output to file, read the file into an Perl array, etc.

    Next, you can read about Test::More and Test::Deep.

    Here's some sample code:
    use Test::More; use Test::Deep; my @input = (5, 8, 12, 9, 78, 1, 5); my @expected = (1, 5, 5, 8, 9, 12, 78); my $i_file = "input.txt"; my $o_file = "output.txt"; my $bin = '/bin/c_program'; write_sample_file(\@input, $i_file); # run the C program system "$bin < $i_file > $o_file"; my @processed = get_data($o_file); cmp_deeply (\@processed, \@expected, 'results are sorted');
      This is going to help immensely. Thank you.
Re: Unit testing with Perl
by dHarry (Abbot) on May 25, 2009 at 13:41 UTC
Re: Unit testing with Perl
by targetsmart (Curate) on May 25, 2009 at 13:36 UTC
      Thank you. It will help
Re: Unit testing with Perl
by ELISHEVA (Prior) on May 26, 2009 at 07:35 UTC

    You can directly call your C routines from within Perl test scripts using the CPAN module, Inline::C.

    Best, beth