1#!./perl 2 3use strict; 4use warnings; 5 6use Test::More tests => 10; 7use List::Util qw(max); 8 9my $v; 10 11ok(defined &max, 'defined'); 12 13$v = max(1); 14is($v, 1, 'single arg'); 15 16$v = max (1,2); 17is($v, 2, '2-arg ordered'); 18 19$v = max(2,1); 20is($v, 2, '2-arg reverse ordered'); 21 22my @a = map { rand() } 1 .. 20; 23my @b = sort { $a <=> $b } @a; 24$v = max(@a); 25is($v, $b[-1], '20-arg random order'); 26 27my $one = Foo->new(1); 28my $two = Foo->new(2); 29my $thr = Foo->new(3); 30 31$v = max($one,$two,$thr); 32is($v, 3, 'overload'); 33 34$v = max($thr,$two,$one); 35is($v, 3, 'overload'); 36 37 38{ package Foo; 39 40use overload 41 '""' => sub { ${$_[0]} }, 42 '0+' => sub { ${$_[0]} }, 43 '>' => sub { ${$_[0]} > ${$_[1]} }, 44 fallback => 1; 45 sub new { 46 my $class = shift; 47 my $value = shift; 48 bless \$value, $class; 49 } 50} 51 52use Math::BigInt; 53 54my $v1 = Math::BigInt->new(2) ** Math::BigInt->new(65); 55my $v2 = $v1 - 1; 56my $v3 = $v2 - 1; 57$v = max($v1,$v2,$v1,$v3,$v1); 58is($v, $v1, 'bigint'); 59 60$v = max($v1, 1, 2, 3); 61is($v, $v1, 'bigint and normal int'); 62 63$v = max(1, 2, $v1, 3); 64is($v, $v1, 'bigint and normal int'); 65 66