Math-PlanePath-Toothpick

 view release on metacpan or  search on metacpan

t/ToothpickTree.t  view on Meta::CPAN

  my $check_version = $want_version + 1000;
  ok (! eval { Math::PlanePath::ToothpickTree->VERSION($check_version); 1 },
      1,
      "VERSION class check $check_version");

  my $path = Math::PlanePath::ToothpickTree->new;
  ok ($path->VERSION,  $want_version, 'VERSION object method');

  ok (eval { $path->VERSION($want_version); 1 },
      1,
      "VERSION object check $want_version");
  ok (! eval { $path->VERSION($check_version); 1 },
      1,
      "VERSION object check $check_version");
}



#------------------------------------------------------------------------------
# level_to_n_range()

foreach my $elem ([ '4',         [10,42         ], [3,7,15] ],
                  [ '3',         [ 8,32         ], [3,7,15] ],
                  [ '2',         [ 4,20         ], [2,6,14,30] ],
                  [ '1',         [ 1, 9, 41     ], [1,5,13,29] ],
                  [ 'octant',    [ 0, 6, 26     ], [0,4,12,28] ],
                  [ 'octant_up', [ 0, 6, 26     ], [0,4,12,28] ],
                  [ 'wedge',     [ 5,17, 57,201 ], [3,7,15,31] ],
                  [ 'two_horiz', [15,47,175     ], [3,7,15,31] ],
                 ) {
  my ($parts, $level_n_aref, $depth_aref) = @$elem;
  my $path = Math::PlanePath::ToothpickTree->new (parts => $parts);
  foreach my $level (0 .. $#$level_n_aref) {
    my $want_n_hi = $level_n_aref->[$level];
    # $level should run 0 to $want_n_hi inclusive

    my ($n_lo,$n_hi) = $path->level_to_n_range($level);
    ok ($n_lo, 0);
    ok ($n_hi, $want_n_hi, "parts=$parts level=$level");
    {
      my $got_level = $path->n_to_level($n_hi);
      ok ($level, $got_level);
    }
    {
      my $n = $n_hi+1;
      my $got_level = $path->n_to_level($n);
      ok ($got_level, $level+1,
         "parts=$parts level=$level n=$n got_level=$got_level");
    }
  }
  foreach my $level (0 .. $#$depth_aref) {
    my $depth = $depth_aref->[$level];
    my $n_end = $path->tree_depth_to_n_end($depth);
    my ($n_lo,$n_hi) = $path->level_to_n_range($level);
    ok ($n_hi, $n_end);
  }
}


#------------------------------------------------------------------------------
# oct() formulas

{
  my $path = Math::PlanePath::ToothpickTree->new (parts => 'octant');
  sub octant {
    my ($d) = @_;
    die "oops octant($d)" if $d < 2;
    return $path->tree_depth_to_n($d-2);
  }
}
{
  sub quadrant {
    my ($d) = @_;
    die "oops quadrant($d)" if $d < 2;
    if ($d == 2) { return 0; }
    return octant($d) + octant($d-1) - $d + 3;
  }
  my $path = Math::PlanePath::ToothpickTree->new (parts => '1');
  foreach my $d (2 .. 20) {
    my $p = $path->tree_depth_to_n($d-2);
    my $q = quadrant($d);
    ok ($p, $q);
  }
}
{
  sub half {
    my ($d) = @_;
    die "oops half($d)" if $d < 1;
    if ($d == 1) { return 0; }
    return 2*quadrant($d) + 1;
  }
  my $path = Math::PlanePath::ToothpickTree->new (parts => '2');
  foreach my $d (1 .. 20) {
    my $p = $path->tree_depth_to_n($d-1);
    my $h = half($d);
    ok ($p, $h);
  }
}
{
  sub full {
    my ($d) = @_;
    if ($d == 0) { return 0; }
    if ($d == 1) { return 1; }
    return 4*quadrant($d) + 3;
  }
  my $path = Math::PlanePath::ToothpickTree->new (parts => '4');
  foreach my $d (0 .. 20) {
    my $p = $path->tree_depth_to_n($d);
    my $f = full($d);
    ok ($p, $f);
  }
}
{
  sub corner3_a {
    my ($d) = @_;
    if ($d == 0) { return 0; }
    if ($d == 1) { return 1; }
    return quadrant($d+1) + 2*quadrant($d) + 2;
  }
  sub corner3_b {
    my ($d) = @_;



( run in 0.797 second using v1.01-cache-2.11-cpan-39bf76dae61 )