File: run_test.pl

package info (click to toggle)
icheck 0.9.7-9
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,352 kB
  • sloc: perl: 12,152; makefile: 202; ansic: 100
file content (338 lines) | stat: -rwxr-xr-x 8,640 bytes parent folder | download | duplicates (6)
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
#!/usr/bin/perl

use strict;
use 5.6.0;
use warnings;

use File::Spec;
use POSIX;
use Text::Diff;
use File::Find;
use IO::Handle;
use IPC::Open3;
use IO::File;

my $test = shift or die "No test supplied\n";

-d $test or die "No such directory $test\n";

my $args = File::Spec->catfile($test, 'args');
my $args_canonify = File::Spec->catfile($test, 'args.canonify');
my $args_compare = File::Spec->catfile($test, 'args.compare');
my $baseline = File::Spec->catfile($test, 'baseline');
my $canonical = File::Spec->catfile($test, 'canonical');
my $errors = File::Spec->catfile($test, 'errors');
my $errors_canonify = File::Spec->catfile($test, 'errors.canonify');
my $errors_compare = File::Spec->catfile($test, 'errors.compare');
my $original = File::Spec->catfile($test, 'original');
my $result = File::Spec->catfile($test, 'result');
my $result_canonify = File::Spec->catfile($test, 'result.canonify');
my $result_compare = File::Spec->catfile($test, 'result.compare');
my $source = File::Spec->catfile($test, 'source');
my $diff = File::Spec->catfile($test, 'diff');

my @sources;
if (-d $source)
  {
    find(sub {return $File::Find::prune = 1 if /^\../ or /~$/;
              push @sources, $File::Find::name if -f $_}, $source);
  }
elsif (-f $source)
  {
    push @sources, $source;
  }
else
  {
    undef $source;
  }

undef $args unless -f $args;
undef $args_canonify unless -f $args_canonify;
undef $args_compare unless -f $args_compare;
undef $baseline unless -f $baseline;
undef $canonical unless -f $canonical;
undef $errors unless -f $errors;
undef $errors_canonify unless -f $errors_canonify;
undef $errors_compare unless -f $errors_compare;
undef $original unless -f $original;
undef $result unless -f $result;
undef $result_canonify unless -f $result_canonify;
undef $result_compare unless -f $result_compare;
undef $diff unless -f $diff;

my $canonify = 0;

$canonify = 1 if $source and $canonical;
$canonify = 1 if $original and $diff and $source and not $canonical;

my $compare = 0;

$compare = 1 if ($canonical or $canonify) and $original and $diff;

my $fail = 0;

sub compare_str
  {
    my $desc = shift;
    my $expect = shift;
    my $got = shift;

    return if $expect eq $got;

    print "Differences in $desc\n";
    if (length $expect)
      {
        diff \$expect, \$got, {STYLE => 'Unified', OUTPUT => \*STDOUT};
      }
    else
      {
        print "Expected nothing, got:\n";
        print "---\n";
        print $got;
        print "---\n"
      }
    $fail = 1;
  }

sub compare_int
  {
    my $desc = shift;
    my $expect = shift;
    my $got = shift;

    return if $expect == $got;

    print "Differences in $desc: expected $expect, got $got\n";
    $fail = 1;
  }

