File: product.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (135 lines) | stat: -rw-r--r-- 2,770 bytes parent folder | download | duplicates (4)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!./perl

use strict;
use warnings;

use Test::More tests => 27;

use Config;
use List::Util qw(product);

my $v = product;
is( $v, 1, 'no args');

$v = product(9);
is( $v, 9, 'one arg');

$v = product(1,2,3,4);
is( $v, 24, '4 args');

$v = product(-1);
is( $v, -1, 'one -1');

$v = product(0, 1, 2);
is( $v, 0, 'first factor zero' );

$v = product(0, 1);
is( $v, 0, '0 * 1');

$v = product(1, 0);
is( $v, 0, '1 * 0');

$v = product(0, 0);
is( $v, 0, 'two 0');

# RT139601 cornercases
{
  # Numify the result because some older perl versions see "-0" as a string
  is( 0+product(-1.0, 0), 0, 'product(-1.0, 0)' );
  is( 0+product(-1, 0), 0, 'product(-1, 0)' );
}

my $x = -3;

$v = product($x, 3);
is( $v, -9, 'variable arg');

$v = product(-3.5,3);
is( $v, -10.5, 'real numbers');

my $one  = Foo->new(1);
my $two  = Foo->new(2);
my $four = Foo->new(4);

$v = product($one,$two,$four);
is($v, 8, 'overload');


{ package Foo;

use overload
  '""' => sub { ${$_[0]} },
  '0+' => sub { ${$_[0]} },
  fallback => 1;
  sub new {
    my $class = shift;
    my $value = shift;
    bless \$value, $class;
  }
}

use Math::BigInt;
my $v1 = Math::BigInt->new(2) ** Math::BigInt->new(65);
my $v2 = $v1 - 1;
$v = product($v1,$v2);
is($v, $v1 * $v2, 'bigint');

$v = product(42, $v1);
is($v, $v1 * 42, 'bigint + builtin int');

$v = product(42, $v1, 2);
is($v, $v1 * 42 * 2, 'bigint + builtin int');

{ package example;

  use overload
    '0+' => sub { $_[0][0] },
    '""' => sub { my $r = "$_[0][0]"; $r = "+$r" unless $r =~ m/^\-/; $r .= " [$_[0][1]]"; $r },
    fallback => 1;

  sub new {
    my $class = shift;

    my $this = bless [@_], $class;

    return $this;
  }
}

{
  my $e1 = example->new(7, "test");
  my $t = product($e1, 7, 7);
  is($t, 343, 'overload returning non-overload');
  $t = product(8, $e1, 8);
  is($t, 448, 'overload returning non-overload');
  $t = product(9, 9, $e1);
  is($t, 567, 'overload returning non-overload');
}

SKIP: {
  skip "IV is not at least 64bit", 8 unless $Config{ivsize} >= 8;

  my $t;
  my $min = -(1<<31);
  my $max = (1<<31)-1;

  $t = product($min, $min);
  is($t,  1<<62, 'min * min');
  $t = product($min, $max);
  is($t, (1<<31) - (1<<62), 'min * max');
  $t = product($max, $min);
  is($t, (1<<31) - (1<<62), 'max * min');

  $t = product($max, $max);
  is($t,  4611686014132420609, 'max * max'); # (1<<62)-(1<<32)+1), but Perl 5.6 does not compute constant correctly

  $t = product($min*8, $min);
  cmp_ok($t, '>',  (1<<61), 'min*8*min'); # may be an NV
  $t = product($min*8, $max);
  cmp_ok($t, '<', -(1<<61), 'min*8*max'); # may be an NV
  $t = product($max, $min*8);
  cmp_ok($t, '<', -(1<<61), 'min*max*8'); # may be an NV
  $t = product($max, $max*8);
  cmp_ok($t, '>',  (1<<61), 'max*max*8'); # may be an NV

}