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