File: BigObject.pm

package info (click to toggle)
polymake 4.14-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 35,888 kB
  • sloc: cpp: 168,933; perl: 43,407; javascript: 31,575; ansic: 3,007; java: 2,654; python: 632; sh: 268; xml: 117; makefile: 61
file content (178 lines) | stat: -rw-r--r-- 7,188 bytes parent folder | download | duplicates (2)
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
#  Copyright (c) 1997-2024
#  Ewgenij Gawrilow, Michael Joswig, and the polymake team
#  Technische Universität Berlin, Germany
#  https://polymake.org
#
#  This program 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 2, or (at your option) any
#  later version: http://www.gnu.org/licenses/gpl.txt.
#
#  This program 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.
#-------------------------------------------------------------------------------

use strict;
use namespaces;
use warnings qw(FATAL void syntax misc);

package Polymake::Test::BigObject;

use Polymake::Struct(
   [ '@ISA' => 'Case' ],
   [ new => '$$%' ],
   [ '$gotten' => '#2' ],
   [ '$ignore' => '#%', default => 'undef' ],
   [ '$permuted' => '#%', default => 'undef' ],
   [ '$expected' => '#%', default => 'undef' ],
   [ '$after_cleanup' => '#%' ],
);

sub execute {
   my ($self) = @_;
   my $expected = ($self->expected //= $self->load_object_file);
   my $gotten = $self->gotten;
   if (instanceof Core::BigObject($gotten)) {
      if (defined $self->permuted) {
         ref($self->permuted) eq "ARRAY" && @{$self->permuted}>0
           or croak( "option `permuted' must provide a non-empty list of property names" );
         my @explicit_perm;
         my @prop0 = $expected->type->encode_descending_path($self->permuted->[0]);
         if ($prop0[-1]->flags & Core::Property::Flags::is_permutation) {
            shift @{$self->permuted};
            push @explicit_perm, permutation => \@prop0;
         }
         @{$self->permuted}
           or croak( "option `permuted' should specify at least one data property" );
         my @perm_defining_props = map { $_ => $expected->lookup($_) // croak( "property $_ lacking in object ", $expected->name ) } @{$self->permuted};
         $gotten = $gotten->copy_permuted(@explicit_perm, @perm_defining_props);
      }
      $gotten->cleanup_now if $self->after_cleanup;
   }
   not($self->fail_log = compare_and_report($expected, $gotten, defined($self->ignore) ? (ignore => $self->ignore) : ()));
}

sub load_object_file {
   my ($self) = @_;
   if (is_object(my $obj = $self->name)) {
      $self->name = $obj->name;
      $obj
   } else {
      my $group = $self->subgroup->group;
      $group->env->load_object_file($self->name, $group->application);
   }
}

sub compare_and_report {
   my ($expected, $gotten, @options) = @_;
   if (ref($expected) ne ref($gotten)) {
      return join("",
                  "result type mismatch:\n",
                  "  expected: ", $expected->type->full_name, "\n",
                  "       got: ", instanceof Core::BigObject($gotten) ? $gotten->type->full_name : ref($gotten) || (defined($gotten) ? "SCALAR" : "UNDEF"), "\n");
   }
   if (my @diff = $expected->diff($gotten, @options)) {
      return join("", print_diff($expected, @diff));
   }
   if (my @diff = diff_attachments($expected, $gotten, @options)) {
      return join("", @diff);
   }
   if (length($expected->description)) {
      if (length($gotten->description)) {
         if ($expected->description ne $gotten->description) {
            return "different descriptions: expected '".$expected->description."'\n".
                   "                             got '".$gotten->description.  "'\n";
         }
      } else {
         return "lacking description\n";
      }
   } elsif (length($gotten->description)) {
      return "unexpected description\n";
   }
   ""
}

sub print_diff {
   my $object = shift;
   map {
      my ($expected, $gotten, $subobj) = @$_;
      my $prefix = "";
      while (defined($subobj) && $subobj != $object) {
         $prefix = $subobj->property->name.".".$prefix;
         $subobj = $subobj->parent;
      }
      if (!defined($expected)) {
         ("unexpected property $prefix", $gotten->property->name, "\n")
      } elsif (!defined($gotten)) {
         ("lacking property $prefix", $expected->property->name, "\n")
      } elsif (instanceof Core::PropertyValue::Multiple($expected)) {
         ("multiple subobject number mismatch for property $prefix", $expected->property->name, ":\n",
          "  expected: ", scalar @{$expected->values}, "\n",
          "       got: ", scalar @{$gotten->values}, "\n")
      } elsif (instanceof Core::BigObject($expected->value)) {
         ("object type mismatch for property $prefix", $expected->property->name, ":\n",
          "  expected ", $expected->value->type->full_name, "\n",
          "       got ", $gotten->value->type->full_name, "\n")
      } else {
         my ($expected_val, $gotten_val) = map { substr($_,-1) eq "\n" ? "\n$_" : " $_\n" } ($expected->toString, $gotten->toString);
         my ($expected_canned, $gotten_canned) = map { Core::CPlusPlus::get_canned_cpp_class($_) } ($expected->value, $gotten->value);

         ("different property $prefix", $expected->property->name, ":\n",
          ref($expected->value) ne ref($gotten->value) || $expected_canned ne $gotten_canned
          ? ("  expected: ", ref($expected->value), ($expected_canned && " (=>$expected_canned)"), ":", $expected_val, "\n",
             "       got: ", ref($gotten->value),     ($gotten_canned && " (=>$gotten_canned)"),   ":", $gotten_val, "\n")
          : ("  expected:$expected_val\n",
             "       got:$gotten_val\n"))
      }
   } @_;
}

sub diff_attachments {
   my ($expected, $gotten, %options) = @_;
   my $ignore = $options{ignore};
   my @diff;
   while (my ($name, $at) = each %{$expected->attachments}) {
      next if defined($ignore) && string_list_index($ignore, $name) >= 0;
      if (defined(my $gotten_at = $gotten->attachments->{$name})) {
         if (my $report = Value::compare_and_report($at->[0], $gotten_at->[0], Core::BigObject::ignore_subproperties($ignore, $name))) {
            push @diff, "attachment $name:\n$report\n";
         }
      } else {
         push @diff, "attachment $name: MISSING\n";
      }
   }
   if ($ignore || keys(%{$expected->attachments}) != keys(%{$gotten->attachments})) {
      push @diff, map { "attachment $_: UNEXPECTED\n" }
                  grep { not(exists($expected->attachments->{$_}) || string_list_index($ignore, $_) >= 0)  }
                       keys %{$gotten->attachments};
   }
   @diff
}

####################################################################################
package Polymake::Test::BigObject::Transform;

use Polymake::Struct(
   [ '@ISA' => 'BigObject' ],
   [ new => '$%' ],
);

sub execute {
   my ($self) = @_;
   my $expected = $self->load_object_file;
   local $Verbose::files = 0;
   my $group = $self->subgroup->group;
   my $transformed = load Core::Datafile($group->env->find_object_file($self->name."-in", $group->application));
   # don't update the input file
   $transformed->dont_save;
   not( $self->fail_log = compare_and_report($expected, $transformed) );
}

1

# Local Variables:
# cperl-indent-level:3
# indent-tabs-mode:nil
# End: