1#!/usr/bin/perl -w
2
3# Test is_deeply and friends with circular data structures [rt.cpan.org 7289]
4
5BEGIN {
6    if( $ENV{PERL_CORE} ) {
7        chdir 't';
8        @INC = ('../lib', 'lib');
9    }
10    else {
11        unshift @INC, 't/lib';
12    }
13}
14
15use strict;
16use Test::More tests => 11;
17
18my $a1 = [ 1, 2, 3 ];
19push @$a1, $a1;
20my $a2 = [ 1, 2, 3 ];
21push @$a2, $a2;
22
23is_deeply $a1, $a2;
24ok( eq_array ($a1, $a2) );
25ok( eq_set   ($a1, $a2) );
26
27my $h1 = { 1=>1, 2=>2, 3=>3 };
28$h1->{4} = $h1;
29my $h2 = { 1=>1, 2=>2, 3=>3 };
30$h2->{4} = $h2;
31
32is_deeply $h1, $h2;
33ok( eq_hash  ($h1, $h2) );
34
35my ($r, $s);
36
37$r = \$r;
38$s = \$s;
39
40ok( eq_array ([$s], [$r]) );
41
42
43{
44    # Classic set of circular scalar refs.
45    my($a,$b,$c);
46    $a = \$b;
47    $b = \$c;
48    $c = \$a;
49
50    my($d,$e,$f);
51    $d = \$e;
52    $e = \$f;
53    $f = \$d;
54
55    is_deeply( $a, $a );
56    is_deeply( $a, $d );
57}
58
59
60{
61    # rt.cpan.org 11623
62    # Make sure the circular ref checks don't get confused by a reference 
63    # which is simply repeating.
64    my $a = {};
65    my $b = {};
66    my $c = {};
67
68    is_deeply( [$a, $a], [$b, $c] );
69    is_deeply( { foo => $a, bar => $a }, { foo => $b, bar => $c } );
70    is_deeply( [\$a, \$a], [\$b, \$c] );
71}
72