Do not assume that io buffer flushing happens in the same
[p5sagit/p5-mst-13.2.git] / ext / threads / t / thread.t
1
2 BEGIN {
3     chdir 't' if -d 't';
4     @INC = qw(../lib .);
5     require Config; import Config;
6     unless ($Config{'useithreads'}) {
7         print "1..0 # Skip: no useithreads\n";
8         exit 0;
9     }
10     require "test.pl";
11 }
12
13 use ExtUtils::testlib;
14 use strict;
15 BEGIN { $| = 1; print "1..21\n" };
16 use threads;
17 use threads::shared;
18
19 print "ok 1\n";
20
21 sub content {
22     print shift;
23     return shift;
24 }
25 {
26     my $t = threads->new(\&content, "ok 2\n", "ok 3\n", 1..1000);
27     print $t->join();
28 }
29 {
30     my $lock : shared;
31     my $t;
32     {
33         lock($lock);
34         $t = threads->new(sub { lock($lock); print "ok 5\n"});
35         print "ok 4\n";
36     }
37     $t->join();
38 }
39
40 sub dorecurse {
41     my $val = shift;
42     my $ret;
43     print $val;
44     if(@_) {
45         $ret = threads->new(\&dorecurse, @_);
46         $ret->join;
47     }
48 }
49 {
50     my $t = threads->new(\&dorecurse, map { "ok $_\n" } 6..10);
51     $t->join();
52 }
53
54 {
55     # test that sleep lets other thread run
56     my $t = threads->new(\&dorecurse, "ok 11\n");
57     sleep 1;
58     print "ok 12\n";
59     $t->join();
60 }
61 {
62     my $lock : shared;
63     sub islocked {
64         lock($lock);
65         my $val = shift;
66         my $ret;
67         print $val;
68         if (@_) {
69             $ret = threads->new(\&islocked, shift);
70         }
71         return $ret;
72     }
73 my $t = threads->new(\&islocked, "ok 13\n", "ok 14\n");
74 $t->join->join;
75 }
76
77
78
79 sub testsprintf {
80     my $testno = shift;
81     my $same = sprintf( "%0.f", $testno);
82     return $testno eq $same;
83 }
84
85 sub threaded {
86     my ($string, $string_end) = @_;
87
88   # Do the match, saving the output in appropriate variables
89     $string =~ /(.*)(is)(.*)/;
90   # Yield control, allowing the other thread to fill in the match variables
91     threads->yield();
92   # Examine the match variable contents; on broken perls this fails
93     return $3 eq $string_end;
94 }
95
96
97
98     curr_test(15);
99
100     my $thr1 = threads->new(\&testsprintf, 15);
101     my $thr2 = threads->new(\&testsprintf, 16);
102     
103     my $short = "This is a long string that goes on and on.";
104     my $shorte = " a long string that goes on and on.";
105     my $long  = "This is short.";
106     my $longe  = " short.";
107     my $foo = "This is bar bar bar.";
108     my $fooe = " bar bar bar.";
109     my $thr3 = new threads \&threaded, $short, $shorte;
110     my $thr4 = new threads \&threaded, $long, $longe;
111     my $thr5 = new threads \&testsprintf, 19;
112     my $thr6 = new threads \&testsprintf, 20;
113     my $thr7 = new threads \&threaded, $foo, $fooe;
114
115     ok($thr1->join());
116     ok($thr2->join());
117     ok($thr3->join());
118     ok($thr4->join());
119     ok($thr5->join());
120     ok($thr6->join());
121     ok($thr7->join());
122 }