1
2use Test::More;
3use Test::LMU;
4
5my @list = reverse 0 .. 10000;
6my ($min, $max) = minmax @list;
7is($min, 0);
8is($max, 10000);
9
10# Even number of elements
11push @list, 10001;
12($min, $max) = minmax @list;
13is($min, 0);
14is($max, 10001);
15$list[0] = 17;
16
17# Some floats
18@list = (0, -1.1, 3.14, 1 / 7, 10000, -10 / 3);
19($min, $max) = minmax @list;
20
21# Floating-point comparison cunningly avoided
22is(sprintf("%.2f", $min), "-3.33");
23is($max,                  10000);
24
25# Test with a single negative list value
26my $input = -1;
27($min, $max) = minmax $input;
28is($min, -1);
29is($max, -1);
30
31# COW causes missing max when optimization for 1 argument is applied
32@list = grep { defined $_ } map { my ($min, $max) = minmax(sprintf("%.3g", rand)); ($min, $max) } (0 .. 19);
33is(scalar @list, 40, "minmax swallows max on COW");
34
35# Confirm output are independant copies of input
36$input = 1;
37is($min, -1);
38is($max, -1);
39$min = 2;
40is($max, -1);
41
42# prove overrun
43my $uvmax    = ~0;
44my $ivmax    = $uvmax >> 1;
45my $ivmin    = (0 - $ivmax) - 1;
46my @low_ints = map { $ivmin + $_ } (0 .. 10);
47($min, $max) = minmax @low_ints;
48is($min, $ivmin,      "minmax finds ivmin");
49is($max, $ivmin + 10, "minmax finds ivmin + 10");
50
51my @high_ints = map { $ivmax - $_ } (0 .. 10);
52($min, $max) = minmax @high_ints;
53is($min, $ivmax - 10, "minmax finds ivmax-10");
54is($max, $ivmax,      "minmax finds ivmax");
55
56my @mixed_ints = map { ($ivmin + $_, $ivmax - $_) } (0 .. 10);
57($min, $max) = minmax @mixed_ints;
58is($min, $ivmin, "minmax finds ivmin");
59is($max, $ivmax, "minmax finds ivmax");
60
61my @high_uints = map { $uvmax - $_ } (0 .. 10);
62($min, $max) = minmax @high_uints;
63is($min, $uvmax - 10, "minmax finds uvmax-10");
64is($max, $uvmax,      "minmax finds uvmax");
65
66my @mixed_nums = map { ($ivmin + $_, $uvmax - $_) } (0 .. 10);
67($min, $max) = minmax @mixed_nums;
68is($min, $ivmin, "minmax finds ivmin");
69is($max, $uvmax, "minmax finds uvmax");
70
71leak_free_ok(
72    minmax => sub {
73        @list = (0, -1.1, 3.14, 1 / 7, 10000, -10 / 3);
74        ($min, $max) = minmax @list;
75    }
76);
77
78done_testing;
79