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