5 if ($ENV{'PERL_CORE'}){
7 unshift @INC, '../lib';
10 if (! $Config{'useithreads'}) {
11 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
16 use ExtUtils::testlib;
19 my ($id, $ok, $name) = @_;
21 # You have to do it this way or VMS will get confused.
23 print("ok $id - $name\n");
25 print("not ok $id - $name\n");
26 printf("# Failed test at line %d\n", (caller)[2]);
34 print("1..34\n"); ### Number of tests that will be run ###
41 ok($test++, 1, 'Loaded');
43 ### Start of Testing ###
46 my $x = shared_clone(14);
47 ok($test++, $x == 14, 'number');
49 $x = shared_clone('test');
50 ok($test++, $x eq 'test', 'string');
54 my %hsh = ('foo' => 2);
56 my $x = shared_clone(%hsh);
58 ok($test++, $@ =~ /Usage:/, '1 arg');
60 threads->create(sub {})->join(); # Hide leaks, etc.
65 my $foo :shared = shared_clone($x);
66 ok($test++, $foo eq 'test', 'cloned string');
68 $foo = shared_clone(\$x);
69 ok($test++, $$foo eq 'test', 'cloned scalar ref');
72 ok($test++, $$foo eq 'test', 'cloned scalar ref in thread');
80 $foo = shared_clone(\$foo);
81 ok($test++, ref($foo) eq 'REF', 'Circular ref typ');
82 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref');
85 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref in thread');
88 $x = \$y; $y = \$z; $z = \$x;
89 $foo = shared_clone($x);
94 ok($test++, threads::shared::_id($$foo) == threads::shared::_id($$$$$foo),
95 'Cloned circular refs from thread');
99 my @ary = (qw/foo bar baz/);
100 my $ary = shared_clone(\@ary);
102 ok($test++, $ary->[1] eq 'bar', 'Cloned array');
104 ok($test++, $ary->[1] == 99, 'Clone mod');
105 ok($test++, $ary[1] eq 'bar', 'Original array');
107 threads->create(sub {
108 ok($test++, $ary->[1] == 99, 'Clone mod in thread');
111 $ary->[1] = 'thread';
116 ok($test++, $ary->[1] eq 'thread', 'Clone mod from thread');
117 ok($test++, $ary[1] eq 'bar', 'Original array');
121 my $hsh :shared = shared_clone({'foo' => [qw/foo bar baz/]});
122 ok($test++, is_shared($hsh), 'Shared hash ref');
123 ok($test++, is_shared($hsh->{'foo'}), 'Shared hash ref elem');
124 ok($test++, $$hsh{'foo'}[1] eq 'bar', 'Cloned structure');
128 my $obj = \do { my $bork = 99; };
130 Internals::SvREADONLY($$obj, 1) if ($] >= 5.008003);
132 my $bork = shared_clone($obj);
133 ok($test++, $$bork == 99, 'cloned scalar ref object');
134 ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
135 ok($test++, ref($bork) eq 'Bork', 'Object class');
137 threads->create(sub {
138 ok($test++, $$bork == 99, 'cloned scalar ref object in thread');
139 ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
140 ok($test++, ref($bork) eq 'Bork', 'Object class');
150 'ary' => [ 1, 'foo', [ 86 ], { 'bar' => [ 'baz' ] } ],
154 $obj->{'self'} = $obj;
160 threads->create(sub {
161 $copy = shared_clone($obj);
163 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
164 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
165 ok($test++, is_shared($copy->{'ary'}->[2]), 'Shared element in cloned obj');
170 ok($test++, ref($copy) eq 'Foo', 'Obj cloned by thread');
171 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
172 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
173 ok($test++, $copy->{'ary'}->[3]->{'bar'}->[0] eq 'baz', 'Deeply cloned');
174 ok($test++, ref($copy) eq 'Foo', 'Cloned object class');