Skip to content
Browse files

import Data-Random 0.04 from CPAN

git-cpan-module:   Data-Random
git-cpan-version:  0.04
git-cpan-authorid: ADEO
git-cpan-file:     authors/id/A/AD/ADEO/Data-Random-0.04.tar.gz
  • Loading branch information...
1 parent ea7fa7d commit 67910fad4fbee9bb0eb5f430314015ef73ad41b7 Adekunle Olonoh committed with schwern Feb 26, 2002
Showing with 807 additions and 661 deletions.
  1. +8 −1 Changes
  2. +4 −4 Makefile.PL
  3. +1 −1 README
  4. +286 −223 Random.pm
  5. +37 −41 Random/WordList.pm
  6. +102 −80 t/rand_chars.t
  7. +68 −50 t/rand_date.t
  8. +94 −62 t/rand_datetime.t
  9. +16 −16 t/rand_enum.t
  10. +11 −9 t/rand_image.t
  11. +89 −75 t/rand_set.t
  12. +42 −48 t/rand_time.t
  13. +49 −51 t/rand_words.t
View
9 Changes
@@ -1,6 +1,13 @@
Revision history for Data::Random.
-0.02 Mon Aug 20 2001
+0.04 Tue Feb 26 2002
+ - removed false dependency on Perl 5.005_62
+ - only load Data::Random::WordList when needed
+ (Thanks to Mark Stosberg)
+ - fixed tests so they skip appropriate tests if Date::Calc or
+ GD are not installed
+
+0.03 Mon Aug 20 2001
- added rand_image()
- used Carp::cluck() and returned undef on errors instead of
View
8 Makefile.PL 100644 → 100755
@@ -1,8 +1,8 @@
use ExtUtils::MakeMaker;
WriteMakefile(
- 'NAME' => 'Data::Random',
- 'VERSION_FROM' => 'Random.pm', # finds $VERSION
- 'PMLIBDIRS' => [ 'Random' ],
- 'PREREQ_PM' => {},
+ 'NAME' => 'Data::Random',
+ 'VERSION_FROM' => 'Random.pm', # finds $VERSION
+ 'PMLIBDIRS' => ['Random'],
+ 'PREREQ_PM' => {},
);
View
2 README
@@ -13,4 +13,4 @@ The module also uses Date::Calc for rand_date() and rand_datetime(), and GD for
--
-Adekunle Olonoh <ade@bottledsoftware.com>
+Adekunle Olonoh <olonoh@yahoo.com>
View
509 Random.pm 100644 → 100755
@@ -4,36 +4,35 @@
# A module used to generate random data.
################################################################################
-
package Data::Random;
-
################################################################################
# - Modules and Libraries
################################################################################
-require 5.005_62;
+#require 5.005_62;
use lib qw(..);
use Carp qw(cluck);
-use Data::Random::WordList;
-require Exporter;
+#use Data::Random::WordList;
+require Exporter;
################################################################################
# - Global Constants and Variables
################################################################################
use vars qw(
- @ISA
- %EXPORT_TAGS
- @EXPORT_OK
- @EXPORT
+ @ISA
+ %EXPORT_TAGS
+ @EXPORT_OK
+ @EXPORT
);
@ISA = qw(Exporter);
%EXPORT_TAGS = (
- 'all' => [ qw(
+ 'all' => [
+ qw(
rand_words
rand_chars
rand_set
@@ -42,437 +41,500 @@ use vars qw(
rand_time
rand_datetime
rand_image
- ) ]
+ )
+ ]
);
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
-@EXPORT = qw();
-
-$Data::Random::VERSION = '0.03';
+@EXPORT = qw();
+$Data::Random::VERSION = '0.04';
################################################################################
# - Subroutines
################################################################################
-
################################################################################
# rand_words()
################################################################################
sub rand_words {
+
# Get the options hash
my %options = @_;
-
+
# Make sure the wordlist param was specified
cluck('a wordlist must be specified') && return if !$options{'wordlist'};
-
+
# Initialize max and min vars
$options{'min'} ||= 1;
$options{'max'} ||= 1;
-
+
# Make sure the max and min vars are OK
- cluck('min value cannot be larger than max value') && return if $options{'min'} > $options{'max'};
- cluck('min value must be a positive integer') && return if $options{'min'} < 0 || $options{'min'} != int($options{'min'});
- cluck('max value must be a positive integer') && return if $options{'max'} < 0 || $options{'max'} != int($options{'max'});
-
+ cluck('min value cannot be larger than max value') && return
+ if $options{'min'} > $options{'max'};
+ cluck('min value must be a positive integer') && return
+ if $options{'min'} < 0 || $options{'min'} != int( $options{'min'} );
+ cluck('max value must be a positive integer') && return
+ if $options{'max'} < 0 || $options{'max'} != int( $options{'max'} );
+
# Initialize the size var
- $options{'size'} ||= int(rand($options{'max'} - $options{'min'} + 1)) + $options{'min'};
-
+ $options{'size'} ||=
+ int( rand( $options{'max'} - $options{'min'} + 1 ) ) + $options{'min'};
+
# Make sure the size var is OK
- cluck('size value must be a positive integer') && return if $options{'size'} < 0 || $options{'size'} != int($options{'size'});
-
+ cluck('size value must be a positive integer') && return
+ if $options{'size'} < 0 || $options{'size'} != int( $options{'size'} );
+
# Initialize the shuffle flag
- $options{'shuffle'} = exists($options{'shuffle'}) ? $options{'shuffle'} : 1;
-
+ $options{'shuffle'} =
+ exists( $options{'shuffle'} ) ? $options{'shuffle'} : 1;
+
my $wl;
my $close_wl = 1;
-
+
# Check for a pre-existing wordlist object
- if (ref($options{'wordlist'})) {
- $wl = $options{'wordlist'};
+ if ( ref( $options{'wordlist'} ) ) {
+ $wl = $options{'wordlist'};
$close_wl = 0;
}
else {
+ require Data::Random::WordList;
+
# Create a new wordlist object
$wl = new Data::Random::WordList( wordlist => $options{'wordlist'} );
}
-
+
# Get the random words
- my $rand_words = $wl->get_words($options{'size'});
-
+ my $rand_words = $wl->get_words( $options{'size'} );
+
# Close the word list
$wl->close() if $close_wl;
-
+
# Shuffle the words around
shuffle($rand_words) if $options{'shuffle'};
- # Return an array or an array reference, depending on the context in which the sub was called
- if (wantarray()) {
+# Return an array or an array reference, depending on the context in which the sub was called
+ if ( wantarray() ) {
return @$rand_words;
}
else {
return $rand_words;
}
}
-
################################################################################
# rand_chars()
################################################################################
sub rand_chars {
+
# Get the options hash
my %options = @_;
-
+
# Build named character sets if one wasn't supplied
- if (ref($options{'set'}) ne 'ARRAY') {
+ if ( ref( $options{'set'} ) ne 'ARRAY' ) {
my @charset = ();
-
- if ($options{'set'} eq 'all') {
- @charset = (0..9, 'a'..'z', 'A'..'Z', '#', ',', qw(~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; ] [ \ `));
+
+ if ( $options{'set'} eq 'all' ) {
+ @charset =
+ ( 0 .. 9, 'a' .. 'z', 'A' .. 'Z', '#', ',',
+ qw(~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; ] [ \ `)
+ );
}
- elsif ($options{'set'} eq 'alpha') {
- @charset = ('a'..'z', 'A'..'Z');
+ elsif ( $options{'set'} eq 'alpha' ) {
+ @charset = ( 'a' .. 'z', 'A' .. 'Z' );
}
- elsif ($options{'set'} eq 'upperalpha') {
- @charset = ('A'..'Z');
+ elsif ( $options{'set'} eq 'upperalpha' ) {
+ @charset = ( 'A' .. 'Z' );
}
- elsif ($options{'set'} eq 'loweralpha') {
- @charset = ('a'..'z');
+ elsif ( $options{'set'} eq 'loweralpha' ) {
+ @charset = ( 'a' .. 'z' );
}
- elsif ($options{'set'} eq 'numeric') {
- @charset = (0..9);
+ elsif ( $options{'set'} eq 'numeric' ) {
+ @charset = ( 0 .. 9 );
}
- elsif ($options{'set'} eq 'alphanumeric') {
- @charset = (0..9, 'a'..'z', 'A'..'Z');
+ elsif ( $options{'set'} eq 'alphanumeric' ) {
+ @charset = ( 0 .. 9, 'a' .. 'z', 'A' .. 'Z' );
}
- elsif ($options{'set'} eq 'misc') {
- @charset = ('#', ',', qw(~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; ] [ \ `));
+ elsif ( $options{'set'} eq 'misc' ) {
+ @charset =
+ ( '#', ',',
+ qw(~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; ] [ \ `)
+ );
}
-
+
$options{'set'} = \@charset;
}
-
+
return rand_set(%options);
}
-
################################################################################
# rand_set()
################################################################################
sub rand_set {
+
# Get the options hash
my %options = @_;
-
+
# Make sure the set array was defined
cluck('set array is not defined') && return if !$options{'set'};
-
- $options{'size'} = 1 unless exists($options{'min'}) || exists($options{'max'}) || exists($options{'size'});
-
+
+ $options{'size'} = 1
+ unless exists( $options{'min'} ) || exists( $options{'max'} )
+ || exists( $options{'size'} );
+
# Initialize max and min vars
$options{'min'} ||= 0;
- $options{'max'} ||= @{$options{'set'}};
-
+ $options{'max'} ||= @{ $options{'set'} };
+
# Make sure the max and min vars are OK
- cluck('min value cannot be larger than max value') && return if $options{'min'} > $options{'max'};
- cluck('min value must be a positive integer') && return if $options{'min'} < 0 || $options{'min'} != int($options{'min'});
- cluck('max value must be a positive integer') && return if $options{'max'} < 0 || $options{'max'} != int($options{'max'});
-
+ cluck('min value cannot be larger than max value') && return
+ if $options{'min'} > $options{'max'};
+ cluck('min value must be a positive integer') && return
+ if $options{'min'} < 0 || $options{'min'} != int( $options{'min'} );
+ cluck('max value must be a positive integer') && return
+ if $options{'max'} < 0 || $options{'max'} != int( $options{'max'} );
+
# Initialize the size var
- $options{'size'} ||= int(rand($options{'max'} - $options{'min'} + 1)) + $options{'min'};
-
+ $options{'size'} ||=
+ int( rand( $options{'max'} - $options{'min'} + 1 ) ) + $options{'min'};
+
# Make sure the size var is OK
- cluck('size value must be a positive integer') && return if $options{'size'} < 0 || $options{'size'} != int($options{'size'});
- cluck('size value exceeds set size') && return if $options{'size'} > @{$options{'set'}};
-
+ cluck('size value must be a positive integer') && return
+ if $options{'size'} < 0 || $options{'size'} != int( $options{'size'} );
+ cluck('size value exceeds set size') && return
+ if $options{'size'} > @{ $options{'set'} };
+
# Initialize the shuffle flag
- $options{'shuffle'} = exists($options{'shuffle'}) ? $options{'shuffle'} : 1;
-
+ $options{'shuffle'} =
+ exists( $options{'shuffle'} ) ? $options{'shuffle'} : 1;
+
# Get the random items
my %results = ();
- for(my $i = 0; $i < $options{'size'}; $i++) {
+ for ( my $i = 0 ; $i < $options{'size'} ; $i++ ) {
my $result;
-
+
do {
- $result = int(rand(@{$options{'set'}}));
- } while (exists($results{$result}));
+ $result = int( rand( @{ $options{'set'} } ) );
+ } while ( exists( $results{$result} ) );
$results{$result} = 1;
}
-
+
my @results = sort { $a <=> $b } keys %results;
-
+
# Shuffle the items
- shuffle(\@results) if $options{'shuffle'};
+ shuffle( \@results ) if $options{'shuffle'};
- # Return an array or an array reference, depending on the context in which the sub was called
- if (wantarray()) {
- return @{$options{'set'}}[@results];
+# Return an array or an array reference, depending on the context in which the sub was called
+ if ( wantarray() ) {
+ return @{ $options{'set'} }[@results];
}
else {
- return \@{$options{'set'}}[@results];
+ return \@{ $options{'set'} }[@results];
}
}
-
################################################################################
# rand_enum()
################################################################################
sub rand_enum {
+
# Get the options hash
my %options = @_;
-
+
# Make sure the set array was defined
cluck('set array is not defined') && return if !$options{'set'};
-
- return $options{'set'}->[int(rand(@{$options{'set'}}))];
-}
+ return $options{'set'}->[ int( rand( @{ $options{'set'} } ) ) ];
+}
################################################################################
# rand_date()
################################################################################
sub rand_date {
+
# Get the options hash
my %options = @_;
-
+
# use the Date::Calc module
eval q{ use Date::Calc };
-
+
cluck($@) && return if $@;
-
- my ($min_year, $min_month, $min_day, $max_year, $max_month, $max_day);
-
+
+ my ( $min_year, $min_month, $min_day, $max_year, $max_month, $max_day );
+
# Get today's date
- my ($year, $month, $day) = Date::Calc::Today();
-
- if ($options{'min'}) {
- if ($options{'min'} eq 'now') {
- ($min_year, $min_month, $min_day) = ($year, $month, $day);
+ my ( $year, $month, $day ) = Date::Calc::Today();
+
+ if ( $options{'min'} ) {
+ if ( $options{'min'} eq 'now' ) {
+ ( $min_year, $min_month, $min_day ) = ( $year, $month, $day );
}
else {
- ($min_year, $min_month, $min_day) = split(/\-/, $options{'min'});
+ ( $min_year, $min_month, $min_day ) =
+ split ( /\-/, $options{'min'} );
}
}
else {
- ($min_year, $min_month, $min_day) = ($year, $month, $day);
+ ( $min_year, $min_month, $min_day ) = ( $year, $month, $day );
}
- if ($options{'max'}) {
- if ($options{'max'} eq 'now') {
- ($max_year, $max_month, $max_day) = ($year, $month, $day);
+ if ( $options{'max'} ) {
+ if ( $options{'max'} eq 'now' ) {
+ ( $max_year, $max_month, $max_day ) = ( $year, $month, $day );
}
else {
- ($max_year, $max_month, $max_day) = split(/\-/, $options{'max'});
+ ( $max_year, $max_month, $max_day ) =
+ split ( /\-/, $options{'max'} );
}
}
else {
- ($max_year, $max_month, $max_day) = Date::Calc::Add_Delta_YMD($min_year, $min_month, $min_day, 1, 0, 0);
- }
-
- my $delta_days = Date::Calc::Delta_Days(
- $min_year, $min_month, $min_day,
- $max_year, $max_month, $max_day,
- );
-
+ ( $max_year, $max_month, $max_day ) =
+ Date::Calc::Add_Delta_YMD( $min_year, $min_month, $min_day, 1, 0, 0 );
+ }
+
+ my $delta_days =
+ Date::Calc::Delta_Days( $min_year, $min_month, $min_day, $max_year,
+ $max_month, $max_day, );
+
cluck('max date is later than min date') && return if $delta_days < 0;
-
- $delta_days = int(rand($delta_days + 1));
-
- ($year, $month, $day) = Date::Calc::Add_Delta_Days($min_year, $min_month, $min_day, $delta_days);
-
- return sprintf("%04u-%02u-%02u", $year, $month, $day);
-}
+ $delta_days = int( rand( $delta_days + 1 ) );
+
+ ( $year, $month, $day ) =
+ Date::Calc::Add_Delta_Days( $min_year, $min_month, $min_day,
+ $delta_days );
+
+ return sprintf( "%04u-%02u-%02u", $year, $month, $day );
+}
################################################################################
# rand_time()
################################################################################
sub rand_time {
+
# Get the options hash
my %options = @_;
-
- my ($min_hour, $min_min, $min_sec, $max_hour, $max_min, $max_sec);
-
- if ($options{'min'}) {
- if ($options{'min'} eq 'now') {
+
+ my ( $min_hour, $min_min, $min_sec, $max_hour, $max_min, $max_sec );
+
+ if ( $options{'min'} ) {
+ if ( $options{'min'} eq 'now' ) {
+
# Get the current time
- my ($hour, $min, $sec) = (localtime())[2, 1, 0];
-
- ($min_hour, $min_min, $min_sec) = ($hour, $min, $sec);
+ my ( $hour, $min, $sec ) = ( localtime() )[ 2, 1, 0 ];
+
+ ( $min_hour, $min_min, $min_sec ) = ( $hour, $min, $sec );
}
else {
- ($min_hour, $min_min, $min_sec) = split(/\:/, $options{'min'});
-
- cluck('minimum time is not in valid time format HH:MM:SS') && return if ($min_hour > 23) || ($min_hour < 0);
- cluck('minimum time is not in valid time format HH:MM:SS') && return if ($min_min > 59) || ($min_min < 0);
- cluck('minimum time is not in valid time format HH:MM:SS') && return if ($min_sec > 59) || ($min_sec < 0);
+ ( $min_hour, $min_min, $min_sec ) = split ( /\:/, $options{'min'} );
+
+ cluck('minimum time is not in valid time format HH:MM:SS') && return
+ if ( $min_hour > 23 ) || ( $min_hour < 0 );
+ cluck('minimum time is not in valid time format HH:MM:SS') && return
+ if ( $min_min > 59 ) || ( $min_min < 0 );
+ cluck('minimum time is not in valid time format HH:MM:SS') && return
+ if ( $min_sec > 59 ) || ( $min_sec < 0 );
}
}
else {
- ($min_hour, $min_min, $min_sec) = (0, 0, 0);
+ ( $min_hour, $min_min, $min_sec ) = ( 0, 0, 0 );
}
- if ($options{'max'}) {
- if ($options{'max'} eq 'now') {
+ if ( $options{'max'} ) {
+ if ( $options{'max'} eq 'now' ) {
+
# Get the current time
- my ($hour, $min, $sec) = (localtime())[2, 1, 0];
-
- ($max_hour, $max_min, $max_sec) = ($hour, $min, $sec);
+ my ( $hour, $min, $sec ) = ( localtime() )[ 2, 1, 0 ];
+
+ ( $max_hour, $max_min, $max_sec ) = ( $hour, $min, $sec );
}
else {
- ($max_hour, $max_min, $max_sec) = split(/\:/, $options{'max'});
-
- cluck('maximum time is not in valid time format HH:MM:SS') && return if ($max_hour > 23) || ($max_hour < 0);
- cluck('maximum time is not in valid time format HH:MM:SS') && return if ($max_min > 59) || ($max_min < 0);
- cluck('maximum time is not in valid time format HH:MM:SS') && return if ($max_sec > 59) || ($max_sec < 0);
+ ( $max_hour, $max_min, $max_sec ) = split ( /\:/, $options{'max'} );
+
+ cluck('maximum time is not in valid time format HH:MM:SS') && return
+ if ( $max_hour > 23 ) || ( $max_hour < 0 );
+ cluck('maximum time is not in valid time format HH:MM:SS') && return
+ if ( $max_min > 59 ) || ( $max_min < 0 );
+ cluck('maximum time is not in valid time format HH:MM:SS') && return
+ if ( $max_sec > 59 ) || ( $max_sec < 0 );
}
}
else {
- ($max_hour, $max_min, $max_sec) = (23, 59, 59);
+ ( $max_hour, $max_min, $max_sec ) = ( 23, 59, 59 );
}
-
+
my $min_secs = $min_hour * 3600 + $min_min * 60 + $min_sec;
- my $max_secs = ($max_hour * 3600) + ($max_min * 60) + $max_sec;
-
+ my $max_secs = ( $max_hour * 3600 ) + ( $max_min * 60 ) + $max_sec;
+
my $delta_secs = $max_secs - $min_secs;
-
+
cluck('min time is later than max time') && return if $delta_secs < 0;
-
- $delta_secs = int(rand($delta_secs + 1));
-
+
+ $delta_secs = int( rand( $delta_secs + 1 ) );
+
my $result_secs = $min_secs + $delta_secs;
- my $hour = int($result_secs / 3600);
- my $min = int(($result_secs - ($hour * 3600)) / 60);
- my $sec = $result_secs % 60;
-
- return sprintf("%02u:%02u:%02u", $hour, $min, $sec);
-}
+ my $hour = int( $result_secs / 3600 );
+ my $min = int( ( $result_secs - ( $hour * 3600 ) ) / 60 );
+ my $sec = $result_secs % 60;
+ return sprintf( "%02u:%02u:%02u", $hour, $min, $sec );
+}
################################################################################
# rand_datetime()
################################################################################
sub rand_datetime {
+
# Get the options hash
my %options = @_;
-
+
# use the Date::Calc module
eval q{ use Date::Calc };
-
+
cluck($@) && return if $@;
-
- my ($min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec, $max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec);
-
+
+ my (
+ $min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec,
+ $max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec
+ );
+
# Get today's date
- my ($year, $month, $day, $hour, $min, $sec) = Date::Calc::Today_and_Now();
-
- if ($options{'min'}) {
- if ($options{'min'} eq 'now') {
- ($min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec) = ($year, $month, $day, $hour, $min, $sec);
+ my ( $year, $month, $day, $hour, $min, $sec ) = Date::Calc::Today_and_Now();
+
+ if ( $options{'min'} ) {
+ if ( $options{'min'} eq 'now' ) {
+ ( $min_year, $min_month, $min_day,
+ $min_hour, $min_min, $min_sec )
+ = ( $year, $month, $day, $hour, $min, $sec );
}
else {
- ($min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec) = $options{'min'} =~ /^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/;
+ ( $min_year, $min_month, $min_day,
+ $min_hour, $min_min, $min_sec )
+ = $options{'min'} =~ /^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/;
}
}
else {
- ($min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec) = ($year, $month, $day, 0, 0, 0);
+ ( $min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec ) =
+ ( $year, $month, $day, 0, 0, 0 );
}
- if ($options{'max'}) {
- if ($options{'max'} eq 'now') {
- ($max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec) = ($year, $month, $day, $hour, $min, $sec);
+ if ( $options{'max'} ) {
+ if ( $options{'max'} eq 'now' ) {
+ ( $max_year, $max_month, $max_day,
+ $max_hour, $max_min, $max_sec )
+ = ( $year, $month, $day, $hour, $min, $sec );
}
else {
- ($max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec) = $options{'max'} =~ /^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/;
+ ( $max_year, $max_month, $max_day,
+ $max_hour, $max_min, $max_sec )
+ = $options{'max'} =~ /^(\d+)-(\d+)-(\d+) (\d+):(\d+):(\d+)$/;
}
}
else {
- ($max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec) = (Date::Calc::Add_Delta_YMD($min_year, $min_month, $min_day, 1, 0, 0), 23, 59, 59);
- }
-
- my ($delta_days, $delta_hours, $delta_mins, $delta_secs) = Date::Calc::Delta_DHMS(
+ ( $max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec ) =
+ ( Date::Calc::Add_Delta_YMD( $min_year, $min_month, $min_day, 1, 0,
+ 0 ), 23, 59, 59 );
+ }
+
+ my ( $delta_days, $delta_hours, $delta_mins, $delta_secs ) =
+ Date::Calc::Delta_DHMS(
$min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec,
$max_year, $max_month, $max_day, $max_hour, $max_min, $max_sec,
);
-
- cluck('max date is later than min date') && return if ($delta_days < 0) || ($delta_hours < 0) || ($delta_mins < 0) || ($delta_secs < 0);
-
- $delta_secs = ($delta_days * 86400) + ($delta_hours * 3600) + ($delta_mins * 60) + $delta_secs;
-
- $delta_secs = int(rand($delta_secs + 1));
-
- ($year, $month, $day, $hour, $min, $sec) = Date::Calc::Add_Delta_DHMS($min_year, $min_month, $min_day, $min_hour, $min_min, $min_sec, 0, 0, 0, $delta_secs);
-
- return sprintf("%04u-%02u-%02u %02u:%02u:%02u", $year, $month, $day, $hour, $min, $sec);
-}
+ cluck('max date is later than min date') && return
+ if ( $delta_days < 0 ) || ( $delta_hours < 0 ) || ( $delta_mins < 0 )
+ || ( $delta_secs < 0 );
+
+ $delta_secs =
+ ( $delta_days * 86400 ) + ( $delta_hours * 3600 ) + ( $delta_mins * 60 ) +
+ $delta_secs;
+
+ $delta_secs = int( rand( $delta_secs + 1 ) );
+
+ ( $year, $month, $day, $hour, $min, $sec ) = Date::Calc::Add_Delta_DHMS(
+ $min_year, $min_month, $min_day, $min_hour,
+ $min_min, $min_sec, 0, 0,
+ 0, $delta_secs
+ );
+
+ return
+ sprintf( "%04u-%02u-%02u %02u:%02u:%02u", $year, $month, $day, $hour,
+ $min, $sec );
+}
################################################################################
# rand_image()
################################################################################
sub rand_image {
+
# Get the options hash
my %options = @_;
-
+
$options{'minwidth'} ||= 1;
$options{'maxwidth'} ||= 100;
- $options{'width'} ||= int(rand($options{'maxwidth'} - $options{'minwidth'} + 1)) + $options{'minwidth'};
-
+ $options{'width'} ||=
+ int( rand( $options{'maxwidth'} - $options{'minwidth'} + 1 ) ) +
+ $options{'minwidth'};
+
$options{'minheight'} ||= 1;
$options{'maxheight'} ||= 100;
- $options{'height'} ||= int(rand($options{'maxheight'} - $options{'minheight'} + 1)) + $options{'minheight'};
+ $options{'height'} ||=
+ int( rand( $options{'maxheight'} - $options{'minheight'} + 1 ) ) +
+ $options{'minheight'};
$options{'minpixels'} ||= 0;
$options{'maxpixels'} ||= $options{'width'} * $options{'height'};
- $options{'pixels'} ||= int(rand($options{'maxpixels'} - $options{'minpixels'} + 1)) + $options{'minpixels'};
-
+ $options{'pixels'} ||=
+ int( rand( $options{'maxpixels'} - $options{'minpixels'} + 1 ) ) +
+ $options{'minpixels'};
+
$options{'bgcolor'} ||= _color();
$options{'fgcolor'} ||= _color();
-
+
eval q{ use GD; };
-
+
cluck($@) && return if $@;
-
- my $image = new GD::Image($options{'width'}, $options{'height'});
-
- my $bgcolor = $image->colorAllocate(@{$options{'bgcolor'}});
- my $fgcolor = $image->colorAllocate(@{$options{'fgcolor'}});
-
- $image->rectangle(0, 0, $options{'width'}, $options{'height'}, $bgcolor);
-
- for(my $i = 0; $i < $options{'pixels'}; $i++) {
- my $x = int(rand($options{'width'} + 1));
- my $y = int(rand($options{'height'} + 1));
-
- $image->setPixel($x, $y, $fgcolor);
+
+ my $image = new GD::Image( $options{'width'}, $options{'height'} );
+
+ my $bgcolor = $image->colorAllocate( @{ $options{'bgcolor'} } );
+ my $fgcolor = $image->colorAllocate( @{ $options{'fgcolor'} } );
+
+ $image->rectangle( 0, 0, $options{'width'}, $options{'height'}, $bgcolor );
+
+ for ( my $i = 0 ; $i < $options{'pixels'} ; $i++ ) {
+ my $x = int( rand( $options{'width'} + 1 ) );
+ my $y = int( rand( $options{'height'} + 1 ) );
+
+ $image->setPixel( $x, $y, $fgcolor );
}
-
+
return $image->png();
-
+
sub _color {
- return [ int(rand(266)), int(rand(266)), int(rand(266)) ];
+ return [ int( rand(266) ), int( rand(266) ), int( rand(266) ) ];
}
}
-
################################################################################
# shuffle()
################################################################################
sub shuffle {
my $array = shift;
- for (my $i = @$array - 1; $i >= 0; $i--) {
- my $j = int(rand($i + 1));
+ for ( my $i = @$array - 1 ; $i >= 0 ; $i-- ) {
+ my $j = int( rand( $i + 1 ) );
- @$array[$i, $j] = @$array[$j, $i] if $i != $j;
+ @$array[ $i, $j ] = @$array[ $j, $i ] if $i != $j;
}
}
-
1;
-
=head1 NAME
Data::Random - Perl module to generate random data
@@ -765,11 +827,11 @@ fgcolor - the foreground color of the image. The value must be a reference to a
=head1 VERSION
-0.03
+0.04
=head1 AUTHOR
-Adekunle Olonoh, ade@bottledsoftware.com
+Adekunle Olonoh, olonoh@yahoo.com
=head1 CREDITS
@@ -785,3 +847,4 @@ Copyright (c) 2000 Adekunle Olonoh. All rights reserved. This program is free so
Data::Random::WordList
=cut
+
View
78 Random/WordList.pm
@@ -7,120 +7,115 @@
# Date: October 2000
################################################################################
-
package Data::Random::WordList;
-
################################################################################
# - Modules and Libraries
################################################################################
-require 5.005_62;
+#require 5.005_62;
use FileHandle;
-
################################################################################
# - Global Constants and Variables
################################################################################
-$Data::Random::WordList::VERSION = '0.02';
-
+$Data::Random::WordList::VERSION = '0.04';
################################################################################
# - Subroutines
################################################################################
-
################################################################################
# new()
################################################################################
sub new {
- my $proto = shift;
+ my $proto = shift;
my %options = @_;
-
+
# Check if what was passed in was a prototype reference or a class name
my $class = ref($proto) || $proto;
-
+
# Create a new filehandle object
- my $fh = new FileHandle $options{'wordlist'} or die "could not open $options{'wordlist'} : $!";
-
+ my $fh = new FileHandle $options{'wordlist'}
+ or die "could not open $options{'wordlist'} : $!";
+
# Calculate the number of lines in the file
my $size = 0;
- while(<$fh>) {
+ while (<$fh>) {
$size++;
}
# Create the object
my $self = bless {
- 'fh' => $fh,
+ 'fh' => $fh,
'size' => $size,
}, $class;
return $self;
}
-
################################################################################
# close()
################################################################################
sub close {
my $self = shift;
-
+
# Close the filehandle
$self->{'fh'}->close;
}
-
################################################################################
# get_words()
################################################################################
sub get_words {
my $self = shift;
- my $num = shift || 1;
+ my $num = shift || 1;
my $fh = $self->{'fh'};
-
+
# Perform some error checking
- die 'the size value must be a positive integer' if $num < 0 || $num != int($num);
- die "$num words were requested but only $self->{'size'} words exist in the wordlist" if $num > $self->{'size'};
-
+ die 'the size value must be a positive integer'
+ if $num < 0 || $num != int($num);
+ die
+"$num words were requested but only $self->{'size'} words exist in the wordlist"
+ if $num > $self->{'size'};
+
# Pick which lines we want
my %rand_lines = ();
- for(my $i = 0; $i < $num; $i++) {
+ for ( my $i = 0 ; $i < $num ; $i++ ) {
my $rand_line;
-
+
do {
- $rand_line = int(rand($self->{'size'}));
- } while (exists($rand_lines{$rand_line}));
+ $rand_line = int( rand( $self->{'size'} ) );
+ } while ( exists( $rand_lines{$rand_line} ) );
$rand_lines{$rand_line} = 1;
}
-
- my $line = 0;
+
+ my $line = 0;
my @rand_words = ();
-
+
# Seek to the beginning of the filehandle
- $fh->seek(0, 0) or die "could not seek to position 0 in wordlist: $!";
-
+ $fh->seek( 0, 0 ) or die "could not seek to position 0 in wordlist: $!";
+
# Now get the lines
- while(<$fh>) {
- chomp;
- push(@rand_words, $_) if $rand_lines{$line};
+ while (<$fh>) {
+ chomp;
+ push ( @rand_words, $_ ) if $rand_lines{$line};
- $line++;
+ $line++;
}
- # Return an array or an array reference, depending on the context in which the sub was called
- if (wantarray()) {
+# Return an array or an array reference, depending on the context in which the sub was called
+ if ( wantarray() ) {
return @rand_words;
}
else {
return \@rand_words;
}
}
-
1;
-
=head1 NAME
Data::Random::WordList - Perl module to get random words from a word list
@@ -169,11 +164,11 @@ Closes the filehandle associated with the word list. It's good practice to do t
=head1 VERSION
-0.02
+0.04
=head1 AUTHOR
-Adekunle Olonoh, ade@bottledsoftware.com
+Adekunle Olonoh, olonoh@yahoo.com
=head1 COPYRIGHT
@@ -184,3 +179,4 @@ Copyright (c) 2000 Adekunle Olonoh. All rights reserved. This program is free so
Data::Random
=cut
+
View
182 t/rand_chars.t 100644 → 100755
@@ -6,163 +6,185 @@ BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_chars );
-print $Data::Random::VERSION,"\n";
-
use vars qw( %charsets );
%charsets = (
- all => [ 0..9, 'a'..'z', 'A'..'Z', '#', ',', qw( ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; [ ] \ ` ) ],
- alpha => [ 'a'..'z', 'A'..'Z' ],
- upperalpha => [ 'A'..'Z' ],
- loweralpha => [ 'a'..'z' ],
- numeric => [ 0..9 ],
- alphanumeric => [ 0..9, 'a'..'z', 'A'..'Z' ],
- misc => [ '#', ',', qw( ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; [ ] \ ` ) ],
+ all => [
+ 0 .. 9,
+ 'a' .. 'z',
+ 'A' .. 'Z',
+ '#',
+ ',',
+ qw( ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; [ ] \ ` )
+ ],
+ alpha => [ 'a' .. 'z', 'A' .. 'Z' ],
+ upperalpha => [ 'A' .. 'Z' ],
+ loweralpha => [ 'a' .. 'z' ],
+ numeric => [ 0 .. 9 ],
+ alphanumeric => [ 0 .. 9, 'a' .. 'z', 'A' .. 'Z' ],
+ misc => [
+ '#',
+ ',',
+ qw( ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; [ ] \ ` )
+ ],
);
my %valid_chars;
-foreach my $charset (keys %charsets) {
- @{$valid_chars{$charset}}{@{$charsets{$charset}}} = ();
+foreach my $charset ( keys %charsets ) {
+ @{ $valid_chars{$charset} }{ @{ $charsets{$charset} } } = ();
}
# Test default w/ no params -- should return one entry
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
+ while ( $pass && $i < $num_chars ) {
my @chars = rand_chars( set => $charsets{$charset} );
-
- $pass = 0 unless ( @chars == 1 && exists($valid_chars{$charset}->{$chars[0]}) );
+
+ $pass = 0
+ unless ( @chars == 1
+ && exists( $valid_chars{$charset}->{ $chars[0] } ) );
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test size option
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
+ while ( $pass && $i < $num_chars ) {
my @chars = rand_chars( set => $charset, size => $i + 1 );
-
- $pass = 0 unless @chars == ($i + 1);
-
+
+ $pass = 0 unless @chars == ( $i + 1 );
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test max/min option
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_chars( set => $charsets{$charset}, min => $i, max => $num_chars );
-
+ while ( $pass && $i < $num_chars ) {
+ my @chars = rand_chars(
+ set => $charsets{$charset},
+ min => $i,
+ max => $num_chars
+ );
+
$pass = 0 unless ( @chars >= $i && @chars <= $num_chars );
-
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test size w/ min/max set
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_chars( set => $charsets{$charset}, size => $i + 1, min => $i, max => $num_chars );
-
- $pass = 0 unless @chars == ($i + 1);
-
+ while ( $pass && $i < $num_chars ) {
+ my @chars = rand_chars(
+ set => $charsets{$charset},
+ size => $i + 1,
+ min => $i,
+ max => $num_chars
+ );
+
+ $pass = 0 unless @chars == ( $i + 1 );
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test w/ shuffle set to 0
{
my $pass = 1;
-
+
sub _get_index {
- my ($charset, $char) = @_;
-
+ my ( $charset, $char ) = @_;
+
my $i = 0;
- while ( $charsets{$charset}->[$i] ne $char && $i < @{$charsets{$charset}} ) {
+ while ( $charsets{$charset}->[$i] ne $char
+ && $i < @{ $charsets{$charset} } )
+ {
$i++;
}
-
+
$i;
}
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_chars( set => $charsets{$charset}, size => 2, shuffle => 0 );
-
- $pass = 0 unless ( @chars == 2 && _get_index($charset, $chars[0]) < _get_index($charset, $chars[1]) );
-
+ while ( $pass && $i < $num_chars ) {
+ my @chars =
+ rand_chars( set => $charsets{$charset}, size => 2, shuffle => 0 );
+
+ $pass = 0
+ unless ( @chars == 2
+ && _get_index( $charset, $chars[0] ) <
+ _get_index( $charset, $chars[1] ) );
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
View
118 t/rand_date.t 100644 → 100755
@@ -5,106 +5,124 @@ BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_date );
-use Date::Calc;
+# Try to load Date::Calc
+eval q{ use Date::Calc };
-# Get today's date
-my ($year, $month, $day) = Date::Calc::Today();
+# If the module cannot be loaded, skip tests
+print "1..0 # Skipped: Date::Calc not installed\n" and exit if $@;
+# Get today's date
+my ( $year, $month, $day ) = Date::Calc::Today();
# Test default w/ no params -- should return a date between today and 1 year from now
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
+ while ( $pass && $i < $max_days ) {
my $date = rand_date();
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, split(/\-/, $date) );
-
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
+
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min option
{
my $pass = 1;
-
+
my $max_days = Date::Calc::Delta_Days( 1978, 9, 21, 1979, 9, 21 );
-
+
my $i = 0;
- while ($pass && $i < $max_days) {
+ while ( $pass && $i < $max_days ) {
my $date = rand_date( min => '1978-9-21' );
-
- my $delta = Date::Calc::Delta_Days( 1978, 9, 21, split(/\-/, $date) );
-
+
+ my $delta =
+ Date::Calc::Delta_Days( 1978, 9, 21, split ( /\-/, $date ) );
+
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test max option
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
- my $date = rand_date( max => join('-', Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 )) );
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, split(/\-/, $date) );
-
+ while ( $pass && $i < $max_days ) {
+ my $date =
+ rand_date( max =>
+ join ( '-',
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) ) );
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
+
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
- my $date = rand_date( min => "$year-$month-$day", max => join('-', Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 )) );
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, split(/\-/, $date) );
-
+ while ( $pass && $i < $max_days ) {
+ my $date = rand_date(
+ min => "$year-$month-$day",
+ max =>
+ join ( '-',
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) )
+ );
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day, split ( /\-/, $date ) );
+
$pass = 0 unless $delta >= 0 && $delta <= $max_days;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options using "now"
{
my $pass = 1;
-
+
my $date = rand_date( min => 'now', max => 'now' );
-
- my ($new_year, $new_month, $new_day) = split(/\-/, $date);
-
- $pass = 0 unless $new_year == $year && $new_month == $month && $new_day == $day;
-
+
+ my ( $new_year, $new_month, $new_day ) = split ( /\-/, $date );
+
+ $pass = 0
+ unless $new_year == $year && $new_month == $month && $new_day == $day;
+
ok($pass);
}
View
156 t/rand_datetime.t 100644 → 100755
@@ -5,119 +5,151 @@ BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_datetime );
-use Date::Calc;
+# Try to load Date::Calc
+eval q{ use Date::Calc };
-# Get today's date
-my ($year, $month, $day) = Date::Calc::Today();
+# If the module cannot be loaded, skip tests
+print "1..0 # Skipped: Date::Calc not installed\n" and exit if $@;
+# Get today's date
+my ( $year, $month, $day ) = Date::Calc::Today();
# Test default w/ no params -- should return a date between today and 1 year from now
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
+ while ( $pass && $i < $max_days ) {
my $date = rand_datetime();
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
-
- $pass = 0 unless $delta >= 0 && $delta <= $max_days && _to_secs((split(/ /, $date))[1]);
-
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
+
+ $pass = 0
+ unless $delta >= 0 && $delta <= $max_days
+ && _to_secs( ( split ( / /, $date ) )[1] );
+
$i++;
}
-
+
ok($pass);
}
-
# Test min option
{
my $pass = 1;
-
+
my $max_days = Date::Calc::Delta_Days( 1978, 9, 21, 1979, 9, 21 );
-
+
my $i = 0;
- while ($pass && $i < $max_days) {
+ while ( $pass && $i < $max_days ) {
my $date = rand_datetime( min => '1978-9-21 4:0:0' );
-
- my $delta = Date::Calc::Delta_Days( 1978, 9, 21, ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
-
- $pass = 0 unless $delta >= 0 && $delta <= $max_days && _to_secs((split(/ /, $date))[1]);
-
+
+ my $delta =
+ Date::Calc::Delta_Days( 1978, 9, 21,
+ ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
+
+ $pass = 0
+ unless $delta >= 0 && $delta <= $max_days
+ && _to_secs( ( split ( / /, $date ) )[1] );
+
$i++;
}
-
+
ok($pass);
}
-
# Test max option
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
- my $date = rand_datetime( max => join('-', Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 )).' 0:0:0' );
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
-
- $pass = 0 unless $delta >= 0 && $delta <= $max_days && _to_secs((split(/ /, $date))[1]);
-
+ while ( $pass && $i < $max_days ) {
+ my $date =
+ rand_datetime( max =>
+ join ( '-',
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) )
+ . ' 0:0:0' );
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
+
+ $pass = 0
+ unless $delta >= 0 && $delta <= $max_days
+ && _to_secs( ( split ( / /, $date ) )[1] );
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options
{
my $pass = 1;
-
- my $max_days = Date::Calc::Delta_Days( $year, $month, $day, Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
-
+
+ my $max_days =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) );
+
my $i = 0;
- while ($pass && $i < $max_days) {
- my $date = rand_datetime( min => "$year-$month-$day 12:0:0", max => join('-', Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 )).' 12:0:0' );
-
- my $delta = Date::Calc::Delta_Days( $year, $month, $day, ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
-
- $pass = 0 unless $delta >= 0 && $delta <= $max_days && _to_secs((split(/ /, $date))[1]);
-
+ while ( $pass && $i < $max_days ) {
+ my $date = rand_datetime(
+ min => "$year-$month-$day 12:0:0",
+ max =>
+ join ( '-',
+ Date::Calc::Add_Delta_YMD( $year, $month, $day, 1, 0, 0 ) )
+ . ' 12:0:0'
+ );
+
+ my $delta =
+ Date::Calc::Delta_Days( $year, $month, $day,
+ ( $date =~ /^(\d+)-(\d+)-(\d+)/ ) );
+
+ $pass = 0
+ unless $delta >= 0 && $delta <= $max_days
+ && _to_secs( ( split ( / /, $date ) )[1] );
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options using "now"
{
my $pass = 1;
-
+
my $date = rand_datetime( min => 'now', max => 'now' );
-
- my ($new_year, $new_month, $new_day) = ( $date =~ /^(\d+)-(\d+)-(\d+)/ );
-
- $pass = 0 unless $new_year == $year && $new_month == $month && $new_day == $day && _to_secs((split(/ /, $date))[1]);
-
+
+ my ( $new_year, $new_month, $new_day ) = ( $date =~ /^(\d+)-(\d+)-(\d+)/ );
+
+ $pass = 0
+ unless $new_year == $year && $new_month == $month && $new_day == $day
+ && _to_secs( ( split ( / /, $date ) )[1] );
+
ok($pass);
}
-
sub _to_secs {
my $time = shift;
-
- my ($hour, $min, $sec) = split(/\:/, $time);
-
- return if ($hour > 23) || ($hour < 0);
- return if ($min > 59) || ($min < 0);
- return if ($sec > 59) || ($sec < 0);
-
+
+ my ( $hour, $min, $sec ) = split ( /\:/, $time );
+
+ return if ( $hour > 23 ) || ( $hour < 0 );
+ return if ( $min > 59 ) || ( $min < 0 );
+ return if ( $sec > 59 ) || ( $sec < 0 );
+
return $hour * 3600 + $min * 60 + $sec;
}
View
32 t/rand_enum.t 100644 → 100755
@@ -6,41 +6,41 @@ BEGIN { plan tests => 1 }
use lib qw(..);
use Data::Random qw( rand_enum );
-print $Data::Random::VERSION,"\n";
-
use vars qw( %charsets );
%charsets = (
- a => [ ],
- b => [ 'A' ],
+ a => [],
+ b => ['A'],
c => [ 'A', 'B' ],
d => [ 'A' .. 'Z' ],
);
my %valid_chars;
-foreach my $charset (keys %charsets) {
- @{$valid_chars{$charset}}{@{$charsets{$charset}}} = ();
+foreach my $charset ( keys %charsets ) {
+ @{ $valid_chars{$charset} }{ @{ $charsets{$charset} } } = ();
}
# Test default w/ no params -- should return one entry
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
+ while ( $pass && $i < $num_chars ) {
my @chars = rand_enum( set => $charsets{$charset} );
-
- $pass = 0 unless ( @chars == 1 && exists($valid_chars{$charset}->{$chars[0]}) );
+
+ $pass = 0
+ unless ( @chars == 1
+ && exists( $valid_chars{$charset}->{ $chars[0] } ) );
$i++;
}
-
+
}
-
+
ok($pass);
}
View
20 t/rand_image.t 100644 → 100755
@@ -7,24 +7,26 @@ use lib qw(..);
use Data::Random qw( rand_image );
use File::Spec;
-print $Data::Random::VERSION,"\n";
-
use vars qw( $imagefile );
-$imagefile = File::Spec->tmpdir().'/Data_Random_'.time().'.tmp';
+# Try to load GD
+eval q{ use GD };
+
+# If the module cannot be loaded, skip tests
+print "1..0 # Skipped: GD not installed\n" and exit if $@;
+$imagefile = File::Spec->tmpdir() . '/Data_Random_' . time() . '.tmp';
# Test writing an image to a file
{
- open(FILE, ">$imagefile");
+ open( FILE, ">$imagefile" );
binmode(FILE);
- print FILE rand_image( bgcolor => [0, 0, 0] );
+ print FILE rand_image( bgcolor => [ 0, 0, 0 ] );
close(FILE);
-
- ok(!(-z $imagefile));
-}
+ ok( !( -z $imagefile ) );
+}
END {
- unlink($imagefile);
+ unlink($imagefile) if $imagefile;
}
View
164 t/rand_set.t 100644 → 100755
@@ -6,162 +6,176 @@ BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_set );
-print $Data::Random::VERSION,"\n";
-
use vars qw( %charsets );
%charsets = (
- a => [ ],
- b => [ 'A' ],
+ a => [],
+ b => ['A'],
c => [ 'A', 'B' ],
d => [ 'A' .. 'Z' ],
);
my %valid_chars;
-foreach my $charset (keys %charsets) {
- @{$valid_chars{$charset}}{@{$charsets{$charset}}} = ();
+foreach my $charset ( keys %charsets ) {
+ @{ $valid_chars{$charset} }{ @{ $charsets{$charset} } } = ();
}
# Test default w/ no params -- should return one entry
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
+ while ( $pass && $i < $num_chars ) {
my @chars = rand_set( set => $charsets{$charset} );
-
- $pass = 0 unless ( @chars == 1 && exists($valid_chars{$charset}->{$chars[0]}) );
+
+ $pass = 0
+ unless ( @chars == 1
+ && exists( $valid_chars{$charset}->{ $chars[0] } ) );
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test size option
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
+ while ( $pass && $i < $num_chars ) {
my @chars = rand_set( set => $charsets{$charset}, size => $i + 1 );
-
- $pass = 0 unless @chars == ($i + 1);
-
+
+ $pass = 0 unless @chars == ( $i + 1 );
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test max/min option
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_set( set => $charsets{$charset}, min => $i, max => $num_chars );
-
+ while ( $pass && $i < $num_chars ) {
+ my @chars = rand_set(
+ set => $charsets{$charset},
+ min => $i,
+ max => $num_chars
+ );
+
$pass = 0 unless ( @chars >= $i && @chars <= $num_chars );
-
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test size w/ min/max set
{
my $pass = 1;
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
+
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_set( set => $charsets{$charset}, size => $i + 1, min => $i, max => $num_chars );
-
- $pass = 0 unless @chars == ($i + 1);
-
+ while ( $pass && $i < $num_chars ) {
+ my @chars = rand_set(
+ set => $charsets{$charset},
+ size => $i + 1,
+ min => $i,
+ max => $num_chars
+ );
+
+ $pass = 0 unless @chars == ( $i + 1 );
+
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
-
+
}
-
+
ok($pass);
}
-
# Test w/ shuffle set to 0
{
my $pass = 1;
-
+
sub _get_index {
- my ($charset, $char) = @_;
-
+ my ( $charset, $char ) = @_;
+
my $i = 0;
- while ( $charsets{$charset}->[$i] ne $char && $i < @{$charsets{$charset}} ) {
+ while ( $charsets{$charset}->[$i] ne $char
+ && $i < @{ $charsets{$charset} } )
+ {
$i++;
}
-
+
$i;
}
-
- foreach my $charset (keys %charsets) {
-
- my $num_chars = @{$charsets{$charset}};
-
- if ($num_chars >= 2) {
- my $i = 0;
- while ($pass && $i < $num_chars) {
- my @chars = rand_set( set => $charsets{$charset}, size => 2, shuffle => 0 );
- $pass = 0 unless ( @chars == 2 && _get_index($charset, $chars[0]) < _get_index($charset, $chars[1]) );
+ foreach my $charset ( keys %charsets ) {
+
+ my $num_chars = @{ $charsets{$charset} };
+
+ if ( $num_chars >= 2 ) {
+ my $i = 0;
+ while ( $pass && $i < $num_chars ) {
+ my @chars = rand_set(
+ set => $charsets{$charset},
+ size => 2,
+ shuffle => 0
+ );
+
+ $pass = 0
+ unless ( @chars == 2
+ && _get_index( $charset, $chars[0] ) <
+ _get_index( $charset, $chars[1] ) );
foreach (@chars) {
- $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ $pass = 0 unless exists( $valid_chars{$charset}->{$_} );
}
$i++;
}
}
-
+
}
-
+
ok($pass);
}
View
90 t/rand_time.t 100644 → 100755
@@ -6,109 +6,103 @@ BEGIN { plan tests => 5 }
use lib qw(..);
use Data::Random qw( rand_time );
-
# Test default w/ no params
{
my $pass = 1;
-
- my $max_secs = 3600; # 86400
-
+
+ my $max_secs = 3600; # 86400
+
my $i = 0;
- while ($pass && $i < $max_secs) {
+ while ( $pass && $i < $max_secs ) {
my $time = rand_time();
-
+
$pass = 0 unless _to_secs($time);
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min option
{
my $pass = 1;
-
- my $max_secs = 3600; # 72000
-
+
+ my $max_secs = 3600; # 72000
+
my $i = 0;
- while ($pass && $i < $max_secs) {
+ while ( $pass && $i < $max_secs ) {
my $time = rand_time( min => '4:0:0' );
-
+
$pass = 0 unless _to_secs($time) >= 14400;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test max option
{
my $pass = 1;
-
- my $max_secs = 3600; # 14400
-
+
+ my $max_secs = 3600; # 14400
+
my $i = 0;
- while ($pass && $i < $max_secs) {
+ while ( $pass && $i < $max_secs ) {
my $time = rand_time( max => '4:0:0' );
-
+
$pass = 0 unless _to_secs($time) <= 14400;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options
{
my $pass = 1;
-
+
my $max_secs = 3600;
-
+
my $i = 0;
- while ($pass && $i < $max_secs) {
+ while ( $pass && $i < $max_secs ) {
my $time = rand_time( min => '9:0:0', max => '10:0:0' );
-
+
my $secs = _to_secs($time);
-
+
$pass = 0 unless $secs >= 32400 && $secs <= 36000;
-
+
$i++;
}
-
+
ok($pass);
}
-
# Test min + max options using "now"
{
my $pass = 1;
-
+
my $time = rand_time( min => 'now', max => 'now' );
-
- my ($hour, $min, $sec) = (localtime())[2, 1, 0];
-
- my ($new_hour, $new_min, $new_sec) = split(/\:/, $time);
-
+
+ my ( $hour, $min, $sec ) = ( localtime() )[ 2, 1, 0 ];
+
+ my ( $new_hour, $new_min, $new_sec ) = split ( /\:/, $time );
+
$pass = 0 unless $new_hour == $hour && $new_min == $min && $new_sec == $sec;
-