File: GosperReplicate.t

package info (click to toggle)
libmath-planepath-perl 129-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 8,100 kB
  • sloc: perl: 115,748; ansic: 299; sh: 272; lisp: 73; makefile: 13
file content (401 lines) | stat: -rw-r--r-- 12,142 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
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
#!/usr/bin/perl -w

# Copyright 2017, 2018, 2019, 2020 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 => 59;

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

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

require Math::PlanePath::GosperReplicate;


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

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

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

  my $path = Math::PlanePath::GosperReplicate->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::GosperReplicate->new;
  ok ($path->n_start, 0, 'n_start()');
  ok ($path->x_negative, 1, 'x_negative()');
  ok ($path->y_negative, 1, 'y_negative()');
}


#------------------------------------------------------------------------------
# _digits_rotate_lowtohigh()
# _digits_unrotate_lowtohigh()

sub add_to_digit {
  my ($digit, $add) = @_;
  if ($digit) {
    $digit = ((($digit-1) + $add) % 6) + 1;
  }
  return $digit;
}
sub digits_rotate_lowtohigh_by_successive {
  my ($aref) = @_;
  my @new = @$aref;
  foreach my $i (0 .. $#$aref) {
    if ($aref->[$i]) {
      foreach my $j (0 .. $i-1) {   # below $i
        $new[$j] = add_to_digit($new[$j], $aref->[$i] - 1);
      }
    }
  }
  @$aref = @new;
}

{
  foreach my $elem (
                    [ [], [] ],
                    [ [0], [0] ],
                    [ [1], [1] ],
                    [ [6], [6] ],
                    [ [0,1], [0,1] ],
                    [ [6,1], [6,1] ],
                    [ [1,2], [2,2] ],
                    [ [3,3], [5,3] ],
                    [ [6,3], [2,3] ],
                    [ [1,1,2], [2,2,2] ],
                    [ [1,1,3], [3,3,3] ],
                    [ [1,1,4], [4,4,4] ],
                    [ [2,1,4], [5,4,4] ],
                    [ [1,2,4], [5,5,4] ],
                   ) {
    my ($digits, $rotated) = @$elem;
    {
      my @got = @$digits;
      Math::PlanePath::GosperReplicate::_digits_rotate_lowtohigh(\@got);
      ok(join(',',@got),join(',',@$rotated),
         "_digits_rotate_lowtohigh of ".join(',',@$digits));
    }
    {
      my @got = @$rotated;
      Math::PlanePath::GosperReplicate::_digits_unrotate_lowtohigh(\@got);
      ok(join(',',@got),join(',',@$digits),
         "_digits_unrotate_lowtohigh of ".join(',',@$rotated));
    }
  }
}

{
  # rotate / unrotate reversible
  my $bad = 0;
  foreach my $n (0 .. 7**3) {
    my @digits = Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7);
    my $digits = join(',',reverse @digits);

    my @rot = @digits;
    Math::PlanePath::GosperReplicate::_digits_rotate_lowtohigh(\@rot);
    my $rot = join(',',reverse @rot);

    my @rot_by_successive = @digits;
    digits_rotate_lowtohigh_by_successive(\@rot_by_successive);
    my $rot_by_successive = join(',',reverse @rot_by_successive);
    unless ($rot eq $rot_by_successive) {
      MyTestHelpers::diag("n=$n digits=$digits rot=$rot rot_by_successive=$rot_by_successive");
      if (++$bad >= 5) { last; }
    }

    my @unrot_again = @rot;
    Math::PlanePath::GosperReplicate::_digits_unrotate_lowtohigh(\@unrot_again);

    my $unrot_again = join(',',reverse @unrot_again);

    unless ($digits eq $unrot_again) {
      MyTestHelpers::diag("n=$n digits=$digits rot=$rot unrot_again=$unrot_again");
      if (++$bad >= 1) { last; }
    }
  }
  ok ($bad, 0);
}

#------------------------------------------------------------------------------
# boundary squares when rotate

