6 use Test::More tests => 32;
10 my @scalops = Sub::Nary::scalops();
11 my $nbr = Sub::Nary::scalops();
13 is($nbr, scalar @scalops, 'scalops return values in list/scalar context are consistent');
15 *normalize = *Sub::Nary::normalize{CODE};
17 is_deeply(normalize('list'), { list => 1 }, 'normalize list');
18 is_deeply(normalize(1), { 1 => 1 }, 'normalize const');
19 is_deeply(normalize({}), { 0 => 1 }, 'normalize empty-ref');
20 is_deeply(normalize({ list => 1, 2 => 2, 3 => 1 }),
21 { list => 0.25, 2 => 0.5, 3 => 0.25 },
22 'normalize list and consts');
24 *scale = *Sub::Nary::scale{CODE};
26 is_deeply(scale(1, {}), undef, 'scale 1, empty-ref');
27 is_deeply(scale(0.5, {}), undef, 'scale 0.5, empty-ref');
28 is_deeply(scale(0.5, { list => 2 }), { list => 1 }, 'scale 0.5, list');
29 is_deeply(scale(0.5, { list => 2, 1 => 2 }), { list => 1, 1 => 1 },
30 'scale 0.5, list/const');
32 *add = *Sub::Nary::add{CODE};
34 is_deeply(add('list'), { list => 1 }, 'add list');
35 is_deeply(add(1, 'list'), { 1 => 1, list => 1 }, 'add const, list');
36 is_deeply(add({ }, 'list'), { list => 1 }, 'add empty-ref, list');
37 is_deeply(add({ 1 => 1 }, 'list'), { 1 => 1, list => 1 }, 'add ref, list');
38 is_deeply(add({ 1 => 1 }, 1), { 1 => 2 }, 'add ref, prev-const');
40 *cumulate = *Sub::Nary::cumulate{CODE};
42 is_deeply(cumulate('list', 1, 1), 'list', 'cumulate const, non-zero, non-zero');
43 is_deeply(cumulate({ }, 1, 1), undef, 'cumulate empty-ref, non-zero, non-zero');
44 is_deeply(cumulate({ 1 => 1 }, 1, 0), { 1 => 1 }, 'cumulate ref, non-zero, zero');
45 is_deeply(cumulate({ 1 => 1 }, 2, 1), { 1 => 2 }, 'cumulate ref, 2, 1');
46 is_deeply(cumulate({ 1 => 0.5, 2 => 0.5 }, 1, 0.5), { 1 => 0.5, 2 => 0.5 },
47 'cumulate ref, 1, frac');
48 is_deeply(cumulate({ 1 => 0.5, 2 => 0.5 }, 2, 0.5), { 1 => 0.75, 2 => 0.75 },
49 'cumulate ref, 1, frac');
51 *combine = *Sub::Nary::combine{CODE};
53 is_deeply(combine(undef), undef, 'combine undef');
54 is_deeply(combine({}), {}, 'combine empty-ref');
55 is_deeply(combine({}, {}), {}, 'combine empty-ref, empty-ref');
56 is_deeply(combine(1), { 1 => 1 }, 'combine const');
57 is_deeply(combine(1, 2), { 3 => 1 }, 'combine const, const');
58 is_deeply(combine(1, 'list'), { 'list' => 1 }, 'combine const, list');
59 is_deeply(combine(1,{'list'=>1}), { 'list' => 1 }, 'combine const, list');
60 is_deeply(combine(1, { 1 => 0.5, 2 => 0.5 }), { 2 => 0.5, 3 => 0.5 },
61 'combine const, hashref');
62 is_deeply(combine(1, { 1 => 0.5, 'list' => 0.5 }), { 2 => 0.5, 'list' => 0.5 },
63 'combine const, hashref with list');
64 my $x = { 1 => 0.5, 2 => 0.5 };
65 is_deeply(combine($x, $x), { 2 => 0.25, 3 => 0.5, 4 => 0.25 },
66 'combine hashref, hashref');
67 is_deeply(combine($x, 'list', $x), { list => 1 },
68 'combine hashref, list, hashref');
69 $x = { 1 => 0.5, list => 0.5 };
70 is_deeply(combine($x, $x), { 2 => 0.25, list => 0.75 },
71 'combine hashref with list, hashref with list');