Skip to content

Commit

Permalink
Fix PowerSet.equals() when comparing to another PowerSet whose items …
Browse files Browse the repository at this point in the history
…are the same, but in a different iteration order.

RELNOTES:
  Fix issue where PowerSet.equals(PowerSet) would erroneously return
  false if the PowerSet's underlying Sets were equal, but in a different
  iteration order.

Fixes #3891, #3890

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=309745434
  • Loading branch information
RyanSkonnord authored and nick-someone committed May 4, 2020
1 parent 24fe5d9 commit 215b1f0
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 2 deletions.
Expand Up @@ -949,6 +949,13 @@ public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() {
}
}

public void testPowerSetEquals_independentOfOrder() {
ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3, 4);
Set<Set<Integer>> forward = powerSet(elements);
Set<Set<Integer>> reverse = powerSet(ImmutableSet.copyOf(elements.asList().reverse()));
new EqualsTester().addEqualityGroup(forward, reverse).testEquals();
}

/**
* Test that a hash code miscomputed by "input.hashCode() * tooFarValue / 2" is correct under our
* {@code hashCode} implementation.
Expand Down
2 changes: 1 addition & 1 deletion android/guava/src/com/google/common/collect/Sets.java
Expand Up @@ -1505,7 +1505,7 @@ public boolean contains(@NullableDecl Object obj) {
public boolean equals(@NullableDecl Object obj) {
if (obj instanceof PowerSet) {
PowerSet<?> that = (PowerSet<?>) obj;
return inputSet.equals(that.inputSet);
return inputSet.keySet().equals(that.inputSet.keySet());
}
return super.equals(obj);
}
Expand Down
5 changes: 5 additions & 0 deletions guava-gwt/test/com/google/common/collect/SetsTest_gwt.java
Expand Up @@ -303,6 +303,11 @@ public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() throws Exceptio
testCase.testPowerSetEqualsAndHashCode_verifyAgainstHashSet();
}

public void testPowerSetEquals_independentOfOrder() throws Exception {
com.google.common.collect.SetsTest testCase = new com.google.common.collect.SetsTest();
testCase.testPowerSetEquals_independentOfOrder();
}

public void testPowerSetHashCode_inputHashCodeTimesTooFarValueIsZero() throws Exception {
com.google.common.collect.SetsTest testCase = new com.google.common.collect.SetsTest();
testCase.testPowerSetHashCode_inputHashCodeTimesTooFarValueIsZero();
Expand Down
7 changes: 7 additions & 0 deletions guava-tests/test/com/google/common/collect/SetsTest.java
Expand Up @@ -961,6 +961,13 @@ public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() {
}
}

public void testPowerSetEquals_independentOfOrder() {
ImmutableSet<Integer> elements = ImmutableSet.of(1, 2, 3, 4);
Set<Set<Integer>> forward = powerSet(elements);
Set<Set<Integer>> reverse = powerSet(ImmutableSet.copyOf(elements.asList().reverse()));
new EqualsTester().addEqualityGroup(forward, reverse).testEquals();
}

/**
* Test that a hash code miscomputed by "input.hashCode() * tooFarValue / 2" is correct under our
* {@code hashCode} implementation.
Expand Down
2 changes: 1 addition & 1 deletion guava/src/com/google/common/collect/Sets.java
Expand Up @@ -1596,7 +1596,7 @@ public boolean contains(@Nullable Object obj) {
public boolean equals(@Nullable Object obj) {
if (obj instanceof PowerSet) {
PowerSet<?> that = (PowerSet<?>) obj;
return inputSet.equals(that.inputSet);
return inputSet.keySet().equals(that.inputSet.keySet());
}
return super.equals(obj);
}
Expand Down

0 comments on commit 215b1f0

Please sign in to comment.