1# 01-method.t 2# 3# Test suite for Set::Partition 4# Test the module methods 5# 6# copyright (C) 2006 David Landgren 7 8use strict; 9 10eval qq{use Test::More tests => 81}; 11if( $@ ) { 12 warn "# Test::More not available, no tests performed\n"; 13 print "1..1\nok 1\n"; 14 exit 0; 15} 16 17use Set::Partition; 18 19my $Unchanged = 'The scalar remains the same'; 20$_ = $Unchanged; 21 22{ 23 my $s = Set::Partition->new( list => [qw(x y z)] ); 24 my $p = $s->next; 25 is_deeply( $p, [[ qw(x y z) ]], 'unpartitioned set' ); 26 27 $p = $s->next; 28 ok( !defined($p), '...exhausted'); 29 30 $p = $s->reset; 31 $p = $s->next; 32 is_deeply( $p, [[ qw(x y z) ]], 'unpartitioned reset' ); 33} 34 35{ 36 eval { my $s = Set::Partition->new( list => ['m'], partition => [1, 2] ) }; 37 ok( $@, 'die on out-of-range partitioning' ); 38} 39 40{ 41 my $s = Set::Partition->new( list => [qw(g h)], partition => [1, 1] ); 42 my $p = $s->next; 43 is_deeply( $p, [ [qw(g)], [qw(h)] ], 'set 1,1 first' ); 44 $p = $s->next; 45 is_deeply( $p, [ [qw(h)], [qw(g)] ], 'set 1,1 second' ); 46 $p = $s->next; 47 ok( !defined($p), 'set 1,1 exhausted'); 48} 49 50{ 51 my $s = Set::Partition->new( list => [qw(p q r s)], partition => [3] ); 52 my $p = $s->next; 53 is_deeply( $p, [ [qw(p q r)], [qw(s)] ], 'set 3,1 first' ); 54 $p = $s->next; 55 is_deeply( $p, [ [qw(p q s)], [qw(r)] ], 'set 3,1 second' ); 56 $p = $s->next; 57 is_deeply( $p, [ [qw(p r s)], [qw(q)] ], 'set 3,1 third' ); 58 $p = $s->next; 59 is_deeply( $p, [ [qw(q r s)], [qw(p)] ], 'set 3,1 fourth' ); 60 $p = $s->next; 61 ok( !defined($p), 'set 3,1 exhausted'); 62} 63 64{ 65 my $s = Set::Partition->new( list => [qw(p q r s)], partition => [3, 0, 1] ); 66 my $p = $s->next; 67 is_deeply( $p, [ [qw(p q r)], undef, [qw(s)] ], 'set 3,0,1 first' ); 68 $p = $s->next; 69 is_deeply( $p, [ [qw(p q s)], undef, [qw(r)] ], 'set 3,0,1 second' ); 70 $p = $s->next; 71 is_deeply( $p, [ [qw(p r s)], undef, [qw(q)] ], 'set 3,0,1 third' ); 72 $p = $s->next; 73 is_deeply( $p, [ [qw(q r s)], undef, [qw(p)] ], 'set 3,0,1 fourth' ); 74 $p = $s->next; 75 ok( !defined($p), 'set 3,0,1 exhausted'); 76} 77 78{ 79 my $s = Set::Partition->new( 80 list => { 81 a => 'apple', 82 b => 'banana', 83 c => 'cherry', 84 }, 85 partition => [2, 1], 86 ); 87 my @p = ($s->next, $s->next, $s->next); 88 ok( !defined($s->next), 'set hash exhausted'); 89 90 my $result; 91 for my $p (@p) { 92 $result->{join(';', map {join( '+', sort keys %$_)} @$p)} 93 = join(',', map {join( '+', sort values %$_)} @$p); 94 } 95 is_deeply( $result, 96 { 97 'a+b;c' => 'apple+banana,cherry', 98 'a+c;b' => 'apple+cherry,banana', 99 'b+c;a' => 'banana+cherry,apple', 100 }, 101 'hash partitions' 102 ); 103} 104 105{ 106 my $s = Set::Partition->new( list => ['a' .. 'f'], partition => [3, 1, 2] ); 107 my $nr = 0; 108 while (defined(my $expected = <DATA>)) { 109 chomp $expected; 110 my $p = $s->next; 111 my $actual = join( ' ', map {"(@$_)"} @$p ); 112 ++$nr; 113 is( $expected, $actual, "a..f by 3,1,2 $nr" ); 114 } 115 my $p = $s->next; 116 ok( !defined($p), 'a..f by 3,1,2 exhausted'); 117} 118 119cmp_ok( $_, 'eq', $Unchanged, '$_ has not been altered' ); 120 121__DATA__ 122(a b c) (d) (e f) 123(a b c) (e) (d f) 124(a b c) (f) (d e) 125(a b d) (c) (e f) 126(a b e) (c) (d f) 127(a b f) (c) (d e) 128(a b d) (e) (c f) 129(a b d) (f) (c e) 130(a b e) (d) (c f) 131(a b f) (d) (c e) 132(a b e) (f) (c d) 133(a b f) (e) (c d) 134(a c d) (b) (e f) 135(a c e) (b) (d f) 136(a c f) (b) (d e) 137(a d e) (b) (c f) 138(a d f) (b) (c e) 139(a e f) (b) (c d) 140(a c d) (e) (b f) 141(a c d) (f) (b e) 142(a c e) (d) (b f) 143(a c f) (d) (b e) 144(a c e) (f) (b d) 145(a c f) (e) (b d) 146(a d e) (c) (b f) 147(a d f) (c) (b e) 148(a e f) (c) (b d) 149(a d e) (f) (b c) 150(a d f) (e) (b c) 151(a e f) (d) (b c) 152(b c d) (a) (e f) 153(b c e) (a) (d f) 154(b c f) (a) (d e) 155(b d e) (a) (c f) 156(b d f) (a) (c e) 157(b e f) (a) (c d) 158(c d e) (a) (b f) 159(c d f) (a) (b e) 160(c e f) (a) (b d) 161(d e f) (a) (b c) 162(b c d) (e) (a f) 163(b c d) (f) (a e) 164(b c e) (d) (a f) 165(b c f) (d) (a e) 166(b c e) (f) (a d) 167(b c f) (e) (a d) 168(b d e) (c) (a f) 169(b d f) (c) (a e) 170(b e f) (c) (a d) 171(b d e) (f) (a c) 172(b d f) (e) (a c) 173(b e f) (d) (a c) 174(c d e) (b) (a f) 175(c d f) (b) (a e) 176(c e f) (b) (a d) 177(d e f) (b) (a c) 178(c d e) (f) (a b) 179(c d f) (e) (a b) 180(c e f) (d) (a b) 181(d e f) (c) (a b) 182