Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Aquilina committed Jul 13, 2011
0 parents commit 754e9f9
Show file tree
Hide file tree
Showing 6 changed files with 204 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -0,0 +1,4 @@
Revision history for Perl extension Business::CUSIP::Random

0.01 Tue Jul 12 20:27:21 2011
- original version
6 changes: 6 additions & 0 deletions MANIFEST
@@ -0,0 +1,6 @@
Changes
Makefile.PL
MANIFEST
README
t/cusip.t
lib/Business/CUSIP/Random.pm
12 changes: 12 additions & 0 deletions 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 <aquilina@cpan.org>') : ()),
);
23 changes: 23 additions & 0 deletions 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.
136 changes: 136 additions & 0 deletions 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<fixed_income>
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<fixed_income>
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<fixed_income>
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 <aquilina@cpan.org>
Grant Street Group <developers@grantstreet.com>
=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;

23 changes: 23 additions & 0 deletions 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');

0 comments on commit 754e9f9

Please sign in to comment.