File: 03.t

package info (click to toggle)
libmath-combinatorics-perl 0.09-6
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 144 kB
  • sloc: perl: 412; makefile: 2
file content (93 lines) | stat: -rw-r--r-- 2,455 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
BEGIN {
  use lib 'lib';
  use strict;
  use Test::More;

  plan tests => 3;

  use_ok('Data::Dumper');
  use_ok('Math::Combinatorics');
}

my @S = qw(d a c b);
my @R = map {\$_} qw(d a c b);

my $rsort = sub { my ($a,$b) = @_;  return $b cmp $a };
#my $rsort = sub { my ($a,$b) = @_;  return $b cmp $a };

my $set     = Math::Combinatorics->new(
    count   => scalar(@S)  ,
    data    =>      [ @S ] ,
    compare => $rsort      ,
);
$c = join '', $set->next_combination();
is( $c, q{dcba}, q{expecting reverse ordered array}      );

__DATA__
#! /usr/bin/perl

use warnings;
use strict;

use lib q{.};
use Math::Combinatorics qw<combine>;
use Data::Dumper;

use Test::More tests => 5;

    # test with a regular array and with an array of references
    #
my @TEST_ARR =    qw{d a c b};
my @TEST_REF = \( qw{d a c b} );

# Combine without passing a compare function, Combination should be sorted
# by the internal compare function, which sorts alphabetically.
#
{
    my $c = join q{}, @{ (combine(scalar @TEST_ARR, @TEST_ARR))[0] };
    is( $c, q{abcd}, q{expecting ordered array} );
}

# Combine with passing a compare function as defined in the manual. I expect
# it to die.
#
{
    my $alphabetically = sub {
        my $a = shift;   die qq{REFARGS\n}  if ref $a;
        my $b = shift;   die qq{REFARGS\n}  if ref $b;
        return $a cmp $b;
    };
    my $c = join q{}, eval {
        my $set     = Math::Combinatorics->new(
            count   => scalar @TEST_ARR   ,
            data    =>      [ @TEST_ARR ] ,
            compare => $alphabetically    ,
        );
        $set->next_combination();
    };
    is( $@, q{},     q{expecting compare not to die} );
    is( $c, q{abcd}, q{expecting ordered array}      );
}

# do the same with a list of references
#
{
    my $alphabetically = sub {
        my $a = shift;   die qq{NOREFARGS\n}  unless q{SCALAR} eq ref $a;
        my $b = shift;   die qq{NOREFARGS\n}  unless q{SCALAR} eq ref $a;
        return $$a cmp $$b;
    };

    # we now have an array of refs, so we need a map {} in between
    #
    my $c = join q{}, map { $$_ } eval {
        my $set     = Math::Combinatorics->new(
            count   => scalar @TEST_REF   ,
            data    =>      [ @TEST_REF ] ,
            compare => $alphabetically    ,
        );
        $set->next_combination();
    };
    is( $@, q{},     q{expecting compare not to die} );
    is( $c, q{abcd}, q{expecting ordered array}      );
}