From 215b1f0dd715d58d61aa31e3a7a8ca4ac48e218a Mon Sep 17 00:00:00 2001 From: Ryan Skonnord Date: Mon, 4 May 2020 08:10:30 -0700 Subject: [PATCH] Fix PowerSet.equals() when comparing to another PowerSet whose items 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 --- .../test/com/google/common/collect/SetsTest.java | 7 +++++++ android/guava/src/com/google/common/collect/Sets.java | 2 +- guava-gwt/test/com/google/common/collect/SetsTest_gwt.java | 5 +++++ guava-tests/test/com/google/common/collect/SetsTest.java | 7 +++++++ guava/src/com/google/common/collect/Sets.java | 2 +- 5 files changed, 21 insertions(+), 2 deletions(-) diff --git a/android/guava-tests/test/com/google/common/collect/SetsTest.java b/android/guava-tests/test/com/google/common/collect/SetsTest.java index 37061d47b80e..8aa8d8312452 100644 --- a/android/guava-tests/test/com/google/common/collect/SetsTest.java +++ b/android/guava-tests/test/com/google/common/collect/SetsTest.java @@ -949,6 +949,13 @@ public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() { } } + public void testPowerSetEquals_independentOfOrder() { + ImmutableSet elements = ImmutableSet.of(1, 2, 3, 4); + Set> forward = powerSet(elements); + Set> 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. diff --git a/android/guava/src/com/google/common/collect/Sets.java b/android/guava/src/com/google/common/collect/Sets.java index 8607828b1835..3b4dcd113b77 100644 --- a/android/guava/src/com/google/common/collect/Sets.java +++ b/android/guava/src/com/google/common/collect/Sets.java @@ -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); } diff --git a/guava-gwt/test/com/google/common/collect/SetsTest_gwt.java b/guava-gwt/test/com/google/common/collect/SetsTest_gwt.java index 338c9179e532..f5da6e817ea3 100644 --- a/guava-gwt/test/com/google/common/collect/SetsTest_gwt.java +++ b/guava-gwt/test/com/google/common/collect/SetsTest_gwt.java @@ -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(); diff --git a/guava-tests/test/com/google/common/collect/SetsTest.java b/guava-tests/test/com/google/common/collect/SetsTest.java index af00ba2da8f6..86cb6c89c23e 100644 --- a/guava-tests/test/com/google/common/collect/SetsTest.java +++ b/guava-tests/test/com/google/common/collect/SetsTest.java @@ -961,6 +961,13 @@ public void testPowerSetEqualsAndHashCode_verifyAgainstHashSet() { } } + public void testPowerSetEquals_independentOfOrder() { + ImmutableSet elements = ImmutableSet.of(1, 2, 3, 4); + Set> forward = powerSet(elements); + Set> 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. diff --git a/guava/src/com/google/common/collect/Sets.java b/guava/src/com/google/common/collect/Sets.java index b2e394bb0e44..19aa9dec8884 100644 --- a/guava/src/com/google/common/collect/Sets.java +++ b/guava/src/com/google/common/collect/Sets.java @@ -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); }