diff --git a/Changes b/Changes new file mode 100644 index 0000000..2f7ee63 --- /dev/null +++ b/Changes @@ -0,0 +1,4 @@ +Revision history for Perl extension Business::CUSIP::Random + +0.01 Tue Jul 12 20:27:21 2011 + - original version diff --git a/MANIFEST b/MANIFEST new file mode 100644 index 0000000..ee1e65c --- /dev/null +++ b/MANIFEST @@ -0,0 +1,6 @@ +Changes +Makefile.PL +MANIFEST +README +t/cusip.t +lib/Business/CUSIP/Random.pm diff --git a/Makefile.PL b/Makefile.PL new file mode 100644 index 0000000..f93495a --- /dev/null +++ b/Makefile.PL @@ -0,0 +1,12 @@ +use 5.008006; +use ExtUtils::MakeMaker; +# See lib/ExtUtils/MakeMaker.pm for details of how to influence +# the contents of the Makefile that is written. +WriteMakefile( + NAME => 'Business::CUSIP::Random', + VERSION_FROM => 'lib/Business/CUSIP/Random.pm', # finds $VERSION + PREREQ_PM => { 'Business::CUSIP' => 0 }, + ($] >= 5.005 ? ## Add these new keywords supported since 5.005 + (ABSTRACT_FROM => 'lib/Business/CUSIP/Random.pm', # retrieve abstract from module + AUTHOR => 'Michael Aquilina ') : ()), +); diff --git a/README b/README new file mode 100644 index 0000000..c93b8e6 --- /dev/null +++ b/README @@ -0,0 +1,23 @@ +Business::CUSIP::Random +==================== + +Generate random CUSIP numbers for testing + +INSTALLATION + +To install this module, type the following: + +perl Makefile.pl +make +make test +make install + +DEPENDENCIES + +This module requires these other modules and libraries: + + Business::CUSIP + +COPYRIGHT AND LICENSE + +Copyright (C) 2011 Michael Aquilina. All rights reserved. diff --git a/lib/Business/CUSIP/Random.pm b/lib/Business/CUSIP/Random.pm new file mode 100644 index 0000000..9d9a799 --- /dev/null +++ b/lib/Business/CUSIP/Random.pm @@ -0,0 +1,136 @@ +package Business::CUSIP::Random; + +use strict; +use warnings; +use Business::CUSIP; + +our $VERSION = '0.01'; + +=head1 NAME + +Business::CUSIP::Random - Generate random CUSIP numbers for testing + +=head1 SYNOPSIS + +use Business::CUSIP::Random; + +my $cusip; + +$cusip = Business::CUSIP::Random->generate; # returns a Business::CUSIP object + +# or... + +$cusip = Business::CUSIP::Random->generate_string; # returns a string + +=head1 DESCRIPTION + +Generates a random CUSIP (Committee on Uniform Security Identification Procedures) number for use in testing. + +=head1 METHODS + +=head2 generate + +=head2 generate_string + +Returns a randomly-generated, valid CUSIP number. + +generate() returns a Business::CUSIP object, while generate_string() returns a string. + +Takes the following optional parameter as a hash: + +=over 4 + +=item * B + +If true, the CUSIP generated will follow the format defined for fixed-income securities. + +=back + +=cut + +sub generate { + my ($class, %params) = @_; + + my $cusip = $class->rand_issuer_number(%params) . + $class->rand_issue_number(%params); + my $cusip_obj = Business::CUSIP->new($cusip, $params{fixed_income}); + $cusip .= $cusip_obj->check_digit; + $cusip_obj->cusip($cusip); + return $cusip_obj; +} + +sub generate_string { + my $class = shift; + return $class->generate(@_)->cusip; +} + +=head2 rand_issuer_number + +Returns a random CUSIP issuer number + +Takes the following optional parameter as a hash: + +=over 4 + +=item * B + +If true, the issuer number generated will follow the format defined for fixed-income securities. + +=back + +=cut + +sub rand_issuer_number { + my ($class, %params) = @_; + return $class->_pick(3, 0..9) . $class->_pick(3, 0..9, 'A'..'Z'); +} + +=head2 rand_issue_number + +Generates a random CUSIP issue number. + +Takes the following optional parameter as a hash: + +=over 4 + +=item * B + +If true, the issue number generated will follow the format defined for fixed-income securities. + +=back + +=cut + +sub rand_issue_number { + my ($class, %params) = @_; + return $params{fixed_income} ? $class->_pick(2, 'A'..'H', 'J'..'N', 'P'..'Z', 2..9) + : $class->_pick(1, 10..88); +} + +sub _pick { + my ($class, $count, @chars) = @_; + my $res = ''; + $res .= $chars[rand @chars] for (1..$count); + return $res; +} + +=head1 DEPENDENCIES + +Business::CUSIP + +=head1 AUTHORS + +Michael Aquilina + +Grant Street Group + +=head1 COPYRIGHT AND LICENSE + +Copyright (C) 2011 Michael Aquilina. + +This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. + +=cut + +1; + diff --git a/t/cusip.t b/t/cusip.t new file mode 100644 index 0000000..00d4132 --- /dev/null +++ b/t/cusip.t @@ -0,0 +1,23 @@ +use strict; +use warnings; + +use Business::CUSIP; +use Business::CUSIP::Random; + +use Test::More tests => 1002; + +srand($$); +for (1..1000) { + my $fixed_inc = int rand(2); + my $cusip = Business::CUSIP::Random->generate(fixed_income => $fixed_inc); + my $fixed_inc_msg = $fixed_inc ? ' fixed income' : ''; + my $message = $cusip->cusip . " is a valid$fixed_inc_msg CUSIP"; + ok($cusip->is_valid, $message); +} + +my $cusip = Business::CUSIP::Random->generate_string; +ok(! ref $cusip, 'generate_string returns a string'); + +$cusip = Business::CUSIP->new($cusip); +ok( $cusip->is_valid, 'generate_string returns a *valid* string'); +