Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

assert_same_elements optimization #1

Merged
1 commit merged into from

1 participant

Trent Ogren
Trent Ogren

For arrays that have duplicate elements, assert_same_elements counts how many times that element occurs in the array multiple times. I made a branch that prevents that re-work
https://github.com/misfo/shoulda-context/tree/assert_same_elements-optimization

Also, this branch will walk your dog and give your grandma high fives

Trent Ogren misfo small performance optimization of assert_same_elements
prevent re-counting the number of occurrences there are in arrays for duplicated values
a53799c
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Jan 28, 2011
  1. Trent Ogren

    small performance optimization of assert_same_elements

    misfo authored
    prevent re-counting the number of occurrences there are in arrays for duplicated values
This page is out of date. Refresh to see the latest.
4 lib/shoulda/context/assertions.rb
View
@@ -9,8 +9,8 @@ def assert_same_elements(a1, a2, msg = nil)
[a1, a2].each {|a| assert_respond_to(a, m, "Are you sure that #{a.inspect} is an array? It doesn't respond to #{m}.") }
end
- assert a1h = a1.inject({}) { |h,e| h[e] = a1.select { |i| i == e }.size; h }
- assert a2h = a2.inject({}) { |h,e| h[e] = a2.select { |i| i == e }.size; h }
+ assert a1h = a1.inject({}) { |h,e| h[e] ||= a1.select { |i| i == e }.size; h }
+ assert a2h = a2.inject({}) { |h,e| h[e] ||= a2.select { |i| i == e }.size; h }
assert_equal(a1h, a2h, msg)
end
7 test/shoulda/helpers_test.rb
View
@@ -26,10 +26,17 @@ class HelpersTest < Test::Unit::TestCase # :nodoc:
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements(@a, [3, 3, "def", "abc"])
end
+ assert_same_elements([@a, "abc"].flatten, ["abc", 3, "def", "abc"])
assert_raises(Test::Unit::AssertionFailedError) do
assert_same_elements([@a, "abc"].flatten, [3, 3, "def", "abc"])
end
end
+
+ should "only count the number of occurrences once for each unique value" do
+ a1 = [@a, "abc"].flatten
+ a1.expects(:select).times(3).returns(["abc", "abc"], ["def"], [3])
+ assert_same_elements(a1, ["abc", 3, "def", "abc"])
+ end
end
context "a matching matcher" do
Something went wrong with that request. Please try again.