#146 ✓resolved
Lawrence Pit

Hash equality

Reported by Lawrence Pit | July 27th, 2009 @ 04:33 AM | in 3.0

Consider this (simplified) code:

  function toArray(iterable) {
    var length = iterable.length, results = new Array(length);
    while (length--) results[length] = iterable[length];
    return results;
  }

I.e., note how the returned array is exactly the same, but it is build by copying in reverse order.

and this test:

  it "should copy the array"
    var args = function() { return toArray(arguments) }(9,8,7);
    args.should.eql [9,8,7]
  end

This work in Firefox, but fails in IE7 where it responds with "expected [7,8,9] to eql [9,8,7]"

It appears that the hash value that JSpec calculates for arrays is dependent on how the array is built. This is because the hash value algorithm iterates over a hash, even if the value is an array.

I expect the same issue can occur with testing for object equality. For example, {x:1, y:2}.should.eql {y:2, x:1} would probably fail, where I think it should succeed.

[I just tested this, it does indeed fail, also in firefox]

I think the hash algorithm should not use a "for (key in object)" loop (in the +each+ function) if the object is an array: it should really iterate over the indexes instead to preserve the correct order. If it is an object I would suggest it should sort on the keys first before calling the callback functions.

Comments and changes to this ticket

  • TJ Holowaychuk

    TJ Holowaychuk July 27th, 2009 @ 05:52 PM

    • State changed from “new” to “open”
    • Tag set to bug
    • Assigned user set to “TJ Holowaychuk”
    • Milestone set to 3.0

    True, your right. It does require that the order is the same.
    I agree that hashes should allow arbitrary order, however arrays I would argue that they should not
    since many specs utilize this to check order as well.

    Perhaps you should use:

      args.should.include 9, 8, 7
    
    One a side note, if you want arguments as an array simply do:
    Array.prototype.slice.call(arguments)
    
  • TJ Holowaychuk

    TJ Holowaychuk July 27th, 2009 @ 05:53 PM

    • Title changed from “Bug when testing for array equality” to “Hash equality”
  • TJ Holowaychuk

    TJ Holowaychuk July 27th, 2009 @ 06:08 PM

    • State changed from “open” to “resolved”

    Hash equality is fixed in 2.8.1, thanks!

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Tiny, full-featured JavaScript BDD framework.

People watching this ticket

Tags

Pages