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


Test::RandomResults - Test non-deterministic functions


This module aims to provide ways of testing functions that are meant to return results that are random; that is, non-deterministic functions.

Some of the tests provided here might be easily achieved with other testing modules. The reason why they're here is that this way users become aware of how to test their non-deterministic functions.


This is a work in progress. Comments are welcome.


  use Test::More plan => $Num_Tests;
  use Test::RandomResults;

  is_in( my_function, [ $list, $of, $items ], "result is inside list" );

  in_between( my_function, sub { $_[0] cmp $_[1] }, 1, 10, "result between 1 and 10");

  length_lt( my_function, $limit, "length less than $limit");

  length_le( my_function, $limit, "length less or equal to $limit");

  length_eq( my_function, $limit, "length equal to $limit");

  length_ge( my_function, $limit, "length greater of equal to $limit");

  length_gt( my_function, $limit, "length greater than $limit");


Whenever Test::RandomResults is invoked, a new seed is generated and outputed as diagnostics. This is done so that you can use it to debug your code, if needed.



Tests if an element belongs to an array.

  is_in( my_function, [1, 2, 3], 'either 1, 2 or 3');


Tests if an element is within two boundaries.

The second parameter to this function is what it uses to do the comparisons.

To compare strings:

  in_between( my_function, { $_[0] cmp $_[1] }, "aaa", "zzz",
              'result is between "aaa" and "zzz"' );

To compare numbers:

  in_between( my_function, { $_[0] <=> $_[1] }, 1, 10, 'result is between 1 and 10' );

To compare something else:

  in_between( my_function, &your_function_here, $lower_boundary, $upper_boundary,
              'result is between boundaries' );

As you can see, the function should use $_[0] and $_[1] to do the comparison. As with <=> and cmp, the function should return 1, 0 or -1 depending on whether the first argument ($_[0]) is greater, equal to, or less than the second one ($_[1]).

in_between swaps the lower and upper limits, if need be (this means that checking whether a value is between 1 and 10 is the same as checking between 10 and 1).


Tests if length is less than a limit.

  length_lt( my_function, $limit, "length less than $limit");


Tests if length is less or equal to a limit.

  length_le( my_function, $limit, "length less or equal to $limit");


Tests if length is equal to a limit.

  length_eq( my_function, $limit, "length equal to $limit");


Tests if length is greater of equal to a limit.

  length_ge( my_function, $limit, "length greater of equal to $limit");


Tests if length is greater than a limit.

  length_gt( my_function, $limit, "length greater than $limit");


* Check if N results of a function are evenly_distributed

* Allow the user to choose the seed when invoking Test::RandomResults


Jose Castro, <>


Please report any bugs or feature requests to, or through the web interface at I will be notified, and then you'll automatically be notified of progress on your bug as I make changes.


Copyright 2005 Jose Castro, All Rights Reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.