#!/usr/bin/perl -w # test.pl - Don Yang (uguu.org) # # Generate all variations and check that output are as expected. # # 12/09/07 use strict; die "$0 \n" unless $#ARGV == 0; my $input_source = $ARGV[0]; my $output1 = "test.out.pl"; my $output2 = "test.out.gen.pl"; my $output3 = "test.out.gen2.pl"; for(my $i = 100000; $i < 100100; $i++) { # Load input my $file; open $file, "< $input_source" or die $!; my $code = join '', <$file>; close $file; # Generate output with modified timestamp $code =~ s/time\(\)/$i/; open $file, "> $output1" or die $!; print $file $code; close $file; my $cmd = "perl $output1 > $output2"; system $cmd; if( $? != 0 ) { die "$cmd returned $?\n"; } # Check that output is not empty open $file, "< $output2" or die $!; my $code2 = join '', <$file>; close $file; if( $code2 =~ /^\s*$/s ) { die "Empty output at iteration $i\n"; } # Check that output is executable $cmd = "perl -c -w $output2"; system $cmd; if( $? != 0 ) { die "$cmd returned $?\n"; } # Run output to generate final code $cmd = "perl $output2 > $output3"; system $cmd; if( $? != 0 ) { die "$cmd returned $?\n"; } # Check that final code is identical to first generated output open $file, "< $output3" or die $!; my $code3 = join '', <$file>; close $file; if( $code2 ne $code3 ) { die "Output mismatched at iteration $i\n"; } } # Check that 100 unique outputs are generated my %variations; for(my $i = 0; $i < 200; $i++) { # Load input my $file; open $file, "< $input_source" or die $!; my $code = join '', <$file>; close $file; # Generate output with modified timestamp $code = "use subs 'time';sub time{return $i;}\n" . $code; open $file, "> $output1" or die $!; print $file $code; close $file; my $cmd = "perl $output1|"; my $pipe; open $pipe, $cmd or die $!; my $code2 = join '', <$pipe>; close $pipe; # Check that output is unique for first 100 iterations, and cycles # for the next 100 iterations. if( $i < 100 ) { if( exists $variations{$code2} ) { die "Unexpected duplicate at iteration $i\n"; } $variations{$code2} = 1; } else { $variations{$code2}++; if( $variations{$code2} != 2 ) { die "Unexpected duplicate at iteration $i\n"; } } } # Cleanup unlink $output1; unlink $output2; unlink $output3;