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