1use strict; 2use warnings; 3 4BEGIN { 5 use Config; 6 if (! $Config{'useithreads'}) { 7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n"); 8 exit(0); 9 } 10} 11 12use ExtUtils::testlib; 13 14sub ok { 15 my ($id, $ok, $name) = @_; 16 17 # You have to do it this way or VMS will get confused. 18 if ($ok) { 19 print("ok $id - $name\n"); 20 } else { 21 print("not ok $id - $name\n"); 22 printf("# Failed test at line %d\n", (caller)[2]); 23 } 24 25 return ($ok); 26} 27 28BEGIN { 29 $| = 1; 30 print("1..44\n"); ### Number of tests that will be run ### 31}; 32 33use threads; 34use threads::shared; 35ok(1, 1, 'Loaded'); 36 37### Start of Testing ### 38 39my @foo; 40share(@foo); 41ok(2,1,"shared \@foo"); 42$foo[0] = "hi"; 43ok(3, $foo[0] eq 'hi', "Check assignment works"); 44$foo[0] = "bar"; 45ok(4, $foo[0] eq 'bar', "Check overwriting works"); 46ok(5, !defined $foo[1], "Check undef value"); 47$foo[2] = "test"; 48ok(6, $foo[2] eq "test", "Check extending the array works"); 49ok(7, !defined $foo[1], "Check undef value again"); 50ok(8, scalar(@foo) == 3, "Check the length of the array"); 51ok(9,$#foo == 2, "Check last element of array"); 52threads->create(sub { $foo[0] = "thread1" })->join; 53ok(10, $foo[0] eq "thread1", "Check that a value can be changed in another thread"); 54push(@foo, "another value"); 55ok(11, $foo[3] eq "another value", "Check that push works"); 56push(@foo, 1,2,3); 57ok(12, $foo[-1] == 3, "More push"); 58ok(13, $foo[-2] == 2, "More push"); 59ok(14, $foo[4] == 1, "More push"); 60threads->create(sub { push @foo, "thread2" })->join(); 61ok(15, $foo[7] eq "thread2", "Check push in another thread"); 62unshift(@foo, "start"); 63ok(16, $foo[0] eq "start", "Check unshift"); 64unshift(@foo, 1,2,3); 65ok(17, $foo[0] == 1, "Check multiple unshift"); 66ok(18, $foo[1] == 2, "Check multiple unshift"); 67ok(19, $foo[2] == 3, "Check multiple unshift"); 68threads->create(sub { unshift @foo, "thread3" })->join(); 69ok(20, $foo[0] eq "thread3", "Check unshift from another thread"); 70my $var = pop(@foo); 71ok(21, $var eq "thread2", "Check pop"); 72threads->create(sub { my $foo = pop @foo; ok(22, $foo == 3, "Check pop works in a thread")})->join(); 73$var = pop(@foo); 74ok(23, $var == 2, "Check pop after thread"); 75$var = shift(@foo); 76ok(24, $var eq "thread3", "Check shift"); 77threads->create(sub { my $foo = shift @foo; ok(25, $foo == 1, "Check shift works in a thread"); 78})->join(); 79$var = shift(@foo); 80ok(26, $var == 2, "Check shift after thread"); 81{ 82 my @foo2; 83 share @foo2; 84 my $empty = shift @foo2; 85 ok(27, !defined $empty, "Check shift on empty array"); 86 $empty = pop @foo2; 87 ok(28, !defined $empty, "Check pop on empty array"); 88} 89my $i = 0; 90foreach my $var (@foo) { 91 $i++; 92} 93ok(29, scalar @foo == $i, "Check foreach"); 94my $ref = \@foo; 95ok(30, $ref->[0] == 3, "Check reference access"); 96threads->create(sub { $ref->[0] = "thread4"})->join(); 97ok(31, $ref->[0] eq "thread4", "Check that it works after another thread"); 98undef($ref); 99threads->create(sub { @foo = () })->join(); 100ok(32, @foo == 0, "Check that array is empty"); 101ok(33, exists($foo[0]) == 0, "Check that zero index doesn't index"); 102@foo = ("sky"); 103ok(34, exists($foo[0]) == 1, "Check that zero index exists now"); 104ok(35, $foo[0] eq "sky", "And check that it also contains the right value"); 105$#foo = 20; 106$foo[20] = "sky"; 107ok(36, delete($foo[20]) eq "sky", "Check delete works"); 108 109threads->create(sub { delete($foo[0])})->join(); 110ok(37, !defined delete($foo[0]), "Check that delete works from a thread"); 111 112@foo = (1,2,3,4,5); 113 114{ 115 my ($t1,$t2) = @foo[2,3]; 116 ok(38, $t1 == 3, "Check slice"); 117 ok(39, $t2 == 4, "Check slice again"); 118 my @t1 = @foo[1...4]; 119 ok(40, $t1[0] == 2, "Check slice list"); 120 ok(41, $t1[2] == 4, "Check slice list 2"); 121 threads->create(sub { @foo[0,1] = ("hej","hop") })->join(); 122 ok(42,$foo[0] eq "hej", "Check slice assign"); 123} 124{ 125 eval { 126 my @t1 = splice(@foo,0,2,"hop", "hej"); 127 }; 128 ok(43, my $temp1 = $@ =~/Splice not implemented for shared arrays/, "Check that the warning message is correct for non splice"); 129} 130 131ok(44, is_shared(@foo), "Check for sharing"); 132 133exit(0); 134 135# EOF 136