xref: /openbsd-src/gnu/usr.bin/perl/cpan/Scalar-List-Utils/t/max.t (revision f2da64fbbbf1b03f09f390ab01267c93dfd77c4c)
1#!./perl
2
3BEGIN {
4    unless (-d 'blib') {
5	chdir 't' if -d 't';
6	@INC = '../lib';
7	require Config; import Config;
8	keys %Config; # Silence warning
9	if ($Config{extensions} !~ /\bList\/Util\b/) {
10	    print "1..0 # Skip: List::Util was not built\n";
11	    exit 0;
12	}
13    }
14}
15
16use strict;
17use Test::More tests => 10;
18use List::Util qw(max);
19
20my $v;
21
22ok(defined &max, 'defined');
23
24$v = max(1);
25is($v, 1, 'single arg');
26
27$v = max (1,2);
28is($v, 2, '2-arg ordered');
29
30$v = max(2,1);
31is($v, 2, '2-arg reverse ordered');
32
33my @a = map { rand() } 1 .. 20;
34my @b = sort { $a <=> $b } @a;
35$v = max(@a);
36is($v, $b[-1], '20-arg random order');
37
38my $one = Foo->new(1);
39my $two = Foo->new(2);
40my $thr = Foo->new(3);
41
42$v = max($one,$two,$thr);
43is($v, 3, 'overload');
44
45$v = max($thr,$two,$one);
46is($v, 3, 'overload');
47
48
49{ package Foo;
50
51use overload
52  '""' => sub { ${$_[0]} },
53  '0+' => sub { ${$_[0]} },
54  '>'  => sub { ${$_[0]} > ${$_[1]} },
55  fallback => 1;
56  sub new {
57    my $class = shift;
58    my $value = shift;
59    bless \$value, $class;
60  }
61}
62
63use Math::BigInt;
64
65my $v1 = Math::BigInt->new(2) ** Math::BigInt->new(65);
66my $v2 = $v1 - 1;
67my $v3 = $v2 - 1;
68$v = max($v1,$v2,$v1,$v3,$v1);
69is($v, $v1, 'bigint');
70
71$v = max($v1, 1, 2, 3);
72is($v, $v1, 'bigint and normal int');
73
74$v = max(1, 2, $v1, 3);
75is($v, $v1, 'bigint and normal int');
76
77