File Coverage

blib/lib/App/Math/Tutor/Role/Power.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 5 5 100.0
pod n/a
total 20 20 100.0


line stmt bran cond sub pod time code
1             package App::Math::Tutor::Role::Power;
2              
3 1     1   663 use warnings;
  1         2  
  1         25  
4 1     1   4 use strict;
  1         2  
  1         24  
5              
6             =head1 NAME
7              
8             App::Math::Tutor::Role::Power - role for power mathematics
9              
10             =cut
11              
12 1     1   5 use Moo::Role;
  1         1  
  1         5  
13 1     1   276 use App::Math::Tutor::Numbers;
  1         2  
  1         38  
14 1     1   4 use Module::Runtime qw/use_module/;
  1         1  
  1         6  
15              
16             with "App::Math::Tutor::Role::VulFrac"; # for _check_vulgar_fraction
17              
18             our $VERSION = '0.004';
19              
20             sub _check_power_to
21             {
22             return $_[0]->basis != 0 and $_[0]->basis != 1 and $_[0]->exponent != 0;
23             }
24              
25             has power_types => (
26             is => "lazy",
27             );
28              
29             requires "format";
30              
31             sub _build_power_types
32             {
33             return [
34             {
35             name => "power",
36             numbers => 1,
37             builder => sub { return int( rand( $_[1] ) + 1 ); },
38             },
39             {
40             name => "sqrt",
41             numbers => 1,
42             builder => sub {
43             return
44             VulFrac->new( num => 1,
45             denum => int( rand( $_[1] ) + 1 ) );
46             },
47             },
48             {
49             name => "power+sqrt",
50             numbers => 2,
51             builder => sub {
52             my $vf;
53             do
54             {
55             $vf = VulFrac->new( num => int( rand( $_[1] ) + 1 ),
56             denum => int( rand( $_[1] ) + 1 ) );
57             } while ( !$_[0]->_check_vulgar_fraction($vf) );
58             return $vf;
59             },
60             },
61             ];
62             }
63              
64             sub _guess_power_to
65             {
66             my ( $max_basis, $max_exponent ) = @{ $_[0]->format };
67             my @types = @{ $_[0]->power_types };
68             my $type = int( rand( scalar @types ) );
69             my ( $basis, $exponent ) =
70             ( int( rand($max_basis) ), $types[$type]->{builder}->( $_[0], $max_exponent ) );
71             return
72             Power->new(
73             basis => $basis,
74             exponent => $exponent,
75             mode => int( rand(2) )
76             );
77             }
78              
79             =head1 METHODS
80              
81             =head2 get_power_to
82              
83             Returns as many powers as requested. Does Factory :)
84              
85             =cut
86              
87             sub get_power_to
88             {
89             my ( $self, $amount ) = @_;
90             my @result;
91              
92             while ( $amount-- )
93             {
94             my $pt;
95             do
96             {
97             $pt = $self->_guess_power_to;
98             } while ( !_check_power_to($pt) );
99              
100             push @result, $pt;
101             }
102              
103             return @result;
104             }
105              
106             =head1 LICENSE AND COPYRIGHT
107              
108             Copyright 2010-2014 Jens Rehsack.
109              
110             This program is free software; you can redistribute it and/or modify it
111             under the terms of either: the GNU General Public License as published
112             by the Free Software Foundation; or the Artistic License.
113              
114             See http://dev.perl.org/licenses/ for more information.
115              
116             =cut
117              
118             1;