[ANNOUNCE] Test::MockRandom 0.92

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
    Test::MockRandom - Replaces random number generation with non-random
    number generation

      # functional
      use Test::MockRandom;
      if ( rand() == 0.5 ) { print "good guess!" };
      # object-oriented
      use Test::MockRandom ();
      my $nrng = Test::MockRandom->new(0.42);
      $nrng->rand(); # returns 0.42
      # override rand in another package
      use Test::MockRandom 'Some::Other::Package';
      use Some::Other::Package; # contains sub foo { return rand }
      Some::Other::Package::foo; # returns 0.13
      # using a seed list and "oneish"
      srand(0.23, 0.34, oneish() );
      rand(); # returns 0.23
      rand(); # returns 0.34
      rand(); # returns a number just barely less than one
      rand(); # returns 0, as the seed array is empty
    This perhaps ridiculous-seeming module was created to test routines that
    manipulate random numbers by providing a known output from "rand". Given
    a list of seeds with "srand", it will return each in turn. After seeded
    random numbers are exhausted, it will always return 0. Seed numbers must
    be of a form that meets the expected output from "rand" as called with
    no arguments -- i.e. they must be between 0 (inclusive) and 1
    (exclusive). In order to facilitate generating and testing a nearly-one
    number, this module exports the function "oneish", which returns a
    number just fractionally less than one.

    Depending on how this module is called with "use", it will export "rand"
    either to the current package or to another specified package (e.g. a
    class being tested) or even globally. This module also includes the
    function "export_rand_to" which can be used to explictly override rand
    in another package after "use" has been called. See "USAGE" for details.

    Alternatively, this module can be used to generate objects, with each
    object maintaining its own distinct seed array.

  Overriding "rand" in the current package
    To override "rand" in the current package, simply "use" the module as

     use Test::MockRandom;

    This imports "rand" and "srand" into the current namespace, masking any
    such calls from reaching the built-in functions. It also imports
    "oneish", and "export_rand_to".

  Overriding "rand" in a different package with "use"
    There are two ways to override "rand" in different package. The simplest
    is to provide the name(s) of the package to be overridden in the "use"
    statement. This will export "rand" to the listed packages and will
    export "srand", "oneish", and "export_rand_to" to the current package.
    You must "use" Test::MockRandom before you "use" the target package.
    This is a typical case for testing a module that uses random numbers:

     use Test::More;
     use Test::MockRandom qw( Some::Package );
     BEGIN { use_ok( Some::Package ) }
     # assume sub foo { return rand } in Some::Package
     Some::Package::foo() # returns 0.5

    If you wish to export "rand" to both another package and the current
    package, simply include the current package in the list provided to
    "use". All of the following idioms work.

     use Test::MockRandom qw( main Some::Package );
     use Test::MockRandom __PACKAGE__, 'Some::Package';

     # The following doesn't interpolate __PACKAGE__ as above, but
     # Test::MockRandom will still DWIM and handle it correctly

     use Test::MockRandom qw( __PACKAGE__ Some::Package );
  Overriding "rand" in a different package explicitly with
    In order to override the built-in "rand" in another package,
    Test::MockRandom must export its own "rand" function before the target
    package is compiled. The simple approach (described above) of providing
    the target package in the "use Test::MockRandom" statement accomplishes
    this because "use" is equivalent to a "require" and "import" within a
    "BEGIN" block. To explicitly override "rand" in another package, you can
    also call "export_rand_to", but it must be enclosed in a "BEGIN" block
    of its own:

     use Test::MockRandom;
     BEGIN { Test::MockRandom::export_rand_to( 'AnotherPackage' ); }
     use AnotherPackage;
    This "BEGIN" block must not include a "use" statement for the package to
    be overridden, or perl will compile the package to be overridden before
    the "export_rand_to" function has a chance to execute and override the
    system "rand". This is very important in testing. The "export_rand_to"
    call must be in a separate "BEGIN" block from a "use_ok" test, which
    should be enclosed in a "BEGIN" block of its own:

     use Test::MockRandom;
     BEGIN { Test::MockRandom::export_rand_to( 'AnotherPackage' ); }
     BEGIN { use_ok( 'AnotherPackage' ); }

    Given these cautions, it's probably best to use the simple approach with
    "use", which does the right thing in most circumstances.

  Overriding "rand" globally
    This is just like overriding "rand" in a package, except that you
    override it in "CORE::GLOBAL".

     use Test::MockRandom 'CORE::GLOBAL';
     # or

     BEGIN { Test::MockRandom::export_rand_to('CORE::GLOBAL') }

    You can always access the real built-in "rand" by calling it explicitly
    as "CORE::rand".

  Overriding "rand" in a package that also contains a "rand" function
    This is tricky as the order in which the symbol table is manipulated
    will lead to very different results. This can be done safely (maybe) if
    the module uses the same rand syntax/prototype as the system call. In
    this case, you will need to do an explicit override (as above) but do it
    after importing the package. I.e.:

     use Test::MockRandom;
     use SomeRandPackage;
     BEGIN { Test::MockRandom::export_rand_to('SomeRandPackage');

    The first line is mostly to get the right exporting of auxilliary
    function to the current package. The second line will define a "sub
    rand" in "SomeRandPackage", overriding the results of the first line.
    The third line then re-overrides the "rand". You may see warnings about
    "rand" being redefined.

    Depending on how your "rand" is written and used, there is a good
    likelihood that this isn't going to do what you're expecting, no matter
    what. If your package that defines "rand" relies upon the system
    "CORE::GLOBAL::rand", then you may be best off overriding that instead.

     $obj = new( LIST OF SEEDS );

    Returns a new Test::MockRandom object with the specified list of seeds.

     srand( LIST OF SEEDS );
     $obj->srand( LIST OF SEEDS);

    If called as a bare function call or package method, sets the seed list
    for bare/package calls to "rand". If called as an object method, sets
    the seed list for that object.

     $rv = rand();
     $rv = $obj->rand();
     $rv = rand(3);

    If called as a bare or package function, returns the next value from the
    package seed list. If called as an object method, returns the next value
    from the object seed list.

    If "rand" is called with a numeric argument, it follows the same
    behavior as the built-in function -- it multiplies the argument with the
    next value from the seed array (resulting in a random fractional value
    between 0 and the argument, just like the built-in). If the argument is
    0, undef, or non-numeric, it is treated as if the argument is 1.

    Using this with an argument in testing may be complicated, as limits in
    floating point precision mean that direct numeric comparisons are not
    reliable. E.g.

     rand(3);       # does this return 1.0 or .999999999 etc.

     srand( oneish() );
     if ( rand() == oneish() ) { print "It's almost one." };

    A utility function to return a nearly-one value. Equal to ( 2^32 - 1 ) /
    2^32. Useful in "srand" and test functions.

     export_rand_to( 'Some::Other::Package' );

    This function exports "rand" into another package namespace. This is
    useful in testing object which call "rand". E.g.,

     package Some::Class;
     sub foo { print rand(); }

     package main;
     use Test::MockRandom;
     export_rand_to( 'Some::Class' );
     Some::Class::foo();   # prints "0.5"
    Note that this uses the Test::MockRandom package globals, not class
    objects. So a call to "srand" in the main package still affects the
    results of "rand" called in "Some::Class".

    The effect of this function is highly dependent on when it is called in
    the compile cycle. See "USAGE" for important details and warnings.

    Please report bugs using the CPAN Request Tracker at


     David A. Golden (DAGOLDEN)
     http://dagolden.com /

    Copyright (c) 2004 by David A. Golden

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

    The full text of the license can be found in the LICENSE file included
    with this module.


Re: [ANNOUNCE] Test::MockRandom 0.92

>     Test::MockRandom - Replaces random number generation with non-random
>     number generation

ultra-hip. now if there were only some way to get hashes to produce
their elements in a fixed order... usually sorting them is adequate
for testing.

Re: [ANNOUNCE] Test::MockRandom 0.92

But if you are still paranoid, you can substitute pork butt.

5 lb. lean chuck roast
3 lb. prime baby butt
2 tablespoons each:
   black, white and cayenne peppers
   celery salt
   garlic powder
   parsley flakes
   brown sugar
1 teaspoon sage
2 onions
6 cloves garlic
bunch green onions, chopped

Cut the children?s butts and the beef roast into pieces
   that will fit in the grinder.
Run the meat through using a 3/16 grinding plate.
Add garlic, onions and seasoning then mix well.
Add just enough water for a smooth consistency, then mix again.
Form the sausage mixture into patties or stuff into natural casings.

Stillborn Stew

By definition, this meat cannot be had altogether fresh,
but have the lifeless unfortunate available immediately after delivery,
or use high quality beef or pork roasts (it is cheaper and better to
cut up a whole roast than to buy stew meat).

1 stillbirth, de-boned and cubed
cup vegetable oil
2 large onions
bell pepper
cup red wine
3 Irish potatoes
2 large carrots

This is a simple classic stew that makes natural gravy,
thus it does not have to be thickened.
Brown the meat quickly in very hot oil, remove and set aside.
Brown the onions, celery, pepper and garlic.
De-glaze with wine, return meat to the pan and season well.
Stew on low fire adding small amounts of water and
   seasoning as necessary.
After at least half an hour, add the carrots and potatoes,
   and simmer till root vegetables break with a fork.
Cook a fresh pot of long grained white rice.

Pre-mie Pot Pie

When working with prematurely delivered newborns (or chicken) use sherry;
red wine with beef (buy steak or roast, do not pre-boil).

Pie crust (see index)
Whole fresh pre-mie; eviscerated, head, hands and feet removed
Onions, bell pepper, celery
cup wine
Root vegetables of choice (turnips

Re: [ANNOUNCE] Test::MockRandom 0.92

well. Meat is not necessary every day, don?t
be afraid to alter any dish to vegetarian tastes.

1 premature baby, born dead
Large bunch of mustard greens
2 white onions, 1 cup chopped celery
Vegetable oil (or hog fat)
Salt, pepper, garlic, etc.

Lightly brown onions, celery, garlic and meat in large heavy pot.
Add a little water and the greens (which should be thoroughly cleaned and
Smother slowly for at least 2 hours, adding small amounts of water
   when it starts to stick.
Stir frequently.
When ready - serve with rice, grilled smoked sausage, green salad, and iced tea.
Coffee and apple pie then brandy.

Maternity Ward Pot Luck Dinner

If you can?t get anything fresh from the hospital, nursery, or morgue;
you can at least get rid of all the leftovers in your refrigerator.

1 - 2 lbs. cubed meat (human flesh, chicken, turkey, beef...)
1 -2 lbs. coarsely chopped vegetables
   (carrots, potatoes, turnips, cauliflower, cabbage...)
Bell pepper
salt pepper, etc.  
Olive oil

Brown the meat and some chopped onions, p

Site Timeline