File: cartesian.t

package info (click to toggle)
libset-scalar-perl 1.29-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 240 kB
  • sloc: perl: 932; makefile: 2
file content (65 lines) | stat: -rw-r--r-- 1,519 bytes parent folder | download | duplicates (5)
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
use Set::Scalar;

print "1..9\n";

my $a = Set::Scalar->new(1..2);
my $b = Set::Scalar->new(3..5);

my $c = $a->cartesian_product($b);
my $d = Set::Scalar->cartesian_product($a, $b);
my $e = $a->cartesian_product($a);
my $f = $a->cartesian_product();
my $g = Set::Scalar->cartesian_product($a, $b, $b);
my $h = Set::Scalar->cartesian_product($a, $c);

print "not " unless $c->members == 6;
print "ok 1\n";

print "not " unless $d->members == 6;
print "ok 2\n";

print "not " unless $e->members == 4;
print "ok 3\n";

print "not " unless $f->members == 2;
print "ok 4\n";

sub verify {
    my ($p, @q) = @_;
    my @p = $p->members;
    return unless @p == @q;
    my %p; @p{ map { "@$_" } @p } = @p;
    my %q; @q{ map { "@$_" } @q } = @q;
    my %P = %p; delete @P{ keys %q };
    my %Q = %q; delete @Q{ keys %p };
    return keys %P == 0 && keys %Q == 0;
}

print "not " unless verify($c,
			   [1, 3], [1, 4], [1, 5],
			   [2, 3], [2, 4], [2, 5]);
print "ok 5\n";

print "not " unless verify($d,
			   [1, 3], [1, 4], [1, 5],
			   [2, 3], [2, 4], [2, 5]);
print "ok 6\n";

print "not " unless verify($e,
			   [1, 2], [1, 1],
			   [2, 1], [2, 2]);
print "ok 7\n";

print "not " unless verify($f,
			   [1], [2]);
print "ok 8\n";

print "not " unless verify($g,
			   [1, 3, 3], [1, 4, 3], [1, 5, 3],
			   [2, 3, 3], [2, 4, 3], [2, 5, 3],
			   [1, 3, 4], [1, 4, 4], [1, 5, 4],
			   [2, 3, 4], [2, 4, 4], [2, 5, 4],
			   [1, 3, 5], [1, 4, 5], [1, 5, 5],
			   [2, 3, 5], [2, 4, 5], [2, 5, 5]);
print "ok 9\n";