5 # The reason this does not use a Test module is that
6 # they mess up test numbers between threads
8 # And even when that will be fixed, this is a basic
9 # test and should not rely on shared variables
11 # This will test the basic API, it will not use any coderefs
12 # as they are more advanced
14 #########################
18 if ($ENV{'PERL_CORE'}){
20 unshift @INC, '../lib';
23 unless ($Config{'useithreads'}) {
24 print "1..0 # Skip: no useithreads\n";
29 use ExtUtils::testlib;
31 BEGIN { $| = 1; print "1..28\n" };
36 if ($threads::VERSION && ! exists($ENV{'PERL_CORE'})) {
37 print(STDERR "# Testing threads $threads::VERSION\n");
42 ### Start of Testing ###
47 my ($id, $ok, $name) = @_;
49 # You have to do it this way or VMS will get confused.
50 print $ok ? "ok $id - $name\n" : "not ok $id - $name\n";
52 printf "# Failed test at line %d\n", (caller)[2] unless $ok;
59 ok(2,'bar' eq $_[0],"Test that argument passing works");
61 threads->create('test1','bar')->join();
64 ok(3,'bar' eq $_[0]->[0]->{foo},"Test that passing arguments as references work");
67 threads->create(\&test2,[{foo => 'bar'}])->join();
70 #test execuion of normal sub
71 sub test3 { ok(4,shift() == 1,"Test a normal sub") }
72 threads->create(\&test3,1)->join();
76 ok(5, 1 == $threads::threads,"Check that threads::threads is true");
78 #test trying to detach thread
80 sub test4 { ok(6,1,"Detach test") }
82 my $thread1 = threads->create('test4');
85 threads->yield; # help out non-preemptive thread implementations
87 ok(7,1,"Detach test");
92 threads->create('test6')->join();
93 ok(9,1,"Nested thread test");
97 ok(8,1,"Nested thread test");
100 threads->create('test5')->join();
103 my $self = threads->self();
104 ok(10, $self->tid == 7, "Wanted 7, got ".$self->tid);
105 ok(11, threads->tid() == 7, "Wanted 7, got ".threads->tid());
108 threads->create('test7')->join;
111 my $self = threads->self();
112 ok(12, $self->tid == 8, "Wanted 8, got ".$self->tid);
113 ok(13, threads->tid() == 8, "Wanted 8, got ".threads->tid());
116 threads->create('test8')->join;
119 #check support for threads->self() in main thread
120 ok(14, 0 == threads->self->tid(),"Check so that tid for threads work for main thread");
121 ok(15, 0 == threads->tid(),"Check so that tid for threads work for main thread");
125 local *CLONE = sub { ok(16, threads->tid() == 9, "Tid should be correct in the clone")};
126 threads->create(sub { ok(17, threads->tid() == 9, "And tid be 9 here too") })->join();
132 ok(19, threads->tid() == 10, "In destroy it should be correct too" )
135 threads->create(sub { ok(18, threads->tid() == 10, "And tid be 10 here");
136 $foo = bless {}, 'Foo';
143 my $thr1 = threads->create(sub {});
144 my $thr2 = threads->create(sub {});
145 my $thr3 = threads->object($thr1->tid());
147 ok(20, $thr1 != $thr2, 'Treads not equal');
148 ok(21, $thr1 == $thr3, 'Threads equal');
150 ok(22, threads->object($thr1->tid())->tid() == 11, 'Object method');
151 ok(23, threads->object($thr2->tid())->tid() == 12, 'Object method');
156 my $sub = sub { ok(24, shift() == 1, "Test code ref"); };
157 threads->create($sub, 1)->join();
159 my $thrx = threads->object(99);
160 ok(25, ! defined($thrx), 'No object');
161 $thrx = threads->object();
162 ok(26, ! defined($thrx), 'No object');
163 $thrx = threads->object(undef);
164 ok(27, ! defined($thrx), 'No object');
165 $thrx = threads->object(0);
166 ok(28, ! defined($thrx), 'No object');