This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to threads-shared-1.03
[perl5.git] / ext / threads / shared / t / shared_attr.t
CommitLineData
7473853a 1use strict;
13c1b207 2use warnings;
81f1a921
AB
3
4BEGIN {
7473853a
SP
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);
81f1a921
AB
13 }
14}
15
7473853a 16use ExtUtils::testlib;
81f1a921
AB
17
18sub ok {
19 my ($id, $ok, $name) = @_;
7473853a
SP
20 if (! defined($name)) {
21 $name = '';
22 }
81f1a921
AB
23
24 # You have to do it this way or VMS will get confused.
7473853a
SP
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 }
81f1a921 31
7473853a 32 return ($ok);
81f1a921
AB
33}
34
7473853a
SP
35BEGIN {
36 $| = 1;
37 print("1..101\n"); ### Number of tests that will be run ###
38};
81f1a921 39
81f1a921
AB
40use threads;
41use threads::shared;
7473853a
SP
42ok(1, 1, 'Loaded');
43
44### Start of Testing ###
81f1a921
AB
45
46my $test_count;
47share($test_count);
48$test_count = 2;
49
50for(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");
7473853a 63
81f1a921
AB
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}
7473853a
SP
68
69my $shared :shared = &share({});
70$$shared{'foo'} = 'bar';
71
72for(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# EOF