File: TriangularHypot.t

package info (click to toggle)
libmath-planepath-perl 117-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 6,988 kB
  • ctags: 5,587
  • sloc: perl: 99,131; ansic: 299; sh: 233; lisp: 73; makefile: 4
file content (256 lines) | stat: -rw-r--r-- 7,415 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
#!/usr/bin/perl -w

# Copyright 2011, 2012, 2013, 2014 Kevin Ryde

# This file is part of Math-PlanePath.
#
# Math-PlanePath is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# Math-PlanePath is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
# or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
# for more details.
#
# You should have received a copy of the GNU General Public License along
# with Math-PlanePath.  If not, see <http://www.gnu.org/licenses/>.

use 5.004;
use strict;
use Test;
plan tests => 22;

use lib 't';
use MyTestHelpers;
BEGIN { MyTestHelpers::nowarnings(); }

# uncomment this to run the ### lines
#use Smart::Comments;

require Math::PlanePath::TriangularHypot;


#------------------------------------------------------------------------------
# VERSION

{
  my $want_version = 117;
  ok ($Math::PlanePath::TriangularHypot::VERSION, $want_version,
      'VERSION variable');
  ok (Math::PlanePath::TriangularHypot->VERSION,  $want_version,
      'VERSION class method');

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

  my $path = Math::PlanePath::TriangularHypot->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");
}

#------------------------------------------------------------------------------
# n_start, x_negative, y_negative

{
  my $path = Math::PlanePath::TriangularHypot->new;
  ok ($path->n_start, 1, 'n_start()');
  ok ($path->x_negative, 1, 'x_negative()');
  ok ($path->y_negative, 1, 'y_negative()');

  my @pnames = map {$_->{'name'}} $path->parameter_info_list;
  ok (join(',',@pnames), 'points,n_start');
}


#------------------------------------------------------------------------------
# all x,y covered and distinct n

foreach my $points ('hex_centred','hex','odd','even','all') {
  my $path = Math::PlanePath::TriangularHypot->new (points => $points);
  my $bad = 0;
  my %seen;
  my $xlo = -10;
  my $xhi = 10;
  my $ylo = -10;
  my $yhi = 10;
  my ($nlo, $nhi) = $path->rect_to_n_range($xlo,$ylo, $xhi,$yhi);
  my $count = 0;
 OUTER: for (my $x = $xlo; $x <= $xhi; $x++) {
    for (my $y = $ylo; $y <= $yhi; $y++) {
      my $n = $path->xy_to_n ($x,$y);

      if ($points eq 'even') {
        if (($x ^ $y) & 1) {
          if (defined $n) {
            MyTestHelpers::diag ("$points: x=$x,y=$y is odd should be n=undef");
            last if $bad++ > 10;
          }
          next;
        }
      } elsif ($points eq 'odd') {
        if (! (($x ^ $y) & 1)) {
          if (defined $n) {
            MyTestHelpers::diag ("$points: x=$x,y=$y is even should be n=undef");
            last if $bad++ > 10;
          }
          next;
        }
      } elsif ($points eq 'hex') {
        if (! xy_is_hex($x,$y)) {
          if (defined $n) {
            MyTestHelpers::diag ("$points: x=$x,y=$y is not hex should be n=undef");
            last if $bad++ > 10;
          }
          next;
        }
      } elsif ($points eq 'hex_rotated') {
        if (! xy_is_hex_rotated($x,$y)) {
          if (defined $n) {
            MyTestHelpers::diag ("$points: x=$x,y=$y is not hex_rotated should be n=undef");
            last if $bad++ > 10;
          }
          next;
        }
      } elsif ($points eq 'hex_centred') {
        if (! xy_is_hex_centred($x,$y)) {
          if (defined $n) {
            my $m = ($x+3*$y) % 6;
            MyTestHelpers::diag ("$points: x=$x,y=$y is not hex_centred should be n=undef (m=$m)");
            last if $bad++ > 10;
          }
          next;
        }
      }

      if (! defined $n) {
        MyTestHelpers::diag ("x=$x,y=$y n=undef");
        last OUTER if $bad++ > 10;
        next;
      }
      if ($seen{$n}) {
        MyTestHelpers::diag ("x=$x,y=$y n=$n seen before at $seen{$n}");
        last if $bad++ > 10;
      }
      if ($n < $nlo) {
        MyTestHelpers::diag ("x=$x,y=$y n=$n below nlo=$nlo");
        last OUTER if $bad++ > 10;
      }
      if ($n > $nhi) {
        MyTestHelpers::diag ("x=$x,y=$y n=$n above nhi=$nhi");
        last OUTER if $bad++ > 10;
      }
      $seen{$n} = "$x,$y";
      $count++;
    }
  }
  ok ($bad, 0, "$points xy_to_n() coverage and distinct, $count points");
}

