Skip to content

Commit

Permalink
[Test/More.pir] is_deeply can compare hashes, but depends on insertio…
Browse files Browse the repository at this point in the history
…n order.

Depending on the order in which a hash was built, two hashes can have perfectly identical contents, but a different Iter order.
Thus, after comparing the number of entries, we should base the rest of the comparison on *one* Iter, not two in parallel.
I think we still need more work to handle undef values properly, but this is a step in the right direction.  While we're at it, add a test.

git-svn-id: https://svn.parrot.org/parrot/trunk@39564 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
Infinoid committed Jun 14, 2009
1 parent 248525d commit 2eb4839
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 17 deletions.
18 changes: 6 additions & 12 deletions runtime/parrot/library/Test/More.pir
Expand Up @@ -468,8 +468,7 @@ structures are passed, C<is_deeply> does a deep comparison by walking each
structure. It passes if they are equal and fails otherwise. This will
report the results with the optional test description in C<description>.

This only handles comparisons of array-like structures. It shouldn't be too
hard to extend it for hash-like structures, too.
This handles comparisons of array-like and hash-like structures.

=cut

Expand Down Expand Up @@ -640,27 +639,22 @@ hard to extend it for hash-like structures, too.

compare_contents:
.local pmc l_iter
.local pmc r_iter
.local int count

l_iter = new 'Iterator', l_hash
r_iter = new 'Iterator', r_hash
l_iter = 0
r_iter = 0
count = 0

.local pmc l_key
.local pmc r_key
.local pmc key
.local pmc l_elem
.local pmc r_elem
.local int elems_equal

iter_start:
unless l_iter goto iter_end
l_key = shift l_iter
r_key = shift r_iter
l_elem = l_hash[ l_key ]
r_elem = r_hash[ r_key ]
key = shift l_iter
l_elem = l_hash[ key ]
r_elem = r_hash[ key ]

elems_equal = compare_elements( l_elem, r_elem, position )
unless elems_equal goto elems_not_equal
Expand All @@ -669,7 +663,7 @@ hard to extend it for hash-like structures, too.
goto iter_start

elems_not_equal:
unshift position, l_key
unshift position, key
.return( 0 )

iter_end:
Expand Down
15 changes: 10 additions & 5 deletions t/library/test_more.t
Expand Up @@ -22,7 +22,7 @@
exports = split " ", "plan test_out test_diag test_fail test_pass test_test"
test_namespace.'export_to'(curr_namespace, exports)

plan( 74 )
plan( 75 )

test_skip()
test_todo()
Expand Down Expand Up @@ -346,24 +346,29 @@
test_test( 'failing is_deeply() for hashes with different numbers of keys' )

left['bar'] = 1
right['foo'] = 1
right['bar'] = 1

test_fail( 'more diag' )
is_deeply( left, right, 'more diag' )
test_diag( 'Mismatch: expected 2 elements, received 1' )
test_test( '... with description and proper pluralization' )

right['bar'] = 2
right['foo'] = 2

test_fail()
is_deeply( left, right )
test_diag( 'Mismatch at [bar]: expected 1, received 2' )
test_diag( 'Mismatch at [foo]: expected 1, received 2' )
test_test( 'failing is_deeply() for hash with value mismatch' )

test_fail( '2 is not 1' )
is_deeply( left, right, '2 is not 1' )
test_diag( 'Mismatch at [bar]: expected 1, received 2' )
test_diag( 'Mismatch at [foo]: expected 1, received 2' )
test_test( '... with description' )

right['foo'] = 1
test_pass()
is_deeply( left, right )
test_test( 'passing test is_deeply() for hashes created in different orders' )
.end

.sub test_is_deeply_mismatch
Expand Down

0 comments on commit 2eb4839

Please sign in to comment.