This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / t / op / int.t
CommitLineData
8d063cd8
LW
1#!./perl
2
04c29d09
GS
3BEGIN {
4 chdir 't' if -d 't';
44885db3 5 require './test.pl';
624c42e2 6 set_up_inc('../lib');
2efdfb1e 7 require Config;
04c29d09
GS
8}
9
14d26b44 10plan 19;
8d063cd8
LW
11
12# compile time evaluation
13
7231abfa
BE
14my $test1_descr = 'compile time evaluation 1.234';
15if (int(1.234) == 1) {pass($test1_descr)} else {fail($test1_descr)}
8d063cd8 16
7231abfa
BE
17my $test2_descr = 'compile time evaluation -1.234';
18if (int(-1.234) == -1) {pass($test2_descr)} else {fail($test2_descr)}
8d063cd8
LW
19
20# run time evaluation
21
22$x = 1.234;
7231abfa
BE
23cmp_ok(int($x), '==', 1, 'run time evaluation 1');
24cmp_ok(int(-$x), '==', -1, 'run time evaluation -1');
4bb9f687
GS
25
26$x = length("abc") % -10;
7231abfa 27cmp_ok($x, '==', -7, 'subtract from string length');
4bb9f687
GS
28
29{
44885db3 30 my $fail;
4bb9f687
GS
31 use integer;
32 $x = length("abc") % -10;
ddc90e86 33 $y = (3/-10)*-10;
7231abfa
BE
34 ok($x+$y == 3, 'x+y equals 3') or ++$fail;
35 ok(abs($x) < 10, 'abs(x) < 10') or ++$fail;
44885db3
NC
36 if ($fail) {
37 diag("\$x == $x", "\$y == $y");
38 }
4bb9f687 39}
0a3ece55 40
0a3ece55 41@x = ( 6, 8, 10);
44885db3 42cmp_ok($x["1foo"], '==', 8, 'check bad strings still get converted');
853d5ea7 43
7231abfa
BE
44# 4,294,967,295 is largest unsigned 32 bit integer
45
853d5ea7
NC
46$x = 4294967303.15;
47$y = int ($x);
44885db3 48is($y, "4294967303", 'check values > 32 bits work');
853d5ea7
NC
49
50$y = int (-$x);
51
7231abfa 52is($y, "-4294967303", 'negative value more than maximum unsigned 32 bit value');
853d5ea7
NC
53
54$x = 4294967294.2;
55$y = int ($x);
56
7231abfa 57is($y, "4294967294", 'floating point value slightly less than the largest unsigned 32 bit');
853d5ea7
NC
58
59$x = 4294967295.7;
60$y = int ($x);
61
7231abfa 62is($y, "4294967295", 'floating point value slightly more than largest unsigned 32 bit');
853d5ea7
NC
63
64$x = 4294967296.11312;
65$y = int ($x);
66
7231abfa 67is($y, "4294967296", 'floating point value more than largest unsigned 32 bit');
853d5ea7
NC
68
69$y = int(279964589018079/59);
7231abfa 70cmp_ok($y, '==', 4745162525730, 'compile time division, result of about 42 bits');
853d5ea7
NC
71
72$y = 279964589018079;
73$y = int($y/59);
7231abfa 74cmp_ok($y, '==', 4745162525730, 'run time divison, result of about 42 bits');
2efdfb1e
TC
75
76SKIP:
77{ # see #126635
78 my $large;
79 $large = eval "0xffff_ffff" if $Config::Config{ivsize} == 4;
80 $large = eval "0xffff_ffff_ffff_ffff" if $Config::Config{ivsize} == 8;
81 $large or skip "Unusual ivsize", 1;
82 for my $x ($large, -1) {
83 cmp_ok($x, "==", int($x), "check $x == int($x)");
84 }
85}
14d26b44
TC
86
87is(1+"0x10", 1, "check string '0x' prefix not treated as hex");
88is(1+"0b10", 1, "check string '0b' prefix not treated as binary");