Add s///r (non-destructive substitution).
[p5sagit/p5-mst-13.2.git] / dist / Thread-Queue / t / 06_insert.t
CommitLineData
54c7876f 1use strict;
2use warnings;
3
4BEGIN {
54c7876f 5 use Config;
6 if (! $Config{'useithreads'}) {
3d4f2f89 7 print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
54c7876f 8 exit(0);
9 }
10}
11
12use threads;
13use Thread::Queue;
14
15if ($] == 5.008) {
16 require 't/test.pl'; # Test::More work-alike for Perl 5.8.0
17} else {
18 require Test::More;
19}
20Test::More->import();
21plan('tests' => 16);
22
23my $q = Thread::Queue->new(1..10);
24ok($q, 'New queue');
25
26threads->create(sub {
27 $q->insert(5);
28 $q->insert(-5);
29 $q->insert(100);
30 $q->insert(-100);
31})->join();
32
33my @x = $q->dequeue_nb(100);
34is_deeply(\@x, [1..10], 'No-op inserts');
35
36
37$q = Thread::Queue->new(1..10);
38ok($q, 'New queue');
39
40threads->create(sub {
41 $q->insert(10, qw/tail/);
42 $q->insert(0, qw/head/);
43})->join();
44
45@x = $q->dequeue_nb(100);
46is_deeply(\@x, ['head',1..10,'tail'], 'Edge inserts');
47
48
49$q = Thread::Queue->new(1..10);
50ok($q, 'New queue');
51
52threads->create(sub {
53 $q->insert(5, qw/foo bar/);
54 $q->insert(-2, qw/qux/);
55})->join();
56
57@x = $q->dequeue_nb(100);
58is_deeply(\@x, [1..5,'foo','bar',6..8,'qux',9,10], 'Middle inserts');
59
60
61$q = Thread::Queue->new(1..10);
62ok($q, 'New queue');
63
64threads->create(sub {
65 $q->insert(20, qw/tail/);
66 $q->insert(-20, qw/head/);
67})->join();
68
69@x = $q->dequeue_nb(100);
70is_deeply(\@x, ['head',1..10,'tail'], 'Extreme inserts');
71
72
73$q = Thread::Queue->new();
74ok($q, 'New queue');
75threads->create(sub { $q->insert(0, 1..3); })->join();
76@x = $q->dequeue_nb(100);
77is_deeply(\@x, [1..3], 'Empty queue insert');
78
79$q = Thread::Queue->new();
80ok($q, 'New queue');
81threads->create(sub { $q->insert(20, 1..3); })->join();
82@x = $q->dequeue_nb(100);
83is_deeply(\@x, [1..3], 'Empty queue insert');
84
85$q = Thread::Queue->new();
86ok($q, 'New queue');
87threads->create(sub { $q->insert(-1, 1..3); })->join();
88@x = $q->dequeue_nb(100);
89is_deeply(\@x, [1..3], 'Empty queue insert');
90
91$q = Thread::Queue->new();
92ok($q, 'New queue');
93threads->create(sub {
94 $q->insert(2, 1..3);
95 $q->insert(1, 'foo');
96})->join();
97@x = $q->dequeue_nb(100);
98is_deeply(\@x, [1,'foo',2,3], 'Empty queue insert');
99
3d4f2f89 100exit(0);
101
54c7876f 102# EOF