Blame t/just_plain_nasty.t

Packit 14c646
#!/usr/bin/perl
Packit 14c646
Packit 14c646
# This is a test suite to cover all the nasty and horrible data
Packit 14c646
# structures that cause bizarre corner cases.
Packit 14c646
Packit 14c646
#  Everyone's invited! :-D
Packit 14c646
Packit 14c646
sub BEGIN {
Packit 14c646
    unshift @INC, 't';
Packit 14c646
    unshift @INC, 't/compat' if $] < 5.006002;
Packit 14c646
    require Config; import Config;
Packit 14c646
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
Packit 14c646
        print "1..0 # Skip: Storable was not built\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
use strict;
Packit 14c646
BEGIN {
Packit 14c646
    if (!eval q{
Packit 14c646
        use Test::More;
Packit 14c646
        use B::Deparse 0.61;
Packit 14c646
        use 5.006;
Packit 14c646
        1;
Packit 14c646
    }) {
Packit 14c646
        print "1..0 # skip: tests only work with B::Deparse 0.61 and at least perl 5.6.0\n";
Packit 14c646
        exit;
Packit 14c646
    }
Packit 14c646
    require File::Spec;
Packit 14c646
    if ($File::Spec::VERSION < 0.8) {
Packit 14c646
        print "1..0 # Skip: newer File::Spec needed\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
use Storable qw(freeze thaw);
Packit 14c646
Packit 14c646
$Storable::flags = Storable::FLAGS_COMPAT;
Packit 14c646
Packit 14c646
#$Storable::DEBUGME = 1;
Packit 14c646
BEGIN {
Packit 14c646
    plan tests => 34;
Packit 14c646
}
Packit 14c646
Packit 14c646
{
Packit 14c646
    package Banana;
Packit 14c646
    use overload   
Packit 14c646
	'<=>' => \&compare,
Packit 14c646
	    '==' => \&equal,
Packit 14c646
		'""' => \&real,
Packit 14c646
		fallback => 1;
Packit 14c646
    sub compare { return int(rand(3))-1 };
Packit 14c646
    sub equal { return 1 if rand(1) > 0.5 }
Packit 14c646
    sub real { return "keep it so" }
Packit 14c646
}
Packit 14c646
Packit 14c646
my (@a);
Packit 14c646
Packit 14c646
for my $dbun (1, 0) {  # dbun - don't be utterly nasty - being utterly
Packit 14c646
                       # nasty means having a reference to the object
Packit 14c646
                       # directly within itself. otherwise it's in the
Packit 14c646
                       # second array.
Packit 14c646
    my $nasty = [
Packit 14c646
		 ($a[0] = bless [ ], "Banana"),
Packit 14c646
		 ($a[1] = [ ]),
Packit 14c646
		];
Packit 14c646
Packit 14c646
    $a[$dbun]->[0] = $a[0];
Packit 14c646
Packit 14c646
    is(ref($nasty), "ARRAY", "Sanity found (now to play with it :->)");
Packit 14c646
Packit 14c646
    $Storable::Deparse = $Storable::Deparse = 1;
Packit 14c646
    $Storable::Eval = $Storable::Eval = 1;
Packit 14c646
Packit 14c646
    headit("circular overload 1 - freeze");
Packit 14c646
    my $icicle = freeze $nasty;
Packit 14c646
    #print $icicle;   # cat -ve recommended :)
Packit 14c646
    headit("circular overload 1 - thaw");
Packit 14c646
    my $oh_dear = thaw $icicle;
Packit 14c646
    is(ref($oh_dear), "ARRAY", "dclone - circular overload");
Packit 14c646
    is($oh_dear->[0], "keep it so", "amagic ok 1");
Packit 14c646
    is($oh_dear->[$dbun]->[0], "keep it so", "amagic ok 2");
Packit 14c646
Packit 14c646
    headit("closure dclone - freeze");
Packit 14c646
    $icicle = freeze sub { "two" };
Packit 14c646
    #print $icicle;
Packit 14c646
    headit("closure dclone - thaw");
Packit 14c646
    my $sub2 = thaw $icicle;
Packit 14c646
    is($sub2->(), "two", "closures getting dcloned OK");
Packit 14c646
Packit 14c646
    headit("circular overload, after closure - freeze");
Packit 14c646
    #use Data::Dumper;
Packit 14c646
    #print Dumper $nasty;
Packit 14c646
    $icicle = freeze $nasty;
Packit 14c646
    #print $icicle;
Packit 14c646
    headit("circular overload, after closure - thaw");
Packit 14c646
    $oh_dear = thaw $icicle;
Packit 14c646
    is(ref($oh_dear), "ARRAY", "dclone - after a closure dclone");
Packit 14c646
    is($oh_dear->[0], "keep it so", "amagic ok 1");
Packit 14c646
    is($oh_dear->[$dbun]->[0], "keep it so", "amagic ok 2");
Packit 14c646
Packit 14c646
    push @{$nasty}, sub { print "Goodbye, cruel world.\n" };
Packit 14c646
    headit("closure freeze AFTER circular overload");
Packit 14c646
    #print Dumper $nasty;
Packit 14c646
    $icicle = freeze $nasty;
Packit 14c646
    #print $icicle;
Packit 14c646
    headit("circular thaw AFTER circular overload");
Packit 14c646
    $oh_dear = thaw $icicle;
Packit 14c646
    is(ref($oh_dear), "ARRAY", "dclone - before a closure dclone");
Packit 14c646
    is($oh_dear->[0], "keep it so", "amagic ok 1");
Packit 14c646
    is($oh_dear->[$dbun]->[0], "keep it so", "amagic ok 2");
Packit 14c646
Packit 14c646
    @{$nasty} = @{$nasty}[0, 2, 1];
Packit 14c646
    headit("closure freeze BETWEEN circular overload");
Packit 14c646
    #print Dumper $nasty;
Packit 14c646
    $icicle = freeze $nasty;
Packit 14c646
    #print $icicle;
Packit 14c646
    headit("circular thaw BETWEEN circular overload");
Packit 14c646
    $oh_dear = thaw $icicle;
Packit 14c646
    is(ref($oh_dear), "ARRAY", "dclone - between a closure dclone");
Packit 14c646
    is($oh_dear->[0], "keep it so", "amagic ok 1");
Packit 14c646
    is($oh_dear->[$dbun?2:0]->[0], "keep it so", "amagic ok 2");
Packit 14c646
Packit 14c646
    @{$nasty} = @{$nasty}[1, 0, 2];
Packit 14c646
    headit("closure freeze BEFORE circular overload");
Packit 14c646
    #print Dumper $nasty;
Packit 14c646
    $icicle = freeze $nasty;
Packit 14c646
    #print $icicle;
Packit 14c646
    headit("circular thaw BEFORE circular overload");
Packit 14c646
    $oh_dear = thaw $icicle;
Packit 14c646
    is(ref($oh_dear), "ARRAY", "dclone - after a closure dclone");
Packit 14c646
    is($oh_dear->[1], "keep it so", "amagic ok 1");
Packit 14c646
    is($oh_dear->[$dbun+1]->[0], "keep it so", "amagic ok 2");
Packit 14c646
}
Packit 14c646
Packit 14c646
sub headit {
Packit 14c646
Packit 14c646
    return;  # comment out to get headings - useful for scanning
Packit 14c646
             # output with $Storable::DEBUGME = 1
Packit 14c646
Packit 14c646
    my $title = shift;
Packit 14c646
Packit 14c646
    my $size_left = (66 - length($title)) >> 1;
Packit 14c646
    my $size_right = (67 - length($title)) >> 1;
Packit 14c646
Packit 14c646
    print "# ".("-" x $size_left). " $title "
Packit 14c646
	.("-" x $size_right)."\n";
Packit 14c646
}
Packit 14c646