Test::Numeric - Testing utilities for numbers.
use Test::Numeric tests => 8; # The following functions are all exported by Test::Numeric
is_number '12.34e56', "valid number"; is_number '-12.34E56', "valid number"; isnt_number 'test', "not a number"; is_even 2, "an even number"; is_odd 3, "an odd number"; is_integer '123', 'an integer'; isnt_integer '123.45', 'not an integer'; is_formatted '1-.2', '123.45'; isnt_formatted '1-.2', '123.4';
This is a simple testing module that lets you do several tests on numbers. You can check that it is a number, check that it is an integer, check if they are odd or even and finally check if they are of a certain form.
is_number $number, $name;
is_number
tests whether $number
is a number. The number can be positive or negative, it can have a formatted point and an exponent. These are all valid numbers: 1, 23, 0.34, .34, -12.34e56
The opposite of is_number
.
is_integer $number, $name;
is_integer
tests if $number
is an integer, ie a whole number. Fails if the number is not a number r not a number at all.
The opposite of is_integer
. Note that isnt_integer
will fail if the number is not a number. So 'abc' may not be an integer but isnt_integer
will still fail.
is_even $number, $name;
is_even
tests if the number given is even. Fails for non-integers. Zero is even.
As is_even
, but for odd numbers.
is_formatted $format, $number, $name;
is_formatted
allows you to test that the number complies with a certain format. $format
tells the function what to check for and is of the form pre.suf
where pre
and suf
are the number of digits before and after the decimal point. They are either just a number ( eg. '3.2' for something like 123.12 ) or a range ( eg. '3.1-2' ) for either 123.1 or 123.12 ).
The range can be open-ended, for example '0-.2' will match any number of digits before the decimal place, and exactly two after.
If the format is incorrect then the test will fail and a warning printed.
This test is intended for things such as id numbers where the number must be something like 000123
.
The same as is_formatted but negated.
is_money $number, $name;
This is a conveniance function to test if the value looks like money, ie has a format of 0-.2
- which is tw decimal points. Internally it just calls is_formatted with the correct format.
The opposite of is_money
.
Edmund von der Burg <evdb@ecclestoad.co.uk>
Bug reports, patches, suggestions etc are all welcomed.
Copyright 2004 by Edmund von der Burg <evdb@ecclestoad.co.uk>.
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
Test::Tutorial for testing basics, Test::Builder for the module on which this one is built.