xref: /openbsd-src/gnu/usr.bin/perl/t/op/int.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = '../lib';
6    require './test.pl';
7}
8
9plan 15;
10
11# compile time evaluation
12
13my $test1_descr = 'compile time evaluation 1.234';
14if (int(1.234) == 1) {pass($test1_descr)} else {fail($test1_descr)}
15
16my $test2_descr = 'compile time evaluation -1.234';
17if (int(-1.234) == -1) {pass($test2_descr)} else {fail($test2_descr)}
18
19# run time evaluation
20
21$x = 1.234;
22cmp_ok(int($x), '==', 1, 'run time evaluation 1');
23cmp_ok(int(-$x), '==', -1, 'run time evaluation -1');
24
25$x = length("abc") % -10;
26cmp_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);
41cmp_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);
47is($y, "4294967303", 'check values > 32 bits work');
48
49$y = int (-$x);
50
51is($y, "-4294967303", 'negative value more than maximum unsigned 32 bit value');
52
53$x = 4294967294.2;
54$y = int ($x);
55
56is($y, "4294967294", 'floating point value slightly less than the largest unsigned 32 bit');
57
58$x = 4294967295.7;
59$y = int ($x);
60
61is($y, "4294967295", 'floating point value slightly more than largest unsigned 32 bit');
62
63$x = 4294967296.11312;
64$y = int ($x);
65
66is($y, "4294967296", 'floating point value more than largest unsigned 32 bit');
67
68$y = int(279964589018079/59);
69cmp_ok($y, '==', 4745162525730, 'compile time division, result of about 42 bits');
70
71$y = 279964589018079;
72$y = int($y/59);
73cmp_ok($y, '==', 4745162525730, 'run time divison, result of about 42 bits');
74