diff options
author | Jerry D. Hedden <jdhedden@cpan.org> | 2008-02-15 09:02:14 -0500 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2008-02-18 13:37:40 +0000 |
commit | 54c7876f687059dc7b09511db127f9ac439f8d8d (patch) | |
tree | ce8bb7d8337f5177170d0e03c413ebf2fd804f5b /lib/Thread/Queue/t/05_extract.t | |
parent | 3fe25e0f6cb886de8a6ea0821c0c60588e818e66 (diff) | |
download | perl-54c7876f687059dc7b09511db127f9ac439f8d8d.tar.gz |
Thread::Queue 2.03
From: "Jerry D. Hedden" <jdhedden@cpan.org>
Message-ID: <1ff86f510802151102s41bebc4xab19aa6e464dbf04@mail.gmail.com>
p4raw-id: //depot/perl@33331
Diffstat (limited to 'lib/Thread/Queue/t/05_extract.t')
-rw-r--r-- | lib/Thread/Queue/t/05_extract.t | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/lib/Thread/Queue/t/05_extract.t b/lib/Thread/Queue/t/05_extract.t new file mode 100644 index 0000000000..8c2fb2f304 --- /dev/null +++ b/lib/Thread/Queue/t/05_extract.t @@ -0,0 +1,76 @@ +use strict; +use warnings; + +BEGIN { + if ($ENV{'PERL_CORE'}){ + chdir('t'); + unshift(@INC, '../lib'); + } + use Config; + if (! $Config{'useithreads'}) { + print("1..0 # Skip: Perl not compiled with 'useithreads'\n"); + exit(0); + } +} + +use threads; +use Thread::Queue; + +if ($] == 5.008) { + require 't/test.pl'; # Test::More work-alike for Perl 5.8.0 +} else { + require Test::More; +} +Test::More->import(); +plan('tests' => 20); + +my $q = Thread::Queue->new(1..10); +ok($q, 'New queue'); + +threads->create(sub { + # Default count = 1 + is($q->extract(), 1, 'No args'); # 2..10 left + is($q->extract(0), 2, 'Head'); # 3..10 left + is($q->extract(5), 8, 'Pos index'); # 3..7,9,10 left + is($q->extract(-3), 7, 'Neg index'); # 3..6,9,10 left + my $x = $q->extract(20); # unchanged + ok(! defined($x), 'Big index'); + $x = $q->extract(-20); # unchanged + ok(! defined($x), 'Big neg index'); +})->join(); + +$q = Thread::Queue->new(1..10); +ok($q, 'New queue'); + +threads->create(sub { + my @x = $q->extract(0, 2); # 3..10 left + is_deeply(\@x, [1,2], '2 from head'); + @x = $q->extract(6, 2); # 3..8 left + is_deeply(\@x, [9,10], '2 from tail'); + @x = $q->extract(2, 2); # 3,4,7,8 left + is_deeply(\@x, [5,6], '2 from middle'); + @x = $q->extract(2, 4); # 3,4 left + is_deeply(\@x, [7,8], 'Lots from tail'); + @x = $q->extract(3, 4); # unchanged + is_deeply(\@x, [], 'Too far'); +})->join(); + +$q = Thread::Queue->new(1..10); +ok($q, 'New queue'); + +threads->create(sub { + my @x = $q->extract(-4, 2); # 1..6,9,10 left + is_deeply(\@x, [7,8], 'Neg index'); + @x = $q->extract(-2, 4); # 1..6 left + is_deeply(\@x, [9,10], 'Lots from tail'); + @x = $q->extract(-6, 2); # 3..6 left + is_deeply(\@x, [1,2], 'Max neg index'); + @x = $q->extract(-10, 3); # unchanged + is_deeply(\@x, [], 'Too far'); + @x = $q->extract(-6, 3); # 4..6 left + is_deeply(\@x, [3], 'Neg overlap'); + @x = $q->extract(-5, 10); # empty + is_deeply(\@x, [4..6], 'Neg big overlap'); +})->join(); + +# EOF |