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

NAME

Test::Approximate -- compare two number for approximate equality, deeply

SYNOPSIS

    use Test::Approximate;

    is_approx(1, 1.0001, 'approximate equal', '1%');
    is_approx(0.0001001, '1e-04', 'str vs num', '1%');
    is_approx(1000, 1000.01, 'absolute tolerance', '0.1');


    use Test::Deep;
    use Test::Approximate;

    $got = [ 1.00001, 2, 3, 4 ];
    $expect = [ 1, 2, 3, 4 ];
    cmp_deeply($got, approx($expect, '1%'), 'array');

    $got = { a => 1, b => 1e-3, c => [ 1.1, 2.5, 5, 1e-9 ] };
    $expect = { a => 1.0001, b => 1e-03, c => [ 1.1, 2.5, 5, 1.00001e-9 ] };
    cmp_deeply( $got, approx($expect, '0.01%'), 'hash mix array');

DESCRIPTION

This module can test two scalar string or number numberic approximate equal, and deeply test two array or hash or array of hash etc.

There is already a nice module do this -- Test::Approx. I wrote this one because Test::Approx can't do a deeply test, and I have not found a module do the same thing.

FUNCTIONS

is_approx($got, $expected, [$msg, $tolerance])

Test $got and $expected 's difference.

This function is partly borrowed from Test::Approx, without the string Levenshtein difference. Only do a numeric difference; If you compare two string, the test will pass only when the two string is equal.

$test_name defaults to 'got' =~ 'expected'

$tolerance is used to determine how different the scalars can be, it defaults to 1%. It can also be set as a number representing a threshold. To determine which:

  $tolerance = '6%'; # threshold = calculated at 6%
  $tolerance = 0.06; # threshold = 0.06
approx($aoh, $tolerance)

This function is used to do a deelpy approximate test, with Test::Deep

    cmp_deeply($got, approx($expected, '1%'), 'test msg')

This will do a approximate compare every element of an array, and every value of a hash with the given tolerance, If the data is an complicate structure like hash of array , array of hash etc, it will walk all the element , and do a deep compare as you wish.

It is useful when you want do a deep approximate compare with a big data.

EXPORTS

is_approx, approx

AUTHOR

tadegenban <tadegenban@gmail.com>

COPYRIGHT

Copyright (c) 2014 tadegenban. Released under the same terms as Perl itself.

SEE ALSO

Text::Approx, Test::Builder, Test::Deep::Between,