{
  my @dir6_to_dx = (2, 1,-1,-2, -1, 1);
  my @dir6_to_dy = (0, 1, 1, 0, -1,-1);

  sub to_base7 {
    my ($n,$k) = @_;
    my @digits = reverse Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7);
    if (defined $k) {
      while (@digits < $k) { unshift @digits, 0; }
    }
    return join('',@digits);
  }

  my $path = Math::PlanePath::GosperReplicate->new
    (numbering_type => 'rotate');
  my $bad = 0;
 K: foreach my $k (0 .. 4) {
    my ($n_lo, $n_hi) = $path->level_to_n_range($k);
    ### $k
    ### $n_hi
    my $Bpred_by_path = sub {
      my ($n) = @_;
      my ($x,$y) = $path->n_to_xy($n);
      foreach my $dir6 (0 .. 5) {
        my $n2 = $path->xy_to_n ($x+$dir6_to_dx[$dir6],
                                 $y+$dir6_to_dy[$dir6]);
        ### $n2
        if ($n2 > $n_hi) { return 1; }
      }
      return 0;
    };
    my $Bpred_by_digits = sub {
      my ($n) = @_;
      my @digits = reverse    # high to low
        Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7);
      while (@digits < $k) { unshift @digits, 0; }
      foreach my $digit (@digits) {
        if ($digit == 0) { return 0; }
      }
      shift @digits;  # skip high
      foreach my $digit (@digits) {
        if ($digit == 4 || $digit == 5) { return 0; }
      }
      @digits = grep {$_!=1} @digits;  # ignore 1s
      foreach my $i (0 .. $#digits-1) {
        if (($digits[$i] == 3 && $digits[$i+1] == 2)
            || ($digits[$i] == 3 && $digits[$i+1] == 3)
            || ($digits[$i] == 6 && $digits[$i+1] == 6)) {
          return 0;
        }
      }
      return 1;
    };
    foreach my $n ($n_lo .. $n_hi) {
      my $by_path   = $Bpred_by_path->($n);
      my $by_digits = $Bpred_by_digits->($n);
      if ($by_path != $by_digits) {
        my ($x,$y) = $path->n_to_xy($n);
        my $n7 = to_base7($n,$k);
        MyTestHelpers::diag("k=$k n=$n [$n7] $x,$y path $by_path digits $by_digits   ($n_lo to $n_hi)");
        if (++$bad >= 10) { last K; }
      }
    }
  }
  ok ($bad, 0);
}


#------------------------------------------------------------------------------
# digit rotation per the POD

foreach my $numbering_type ('fixed','rotate') {
  my $path = Math::PlanePath::GosperReplicate->new
    (numbering_type => $numbering_type);
  my $bad = 0;
  my $k = 3;
  my $pow = 7**$k;
  foreach my $turn (0,1,2,5,6) {
    foreach my $n (0 .. $pow-1) {
      my ($x,$y) = $path->n_to_xy($n);
      my ($tx,$ty) = ($x,$y);
      foreach (1 .. $turn) { ($tx,$ty) = xy_rotate_plus60($tx,$ty); }
      my $txy = "$tx,$ty";
      my $an = digits_add($n,$turn, $numbering_type);
      my ($ax,$ay) = $path->n_to_xy($an);
      my $axy = "$ax,$ay";
      if ($txy ne $axy) {
        my $n7 = join('',reverse Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7));
        my $an7 = join('',reverse Math::PlanePath::Base::Digits::digit_split_lowtohigh($an,7));
        print "$numbering_type turn=$turn oops, n=$n [$n7] $x,$y turned $txy cf an=$an [$an7] $axy\n";
        $bad++;
      }
    }
  }
  ok ($bad, 0);
}

sub xy_rotate_plus60 {
  my ($x, $y) = @_;
  return (($x-3*$y)/2,  # rotate +60
          ($x+$y)/2);
}

sub digits_add {
  my ($n,$offset, $numbering_type) = @_;
  my @digits = Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7);
  foreach my $digit (reverse @digits) {
    if ($digit) {
      $digit = (($digit-1 + $offset)%6) + 1;  # mutate @digits
    }
    if ($numbering_type eq 'rotate') { last; }
  }
  return Math::PlanePath::Base::Digits::digit_join_lowtohigh (\@digits, 7);
}

#------------------------------------------------------------------------------
# numbering_type => 'rotate' sub-parts rotation

