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