]> git.vpit.fr Git - perl/modules/Sub-Nary.git/blob - t/15-misc-xs.t
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/Sub-Nary.git] / t / 15-misc-xs.t
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 32;
7
8 use Sub::Nary;
9
10 my @scalops = Sub::Nary::scalops();
11 my $nbr     = Sub::Nary::scalops();
12
13 is($nbr, scalar @scalops, 'scalops return values in list/scalar context are consistent');
14
15 *normalize = *Sub::Nary::normalize{CODE};
16
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');
23
24 *scale = *Sub::Nary::scale{CODE};
25
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');
31
32 *add = *Sub::Nary::add{CODE};
33
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');
39
40 *cumulate = *Sub::Nary::cumulate{CODE};
41
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');
50
51 *combine = *Sub::Nary::combine{CODE};
52
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');
72