summaryrefslogtreecommitdiff
path: root/lib/integer.t
blob: 7ad97b6a9f3319e854ee1d48036689907c67a31c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#!./perl

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}

use integer;

use Test::More;
use Config;

my $x = 4.5;
my $y = 5.6;
my $z;

$z = $x + $y;
is($z, 9, "plus");

$z = $x - $y;
is($z, -1, "minus");

$z = $x * $y;
is($z, 20, "times");

$z = $x / $y;
is($z, 0, "divide");

$z = $x / $y;
is($z, 0, "modulo");

is($x, 4.5, "scalar still floating point");
 
isnt(sqrt($x), 2, "functions still floating point");
 
isnt($x ** .5, 2, "power still floating point");

is(++$x, 5.5, "++ still floating point");
 
SKIP: {
    my $ivsize = $Config{ivsize};
    skip "ivsize == $ivsize", 2 unless $ivsize == 4 || $ivsize == 8;

    if ($ivsize == 4) {
	$z = 2**31 - 1;
	is($z + 1, -2147483648, "left shift");
    } elsif ($ivsize == 8) {
	$z = 2**63 - 1;
	is($z + 1, -9223372036854775808, "left shift");
    }
}

is(~0, -1, "signed instead of unsigned");

# [perl #38485] use integer; 0x80000000/-1;
SKIP: {
    my $ivsize = $Config{ivsize};
    skip "ivsize == $ivsize", 4 unless $ivsize == 4 || $ivsize == 8;

    my $iv_min = $ivsize == 4 ? -2147483648 : -9223372036854775808;
    my $biff;
    eval { $biff = $iv_min / -1 };
    is($@, '', 'IV_MIN / -1 succeeds');
    is($biff, -$iv_min, 'IV_MIN / -1 == -IV_MIN');

    eval { $biff = $iv_min % -1 };
    is($@, '', 'IV_MIN % -1 succeeds');
    is($biff, 0, 'IV_MIN % -1 == 0');
}

done_testing();