File Coverage

ex/sourcing.pl
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1 1     1   2674 use v5.24;
  1         3  
2 1     1   5 use warnings;
  1         1  
  1         43  
3 1     1   5 use Test::More;
  1         1  
  1         7  
4 1     1   221 use Q::S::L qw(superpos fetch_matches with_sources);
  1         1  
  1         290  
5              
6             sub power_sources
7             {
8 4     4   8 my ($number) = @_;
9              
10             # produce all the possible bases end exponents
11 4         37 my $possible_bases = superpos(2 .. sqrt $number);
12 4         806 my $possible_exponents = superpos(2 .. sqrt $number);
13              
14             # produce all possible powers
15 4         789 my $possible_powers = $possible_bases**$possible_exponents;
16              
17             # for every state, get those that match a condition
18             # (any possible power that equals the number)
19             return fetch_matches {
20 4         16 with_sources { $possible_powers == $number }
21 4     4   989 };
  4         24  
22             }
23              
24             my %numbers = (
25              
26             # number => [base, exponent]
27             65536 => [[2, 16], [4, 8], [16, 4], [256, 2]],
28             9 => [[3, 2]],
29             81 => [[3, 4], [9, 2]],
30             3 => [],
31             );
32              
33             while (my ($number, $power) = each %numbers) {
34              
35             # this will be a superposition of all valid powers
36             my $power_superposition = power_sources $number;
37              
38             # there should be 1 or 0 resulting states
39             my $state = $power_superposition->states->[0];
40              
41             if (!defined $state) {
42             is scalar $power->@*, 0;
43             }
44             else {
45             my @sources = $state->source->@*;
46             is scalar @sources, scalar $power->@*;
47              
48             # did we succeed?
49             FACTOR:
50             foreach my $factor ($power->@*) {
51             my ($base, $exponent) = $factor->@*;
52              
53             foreach my $source (@sources) {
54             next FACTOR
55             if $source->[0] eq $base && $source->[1] eq $exponent;
56             }
57             fail;
58             }
59             }
60             }
61              
62             done_testing;
63              
64             __END__