threads::shared 1.22
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / sv_simple.t
CommitLineData
7473853a 1use strict;
13c1b207 2use warnings;
b050c948 3
4BEGIN {
7473853a 5 if ($ENV{'PERL_CORE'}){
6 chdir 't';
7 unshift @INC, '../lib';
8 }
9 use Config;
10 if (! $Config{'useithreads'}) {
6c791b15 11 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
7473853a 12 exit(0);
b050c948 13 }
14}
15
7473853a 16use ExtUtils::testlib;
b050c948 17
18sub ok {
19 my ($id, $ok, $name) = @_;
20
21 # You have to do it this way or VMS will get confused.
7473853a 22 if ($ok) {
23 print("ok $id - $name\n");
24 } else {
25 print("not ok $id - $name\n");
26 printf("# Failed test at line %d\n", (caller)[2]);
27 }
b050c948 28
7473853a 29 return ($ok);
b050c948 30}
31
7473853a 32BEGIN {
33 $| = 1;
34 print("1..11\n"); ### Number of tests that will be run ###
35};
b050c948 36
b050c948 37use threads;
38use threads::shared;
7473853a 39ok(1, 1, 'Loaded');
40
41### Start of Testing ###
42
b050c948 43my $test = "bar";
44share($test);
45ok(2,$test eq "bar","Test magic share fetch");
46$test = "foo";
47ok(3,$test eq "foo","Test magic share assign");
6b85e4fe 48my $c = threads::shared::_refcnt($test);
b050c948 49threads->create(
7473853a 50 sub {
51 ok(4, $test eq "foo","Test magic share fetch after thread");
52 $test = "baz";
6b85e4fe 53 ok(5,threads::shared::_refcnt($test) > $c, "Check that threadcount is correct");
7473853a 54 })->join();
b050c948 55ok(6,$test eq "baz","Test that value has changed in another thread");
6b85e4fe 56ok(7,threads::shared::_refcnt($test) == $c,"Check thrcnt is down properly");
b050c948 57$test = "barbar";
58ok(8, length($test) == 6, "Check length code");
59threads->create(sub { $test = "barbarbar" })->join;
60ok(9, length($test) == 9, "Check length code after different thread modified it");
61threads->create(sub { undef($test)})->join();
62ok(10, !defined($test), "Check undef value");
63
7473853a 64ok(11, is_shared($test), "Check for sharing");
b050c948 65
6c791b15 66exit(0);
67
7473853a 68# EOF