# "hex" is X+3*Y==0or2
# test against 0 to allow for "%" sign varying under "use integer"
sub xy_is_hex {
  my ($x,$y) = @_;
  return (($x+3*$y) % 6 == 0
          || ($x+3*$y-2) % 6 == 0);
}

# "hex_rotated" is X+3*Y==0or4
# test against 0 to allow for "%" sign varying under "use integer"
sub xy_is_hex_rotated {
  my ($x,$y) = @_;
  return (($x+3*$y) % 6 == 0
          || ($x+3*$y-4) % 6 == 0);
}

# "hex_centred" is X+3*Y==2or4
# test against 0 to allow for "%" sign varying under "use integer"
sub xy_is_hex_centred {
  my ($x,$y) = @_;
  return (($x+3*$y-2) % 6 == 0
          || ($x+3*$y-4) % 6 == 0);
}

#------------------------------------------------------------------------------
# monotonic hypotenuse

# (sqrt(3)/2 * y)^2 + (x/2)^2
#     = 3/4 * y^2 + 1/4 * x^2
#     = 1/4 * (3*y^2 + x^2)
sub hex_hypot {
  my ($x, $y) = @_;
  return 3*$y*$y + $x*$x;
}

foreach my $points ('hex_rotated','hex_centred','hex','odd','even','all') {
  my $path = Math::PlanePath::TriangularHypot->new (points => $points);
  my $bad = 0;
  my $n = $path->n_start;
  my ($x,$y) = $path->n_to_xy($n);
  my $h = hex_hypot($x,$y);
  while (++$n < 5000) {
    my ($x2,$y2) = $path->n_to_xy ($n);

    if ($points eq 'even') {
      if (($x2 ^ $y2) & 1) {
        if (defined $n) {
          MyTestHelpers::diag ("$points: x2=$x2,y2=$y2 is odd");
          last if $bad++ > 10;
        }
        next;
      }
    } elsif ($points eq 'odd') {
      if (! (($x2 ^ $y2) & 1)) {
        if (defined $n) {
          MyTestHelpers::diag ("$points: x2=$x2,y2=$y2 is even");
          last if $bad++ > 10;
        }
        next;
      }
    }

    my $h2 = hex_hypot($x2,$y2);
    ### xy: "$x2,$y2  is $h2"
    if ($h2 < $h) {
      MyTestHelpers::diag ("n=$n x=$x2,y=$y2 h=$h2 < prev h=$h x=$x,y=$y");
      last if $bad++ > 10;
    }
    if ($points eq 'even') {
      # odd,all don't always turn left
      if ($n > 2 && ! _turn_func_Left($x,$y, $x2,$y2)) {
        MyTestHelpers::diag ("$points: not turn left at n=$n x=$x2,y=$y2 prev x=$x,y=$y");
        last if $bad++ > 10;
      }
    }
    $h = $h2;
    $x = $x2;
    $y = $y2;
  }
  ok ($bad, 0, "$points: n_to_xy() hypot non-decreasing");
}

sub _turn_func_Left {
  my ($dx,$dy, $next_dx,$next_dy) = @_;
  ### _turn_func_Left() ...
  my $a = $next_dy * $dx;
  my $b = $next_dx * $dy;
  return ($a > $b
          || abs($dx)==abs($next_dx) && abs($dy)==abs($next_dy)  # 0 or 180
          ? 1
          : 0);
}

#------------------------------------------------------------------------------
exit 0;