]> git.vpit.fr Git - perl/modules/Sub-Nary.git/blobdiff - t/15-misc-xs.t
Update VPIT::TestHelpers to 15e8aee3
[perl/modules/Sub-Nary.git] / t / 15-misc-xs.t
index bd6baacc1d70327a48de9dd925ef61b9de0a9f4a..7cc94042aea4da6ecb7147c10ea12504bf970f63 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 12;
+use Test::More tests => 32;
 
 use Sub::Nary;
 
@@ -14,23 +14,59 @@ is($nbr, scalar @scalops, 'scalops return values in list/scalar context are cons
 
 *normalize = *Sub::Nary::normalize{CODE};
 
-is_deeply(normalize(1),  { 1 => 1 }, 'normalize const');
-is_deeply(normalize({}), { 0 => 1 }, 'normalize empty-ref');
+is_deeply(normalize('list'), { list => 1 }, 'normalize list');
+is_deeply(normalize(1),      { 1 => 1 },    'normalize const');
+is_deeply(normalize({}),     { 0 => 1 },    'normalize empty-ref');
+is_deeply(normalize({ list => 1, 2 => 2, 3 => 1 }),
+                    { list => 0.25, 2 => 0.5, 3 => 0.25 },
+                    'normalize list and consts');
 
 *scale = *Sub::Nary::scale{CODE};
 
-is_deeply(scale(1, {}), { 0 => 1 }, 'scale const, empty-ref');
+is_deeply(scale(1,   {}),            undef,         'scale 1, empty-ref');
+is_deeply(scale(0.5, {}),            undef,         'scale 0.5, empty-ref');
+is_deeply(scale(0.5, { list => 2 }), { list => 1 }, 'scale 0.5, list');
+is_deeply(scale(0.5, { list => 2, 1 => 2 }), { list => 1, 1 => 1 },
+                     'scale 0.5, list/const');
 
 *add = *Sub::Nary::add{CODE};
 
-is_deeply(add('list'),             { list => 1 }, 'add list');
-is_deeply(add(1, 'list'),          { list => 1 }, 'add const, list');
-is_deeply(add({ }, 'list'),        { list => 1 }, 'add empty-ref, list');
-is_deeply(add({ 1 => 1 }, 'list'), { list => 1 }, 'add ref, list');
-is_deeply(add({ 1 => 1 }, 1),      { 1 => 2 }, 'add ref, prev-const');
+is_deeply(add('list'),             { list => 1 },         'add list');
+is_deeply(add(1, 'list'),          { 1 => 1, list => 1 }, 'add const, list');
+is_deeply(add({ }, 'list'),        { list => 1 },        'add empty-ref, list');
+is_deeply(add({ 1 => 1 }, 'list'), { 1 => 1, list => 1 }, 'add ref, list');
+is_deeply(add({ 1 => 1 }, 1),      { 1 => 2 },           'add ref, prev-const');
 
 *cumulate = *Sub::Nary::cumulate{CODE};
 
 is_deeply(cumulate('list', 1, 1), 'list', 'cumulate const, non-zero, non-zero');
-is_deeply(cumulate({ 1 => 1 }, 1, 0), { 1 => 1 }, 'cumulate ref, non-zero, zero');
 is_deeply(cumulate({ }, 1, 1), undef, 'cumulate empty-ref, non-zero, non-zero');
+is_deeply(cumulate({ 1 => 1 }, 1, 0), { 1 => 1 }, 'cumulate ref, non-zero, zero');
+is_deeply(cumulate({ 1 => 1 }, 2, 1), { 1 => 2 }, 'cumulate ref, 2, 1');
+is_deeply(cumulate({ 1 => 0.5, 2 => 0.5 }, 1, 0.5), { 1 => 0.5, 2 => 0.5 },
+                   'cumulate ref, 1, frac');
+is_deeply(cumulate({ 1 => 0.5, 2 => 0.5 }, 2, 0.5), { 1 => 0.75, 2 => 0.75 },
+                   'cumulate ref, 1, frac');
+
+*combine = *Sub::Nary::combine{CODE};
+
+is_deeply(combine(undef),         undef,        'combine undef');
+is_deeply(combine({}),            {},           'combine empty-ref');
+is_deeply(combine({}, {}),        {},           'combine empty-ref, empty-ref');
+is_deeply(combine(1),             { 1 => 1 },      'combine const');
+is_deeply(combine(1, 2),          { 3 => 1 },      'combine const, const');
+is_deeply(combine(1, 'list'),     { 'list' => 1 }, 'combine const, list');
+is_deeply(combine(1,{'list'=>1}), { 'list' => 1 }, 'combine const, list');
+is_deeply(combine(1, { 1 => 0.5, 2 => 0.5 }), { 2 => 0.5, 3 => 0.5 },
+                  'combine const, hashref');
+is_deeply(combine(1, { 1 => 0.5, 'list' => 0.5 }), { 2 => 0.5, 'list' => 0.5 },
+                  'combine const, hashref with list');
+my $x = { 1 => 0.5, 2 => 0.5 };
+is_deeply(combine($x, $x), { 2 => 0.25, 3 => 0.5, 4 => 0.25 },
+                  'combine hashref, hashref');
+is_deeply(combine($x, 'list', $x), { list => 1 },
+                  'combine hashref, list, hashref');
+$x = { 1 => 0.5, list => 0.5 };
+is_deeply(combine($x, $x), { 2 => 0.25, list => 0.75 },
+                  'combine hashref with list, hashref with list');
+