The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Lab::Test -- Shared test routines for Lab::Measurement.

SYNOPSIS

 use Lab::Test import => [qw/file_ok compare_ok .../];

 file_ok($filename, "file contents", "contents are equal");

 file_filter_ok($filename, "line 1\nline 2\n", qr/ *$/m,
     "contents equal after removing trailing ws from each line");

 compare_ok($file1, $file2, "files have same contents");

 is_relative_error(10, 11, 0.2, "relative error of 10 and 11 is smaller than 20 percent");
 
 is_num(0.7, 0.7, "numbers are equal");
 
 is_float(1, 1.000000000000001, "floating point numbers are almost equal");
 
 is_absolute_error(10, 11, 2, "absolute error of 10 and 11 is smaller than 2");

 looks_like_number_ok("100e2", "'100e2' looks like a number");

 set_get_test(
     instr => $instr,
     getter => 'get_amplitude',
     setter => 'set_amplitude',
     cache => 'cached_amplitude',
     values => [0.1, 1, 10],
 );

 scpi_set_get_test(
     instr => $instr,
     func => 'sense_sweep_points',
     values => [1, 100, 10000],
 );

DESCRIPTION

Collection of testing routines. This module can be used together with other Test::Builder-based modules like Test::More.

Functions

All functions are exported only on request.

file_ok($file, $expected_contents, $name)

Succeed if $file exists and it's contents are equal to $expected_contents. Uses binary comparison and $expected_contents may not have the unicode flag set.

file_filter_ok($file, $expected_contents, $filter, $name)

Like file_ok but filter the contents of $file with s/$filter//g before comparing with expected_contents.

file_ok_crlf($file, $expected_contents, $name)

Succeed if $file exists and it's contents are equal to $expected_contents. On reading the file, convert CR-LF to LF. Uses binary comparison and $expected_contents may not have the unicode flag set.

Should be only needed to test legacy code. New code should always use binary files, not text files (Set binmode on your handles).

compare_ok($filename1, $filename2, $name)

Succeed if both files exists and their contents are equal.

is_relative_error($got, $expect, $error, $name)

Succeed if the relative error between $got and $expect is smaller or equal than $error. Relative error is defined as abs(($got - $expect) / $expect).

If the absolute value of $got or $expect is smaller than DBL_MIN, that number replaced with DBL_MIN before computing the relative error. This is done to avoid division by zero. Two denormals will always compare equal.

is_num($got, $expect, $name)

Check for $got == $expect. This is unlike Test::More::is, which tests for $got eq $expect.

is_float($got, $expect, $name)

Compare floating point numbers.

Equivalent to is_relative_error($got, $expect, 1e-14, $name).

1e-14 is about 100 times bigger than DBL_EPSILON. The test will succeed even if the numbers are tainted by multiple rounding operations.

is_abs_error($got, $expect, $error, $name)

Similar to is_relative_error, but uses the absolute error.

looks_like_number_ok($number, $name)

Checks if Scalar::Util's looks_like_number returns true for $number.

skip_on_broken_printf

For formatting of floating point numbers perl's printf function relies on the system's printf.

On some platforms, most notably MS-W32, it is not compatible with C99. E.g. you get 1.000000e+001 instead of 1.000000e+01.

This routine skips the test, if a broken printf is detected.

set_get_test

 set_get_test(
     instr => $instr,
     getter => 'get_amplitude',
     setter => 'set_amplitude',
     cache => 'cached_amplitude', # optional
     values => [0.1, 1, 10],
     is_numeric => 1, # this is default
 );
 

Try the setter, getter and cache for each value in values. Check that the getter and cache methods return the correct value. For non-numeric string values, set is_numeric to 0.

scpi_set_get_test

 scpi_set_get_test(
     instr => $instr,
     func => 'sense_sweep_points',
     values => [1, 100, 10000],
     is_numeric => 1, # this is default
 );

Like set_get_test, but assume that the getter, setter and cache are called "${func}_query", $func and "cached_$func" respectively.