This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Add tests for POSIX::Termios->get[io]speed().
[perl5.git] / ext / POSIX / t / termios.t
CommitLineData
674d0cd9 1#!perl -Tw
6e32c255 2
6e32c255 3use strict;
674d0cd9
NC
4use Config;
5use Test::More;
6
60e845e3 7BEGIN {
674d0cd9
NC
8 plan skip_all => "POSIX is unavailable"
9 if $Config{extensions} !~ m!\bPOSIX\b!;
60e845e3
YO
10}
11
674d0cd9 12use POSIX ':termios_h';
6e32c255 13
674d0cd9
NC
14plan skip_all => $@
15 if !eval "POSIX::Termios->new; 1" && $@ =~ /termios not implemented/;
6e32c255 16
6e32c255 17
4e453fcc
NC
18# A termios struct that we've successfully read from a terminal device:
19my $termios;
20
21foreach (undef, qw(STDIN STDOUT STDERR)) {
22 SKIP:
23 {
24 my ($name, $handle);
25 if (defined $_) {
26 $name = $_;
27 $handle = $::{$name};
28 } else {
29 $name = POSIX::ctermid();
30 skip("Can't get name of controlling terminal", 4)
31 unless defined $name;
32 open $handle, '<', $name or skip("can't open $name: $!", 4);
33 }
34
35 skip("$name not a tty", 4) unless -t $handle;
36
37 my $t = eval { POSIX::Termios->new };
38 is($@, '', "calling POSIX::Termios->new");
39 isa_ok($t, "POSIX::Termios", "checking the type of the object");
40
674d0cd9 41 my $fileno = fileno $handle;
4e453fcc 42 my $r = eval { $t->getattr($fileno) };
674d0cd9 43 is($@, '', "calling getattr($fileno) for $name");
4e453fcc
NC
44 if(isnt($r, undef, "returned value ($r) is defined")) {
45 $termios = $t;
46 }
674d0cd9 47 }
6e32c255
AT
48}
49
4e453fcc
NC
50if (defined $termios) {
51 # testing getcc()
52 for my $i (0 .. NCCS-1) {
53 my $r = eval { $termios->getcc($i) };
54 is($@, '', "calling getcc($i)");
55 like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
56 }
011985f1
NC
57 for my $i (NCCS, ~0) {
58 my $r = eval { $termios->getcc($i) };
59 like($@, qr/\ABad getcc subscript/, "calling getcc($i)");
60 is($r, undef, 'returns undef')
61 }
6e32c255 62
4e453fcc
NC
63 for my $method (qw(getcflag getiflag getispeed getlflag getoflag getospeed)) {
64 my $r = eval { $termios->$method() };
65 is($@, '', "calling $method()");
66 like($r, qr/\A-?[0-9]+\z/, 'returns an integer');
67 }
6e32c255
AT
68}
69
5118227b
NC
70{
71 my $t = POSIX::Termios->new();
72 isa_ok($t, "POSIX::Termios", "checking the type of the object");
73
74 # B0 is special
75 my @baud = (B50, B75, B110, B134, B150, B200, B300, B600, B1200, B1800,
76 B2400, B4800, B9600, B19200, B38400);
77
78 # On some platforms (eg Linux-that-I-tested), ispeed and ospeed are both
79 # "stored" in the same bits of c_cflag (as the man page documents)
80 # *as well as in struct members* (which you would assume obviates the need
81 # for using c_cflag), and the get*() functions return the value encoded
82 # within c_cflag, hence it's not possible to set/get them independently.
83 foreach my $out (@baud) {
84 is($t->setispeed(0), '0 but true', "setispeed(0)");
85 is($t->setospeed($out), '0 but true', "setospeed($out)");
86 is($t->getospeed(), $out, "getospeed() for $out");
87 }
88 foreach my $in (@baud) {
89 is($t->setospeed(0), '0 but true', "setospeed(0)");
90 is($t->setispeed($in), '0 but true', "setispeed($in)");
91 is($t->getispeed(), $in, "getispeed() for $in");
92 }
93}
94
674d0cd9 95done_testing();