Skip to content

Commit

Permalink
Fix RegularContiguousSet.intersection to handle singleton results.
Browse files Browse the repository at this point in the history
Pull request from perceptron8 <perceptron8@gmail.com>
#2197

Fixes #2196
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=106153276
  • Loading branch information
perceptron8 authored and cpovirk committed Oct 23, 2015
1 parent c071822 commit 44a2592
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 30 deletions.
66 changes: 37 additions & 29 deletions guava-tests/test/com/google/common/collect/ContiguousSetTest.java
Expand Up @@ -50,27 +50,33 @@
*/
@GwtCompatible(emulated = true)
public class ContiguousSetTest extends TestCase {
private static DiscreteDomain<Integer> NOT_EQUAL_TO_INTEGERS = new DiscreteDomain<Integer>() {
@Override public Integer next(Integer value) {
return integers().next(value);
}

@Override public Integer previous(Integer value) {
return integers().previous(value);
}

@Override public long distance(Integer start, Integer end) {
return integers().distance(start, end);
}

@Override public Integer minValue() {
return integers().minValue();
}

@Override public Integer maxValue() {
return integers().maxValue();
}
};
private static final DiscreteDomain<Integer> NOT_EQUAL_TO_INTEGERS =
new DiscreteDomain<Integer>() {
@Override
public Integer next(Integer value) {
return integers().next(value);
}

@Override
public Integer previous(Integer value) {
return integers().previous(value);
}

@Override
public long distance(Integer start, Integer end) {
return integers().distance(start, end);
}

@Override
public Integer minValue() {
return integers().minValue();
}

@Override
public Integer maxValue() {
return integers().maxValue();
}
};

public void testEquals() {
new EqualsTester()
Expand Down Expand Up @@ -150,11 +156,11 @@ public void testCreate_empty() {
public void testHeadSet() {
ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers());
assertThat(set.headSet(1)).isEmpty();
assertThat(set.headSet(2)).contains(1);
assertThat(set.headSet(2)).containsExactly(1).inOrder();
assertThat(set.headSet(3)).containsExactly(1, 2).inOrder();
assertThat(set.headSet(4)).containsExactly(1, 2, 3).inOrder();
assertThat(set.headSet(Integer.MAX_VALUE)).containsExactly(1, 2, 3).inOrder();
assertThat(set.headSet(1, true)).contains(1);
assertThat(set.headSet(1, true)).containsExactly(1).inOrder();
assertThat(set.headSet(2, true)).containsExactly(1, 2).inOrder();
assertThat(set.headSet(3, true)).containsExactly(1, 2, 3).inOrder();
assertThat(set.headSet(4, true)).containsExactly(1, 2, 3).inOrder();
Expand All @@ -170,10 +176,10 @@ public void testTailSet() {
assertThat(set.tailSet(Integer.MIN_VALUE)).containsExactly(1, 2, 3).inOrder();
assertThat(set.tailSet(1)).containsExactly(1, 2, 3).inOrder();
assertThat(set.tailSet(2)).containsExactly(2, 3).inOrder();
assertThat(set.tailSet(3)).contains(3);
assertThat(set.tailSet(3)).containsExactly(3).inOrder();
assertThat(set.tailSet(Integer.MIN_VALUE, false)).containsExactly(1, 2, 3).inOrder();
assertThat(set.tailSet(1, false)).containsExactly(2, 3).inOrder();
assertThat(set.tailSet(2, false)).contains(3);
assertThat(set.tailSet(2, false)).containsExactly(3).inOrder();
assertThat(set.tailSet(3, false)).isEmpty();
}

Expand All @@ -185,17 +191,17 @@ public void testSubSet() {
ImmutableSortedSet<Integer> set = ContiguousSet.create(Range.closed(1, 3), integers());
assertThat(set.subSet(1, 4)).containsExactly(1, 2, 3).inOrder();
assertThat(set.subSet(2, 4)).containsExactly(2, 3).inOrder();
assertThat(set.subSet(3, 4)).contains(3);
assertThat(set.subSet(3, 4)).containsExactly(3).inOrder();
assertThat(set.subSet(3, 3)).isEmpty();
assertThat(set.subSet(2, 3)).contains(2);
assertThat(set.subSet(2, 3)).containsExactly(2).inOrder();
assertThat(set.subSet(1, 3)).containsExactly(1, 2).inOrder();
assertThat(set.subSet(1, 2)).contains(1);
assertThat(set.subSet(1, 2)).containsExactly(1).inOrder();
assertThat(set.subSet(2, 2)).isEmpty();
assertThat(set.subSet(Integer.MIN_VALUE, Integer.MAX_VALUE)).containsExactly(1, 2, 3).inOrder();
assertThat(set.subSet(1, true, 3, true)).containsExactly(1, 2, 3).inOrder();
assertThat(set.subSet(1, false, 3, true)).containsExactly(2, 3).inOrder();
assertThat(set.subSet(1, true, 3, false)).containsExactly(1, 2).inOrder();
assertThat(set.subSet(1, false, 3, false)).contains(2);
assertThat(set.subSet(1, false, 3, false)).containsExactly(2).inOrder();
}

public void testSubSet_outOfOrder() {
Expand Down Expand Up @@ -319,6 +325,8 @@ public void testIntersection() {
ContiguousSet.create(Range.open(-1, 4), integers()).intersection(set));
assertEquals(ImmutableSet.of(1, 2, 3),
set.intersection(ContiguousSet.create(Range.open(-1, 4), integers())));
assertEquals(
ImmutableSet.of(3), set.intersection(ContiguousSet.create(Range.closed(3, 5), integers())));
}

@GwtIncompatible("suite")
Expand Down
Expand Up @@ -157,7 +157,7 @@ public ContiguousSet<C> intersection(ContiguousSet<C> other) {
} else {
C lowerEndpoint = Ordering.natural().max(this.first(), other.first());
C upperEndpoint = Ordering.natural().min(this.last(), other.last());
return (lowerEndpoint.compareTo(upperEndpoint) < 0)
return (lowerEndpoint.compareTo(upperEndpoint) <= 0)
? ContiguousSet.create(Range.closed(lowerEndpoint, upperEndpoint), domain)
: new EmptyContiguousSet<C>(domain);
}
Expand Down

0 comments on commit 44a2592

Please sign in to comment.