Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Test::More - equal_set

by brewer (Sexton)
on May 31, 2003 at 23:48 UTC ( #262136=perlquestion: print w/replies, xml ) Need Help??

brewer has asked for the wisdom of the Perl Monks concerning the following question:

Hello all,

Today I learned about some of the test modules in CPAN, and decided to apply Test::More to a little module (my first) that I've been playing with.

It was going well - I found some bugs, fixed them and eventually had 50 something 'ok's on my screen. Then I added another test - this one hopes to check that two arrays of hashrefs contain the same things. Since order is not important I thought an ok(equal_set()) would do the trick.

So here's a cutdown version of what I have:

#!/usr/bin/perl use warnings; use strict; use Test::More tests => 2; BEGIN { use_ok('Atoms'); } my @atoms = read_xyz("t/basic.atoms"); # Test remove duplicates my @atoms_snapshot = @atoms; foreach my $atom (@atoms_snapshot) { push @atoms, $atom; } @atoms = remove_duplicates(@atoms); use Data::Dumper; print Dumper \@atoms; print Dumper \@atoms_snapshot; ok(eq_set(\@atoms, \@atoms_snapshot), "Basic remove dups check");
and here's the output:
1..2 ok 1 - use Atoms; $VAR1 = [ { 'X' => '6E-1', 'NAME' => 'Mn', 'Y' => '-6.0E-1', 'Z' => '0.06E1' }, { 'X' => '1', 'NAME' => 'Mn', 'Y' => '1.0', 'Z' => '0.01E+2' }, { 'X' => '-0.5', 'NAME' => 'O', 'Y' => '-0.75', 'Z' => '0.666666666666667' } ]; $VAR1 = [ { 'X' => '1', 'NAME' => 'Mn', 'Y' => '1.0', 'Z' => '0.01E+2' }, { 'X' => '6E-1', 'NAME' => 'Mn', 'Y' => '-6.0E-1', 'Z' => '0.06E1' }, { 'X' => '-0.5', 'NAME' => 'O', 'Y' => '-0.75', 'Z' => '0.666666666666667' } ]; not ok 2 - Basic remove dups check # Failed test (./fake at line 22) # Looks like you failed 1 tests of 2.

I'm sure someone will be able to spot my error.

PS: I realize and you've probably realized that I've not been sufficiently lazy and used Math::VectorReal, but once I've got the tests in place, I should be able to make the changes with confidence...

Kind Regards,


Replies are listed 'Best First'.
Re: Test::More - equal_set
by edoc (Chaplain) on Jun 01, 2003 at 00:40 UTC

    mmm.. I been bit by this, but I think it's what you could call a bug in the Test::More docs.. maybe, depending on how you read it..

    "This is a deep check, but the irrelevancy of order only applies to the top level."

    Basically, the test below shows that order doesn't matter for a shallow test, but once you have more than the "top level", order does matter.

    #!/usr/bin/perl use Test::More qw(no_plan); my (@this,@that); @this = ('Fred','Wilma'); @that = ('Fred','Wilma'); ok(eq_set(\@this, \@that), 'Shallow Order Same'); @this = ('Fred','Wilma'); @that = ('Wilma','Fred'); ok(eq_set(\@this, \@that), 'Shallow Order Diff'); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { fred => 1 }, { wilma => 1 } ); ok(eq_set(\@this, \@that), 'Deep Order Same'); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { wilma => 1 }, { fred => 1 } ); ok(eq_set(\@this, \@that), 'Deep Order Diff'); __END__ prints: ok 1 - Shallow Order Same ok 2 - Shallow Order Diff ok 3 - Deep Order Same not ok 4 - Deep Order Diff # Failed test ( at line 21) 1..4 # Looks like you failed 1 tests of 4.



      Thanks for that, edoc. That makes at least two of us who've interpreted the docs in a way that wasn't intended.

      So, is the best way to proceed to sort my array before I do the test? This time I've already defined a way to order my hashrefs, but I can see that sometimes I might have to write a sorting function when I otherwise might not have needed to.

      So now I'm thinking that it should be possible to write a standard comparison function, that works by examining the output of Data::Dumper to force a well defined (but otherwise meaningless) order on an array, so that it could make equal_set work as I thought the docs said it should.

      Anyone see why this shouldn't work? Can anybody see any traps I might fall into, if i were to try to write this?

      Kind regards,


        ok, knew there had to be something out there... Test::Deep using 'bag' (or set maybe, they look the same in the docs..) Unfortunately it doesn't seem to be a standard Perl module.

        #!/usr/bin/perl use Test::More qw(no_plan); use Test::Deep; my (@this,@that); @this = ( { fred => 1 }, { wilma => 1 } ); @that = ( { wilma => 1 }, { fred => 1 } ); cmp_deeply(\@this, bag(@that), 'cmp_deeply Deep Order Diff');



Re: Test::More - equal_set
by TomDLux (Vicar) on Jun 01, 2003 at 00:35 UTC

    Try Test::Differences, to isolate how the elements differ.

    I haven't used eq_set---when I need to compare deep data structures I tend to go with is_deeply().

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://262136]
Approved by Ovid
Front-paged by Enlil
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (4)
As of 2022-12-08 19:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found