Skip to content

Commit

Permalink
Import upstream fix for overlapping() method.
Browse files Browse the repository at this point in the history
  • Loading branch information
io7m committed Mar 19, 2024
1 parent 57abd70 commit 9f9b9be
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,15 @@ class IntervalTree<S : Comparable<S>> private constructor(
val leftST = this.left
val rightST = this.right

check(this != leftST) { "A node must not be equal to its own left child." }
check(this != rightST) { "A node must not be equal to its own right child." }
check(this != this.parent) { "A node must not be equal to its own parent." }
check(this != leftST) {
"A node must not be equal to its own left child."
}
check(this != rightST) {
"A node must not be equal to its own right child."
}
check(this != this.parent) {
"A node must not be equal to its own parent."
}

if (leftST != null) {
val cmp = leftST.interval.compare(this.interval)
Expand All @@ -126,10 +132,16 @@ class IntervalTree<S : Comparable<S>> private constructor(
}

fun setNewParent(node : Node<S>?) {
check(node != this) { "A node's parent must not be equal to the node." }
check(node != this) {
"A node's parent must not be equal to the node."
}
if (node != null) {
check(node != this.left) { "A node's parent must not be equal to its own left child." }
check(node != this.right) { "A node's parent must not be equal to its own right child." }
check(node != this.left) {
"A node's parent must not be equal to its own left child."
}
check(node != this.right) {
"A node's parent must not be equal to its own right child."
}
}
this.parent = node
this.checkInvariants()
Expand Down Expand Up @@ -294,8 +306,9 @@ class IntervalTree<S : Comparable<S>> private constructor(

current.checkInvariants()

check(current.balanceFactor().isBalanced)
{ "Balance factor of node $current is ${current.balanceFactor()}" }
check(current.balanceFactor().isBalanced) {
"Balance factor of node $current is ${current.balanceFactor()}"
}

this.validateAt(current.left)
this.validateAt(current.right)
Expand Down Expand Up @@ -339,11 +352,13 @@ class IntervalTree<S : Comparable<S>> private constructor(
}

IntervalComparison.LESS_THAN -> {
current.takeOwnershipLeft(this.create(current, current.left, interval))
current.takeOwnershipLeft(
this.create(current, current.left, interval))
}

IntervalComparison.MORE_THAN -> {
current.takeOwnershipRight(this.create(current, current.right, interval))
current.takeOwnershipRight(
this.create(current, current.right, interval))
}
}

Expand Down Expand Up @@ -590,26 +605,26 @@ class IntervalTree<S : Comparable<S>> private constructor(

val currentStream : Stream<IntervalType<S>> =
if (interval.overlaps(current.interval)) {
Stream.of(current.interval)
} else {
Stream.empty()
}
Stream.of(current.interval)
} else {
Stream.empty()
}

val lst = current.left
val leftStream : Stream<IntervalType<S>> =
if (lst != null && lst.maximum.overlaps(interval)) {
if (lst != null && lst.maximum.upper() >= interval.lower()) {
this.overlappingAt(lst, interval)
} else {
Stream.empty()
}
} else {
Stream.empty()
}

val rst = current.right
val rightStream : Stream<IntervalType<S>> =
if (rst != null && rst.maximum.overlaps(interval)) {
if (rst != null) {
this.overlappingAt(rst, interval)
} else {
Stream.empty()
}
} else {
Stream.empty()
}

return Stream.of(leftStream, currentStream, rightStream)
.flatMap { x -> x }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ private Unit logChange(
*/

@Test
public void testSizeEmpty()
public final void testSizeEmpty()
{
this.tree = this.create();
assertEquals(0, this.tree.size());
Expand All @@ -106,7 +106,7 @@ public void testSizeEmpty()
*/

@Test
public void testSizeInsertOne()
public final void testSizeInsertOne()
{
this.tree = this.create();
this.tree.setChangeListener(this::logChange);
Expand All @@ -124,7 +124,7 @@ public void testSizeInsertOne()
*/

@Property
public void testSizeInsertMany(
public final void testSizeInsertMany(
final @ForAll("intervals") List<I> xs)
{
this.tree = this.create();
Expand Down Expand Up @@ -152,7 +152,7 @@ public void testSizeInsertMany(
*/

@Property
public void testInsertFind(
public final void testInsertFind(
final @ForAll("intervals") List<I> xs,
final @ForAll("intervals") List<I> ys)
{
Expand All @@ -177,7 +177,7 @@ public void testInsertFind(
*/

@Test
public void testOverlapsSpecific()
public final void testOverlapsSpecific()
{
this.tree = this.create();
this.tree.setChangeListener(this::logChange);
Expand All @@ -196,7 +196,7 @@ public void testOverlapsSpecific()
assertEquals(4, this.tree.size());

{
final var e = List.of(i0);
final var e = List.of(i0, i3);
final var r = List.copyOf(this.tree.overlapping(i0));
assertAll(
r.stream()
Expand Down Expand Up @@ -256,7 +256,7 @@ public void testOverlapsSpecific()
*/

@Property
public void testOverlapsForall(
public final void testOverlapsForall(
final @ForAll("intervals") List<I> xs)
{
this.tree = this.create();
Expand All @@ -271,6 +271,7 @@ public void testOverlapsForall(

for (final var x : inserted) {
final var r = this.tree.overlapping(x);
assertFalse(r.isEmpty());
assertAll(
r.stream()
.map(s -> (Executable) () -> {
Expand All @@ -288,7 +289,7 @@ public void testOverlapsForall(
*/

@Property
public void testOverlapsEmpty(
public final void testOverlapsEmpty(
final @ForAll("intervals") List<I> xs)
{
this.tree = this.create();
Expand All @@ -307,7 +308,7 @@ public void testOverlapsEmpty(
*/

@Property
public void testRemoveMany(
public final void testRemoveMany(
final @ForAll("intervals") List<I> xs)
{
this.tree = this.create();
Expand Down Expand Up @@ -343,7 +344,7 @@ public void testRemoveMany(
*/

@Test
public void testRemoveSpecific0()
public final void testRemoveSpecific0()
{
this.tree = this.create();
this.tree.setChangeListener(this::logChange);
Expand All @@ -362,7 +363,7 @@ public void testRemoveSpecific0()
*/

@Test
public void testRemoveSpecific1()
public final void testRemoveSpecific1()
{
this.tree = this.create();
this.tree.setChangeListener(this::logChange);
Expand All @@ -387,7 +388,7 @@ public void testRemoveSpecific1()
*/

@Test
public void testRemoveSpecific2()
public final void testRemoveSpecific2()
{
final var i0 =
this.interval(0L, 0L);
Expand Down Expand Up @@ -422,7 +423,7 @@ public void testRemoveSpecific2()
*/

@Property
public void testCollectionOrdered(
public final void testCollectionOrdered(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand All @@ -444,7 +445,7 @@ public void testCollectionOrdered(
*/

@Property
public void testCollectionContainsAll(
public final void testCollectionContainsAll(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand All @@ -462,7 +463,7 @@ public void testCollectionContainsAll(
*/

@Property
public void testCollectionAddAllContainsAll(
public final void testCollectionAddAllContainsAll(
final @ForAll("intervals") List<I> xs)
{
Assumptions.assumeTrue(!xs.isEmpty());
Expand All @@ -484,7 +485,7 @@ public void testCollectionAddAllContainsAll(
*/

@Property
public void testCollectionClear(
public final void testCollectionClear(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand All @@ -502,7 +503,7 @@ public void testCollectionClear(
*/

@Property
public void testCollectionToArray0(
public final void testCollectionToArray0(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand All @@ -522,7 +523,7 @@ public void testCollectionToArray0(
*/

@Property
public void testCollectionToArray1(
public final void testCollectionToArray1(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand All @@ -547,7 +548,7 @@ public void testCollectionToArray1(
*/

@Property
public void testCollectionToArray2(
public final void testCollectionToArray2(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand Down Expand Up @@ -578,7 +579,7 @@ public void testCollectionToArray2(
*/

@Property
public void testCollectionToArray3(
public final void testCollectionToArray3(
final @ForAll("intervals") List<I> xs)
{
final var unique = new TreeSet<>(xs);
Expand Down Expand Up @@ -620,7 +621,7 @@ public Set<Arbitrary<?>> apply(TypeUsage typeUsage)
}

@Property
public void testChanges(
public final void testChanges(
final @ForAll("changes") IntervalTreeChangeType c0,
final @ForAll("changes") IntervalTreeChangeType c1)
{
Expand All @@ -638,7 +639,7 @@ public void testChanges(
*/

@Property
public void testMinimumNonEmpty(
public final void testMinimumNonEmpty(
final @ForAll("intervals") List<I> xs)
{
Assumptions.assumeFalse(xs.isEmpty());
Expand All @@ -662,7 +663,7 @@ public void testMinimumNonEmpty(
*/

@Property
public void testMaximumNonEmpty(
public final void testMaximumNonEmpty(
final @ForAll("intervals") List<I> xs)
{
Assumptions.assumeFalse(xs.isEmpty());
Expand All @@ -684,7 +685,7 @@ public void testMaximumNonEmpty(
*/

@Test
public void testMinimumEmpty()
public final void testMinimumEmpty()
{
this.tree = this.create();

Expand All @@ -699,7 +700,7 @@ public void testMinimumEmpty()
*/

@Test
public void testMaximumEmpty()
public final void testMaximumEmpty()
{
this.tree = this.create();

Expand All @@ -708,4 +709,46 @@ public void testMaximumEmpty()
this.tree.maximum()
);
}

/**
* Embarassing bug.
*/

@Test
public final void testProblematicOverlapTicket1()
{
final var intervals = List.of(
this.interval(0L, 41L),
this.interval(42L, 1915L),
this.interval(1915L, 3657L),
this.interval(3657L, 5396L),
this.interval(5396L, 6699L),
this.interval(6699L, 8238L),
this.interval(8238L, 10593L),
this.interval(10593L, 12645L),
this.interval(12645L, 14885L),
this.interval(14885L, 16012L),
this.interval(16012L, 19337L),
this.interval(19337L, 21869L),
this.interval(21869L, 25744L),
this.interval(25744L, 28440L),
this.interval(28440L, 30062L),
this.interval(30062L, 32565L),
this.interval(32565L, 35904L),
this.interval(35904L, 38703L),
this.interval(38703L, 41820L),
this.interval(41820L, 44927L),
this.interval(44927L, 47841L),
this.interval(47841L, 51632L),
this.interval(51632L, 51705L)
);

this.tree = this.create();
this.tree.addAll(intervals);
final var o = this.tree.overlapping(this.interval(0L, 1L));
assertEquals(
List.of(this.interval(0L, 41L)),
List.copyOf(o)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.jqwik.api.Arbitraries;
import net.jqwik.api.Arbitrary;
import net.jqwik.api.Provide;
import org.junit.jupiter.api.Test;

import java.util.List;

Expand Down

0 comments on commit 9f9b9be

Please sign in to comment.