{
  my $path = Math::PlanePath::GosperReplicate->new (numbering_type=>'rotate');
  my $bad = 0;
  my $k = 3;
  my $pow = 7**$k;
  foreach my $n ($pow .. 2*$pow-1) {
    my ($x,$y) = $path->n_to_xy($n);
    {
      my ($rx,$ry) = $path->n_to_xy($n+$pow);
      ($rx,$ry) = xy_rotate_minus60($rx,$ry);
      my $got = "$rx,$ry";
      my $want = "$x,$y";
      if ($got ne $want) {
        print "oops, got $got want $want\n";
        $bad++;
      }
    }
    {
      my ($rx,$ry) = $path->n_to_xy($n+2*$pow);
      ($rx,$ry) = xy_rotate_minus120($rx,$ry);
      $bad += ("$rx,$ry" ne "$x,$y");
    }
  }
  ok ($bad, 0);
}

sub xy_rotate_minus60 {
  my ($x, $y) = @_;
  return (($x+3*$y)/2,  # rotate -60
          ($y-$x)/2);
}
sub xy_rotate_minus120 {
  my ($x, $y) = @_;
  return ((3*$y-$x)/2,              # rotate -120
          ($x+$y)/-2);
}

#------------------------------------------------------------------------------
# _digits_rotate_lowtohigh()
# _digits_unrotate_lowtohigh()

{
  foreach my $elem (
              [ [], [] ],
              [ [0], [0] ],
              [ [1], [1] ],
              [ [6], [6] ],
              [ [0,1], [0,1] ],
              [ [6,1], [6,1] ],
              [ [3,2], [4,2] ],
              [ [6,3], [2,3] ],
             ) {
    my ($digits, $rotated) = @$elem;
    {
      my @got = @$digits;
      Math::PlanePath::GosperReplicate::_digits_rotate_lowtohigh(\@got);
      ok(join(',',@got),join(',',@$rotated),
         "_digits_rotate_lowtohigh of ".join(',',@$digits));
    }
    {
      my @got = @$rotated;
      Math::PlanePath::GosperReplicate::_digits_unrotate_lowtohigh(\@got);
      ok(join(',',@got),join(',',@$digits),
         "_digits_unrotate_lowtohigh of ".join(',',@$rotated));
    }
  }
}


#------------------------------------------------------------------------------
# No, the replicate shape is each middle of the unit hexagons in GosperIslands.

# # Return true if $n is on the boundary of its expansion level (the level
# # which is the number of base-7 digits in $n).
# sub _rotate_Bpred {
#   my ($n) = @_;
#   my @digits = Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7)
#     or return 1;
#   my $prev = $digits[-1] || return 0;
#   foreach my $digit (reverse @digits[0 .. $#digits-1]) { # high to low
#     if ($digit == 1) {
#     } elsif ($digit == 0 || $digit == 4 || $digit == 5
#              || ($prev == 6 && $digit == 6)
#              || ($prev == 3 && ($digit == 2 || $digit == 3))) {
#       ### _rotate_Bpred(): "not prev=$prev digit=$digit"
#       return 0;
#     }
#     $prev = $digit;
#   }
#   return 1;
# }
# 
# {
#   require Math::PlanePath::GosperIslands;
#   my $islands = Math::PlanePath::GosperIslands->new;
# 
#   my $path = Math::PlanePath::GosperReplicate->new (numbering_type=>'rotate');
#   my $bad = 0;
#   foreach my $n (1 .. 7**2) {
#     my $rotate_Bpred =  _rotate_Bpred($n) ? 1 : 0;
#     my ($x,$y) = $path->n_to_xy($n);
#     my $island_pred  = $islands->xy_is_visited($x,$y) ? 1 : 0;
#     if ($rotate_Bpred ne $island_pred) {
#       print $islands->n_to_xy(7),"\n";
#       print $islands->xy_to_n(5,1),"\n";
#       print $islands->xy_is_visited(5,1),"\n";
#       my $n7 = join('',reverse Math::PlanePath::Base::Digits::digit_split_lowtohigh($n,7));
#       print "oops, n=$n [$n7] at $x,$y rotate $rotate_Bpred islands $island_pred\n";
#       $bad++;
#     }
#   }
#   ok ($bad, 0);
# }


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