Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL
Help

9. Testing Everything Else > Testing Shared Libraries

Testing Shared Libraries

Here’s a secret: Perl’s testing modules aren’t just good for testing Perl. They can test anything you can call from Perl. With a little bit of help from a few other modules, it’s easy to test shared libraries—compiled C code, for example—as if it were normal Perl code.

Note

You must have the Inline::C module installed and you must have a C compiler available and configured.

How do I do that?

Suppose that you want to test your C math library, libm. Specifically, you need to exercise the behavior of the fmax() and fmin() functions, which find the maximum or minimum of two floating point values, respectively. Save the following code as test_libmath.t:

    #!perl
    
    BEGIN
    {
            chdir 't' if -d 't';
    }
    
    use strict;
    use warnings;
    use Test::More tests => 6;
    
    use Inline C =>
            Config   =>
                    LIBS   => '-lm',
                    ENABLE => 'AUTOWRAP'
    ;
    
    Inline->import( C => <<END_HEADERS );
            double fmax( double, double );
            double fmin( double, double );
    END_HEADERS
    
    is( fmax(  1.0,  2.0 ),  2.0, 'fmax() should find maximum of two values'  );
    is( fmax( -1.0,  1.0 ),  1.0, '... and should handle one negative'        );
    is( fmax( -1.0, -7.0 ), -1.0, '... or two negatives'                     );
    is( fmin(  9.3,  1.7 ),  1.7, 'fmin() should find minimum of two values' );....

  

You are currently reading a PREVIEW of this book.

                                                                                                                    

Get instant access to over $1 million worth of books and videos.

  

Start a Free 10-Day Trial


  
  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint