File: signame_canonical.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (75 lines) | stat: -rw-r--r-- 1,862 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#!perl -w

# We assume that TestInit has been used.

BEGIN {
    chdir 't' if -d 't';
    require './test.pl';
    skip_all_if_miniperl();
}

use strict;
use warnings;

use Config;
use Data::Dumper;
$Data::Dumper::Sortkeys = 1;

# Windows doesn't seem to be able to test signals.
skip_all("Signals lock up tests on $^O") if $^O =~ /MSWin32/;

$| = 1;

# Extract the signals from %Config.
my @SIGNAMES = split /\s+/, $Config{sig_name};
my @SIGNUMS  = split /\s+/, $Config{sig_num};

my %SIG_MAP;
foreach my $i ( 0 .. ( scalar @SIGNAMES - 1 ) ) {
    $SIG_MAP{ $SIGNAMES[$i] } = $SIGNUMS[$i];
}

# Find the canonical (first) signal names.
my %CANONICAL_SIG;
my @duplicate_signals;
foreach my $sig (@SIGNAMES) {
    my $signum = $SIG_MAP{$sig};
    $CANONICAL_SIG{$signum} //= $sig;
    push @duplicate_signals, $sig if $CANONICAL_SIG{$signum} ne $sig;
}

plan tests => scalar @duplicate_signals * 5;
watchdog(25);

# Define the duplicate signal handlers.
my $sent = '';

sub handler_is {
    my $signame = shift;
    my $signum  = $SIG_MAP{$signame};

    my $canonical = $CANONICAL_SIG{$signum};

    is( $signame, $canonical, "Signal name for $sent is recieved as the canonical '$canonical' name." );

    return;
}

foreach my $dupe (@duplicate_signals) {
    my $canonical_name = $CANONICAL_SIG{ $SIG_MAP{$dupe} };
    note "Testing $dupe / $canonical_name signal pair";
    {
        local $SIG{$dupe} = \&handler_is;
        is( $SIG{$dupe}, $SIG{$canonical_name}, "Both handlers for $canonical_name/$dupe are set" );

        $sent = $dupe;
        kill $dupe, $$;

        $sent = $canonical_name;
        kill $canonical_name, $$;
    }

    is( $SIG{$dupe},           undef, "The signal $dupe is cleared after local goes out of scope." );
    is( $SIG{$canonical_name}, undef, "The signal $canonical_name is cleared after local goes out of scope." );
}