cmp_ok( this, op, that, name);
Sometimes an eq by is() or a regular expression by like() just isn't good enough. For example what if you would like to check the rand() function of perl? Its result must be between 0 (inclusive) and 1 (non inclusive).
In other case you might have a function that should happen within a certain period of time. You don't have an exact expectation but you know the elapsed time must be between a lower and upper limit.
cmp_ok compares with any operator you like.
use strict; use warnings; use Test::More tests => 2; use MyTools qw(wait_for_input_with_timeout); my $start = time; wait_for_input_with_timeout(3); my $end = time; cmp_ok $end - $start, ">=", 2, "process was waiting at least 2 secs"; cmp_ok $end - $start, "<=", 3, "process was waiting at most 3 secs";
1..2 ok 1 - process was waiting at least 2 secs ok 2 - process was waiting at most 3 secs
1..2 not ok 1 - process was waiting at least 2 secs # Failed test 'process was waiting at least 2 secs' # at t/cmp_ok.t line 11. # '0' # >= # '2' ok 2 - process was waiting at most 3 secs # Looks like you failed 1 test of 2.
- Actually this is a really bad test as it can fail randomnly