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..28\n"); ### Number of tests that will be run ###
41 ok($test++, 1, 'Loaded');
43 ### Start of Testing ###
47 my $x = shared_clone(14);
48 ok($test++, $x == 14, 'number');
50 $x = shared_clone('test');
51 ok($test++, $x eq 'test', 'string');
55 my %hsh = ('foo' => 2);
57 my $x = shared_clone(%hsh);
59 ok($test++, $@ =~ /Usage:/, '1 arg');
61 threads->create(sub {})->join(); # Hide leaks, etc.
66 my $foo :shared = shared_clone($x);
67 ok($test++, $foo eq 'test', 'cloned string');
69 $foo = shared_clone(\$x);
70 ok($test++, $$foo eq 'test', 'cloned scalar ref');
73 ok($test++, $$foo eq 'test', 'cloned scalar ref in thread');
81 $foo = shared_clone(\$foo);
82 ok($test++, ref($foo) eq 'REF', 'Circular ref typ');
83 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref');
86 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref in thread');
89 $x = \$y; $y = \$z; $z = \$x;
90 $foo = shared_clone($x);
95 ok($test++, threads::shared::_id($$foo) == threads::shared::_id($$$$$foo),
96 'Cloned circular refs from thread');
100 my @ary = (qw/foo bar baz/);
101 my $ary = shared_clone(\@ary);
103 ok($test++, $ary->[1] eq 'bar', 'Cloned array');
105 ok($test++, $ary->[1] == 99, 'Clone mod');
106 ok($test++, $ary[1] eq 'bar', 'Original array');
108 threads->create(sub {
109 ok($test++, $ary->[1] == 99, 'Clone mod in thread');
112 $ary->[1] = 'thread';
117 ok($test++, $ary->[1] eq 'thread', 'Clone mod from thread');
118 ok($test++, $ary[1] eq 'bar', 'Original array');
125 'ary' => [ 1, 'foo', [ 86 ], { 'bar' => [ 'baz' ] } ],
129 $obj->{'self'} = $obj;
135 threads->create(sub {
136 $copy = shared_clone($obj);
138 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
139 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
140 ok($test++, is_shared($copy->{'ary'}->[2]), 'Shared element in cloned obj');
145 ok($test++, ref($copy) eq 'Foo', 'Obj cloned by thread');
146 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
147 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
148 ok($test++, $copy->{'ary'}->[3]->{'bar'}->[0] eq 'baz', 'Deeply cloned');
149 ok($test++, ref($copy) eq 'Foo', 'Cloned object class');
153 my $hsh :shared = shared_clone({'foo' => [qw/foo bar baz/]});
154 ok($test++, is_shared($hsh), 'Shared hash ref');
155 ok($test++, is_shared($hsh->{'foo'}), 'Shared hash ref elem');
156 ok($test++, $$hsh{'foo'}[1] eq 'bar', 'Cloned structure');