mirror of
https://gitee.com/dromara/hutool.git
synced 2026-02-09 09:16:26 +08:00
修正测试相邻区间合并失败
对于测试用例: range1 = [1, 3],其上界是 Bound.atMost(3)(右闭区间) range2 = [3, 5],其下界是 Bound.atLeast(3)(左闭区间) 在比较 Bound.atMost(3).compareTo(Bound.atLeast(3)) 时: 两者的值相等(都是3) 调用 compareIfSameBoundValue 方法 bt1 = CLOSE_UPPER_BOUND (code=2) bt2 = CLOSE_LOWER_BOUND (code=-2) 两者是错位的(一个上界一个下界) 由于 bt1.isLowerBound() 为 false(因为是上界),所以返回 -1 这导致 boundedRange.getUpperBound().compareTo(other.getLowerBound()) < 0 为 true,判定区间不相交。 但实际上,[1,3] 和 [3,5] 在点3处是相交的,因为3既包含在第一个区间又包含在第二个区间中。
This commit is contained in:
@@ -200,8 +200,14 @@ class FiniteBound<T extends Comparable<? super T>> implements Bound<T> {
|
||||
if (bt1 == bt2) {
|
||||
return 0;
|
||||
}
|
||||
// 一为左边界,一为右边界,则左边界恒在右边界后
|
||||
// 一为左边界,一为右边界
|
||||
if (bt1.isDislocated(bt2)) {
|
||||
// 特殊情况:右闭区间与左闭区间在同一点时,认为它们重合(用于区间相交判断)
|
||||
if ((bt1 == BoundType.CLOSE_UPPER_BOUND && bt2 == BoundType.CLOSE_LOWER_BOUND) ||
|
||||
(bt1 == BoundType.CLOSE_LOWER_BOUND && bt2 == BoundType.CLOSE_UPPER_BOUND)) {
|
||||
return 0;
|
||||
}
|
||||
// 一般情况:左边界恒在右边界后
|
||||
return bt1.isLowerBound() ? 1 : -1;
|
||||
}
|
||||
// 都为左边界,则封闭边界在前,若都为右边界,则封闭边界在后
|
||||
|
||||
@@ -17,14 +17,28 @@
|
||||
package cn.hutool.v7.core.lang.range;
|
||||
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
/**
|
||||
* test for {@link Bound}
|
||||
*/
|
||||
@SuppressWarnings("EqualsWithItself")
|
||||
public class BoundTest {
|
||||
|
||||
@Test
|
||||
@DisplayName("测试相邻区间合并")
|
||||
void testUnionIfIntersectedWithAdjacentRanges() {
|
||||
BoundedRange<Integer> range1 = BoundedRange.close(1, 3);
|
||||
BoundedRange<Integer> range2 = BoundedRange.close(3, 5);
|
||||
BoundedRange<Integer> result = BoundedRangeOperation.unionIfIntersected(range1, range2);
|
||||
|
||||
assertEquals(Bound.atLeast(1), result.getLowerBound());
|
||||
assertEquals(Bound.atMost(5), result.getUpperBound());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEquals() {
|
||||
final Bound<Integer> bound = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND);
|
||||
|
||||
Reference in New Issue
Block a user