use strict; use warnings; use Benchmark qw( cmpthese ); my $tests = -25; my $rv = ''; ################################################################################ sub array { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. my @outcome_tests = ( [ 97, 100, '99' ], [ 93, 97, '95' ], [ 85, 93, '90' ], [ 75, 85, '80' ], [ 65, 75, '70' ], [ 55, 65, '60' ], [ 45, 55, '50' ], [ 35, 45, '40' ], [ 25, 35, '30' ], [ 15, 25, '20' ], [ 7, 15, '10' ], [ 3, 7, '05' ], [ 0, 3, '00' ], ); TEST: { foreach my $test ( @outcome_tests ) { my ($lower, $upper, $category) = @{ $test }; if (($outcome > $lower && $outcome <= $upper)) { $rv .= $category; last TEST; } } $rv = "DSPAM_ERROR"; } } ################################################################################ sub hash { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. my %h = map { $_ => 10 * int ( ( $_ + 4 ) /10 ) } 8 .. 93; @h{1 .. 3} = (0) x 3; @h{4 .. 7} = (5) x 5; @h{94 .. 97} = (95) x 4; @h{98 .. 100} = (99) x 3; $rv .= $h{$outcome}; } ################################################################################ sub if_else { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. if ( $outcome > 97 ) { $rv .= "99"; } elsif ( $outcome > 93 ) { $rv .= "95"; } elsif ( $outcome > 85 ) { $rv .= "90"; } elsif ( $outcome > 75 ) { $rv .= "80"; } elsif ( $outcome > 65 ) { $rv .= "70"; } elsif ( $outcome > 55 ) { $rv .= "60"; } elsif ( $outcome > 45 ) { $rv .= "50"; } elsif ( $outcome > 35 ) { $rv .= "40"; } elsif ( $outcome > 25 ) { $rv .= "30"; } elsif ( $outcome > 15 ) { $rv .= "20"; } elsif ( $outcome > 7 ) { $rv .= "10"; } elsif ( $outcome > 3 ) { $rv .= "05"; } elsif ( $outcome > 0 ) { $rv .= "00"; } else { $rv = "DSPAM_ERROR"; } } ################################################################################ sub O_1_lookup { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. my @lookup = ( '00', map { ( $_->[2] ) x ( $_->[ 1 ] - $_->[ 0 ] ) } [ 0, 3, '00' ], [ 3, 7, '05' ], [ 7, 15, '10' ], [ 15, 25, '20' ], [ 25, 35, '30' ], [ 35, 45, '40' ], [ 45, 55, '50' ], [ 55, 65, '60' ], [ 65, 75, '70' ], [ 75, 85, '80' ], [ 85, 93, '90' ], [ 93, 97, '95' ], [ 97, 100, '99' ] ); $rv .= $lookup[ $outcome ]; } ################################################################################ sub ternary { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. $rv .= $outcome > 97 ? 99 : $outcome > 93 ? 95 : $outcome > 85 ? 90 : $outcome > 75 ? 80 : $outcome > 65 ? 70 : $outcome > 55 ? 60 : $outcome > 45 ? 50 : $outcome > 35 ? 40 : $outcome > 25 ? 30 : $outcome > 15 ? 20 : $outcome > 7 ? 10 : $outcome > 3 ? '05' : $outcome > 0 ? '00' : q{DSPAM_ERROR}; } ################################################################################ sub binsearch { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. # for my $outcome (@tests) { unless ($outcome > 0 && $outcome < 101) { $rv = 'ERROR'; next; } if ($outcome < 46) { if ($outcome < 16) { if ($outcome < 4) { $rv .= '00'; } else { $rv .= $outcome > 7 ? 10 : '05'; } } elsif ($outcome < 26) { $rv .= 20; } else { $rv .= $outcome > 35 ? 40 : 30; } } elsif ($outcome < 76) { if ($outcome < 56) { $rv .= 50; } else { $rv .= $outcome > 65 ? 70 : 60; } } elsif ($outcome < 94) { $rv .= $outcome > 85 ? 90 : 80; } else { $rv .= $outcome > 97 ? 99 : 95; } # } } ################################################################################ sub substring { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. # for my $outcome (@tests) { my $sol = '000000000505050510101010101010102020202020202020303030303030303040404040404040405050505050505050606060606060606070707070707070708080808080808080909090909090909095959595999999'; if ($outcome > 0 && $outcome < 101) { $rv .= substr($sol, $outcome, 2); } else { $rv = 'ERROR'; } # } } ################################################################################ sub lr { my $outcome = int( rand( 100 ) + 1 ); # Generate some test data. # for my $outcome (@tests) { if ($outcome > 0 && $outcome < 101) { if ($outcome > 3 && $outcome < 7) { $rv = '05'; } else { my $sol = (int(($outcome / 10) + .49) * 10); if ($outcome > 93) { $sol -= $outcome > 97 ? 1 : 5; } $rv .= $sol; } } else { $rv = 'ERROR'; } # } } ################################################################################ cmpthese($tests, { array => \&array, hash => \&hash, if_else => \&if_else, O_1_lookup => \&O_1_lookup, ternary => \&ternary, binsearch => \&binsearch, substr => \&substring, lr => \&lr, });