xref: /openbsd-src/gnu/usr.bin/perl/dist/Thread-Queue/t/05_extract.t (revision f3efcd0145415b7d44d9da97e0ad5c21b186ac61)
1b39c5158Smillertuse strict;
2b39c5158Smillertuse warnings;
3b39c5158Smillert
4b39c5158SmillertBEGIN {
5b39c5158Smillert    use Config;
6b39c5158Smillert    if (! $Config{'useithreads'}) {
7b39c5158Smillert        print("1..0 # SKIP Perl not compiled with 'useithreads'\n");
8b39c5158Smillert        exit(0);
9b39c5158Smillert    }
10b39c5158Smillert}
11b39c5158Smillert
12b39c5158Smillertuse threads;
13b39c5158Smillertuse Thread::Queue;
14b39c5158Smillert
15*f3efcd01Safresh1BEGIN { # perl RT 133382
16b39c5158Smillertif ($] == 5.008) {
17b39c5158Smillert    require 't/test.pl';   # Test::More work-alike for Perl 5.8.0
18b39c5158Smillert} else {
19b39c5158Smillert    require Test::More;
20b39c5158Smillert}
21b39c5158SmillertTest::More->import();
22*f3efcd01Safresh1} # end BEGIN
23b39c5158Smillertplan('tests' => 20);
24b39c5158Smillert
25b39c5158Smillertmy $q = Thread::Queue->new(1..10);
26b39c5158Smillertok($q, 'New queue');
27b39c5158Smillert
28b39c5158Smillertthreads->create(sub {
29b39c5158Smillert    # Default count = 1
30b39c5158Smillert    is($q->extract(),   1, 'No args');          # 2..10 left
31b39c5158Smillert    is($q->extract(0),  2, 'Head');             # 3..10 left
32b39c5158Smillert    is($q->extract(5),  8, 'Pos index');        # 3..7,9,10 left
33b39c5158Smillert    is($q->extract(-3), 7, 'Neg index');        # 3..6,9,10 left
34b39c5158Smillert    my $x = $q->extract(20);                    # unchanged
35b39c5158Smillert    ok(! defined($x), 'Big index');
36b39c5158Smillert    $x = $q->extract(-20);                      # unchanged
37b39c5158Smillert    ok(! defined($x), 'Big neg index');
38b39c5158Smillert})->join();
39b39c5158Smillert
40b39c5158Smillert$q = Thread::Queue->new(1..10);
41b39c5158Smillertok($q, 'New queue');
42b39c5158Smillert
43b39c5158Smillertthreads->create(sub {
44b39c5158Smillert    my @x = $q->extract(0, 2);                  # 3..10 left
45b39c5158Smillert    is_deeply(\@x, [1,2], '2 from head');
46b39c5158Smillert    @x = $q->extract(6, 2);                     # 3..8 left
47b39c5158Smillert    is_deeply(\@x, [9,10], '2 from tail');
48b39c5158Smillert    @x = $q->extract(2, 2);                     # 3,4,7,8 left
49b39c5158Smillert    is_deeply(\@x, [5,6], '2 from middle');
50b39c5158Smillert    @x = $q->extract(2, 4);                     # 3,4 left
51b39c5158Smillert    is_deeply(\@x, [7,8], 'Lots from tail');
52b39c5158Smillert    @x = $q->extract(3, 4);                     # unchanged
53b39c5158Smillert    is_deeply(\@x, [], 'Too far');
54b39c5158Smillert})->join();
55b39c5158Smillert
56b39c5158Smillert$q = Thread::Queue->new(1..10);
57b39c5158Smillertok($q, 'New queue');
58b39c5158Smillert
59b39c5158Smillertthreads->create(sub {
60b39c5158Smillert    my @x = $q->extract(-4, 2);                 # 1..6,9,10 left
61b39c5158Smillert    is_deeply(\@x, [7,8], 'Neg index');
62b39c5158Smillert    @x = $q->extract(-2, 4);                    # 1..6 left
63b39c5158Smillert    is_deeply(\@x, [9,10], 'Lots from tail');
64b39c5158Smillert    @x = $q->extract(-6, 2);                    # 3..6 left
65b39c5158Smillert    is_deeply(\@x, [1,2], 'Max neg index');
66b39c5158Smillert    @x = $q->extract(-10, 3);                   # unchanged
67b39c5158Smillert    is_deeply(\@x, [], 'Too far');
68b39c5158Smillert    @x = $q->extract(-6, 3);                    # 4..6 left
69b39c5158Smillert    is_deeply(\@x, [3], 'Neg overlap');
70b39c5158Smillert    @x = $q->extract(-5, 10);                   # empty
71b39c5158Smillert    is_deeply(\@x, [4..6], 'Neg big overlap');
72b39c5158Smillert})->join();
73b39c5158Smillert
74b39c5158Smillertexit(0);
75b39c5158Smillert
76b39c5158Smillert# EOF
77