Explicit thread context
[p5sagit/p5-mst-13.2.git] / ext / threads / t / context.t
CommitLineData
9d9ff5b1 1use strict;
2use warnings;
3
4BEGIN {
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
16use ExtUtils::testlib;
17
18use threads;
19use threads::shared;
20
21BEGIN {
22 $| = 1;
23 print("1..13\n"); ### Number of tests that will be run ###
24};
25
26my $TEST = 1;
27share($TEST);
28
29ok(1, 'Loaded');
30
31sub ok {
32 my ($ok, $name) = @_;
33
34 lock($TEST);
35 my $id = $TEST++;
36
37 # You have to do it this way or VMS will get confused.
38 if ($ok) {
39 print("ok $id - $name\n");
40 } else {
41 print("not ok $id - $name\n");
42 printf("# Failed test at line %d\n", (caller)[2]);
43 }
44
45 return ($ok);
46}
47
48
49### Start of Testing ###
50
51sub foo
52{
53 my $context = shift;
54 my $wantarray = wantarray();
55
56 if ($wantarray) {
57 ok($context eq 'array', 'Array context');
58 return ('array');
59 } elsif (defined($wantarray)) {
60 ok($context eq 'scalar', 'Scalar context');
61 return 'scalar';
62 } else {
63 ok($context eq 'void', 'Void context');
64 return;
65 }
66}
67
68my ($thr) = threads->create('foo', 'array');
69my ($res) = $thr->join();
70ok($res eq 'array', 'Implicit array context');
71
72$thr = threads->create('foo', 'scalar');
73$res = $thr->join();
74ok($res eq 'scalar', 'Implicit scalar context');
75
76threads->create('foo', 'void');
77($thr) = threads->list();
78$res = $thr->join();
79ok(! defined($res), 'Implicit void context');
80
81$thr = threads->create({'context' => 'array'}, 'foo', 'array');
82($res) = $thr->join();
83ok($res eq 'array', 'Explicit array context');
84
85($thr) = threads->create({'scalar' => 'scalar'}, 'foo', 'scalar');
86$res = $thr->join();
87ok($res eq 'scalar', 'Explicit scalar context');
88
89$thr = threads->create({'void' => 1}, 'foo', 'void');
90$res = $thr->join();
91ok(! defined($res), 'Explicit void context');
92
93# EOF