threads::shared 1.22
[p5sagit/p5-mst-13.2.git] / ext / threads / shared / t / shared_attr.t
1 use strict;
2 use warnings;
3
4 BEGIN {
5     if ($ENV{'PERL_CORE'}){
6         chdir 't';
7         unshift @INC, '../lib';
8     }
9     use Config;
10     if (! $Config{'useithreads'}) {
11         print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
12         exit(0);
13     }
14 }
15
16 use ExtUtils::testlib;
17
18 sub ok {
19     my ($id, $ok, $name) = @_;
20     if (! defined($name)) {
21         $name = '';
22     }
23
24     # You have to do it this way or VMS will get confused.
25     if ($ok) {
26         print("ok $id - $name\n");
27     } else {
28         print("not ok $id - $name\n");
29         printf("# Failed test at line %d\n", (caller)[2]);
30     }
31
32     return ($ok);
33 }
34
35 BEGIN {
36     $| = 1;
37     print("1..101\n");   ### Number of tests that will be run ###
38 };
39
40 use threads;
41 use threads::shared;
42 ok(1, 1, 'Loaded');
43
44 ### Start of Testing ###
45
46 my $test_count;
47 share($test_count);
48 $test_count = 2;
49
50 for(1..10) {
51     my $foo : shared = "foo";
52     ok($test_count++, $foo eq "foo");
53     threads->create(sub { $foo = "bar" })->join();
54     ok($test_count++, $foo eq "bar");
55     my @foo : shared = ("foo","bar");
56     ok($test_count++, $foo[1] eq "bar");
57     threads->create(sub { ok($test_count++, shift(@foo) eq "foo")})->join();
58     ok($test_count++, $foo[0] eq "bar");
59     my %foo : shared = ( foo => "bar" );
60     ok($test_count++, $foo{foo} eq "bar");
61     threads->create(sub { $foo{bar} = "foo" })->join();
62     ok($test_count++, $foo{bar} eq "foo");
63
64     threads->create(sub { $foo{array} = \@foo})->join();
65     threads->create(sub { push @{$foo{array}}, "baz"})->join();
66     ok($test_count++, $foo[-1] eq "baz");
67 }
68
69 my $shared :shared = &share({});
70 $$shared{'foo'} = 'bar';
71
72 for(1..10) {
73   my $str1 = "$shared";
74   my $str2 = "$shared";
75   ok($test_count++, $str1 eq $str2, 'stringify');
76   $str1 = $$shared{'foo'};
77   $str2 = $$shared{'foo'};
78   ok($test_count++, $str1 eq $str2, 'contents');
79 }
80
81 exit(0);
82
83 # EOF