Test arrays containing references, and references to shared arrays.
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / hv_refs.t
CommitLineData
0dc5f2bb 1BEGIN {
2# chdir 't' if -d 't';
3# push @INC ,'../lib';
4 require Config; import Config;
5 unless ($Config{'useithreads'}) {
6 print "1..0 # Skip: no useithreads\n";
7 exit 0;
8 }
9}
10
11
12sub ok {
13 my ($id, $ok, $name) = @_;
14
15 # You have to do it this way or VMS will get confused.
16 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
17
18 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
19
20 return $ok;
21}
22
6b85e4fe 23sub skip {
24 my ($id, $ok, $name) = @_;
25 print "ok $id # skip _thrcnt - $name \n";
26}
0dc5f2bb 27
28use ExtUtils::testlib;
29use strict;
938785a2 30BEGIN { print "1..17\n" };
0dc5f2bb 31use threads;
9c4972d9 32use threads::shared qw(:DEFAULT _thrcnt _refcnt _id);
0dc5f2bb 33ok(1,1,"loaded");
34my $foo;
35share($foo);
36my %foo;
37share(%foo);
38$foo{"foo"} = \$foo;
39ok(2, ${$foo{foo}} == undef, "Check deref");
40$foo = "test";
41ok(3, ${$foo{foo}} eq "test", "Check deref after assign");
42threads->create(sub{${$foo{foo}} = "test2";})->join();
43ok(4, $foo eq "test2", "Check after assign in another thread");
9c4972d9 44skip(5, _thrcnt($foo) == 2, "Check refcount");
0dc5f2bb 45my $bar = delete($foo{foo});
46ok(6, $$bar eq "test2", "check delete");
9c4972d9 47skip(7, _thrcnt($foo) == 1, "Check refcount after delete");
409b1fd3 48threads->create( sub {
9c4972d9 49 my $test;
50 share($test);
51 $test = "thread3";
52 $foo{test} = \$test;
53 })->join();
6b85e4fe 54ok(8, ${$foo{test}} eq "thread3", "Check reference created in another thread");
409b1fd3 55my $gg = $foo{test};
56$$gg = "test";
6b85e4fe 57ok(9, ${$foo{test}} eq "test", "Check reference");
9c4972d9 58skip(10, _thrcnt($gg) == 2, "Check refcount");
409b1fd3 59my $gg2 = delete($foo{test});
9c4972d9 60skip(11, _thrcnt($gg) == 1, "Check refcount");
61ok(12, _id($gg) == _id($gg2),
62 sprintf("Check we get the same thing (%x vs %x)",
63 _id($$gg),_id($$gg2)));
409b1fd3 64ok(13, $$gg eq $$gg2, "And check the values are the same");
65ok(14, keys %foo == 0, "And make sure we realy have deleted the values");
938785a2 66{
67 my (%hash1, %hash2);
68 share(%hash1);
69 share(%hash2);
70 $hash1{hash} = \%hash2;
71 $hash2{"bar"} = "foo";
72 ok(15, $hash1{hash}->{bar} eq "foo", "Check hash references work");
73 threads->create(sub { $hash2{"bar2"} = "foo2"})->join();
74 ok(16, $hash1{hash}->{bar2} eq "foo2", "Check hash references work");
75 threads->create(sub { my (%hash3); share(%hash3); $hash2{hash} = \%hash3; $hash3{"thread"} = "yes"})->join();
76 ok(17, $hash1{hash}->{hash}->{thread} eq "yes", "Check hash created in another thread");
77}
409b1fd3 78