Eviscerate README.macos to match the state of the world
[p5sagit/p5-mst-13.2.git] / dist / threads-shared / t / av_simple.t
1 use strict;
2 use warnings;
3
4 BEGIN {
5     use Config;
6     if (! $Config{'useithreads'}) {
7         print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8         exit(0);
9     }
10 }
11
12 use ExtUtils::testlib;
13
14 sub 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
28 BEGIN {
29     $| = 1;
30     print("1..44\n");   ### Number of tests that will be run ###
31 };
32
33 use threads;
34 use threads::shared;
35 ok(1, 1, 'Loaded');
36
37 ### Start of Testing ###
38
39 my @foo;
40 share(@foo);
41 ok(2,1,"shared \@foo");
42 $foo[0] = "hi";
43 ok(3, $foo[0] eq 'hi', "Check assignment works");
44 $foo[0] = "bar";
45 ok(4, $foo[0] eq 'bar', "Check overwriting works");
46 ok(5, !defined $foo[1], "Check undef value");
47 $foo[2] = "test";
48 ok(6, $foo[2] eq "test", "Check extending the array works");
49 ok(7, !defined $foo[1], "Check undef value again");
50 ok(8, scalar(@foo) == 3, "Check the length of the array");
51 ok(9,$#foo == 2, "Check last element of array");
52 threads->create(sub { $foo[0] = "thread1" })->join;
53 ok(10, $foo[0] eq "thread1", "Check that a value can be changed in another thread");
54 push(@foo, "another value");
55 ok(11, $foo[3] eq "another value", "Check that push works");
56 push(@foo, 1,2,3);
57 ok(12, $foo[-1] == 3, "More push");
58 ok(13, $foo[-2] == 2, "More push");
59 ok(14, $foo[4] == 1, "More push");
60 threads->create(sub { push @foo, "thread2" })->join();
61 ok(15, $foo[7] eq "thread2", "Check push in another thread");
62 unshift(@foo, "start");
63 ok(16, $foo[0] eq "start", "Check unshift");
64 unshift(@foo, 1,2,3);
65 ok(17, $foo[0] == 1, "Check multiple unshift");
66 ok(18, $foo[1] == 2, "Check multiple unshift");
67 ok(19, $foo[2] == 3, "Check multiple unshift");
68 threads->create(sub { unshift @foo, "thread3" })->join();
69 ok(20, $foo[0] eq "thread3", "Check unshift from another thread");
70 my $var = pop(@foo);
71 ok(21, $var eq "thread2", "Check pop");
72 threads->create(sub { my $foo = pop @foo; ok(22, $foo == 3, "Check pop works in a thread")})->join();
73 $var = pop(@foo);
74 ok(23, $var == 2, "Check pop after thread");
75 $var = shift(@foo);
76 ok(24, $var eq "thread3", "Check shift");
77 threads->create(sub { my $foo = shift @foo; ok(25, $foo  == 1, "Check shift works in a thread");
78 })->join();
79 $var = shift(@foo);
80 ok(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 }
89 my $i = 0;
90 foreach my $var (@foo) {
91     $i++;
92 }
93 ok(29, scalar @foo == $i, "Check foreach");
94 my $ref = \@foo;
95 ok(30, $ref->[0] == 3, "Check reference access");
96 threads->create(sub { $ref->[0] = "thread4"})->join();
97 ok(31, $ref->[0] eq "thread4", "Check that it works after another thread");
98 undef($ref);
99 threads->create(sub { @foo = () })->join();
100 ok(32, @foo == 0, "Check that array is empty");
101 ok(33, exists($foo[0]) == 0, "Check that zero index doesn't index");
102 @foo = ("sky");
103 ok(34, exists($foo[0]) == 1, "Check that zero index exists now");
104 ok(35, $foo[0] eq "sky", "And check that it also contains the right value");
105 $#foo = 20;
106 $foo[20] = "sky";
107 ok(36, delete($foo[20]) eq "sky", "Check delete works");
108
109 threads->create(sub { delete($foo[0])})->join();
110 ok(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
131 ok(44, is_shared(@foo), "Check for sharing");
132
133 exit(0);
134
135 # EOF