my $canonical_data;
if ($canonify)
  {
    my $expect_result = 0;
    if ($result_canonify)
      {
        my $fh = new IO::File $result_canonify, 'r';
        $expect_result = <$fh>;
        chomp $expect_result;
      }
    elsif ($result)
      {
        my $fh = new IO::File $result, 'r';
        $expect_result = <$fh>;
        chomp $expect_result;
      }

    my $expect_errors = "";
    if ($errors_canonify)
      {
        my $fh = new IO::File $errors_canonify, 'r';
        $expect_errors .= $_ while <$fh>;
      }
    elsif ($errors)
      {
        my $fh = new IO::File $errors, 'r';
        $expect_errors .= $_ while <$fh>;
      }

    my @args;
    if ($args_canonify)
      {
        my $fh = new IO::File $args_canonify, 'r';
        while (<$fh>) {chomp; push @args, $_}
      }
    elsif ($args)
      {
        my $fh = new IO::File $args, 'r';
        while (<$fh>) {chomp; push @args, $_}
      }

    my $canonify_output = "";
    my $canonify_errors = "";
    my $canonify_result;

    {
      my ($write, $read, $err) = (new IO::Handle, new IO::Handle, new IO::Handle);
      my @cmd = ('./icheck', '--canonify',
                 $baseline ? ('--baseline', $baseline) : (),
                 @sources, @args);
      if ($ENV{TEST_VERBOSE})
        {
          print join(' ', @cmd) . "\n";
        }
      my $pid = open3($write, $read, $err, @cmd);

      close $write;

      $canonify_output .= $_ while <$read>;
      close $read;

      $canonify_errors .= $_ while <$err>;
      close $err;

      waitpid $pid, 0;
      if (WIFSIGNALED($?))
        {
          die "icheck killed by signal " . WTERMSIG($?) . "\n";
        }

      unless (WIFEXITED($?))
        {
          die "icheck died wierdly (waitpid gave $?)\n";
        }

      $canonify_result = WEXITSTATUS($?);
    }

    compare_str('canonify errors', $expect_errors, $canonify_errors);
    compare_int('canonify result', $expect_result, $canonify_result);

    if ($canonical)
      {
        $canonical_data = '';
        my $fh = new IO::File $canonical, 'r';
        $canonical_data .= $_ while <$fh>;
        compare_str('canonify output', $canonical_data, $canonify_output);
      }
    else
      {
        $canonical_data = $canonify_output;
      }

    # Now we check that the canonical form is in fact canonical -
    # canonify(canonify(x)) == canonify(x) for all x
    if ($canonical)
      {
        my ($canonify2_output, $canonify2_errors, $canonify2_result) = ("", "", undef);

        {
          my ($write, $read, $err) = (new IO::Handle, new IO::Handle, new IO::Handle);
          my @cmd = ('./icheck', '--canonify',
                     $baseline ? ('--baseline', $baseline) : (),
                     $baseline ? ($baseline) : (), $canonical);
          if ($ENV{TEST_VERBOSE})
            {
              print join(' ', @cmd) . "\n";
            }
          my $pid = open3($write, $read, $err, @cmd);

          close $write;

          $canonify2_output .= $_ while <$read>;
          close $read;

          $canonify2_errors .= $_ while <$err>;
          close $err;

          waitpid $pid, 0;
          if (WIFSIGNALED($?))
            {
              die "icheck killed by signal " . WTERMSIG($?) . "\n";
            }

          unless (WIFEXITED($?))
            {
              die "icheck died wierdly (waitpid gave $?)\n";
            }

          $canonify2_result = WEXITSTATUS($?);
        }

        # A canonical form *must* parse correctly, so this can't have errors
        compare_str('second canonify output', $canonical_data, $canonify2_output);
        compare_str('second canonify errors', '', $canonify2_errors);
        compare_int('second canonify result', 0, $canonify2_result);
      }
  }

if ($compare)
  {
    my $expect_result = 0;
    if ($result_compare)
      {
        my $fh = new IO::File $result_compare, 'r';
        $expect_result = <$fh>;
        chomp $expect_result;
      }
    elsif ($result)
      {
        my $fh = new IO::File $result, 'r';
        $expect_result = <$fh>;
        chomp $expect_result;
      }

    my $expect_errors = "";
    if ($errors_compare)
      {
        my $fh = new IO::File $errors_compare, 'r';
        $expect_errors .= $_ while <$fh>;
      }
    elsif ($errors)
      {
        my $fh = new IO::File $errors, 'r';
        $expect_errors .= $_ while <$fh>;
      }

    my @args;
    if ($args_compare)
      {
        my $fh = new IO::File $args_compare, 'r';
        while (<$fh>) {chomp; push @args, $_}
      }
    elsif ($args)
      {
        my $fh = new IO::File $args, 'r';
        while (<$fh>) {chomp; push @args, $_}
      }

    my $compare_output = "";
    my $compare_errors = "";
    my $compare_result;

    {
      my ($write, $read, $err) = (new IO::Handle, new IO::Handle, new IO::Handle);
      my @cmd = ('./icheck', '--compare', $original,
                 $canonical ? $canonical : '-',
                 @args);
      if ($ENV{TEST_VERBOSE})
        {
          print join(' ', @cmd) . "\n";
        }
      my $pid = open3($write, $read, $err, @cmd);

      if (not $canonical)
        {
          print $write $canonical_data;
        }
      close $write;

      $compare_output .= $_ while <$read>;
      close $read;

      $compare_errors .= $_ while <$err>;
      close $err;

      waitpid $pid, 0;
      if (WIFSIGNALED($?))
        {
          die "icheck killed by signal " . WTERMSIG($?) . "\n";
        }

      unless (WIFEXITED($?))
        {
          die "icheck died wierdly (waitpid gave $?)\n";
        }

      $compare_result = WEXITSTATUS($?);
    }

    compare_str('compare errors', $expect_errors, $compare_errors);
    compare_int('compare result', $expect_result, $compare_result);

    my $diff_data = '';
    my $fh = new IO::File $diff, 'r';
    $diff_data .= $_ while <$fh>;
    compare_str('compare output', $diff_data, $compare_output);
  }

exit 1 if $fail;