6 if (! $Config{'useithreads'}) {
7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
12 use ExtUtils::testlib;
15 my ($id, $ok, $name) = @_;
17 # You have to do it this way or VMS will get confused.
19 print("ok $id - $name\n");
21 print("not ok $id - $name\n");
22 printf("# Failed test at line %d\n", (caller)[2]);
30 print("1..34\n"); ### Number of tests that will be run ###
37 ok($test++, 1, 'Loaded');
39 ### Start of Testing ###
42 my $x = shared_clone(14);
43 ok($test++, $x == 14, 'number');
45 $x = shared_clone('test');
46 ok($test++, $x eq 'test', 'string');
50 my %hsh = ('foo' => 2);
52 my $x = shared_clone(%hsh);
54 ok($test++, $@ =~ /Usage:/, '1 arg');
56 threads->create(sub {})->join(); # Hide leaks, etc.
61 my $foo :shared = shared_clone($x);
62 ok($test++, $foo eq 'test', 'cloned string');
64 $foo = shared_clone(\$x);
65 ok($test++, $$foo eq 'test', 'cloned scalar ref');
68 ok($test++, $$foo eq 'test', 'cloned scalar ref in thread');
76 $foo = shared_clone(\$foo);
77 ok($test++, ref($foo) eq 'REF', 'Circular ref typ');
78 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref');
81 ok($test++, threads::shared::_id($foo) == threads::shared::_id($$foo), 'Circular ref in thread');
84 $x = \$y; $y = \$z; $z = \$x;
85 $foo = shared_clone($x);
90 ok($test++, threads::shared::_id($$foo) == threads::shared::_id($$$$$foo),
91 'Cloned circular refs from thread');
95 my @ary = (qw/foo bar baz/);
96 my $ary = shared_clone(\@ary);
98 ok($test++, $ary->[1] eq 'bar', 'Cloned array');
100 ok($test++, $ary->[1] == 99, 'Clone mod');
101 ok($test++, $ary[1] eq 'bar', 'Original array');
103 threads->create(sub {
104 ok($test++, $ary->[1] == 99, 'Clone mod in thread');
107 $ary->[1] = 'thread';
112 ok($test++, $ary->[1] eq 'thread', 'Clone mod from thread');
113 ok($test++, $ary[1] eq 'bar', 'Original array');
117 my $hsh :shared = shared_clone({'foo' => [qw/foo bar baz/]});
118 ok($test++, is_shared($hsh), 'Shared hash ref');
119 ok($test++, is_shared($hsh->{'foo'}), 'Shared hash ref elem');
120 ok($test++, $$hsh{'foo'}[1] eq 'bar', 'Cloned structure');
124 my $obj = \do { my $bork = 99; };
126 Internals::SvREADONLY($$obj, 1) if ($] >= 5.008003);
128 my $bork = shared_clone($obj);
129 ok($test++, $$bork == 99, 'cloned scalar ref object');
130 ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
131 ok($test++, ref($bork) eq 'Bork', 'Object class');
133 threads->create(sub {
134 ok($test++, $$bork == 99, 'cloned scalar ref object in thread');
135 ok($test++, ($] < 5.008003) || Internals::SvREADONLY($$bork), 'read-only');
136 ok($test++, ref($bork) eq 'Bork', 'Object class');
146 'ary' => [ 1, 'foo', [ 86 ], { 'bar' => [ 'baz' ] } ],
150 $obj->{'self'} = $obj;
156 threads->create(sub {
157 $copy = shared_clone($obj);
159 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
160 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
161 ok($test++, is_shared($copy->{'ary'}->[2]), 'Shared element in cloned obj');
166 ok($test++, ref($copy) eq 'Foo', 'Obj cloned by thread');
167 ok($test++, ${$copy->{'ref'}} eq 'zip', 'Obj ref in thread');
168 ok($test++, threads::shared::_id($copy) == threads::shared::_id($copy->{'self'}), 'Circular ref in cloned obj');
169 ok($test++, $copy->{'ary'}->[3]->{'bar'}->[0] eq 'baz', 'Deeply cloned');
170 ok($test++, ref($copy) eq 'Foo', 'Cloned object class');