Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

import Data-Random 0.02 from CPAN

git-cpan-module:   Data-Random
git-cpan-version:  0.02
git-cpan-authorid: ADEO
git-cpan-file:     authors/id/A/AD/ADEO/Data-Random-0.02.tar.gz
  • Loading branch information...
commit f0024ded69389ca3991dec2f9df2772ee3422188 1 parent e69e515
Adekunle Olonoh authored schwern committed
View
17 Changes
@@ -0,0 +1,17 @@
+Revision history for Data::Random.
+
+0.02 Wed May 30 2001
+ - correctly made "shuffle" flag default to 1 for rand_words(),
+ rand_chars() and rand_set() (Thanks to David Sarno)
+
+ - fixed bug where rand_words() would sometimes return less words
+ than what's specified in "min" (Thanks to David Sarno)
+
+ - allowed results of rand_set() to be unshuffled
+
+ - added a plethora of tests (how's one supposed to
+ accurately the test validity of random data anyway?)
+
+0.01 Tue Oct 31 2000
+ - original version
+
View
9 MANIFEST
@@ -1,6 +1,13 @@
+Changes
MANIFEST
README
Makefile.PL
Random.pm
Random/WordList.pm
-test.pl
+t/rand_chars.t
+t/rand_date.t
+t/rand_datetime.t
+t/rand_enum.t
+t/rand_set.t
+t/rand_time.t
+t/rand_words.t
View
4 Makefile.PL
@@ -1,8 +1,8 @@
use ExtUtils::MakeMaker;
-# See lib/ExtUtils/MakeMaker.pm for details of how to influence
-# the contents of the Makefile that is written.
+
WriteMakefile(
'NAME' => 'Data::Random',
'VERSION_FROM' => 'Random.pm', # finds $VERSION
'PMLIBDIRS' => [ 'Random' ],
+ 'PREREQ_PM' => {},
);
View
61 Random.pm
@@ -2,9 +2,6 @@
# Data::Random
#
# A module used to generate random data.
-#
-# Author: Adekunle Olonoh
-# Date: October 2000
################################################################################
@@ -49,7 +46,7 @@ use vars qw(
@EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
@EXPORT = qw();
-$Data::Random::VERSION = '0.01';
+$Data::Random::VERSION = '0.02';
################################################################################
@@ -70,7 +67,7 @@ sub rand_words {
# Initialize max and min vars
$options{'min'} ||= 1;
$options{'max'} ||= 1;
-
+
# Make sure the max and min vars are OK
die 'min value cannot be larger than max value' if $options{'min'} > $options{'max'};
die 'min value must be a positive integer' if $options{'min'} < 0 || $options{'min'} != int($options{'min'});
@@ -83,7 +80,7 @@ sub rand_words {
die 'size value must be a positive integer' if $options{'size'} < 0 || $options{'size'} != int($options{'size'});
# Initialize the shuffle flag
- $options{'shuffle'} = $options{'shuffle'} ? 1 : 0;
+ $options{'shuffle'} = exists($options{'shuffle'}) ? $options{'shuffle'} : 1;
my $wl;
my $close_wl = 1;
@@ -103,7 +100,7 @@ sub rand_words {
# Close the word list
$wl->close() if $close_wl;
-
+
# Shuffle the words around
shuffle($rand_words) if $options{'shuffle'};
@@ -167,47 +164,50 @@ sub rand_set {
# Make sure the set array was defined
die 'set array is not defined' if !$options{'set'};
- my @set = @{$options{'set'}};
- my $set_length = scalar @set;
+ $options{'size'} = 1 unless exists($options{'min'}) || exists($options{'max'}) || exists($options{'size'});
# Initialize max and min vars
$options{'min'} ||= 0;
- $options{'max'} ||= @set;
-
+ $options{'max'} ||= @{$options{'set'}};
+
# Make sure the max and min vars are OK
die 'min value cannot be larger than max value' if $options{'min'} > $options{'max'};
die 'min value must be a positive integer' if $options{'min'} < 0 || $options{'min'} != int($options{'min'});
die 'max value must be a positive integer' if $options{'max'} < 0 || $options{'max'} != int($options{'max'});
-
+
# Initialize the size var
$options{'size'} ||= int(rand($options{'max'} - $options{'min'} + 1)) + $options{'min'};
# Make sure the size var is OK
die 'size value must be a positive integer' if $options{'size'} < 0 || $options{'size'} != int($options{'size'});
- die 'size value exceeds set size' if $options{'size'} > $set_length;
+ die 'size value exceeds set size' if $options{'size'} > @{$options{'set'}};
# Initialize the shuffle flag
- $options{'shuffle'} = $options{'shuffle'} ? 1 : 0;
+ $options{'shuffle'} = exists($options{'shuffle'}) ? $options{'shuffle'} : 1;
# Get the random items
- my @results = ();
+ my %results = ();
for(my $i = 0; $i < $options{'size'}; $i++) {
- my $result = int(rand($set_length));
+ my $result;
+
+ do {
+ $result = int(rand(@{$options{'set'}}));
+ } while (exists($results{$result}));
- push(@results, $set[$result]);
- splice(@set, $result, 1);
- $set_length--;
+ $results{$result} = 1;
}
-
+
+ my @results = sort { $a <=> $b } keys %results;
+
# Shuffle the items
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 @results;
+ return @{$options{'set'}}[@results];
}
else {
- return \@results;
+ return \@{$options{'set'}}[@results];
}
}
@@ -234,7 +234,9 @@ sub rand_date {
my %options = @_;
# use the Date::Calc module
- use Date::Calc;
+ eval q{ use Date::Calc };
+
+ die $@ if $@;
my ($min_year, $min_month, $min_day, $max_year, $max_month, $max_day);
@@ -354,8 +356,10 @@ sub rand_datetime {
my %options = @_;
# use the Date::Calc module
- use Date::Calc;
+ eval q{ use Date::Calc };
+ die $@ 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);
# Get today's date
@@ -441,7 +445,7 @@ Data::Random - Perl module to generate random data
my $random_time = rand_time();
my $random_datetime = rand_datetime();
-
+
=head1 DESCRIPTION
@@ -653,12 +657,17 @@ max - the maximum date/time to be returned. It should be in the form "YYYY-MM-DD
=head1 VERSION
-0.01
+0.02
=head1 AUTHOR
Adekunle Olonoh, ade@bottledsoftware.com
+=head1 CREDITS
+
+Hiroki Chalfant
+David Sarno
+
=head1 COPYRIGHT
Copyright (c) 2000 Adekunle Olonoh. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
View
10 Random/WordList.pm
@@ -21,7 +21,7 @@ use FileHandle;
################################################################################
# - Global Constants and Variables
################################################################################
-$Data::Random::WordList::VERSION = '0.01';
+$Data::Random::WordList::VERSION = '0.02';
################################################################################
@@ -85,7 +85,11 @@ sub get_words {
# Pick which lines we want
my %rand_lines = ();
for(my $i = 0; $i < $num; $i++) {
- my $rand_line = int(rand($self->{'size'}));
+ my $rand_line;
+
+ do {
+ $rand_line = int(rand($self->{'size'}));
+ } while (exists($rand_lines{$rand_line}));
$rand_lines{$rand_line} = 1;
}
@@ -165,7 +169,7 @@ Closes the filehandle associated with the word list. It's good practice to do t
=head1 VERSION
-0.01
+0.02
=head1 AUTHOR
View
168 t/rand_chars.t
@@ -0,0 +1,168 @@
+use strict;
+use Test;
+
+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( ~ ! @ $ % ^ & * ( ) _ + = - { } | : " < > ? / . ' ; [ ] \ ` ) ],
+);
+
+my %valid_chars;
+
+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}};
+
+ my $i = 0;
+ while ($pass && $i < $num_chars) {
+ my @chars = rand_chars( set => $charsets{$charset} );
+
+ $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}};
+
+ my $i = 0;
+ while ($pass && $i < $num_chars) {
+ my @chars = rand_chars( set => $charset, size => $i + 1 );
+
+ $pass = 0 unless @chars == ($i + 1);
+
+ foreach (@chars) {
+ $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}};
+
+ my $i = 0;
+ 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}->{$_});
+ }
+
+ $i++;
+ }
+
+ }
+
+ ok($pass);
+}
+
+
+# Test size w/ min/max set
+{
+ my $pass = 1;
+
+ 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);
+
+ foreach (@chars) {
+ $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 $i = 0;
+ while ( $charsets{$charset}->[$i] ne $char && $i < @{$charsets{$charset}} ) {
+ $i++;
+ }
+
+ $i;
+ }
+
+ 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]) );
+
+ foreach (@chars) {
+ $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ }
+
+ $i++;
+ }
+
+ }
+
+ ok($pass);
+}
View
110 t/rand_date.t
@@ -0,0 +1,110 @@
+use strict;
+use Test;
+
+BEGIN { plan tests => 5 }
+
+use lib qw(..);
+use Data::Random qw( rand_date );
+use Date::Calc;
+
+
+# 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 $i = 0;
+ while ($pass && $i < $max_days) {
+ my $date = rand_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) {
+ my $date = rand_date( min => '1978-9-21' );
+
+ 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 $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) );
+
+ $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 $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) );
+
+ $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;
+
+ ok($pass);
+}
View
123 t/rand_datetime.t
@@ -0,0 +1,123 @@
+use strict;
+use Test;
+
+BEGIN { plan tests => 5 }
+
+use lib qw(..);
+use Data::Random qw( rand_datetime );
+use Date::Calc;
+
+
+# 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 $i = 0;
+ 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]);
+
+ $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) {
+ 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]);
+
+ $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 $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]);
+
+ $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 $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]);
+
+ $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]);
+
+ 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);
+
+ return $hour * 3600 + $min * 60 + $sec;
+}
View
46 t/rand_enum.t
@@ -0,0 +1,46 @@
+use strict;
+use Test;
+
+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' ],
+ c => [ 'A', 'B' ],
+ d => [ 'A' .. 'Z' ],
+);
+
+my %valid_chars;
+
+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}};
+
+ my $i = 0;
+ while ($pass && $i < $num_chars) {
+ my @chars = rand_enum( set => $charsets{$charset} );
+
+ $pass = 0 unless ( @chars == 1 && exists($valid_chars{$charset}->{$chars[0]}) );
+
+ $i++;
+ }
+
+ }
+
+ ok($pass);
+}
View
167 t/rand_set.t
@@ -0,0 +1,167 @@
+use strict;
+use Test;
+
+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' ],
+ c => [ 'A', 'B' ],
+ d => [ 'A' .. 'Z' ],
+);
+
+my %valid_chars;
+
+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}};
+
+ my $i = 0;
+ while ($pass && $i < $num_chars) {
+ my @chars = rand_set( set => $charsets{$charset} );
+
+ $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}};
+
+ my $i = 0;
+ while ($pass && $i < $num_chars) {
+ my @chars = rand_set( set => $charsets{$charset}, size => $i + 1 );
+
+ $pass = 0 unless @chars == ($i + 1);
+
+ foreach (@chars) {
+ $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}};
+
+ my $i = 0;
+ 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}->{$_});
+ }
+
+ $i++;
+ }
+
+ }
+
+ ok($pass);
+}
+
+
+# Test size w/ min/max set
+{
+ my $pass = 1;
+
+ 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);
+
+ foreach (@chars) {
+ $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 $i = 0;
+ 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 (@chars) {
+ $pass = 0 unless exists($valid_chars{$charset}->{$_});
+ }
+
+ $i++;
+ }
+ }
+
+ }
+
+ ok($pass);
+}
View
114 t/rand_time.t
@@ -0,0 +1,114 @@
+use strict;
+use Test;
+
+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 $i = 0;
+ 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 $i = 0;
+ 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 $i = 0;
+ 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) {
+ 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);
+
+ $pass = 0 unless $new_hour == $hour && $new_min == $min && $new_sec == $sec;
+
+ 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);
+
+ return $hour * 3600 + $min * 60 + $sec;
+}
View
128 t/rand_words.t
@@ -0,0 +1,128 @@
+use strict;
+use Test;
+
+BEGIN { plan tests => 5 }
+
+use lib qw(..);
+use Data::Random qw( rand_words );
+use File::Spec;
+
+print $Data::Random::VERSION,"\n";
+
+use vars qw( $wordlist );
+
+$wordlist = File::Spec->tmpdir().'/Data_Random_'.time().'.tmp';
+
+open(FILE, ">$wordlist");
+foreach ('A'..'Z') {
+ print FILE "$_\n";
+}
+close(FILE);
+
+my %valid_words;
+@valid_words{'A'..'Z'} = ();
+
+my $num_words = 26;
+
+
+# Test default w/ no params -- should return one entry
+{
+ my $pass = 1;
+
+ my $i = 0;
+ while ($pass && $i < $num_words) {
+ my @words = rand_words( wordlist => $wordlist );
+
+ $pass = 0 unless ( @words == 1 && exists($valid_words{$words[0]}) );
+
+ $i++;
+ }
+
+ ok($pass);
+}
+
+
+# Test size option
+{
+ my $pass = 1;
+
+ my $i = 0;
+ while ($pass && $i < $num_words) {
+ my @words = rand_words( wordlist => $wordlist, size => $i + 1 );
+
+ $pass = 0 unless @words == ($i + 1);
+
+ foreach (@words) {
+ $pass = 0 unless exists($valid_words{$_});
+ }
+
+ $i++;
+ }
+
+ ok($pass);
+}
+
+
+# Test max/min option
+{
+ my $pass = 1;
+
+ my $i = 0;
+ while ($pass && $i < $num_words) {
+ my @words = rand_words( wordlist => $wordlist, min => $i, max => $num_words );
+
+ $pass = 0 unless ( @words >= $i && @words <= $num_words );
+
+ foreach (@words) {
+ $pass = 0 unless exists($valid_words{$_});
+ }
+
+ $i++;
+ }
+
+ ok($pass);
+}
+
+
+# Test size w/ min/max set
+{
+ my $pass = 1;
+
+ my $i = 0;
+ while ($pass && $i < $num_words) {
+ my @words = rand_words( wordlist => $wordlist, size => $i + 1, min => $i, max => $num_words );
+
+ $pass = 0 unless @words == ($i + 1);
+
+ foreach (@words) {
+ $pass = 0 unless exists($valid_words{$_});
+ }
+
+ $i++;
+ }
+
+ ok($pass);
+}
+
+
+
+# Test w/ shuffle set to 0
+{
+ my $pass = 1;
+
+ my $i = 0;
+ while ($pass && $i < $num_words) {
+ my @words = rand_words( wordlist => $wordlist, size => 2, shuffle => 0 );
+
+ $pass = 0 unless ( @words == 2 && !($words[0] gt $words[1]) );
+
+ $i++;
+ }
+
+ ok($pass);
+}
+
+
+END {
+ unlink($wordlist);
+}
View
20 test.pl
@@ -1,20 +0,0 @@
-# Before `make install' is performed this script should be runnable with
-# `make test'. After `make install' it should work as `perl test.pl'
-
-######################### We start with some black magic to print on failure.
-
-# Change 1..1 below to 1..last_test_to_print .
-# (It may become useful if the test is moved to ./t subdirectory.)
-
-BEGIN { $| = 1; print "1..1\n"; }
-END {print "not ok 1\n" unless $loaded;}
-use Data::Random;
-$loaded = 1;
-print "ok 1\n";
-
-######################### End of black magic.
-
-# Insert your test code below (better if it prints "ok 13"
-# (correspondingly "not ok 13") depending on the success of chunk 13
-# of the test code):
-
Please sign in to comment.
Something went wrong with that request. Please try again.