This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Test.pm: avoid noise on STDERR.
[perl5.git] / t / op / int.t
CommitLineData
8d063cd8
LW
1#!./perl
2
04c29d09
GS
3BEGIN {
4 chdir 't' if -d 't';
20822f61 5 @INC = '../lib';
44885db3 6 require './test.pl';
04c29d09
GS
7}
8
44885db3 9plan 15;
8d063cd8
LW
10
11# compile time evaluation
12
7231abfa
BE
13my $test1_descr = 'compile time evaluation 1.234';
14if (int(1.234) == 1) {pass($test1_descr)} else {fail($test1_descr)}
8d063cd8 15
7231abfa
BE
16my $test2_descr = 'compile time evaluation -1.234';
17if (int(-1.234) == -1) {pass($test2_descr)} else {fail($test2_descr)}
8d063cd8
LW
18
19# run time evaluation
20
21$x = 1.234;
7231abfa
BE
22cmp_ok(int($x), '==', 1, 'run time evaluation 1');
23cmp_ok(int(-$x), '==', -1, 'run time evaluation -1');
4bb9f687
GS
24
25$x = length("abc") % -10;
7231abfa 26cmp_ok($x, '==', -7, 'subtract from string length');
4bb9f687
GS
27
28{
44885db3 29 my $fail;
4bb9f687
GS
30 use integer;
31 $x = length("abc") % -10;
ddc90e86 32 $y = (3/-10)*-10;
7231abfa
BE
33 ok($x+$y == 3, 'x+y equals 3') or ++$fail;
34 ok(abs($x) < 10, 'abs(x) < 10') or ++$fail;
44885db3
NC
35 if ($fail) {
36 diag("\$x == $x", "\$y == $y");
37 }
4bb9f687 38}
0a3ece55 39
0a3ece55 40@x = ( 6, 8, 10);
44885db3 41cmp_ok($x["1foo"], '==', 8, 'check bad strings still get converted');
853d5ea7 42
7231abfa
BE
43# 4,294,967,295 is largest unsigned 32 bit integer
44
853d5ea7
NC
45$x = 4294967303.15;
46$y = int ($x);
44885db3 47is($y, "4294967303", 'check values > 32 bits work');
853d5ea7
NC
48
49$y = int (-$x);
50
7231abfa 51is($y, "-4294967303", 'negative value more than maximum unsigned 32 bit value');
853d5ea7
NC
52
53$x = 4294967294.2;
54$y = int ($x);
55
7231abfa 56is($y, "4294967294", 'floating point value slightly less than the largest unsigned 32 bit');
853d5ea7
NC
57
58$x = 4294967295.7;
59$y = int ($x);
60
7231abfa 61is($y, "4294967295", 'floating point value slightly more than largest unsigned 32 bit');
853d5ea7
NC
62
63$x = 4294967296.11312;
64$y = int ($x);
65
7231abfa 66is($y, "4294967296", 'floating point value more than largest unsigned 32 bit');
853d5ea7
NC
67
68$y = int(279964589018079/59);
7231abfa 69cmp_ok($y, '==', 4745162525730, 'compile time division, result of about 42 bits');
853d5ea7
NC
70
71$y = 279964589018079;
72$y = int($y/59);
7231abfa 73cmp_ok($y, '==', 4745162525730, 'run time divison, result of about 42 bits');