xref: /openbsd-src/gnu/usr.bin/perl/dist/threads/t/context.t (revision b39c515898423c8d899e35282f4b395f7cad3298)
1*b39c5158Smillertuse strict;
2*b39c5158Smillertuse warnings;
3*b39c5158Smillert
4*b39c5158SmillertBEGIN {
5*b39c5158Smillert    use Config;
6*b39c5158Smillert    if (! $Config{'useithreads'}) {
7*b39c5158Smillert        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8*b39c5158Smillert        exit(0);
9*b39c5158Smillert    }
10*b39c5158Smillert}
11*b39c5158Smillert
12*b39c5158Smillertuse ExtUtils::testlib;
13*b39c5158Smillert
14*b39c5158Smillertuse threads;
15*b39c5158Smillert
16*b39c5158SmillertBEGIN {
17*b39c5158Smillert    if (! eval 'use threads::shared; 1') {
18*b39c5158Smillert        print("1..0 # SKIP threads::shared not available\n");
19*b39c5158Smillert        exit(0);
20*b39c5158Smillert    }
21*b39c5158Smillert
22*b39c5158Smillert    $| = 1;
23*b39c5158Smillert    print("1..31\n");   ### Number of tests that will be run ###
24*b39c5158Smillert};
25*b39c5158Smillert
26*b39c5158Smillertmy $TEST;
27*b39c5158SmillertBEGIN {
28*b39c5158Smillert    share($TEST);
29*b39c5158Smillert    $TEST = 1;
30*b39c5158Smillert}
31*b39c5158Smillert
32*b39c5158Smillertok(1, 'Loaded');
33*b39c5158Smillert
34*b39c5158Smillertsub ok {
35*b39c5158Smillert    my ($ok, $name) = @_;
36*b39c5158Smillert
37*b39c5158Smillert    lock($TEST);
38*b39c5158Smillert    my $id = $TEST++;
39*b39c5158Smillert
40*b39c5158Smillert    # You have to do it this way or VMS will get confused.
41*b39c5158Smillert    if ($ok) {
42*b39c5158Smillert        print("ok $id - $name\n");
43*b39c5158Smillert    } else {
44*b39c5158Smillert        print("not ok $id - $name\n");
45*b39c5158Smillert        printf("# Failed test at line %d\n", (caller)[2]);
46*b39c5158Smillert    }
47*b39c5158Smillert
48*b39c5158Smillert    return ($ok);
49*b39c5158Smillert}
50*b39c5158Smillert
51*b39c5158Smillert
52*b39c5158Smillert### Start of Testing ###
53*b39c5158Smillert
54*b39c5158Smillertsub foo
55*b39c5158Smillert{
56*b39c5158Smillert    my $context = shift;
57*b39c5158Smillert    my $wantarray = wantarray();
58*b39c5158Smillert
59*b39c5158Smillert    if ($wantarray) {
60*b39c5158Smillert        ok($context eq 'array', 'Array/list context');
61*b39c5158Smillert        return ('array');
62*b39c5158Smillert    } elsif (defined($wantarray)) {
63*b39c5158Smillert        ok($context eq 'scalar', 'Scalar context');
64*b39c5158Smillert        return 'scalar';
65*b39c5158Smillert    } else {
66*b39c5158Smillert        ok($context eq 'void', 'Void context');
67*b39c5158Smillert        return;
68*b39c5158Smillert    }
69*b39c5158Smillert}
70*b39c5158Smillert
71*b39c5158Smillertmy ($thr) = threads->create('foo', 'array');
72*b39c5158Smillertmy ($res) = $thr->join();
73*b39c5158Smillertok($res eq 'array', 'Implicit array context');
74*b39c5158Smillert
75*b39c5158Smillert$thr = threads->create('foo', 'scalar');
76*b39c5158Smillert$res = $thr->join();
77*b39c5158Smillertok($res eq 'scalar', 'Implicit scalar context');
78*b39c5158Smillert
79*b39c5158Smillertthreads->create('foo', 'void');
80*b39c5158Smillert($thr) = threads->list();
81*b39c5158Smillert$res = $thr->join();
82*b39c5158Smillertok(! defined($res), 'Implicit void context');
83*b39c5158Smillert
84*b39c5158Smillert$thr = threads->create({'context' => 'array'}, 'foo', 'array');
85*b39c5158Smillert($res) = $thr->join();
86*b39c5158Smillertok($res eq 'array', 'Explicit array context');
87*b39c5158Smillert
88*b39c5158Smillert($thr) = threads->create({'scalar' => 'scalar'}, 'foo', 'scalar');
89*b39c5158Smillert$res = $thr->join();
90*b39c5158Smillertok($res eq 'scalar', 'Explicit scalar context');
91*b39c5158Smillert
92*b39c5158Smillert$thr = threads->create({'void' => 1}, 'foo', 'void');
93*b39c5158Smillert$res = $thr->join();
94*b39c5158Smillertok(! defined($res), 'Explicit void context');
95*b39c5158Smillert
96*b39c5158Smillert
97*b39c5158Smillertsub bar
98*b39c5158Smillert{
99*b39c5158Smillert    my $context = shift;
100*b39c5158Smillert    my $wantarray = threads->wantarray();
101*b39c5158Smillert
102*b39c5158Smillert    if ($wantarray) {
103*b39c5158Smillert        ok($context eq 'list', 'Array/list context');
104*b39c5158Smillert        return ('list');
105*b39c5158Smillert    } elsif (defined($wantarray)) {
106*b39c5158Smillert        ok($context eq 'scalar', 'Scalar context');
107*b39c5158Smillert        return 'scalar';
108*b39c5158Smillert    } else {
109*b39c5158Smillert        ok($context eq 'void', 'Void context');
110*b39c5158Smillert        return;
111*b39c5158Smillert    }
112*b39c5158Smillert}
113*b39c5158Smillert
114*b39c5158Smillert($thr) = threads->create('bar', 'list');
115*b39c5158Smillertmy $ctx = $thr->wantarray();
116*b39c5158Smillertok($ctx, 'Implicit array context');
117*b39c5158Smillert($res) = $thr->join();
118*b39c5158Smillertok($res eq 'list', 'Implicit array context');
119*b39c5158Smillert
120*b39c5158Smillert$thr = threads->create('bar', 'scalar');
121*b39c5158Smillert$ctx = $thr->wantarray();
122*b39c5158Smillertok(defined($ctx) && !$ctx, 'Implicit scalar context');
123*b39c5158Smillert$res = $thr->join();
124*b39c5158Smillertok($res eq 'scalar', 'Implicit scalar context');
125*b39c5158Smillert
126*b39c5158Smillertthreads->create('bar', 'void');
127*b39c5158Smillert($thr) = threads->list();
128*b39c5158Smillert$ctx = $thr->wantarray();
129*b39c5158Smillertok(! defined($ctx), 'Implicit void context');
130*b39c5158Smillert$res = $thr->join();
131*b39c5158Smillertok(! defined($res), 'Implicit void context');
132*b39c5158Smillert
133*b39c5158Smillert$thr = threads->create({'context' => 'list'}, 'bar', 'list');
134*b39c5158Smillert$ctx = $thr->wantarray();
135*b39c5158Smillertok($ctx, 'Explicit array context');
136*b39c5158Smillert($res) = $thr->join();
137*b39c5158Smillertok($res eq 'list', 'Explicit array context');
138*b39c5158Smillert
139*b39c5158Smillert($thr) = threads->create({'scalar' => 'scalar'}, 'bar', 'scalar');
140*b39c5158Smillert$ctx = $thr->wantarray();
141*b39c5158Smillertok(defined($ctx) && !$ctx, 'Explicit scalar context');
142*b39c5158Smillert$res = $thr->join();
143*b39c5158Smillertok($res eq 'scalar', 'Explicit scalar context');
144*b39c5158Smillert
145*b39c5158Smillert$thr = threads->create({'void' => 1}, 'bar', 'void');
146*b39c5158Smillert$ctx = $thr->wantarray();
147*b39c5158Smillertok(! defined($ctx), 'Explicit void context');
148*b39c5158Smillert$res = $thr->join();
149*b39c5158Smillertok(! defined($res), 'Explicit void context');
150*b39c5158Smillert
151*b39c5158Smillertexit(0);
152*b39c5158Smillert
153*b39c5158Smillert# EOF
154