1use strict;
2use warnings;
3
4use FindBin qw($Bin);
5use lib $Bin;
6
7use Test::More qw(no_plan);
8
9use Algorithm::Combinatorics qw(complete_permutations);
10use Tester;
11
12my $tester = Tester->__new(\&complete_permutations);
13
14my (@result, @expected);
15
16# ---------------------------------------------------------------------
17
18eval { complete_permutations() };
19ok($@, '');
20
21eval { complete_permutations(0) };
22ok($@, '');
23
24# ---------------------------------------------------------------------
25
26@expected = ([]);
27$tester->__test(\@expected, []);
28
29# ---------------------------------------------------------------------
30
31@expected = ();
32$tester->__test(\@expected, ["foo"]);
33
34# ---------------------------------------------------------------------
35
36@expected = (
37    ["bar", "foo"],
38);
39$tester->__test(\@expected, ["foo", "bar"]);
40
41# ---------------------------------------------------------------------
42
43@expected = (
44    ["bar", "baz", "foo"],
45    ["baz", "foo", "bar"],
46);
47$tester->__test(\@expected, ["foo", "bar", "baz"]);
48
49# ---------------------------------------------------------------------
50
51@expected = (
52    [2, 1, 4, 3],
53    [2, 3, 4, 1],
54    [2, 4, 1, 3],
55    [3, 1, 4, 2],
56    [3, 4, 1, 2],
57    [3, 4, 2, 1],
58    [4, 1, 2, 3],
59    [4, 3, 1, 2],
60    [4, 3, 2, 1],
61);
62$tester->__test(\@expected, [1, 2, 3, 4]);
63
64# ----------------------------------------------------------------------
65
66# d(n) = n*d(n-1) + (-1)**n if n > 0, d(0) = 1.
67my $ncomb = 0;
68my $iter = complete_permutations([1..8]);
69while (my $c = $iter->next) {
70    ++$ncomb;
71}
72is($ncomb, 14833, "");
73
74$ncomb = 0;
75$iter = complete_permutations([1..9]);
76while (my $c = $iter->next) {
77    ++$ncomb;
78}
79is($ncomb, 133496, "");