Compare commits

...

8 Commits

Author SHA1 Message Date
Looly
1204a0033e fix test 2025-11-06 16:55:26 +08:00
Looly
50072cd4e7 fix test 2025-11-06 16:53:44 +08:00
Looly
1210d57b79 change name 2025-11-06 16:50:42 +08:00
Looly
5f48b13b88 修正开闭区间错误 2025-11-06 16:48:15 +08:00
Looly
4e0d106e91
!1385 修正开闭区间错误
Merge pull request !1385 from 身有光名/v7-dev
2025-11-06 07:47:00 +00:00
zongze.lee
ebba917222 测试 equals 方法与不同引用对象比较失败 2025-11-06 03:43:41 +08:00
zongze.lee
56ffa92e5e 测试不包含起始元素的迭代
内存溢出
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
	at java.base/java.util.Arrays.copyOf(Arrays.java:3481)
	at java.base/java.util.ArrayList.grow(ArrayList.java:237)
	at java.base/java.util.ArrayList.grow(ArrayList.java:244)
	at java.base/java.util.ArrayList.add(ArrayList.java:454)
	at java.base/java.util.ArrayList.add(ArrayList.java:467)
	at cn.hutool.v7.core.lang.range.RangeTest.testIteratorWithoutIncludeStart(RangeTest.java:49)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
2025-11-06 02:45:40 +08:00
zongze.lee
c0461c16f6 修正测试相邻区间合并失败
对于测试用例:
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既包含在第一个区间又包含在第二个区间中。
2025-11-06 02:09:57 +08:00
9 changed files with 209 additions and 139 deletions

View File

@ -191,7 +191,10 @@ class FiniteBound<T extends Comparable<? super T>> implements Bound<T> {
} }
/** /**
* 当两个边界的值不相等时判断它们在坐标轴上位置的先后顺序 * 当两个边界的值相等时判断它们在坐标轴上位置的先后顺序
*
* @param bound 另一边界
* @return 比较位置顺序-1当前边界在前0重合1当前边界在后
*/ */
private int compareIfSameBoundValue(final Bound<T> bound) { private int compareIfSameBoundValue(final Bound<T> bound) {
final BoundType bt1 = this.getType(); final BoundType bt1 = this.getType();
@ -200,8 +203,14 @@ class FiniteBound<T extends Comparable<? super T>> implements Bound<T> {
if (bt1 == bt2) { if (bt1 == bt2) {
return 0; return 0;
} }
// 一为左边界一为右边界则左边界恒在右边界后 // 一为左边界一为右边界
if (bt1.isDislocated(bt2)) { if (bt1.isDislocated(bt2)) {
// pr#1385@Gitee 特殊情况右闭区间与左闭区间在同一点时认为它们重合用于区间相交判断
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; return bt1.isLowerBound() ? 1 : -1;
} }
// 都为左边界则封闭边界在前若都为右边界则封闭边界在后 // 都为左边界则封闭边界在前若都为右边界则封闭边界在后

View File

@ -165,6 +165,10 @@ public class Range<T> implements Iterable<T>, Serializable {
* @return 下一步进 * @return 下一步进
*/ */
private T safeStep(final T base) { private T safeStep(final T base) {
// 添加边界检查
if (base == null || (base.equals(end))) {
return null;
}
final int index = this.index; final int index = this.index;
T next = null; T next = null;
try { try {

View File

@ -52,7 +52,8 @@ public class PhantomObj<T> extends PhantomReference<T> implements Ref<T>{
if (other == this) { if (other == this) {
return true; return true;
} else if (other instanceof PhantomObj) { } else if (other instanceof PhantomObj) {
return ObjUtil.equals(((PhantomObj<?>) other).get(), get()); // 比较原始对象的哈希码因为虚引用无法获取原始对象
return this.hashCode == ((PhantomObj<?>)other).hashCode;
} }
return false; return false;
} }

View File

@ -3,9 +3,7 @@ package cn.hutool.v7.core.io.file;
import cn.hutool.v7.core.io.IORuntimeException; import cn.hutool.v7.core.io.IORuntimeException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.List; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.*;
public class Issue4121Test { public class Issue4121Test {
@Test @Test
@ -15,15 +13,4 @@ public class Issue4121Test {
}); });
} }
@Test
void testListFileNames_RelativePath() {
// 测试相对路径相对于classpath
List<String> result = FileUtil.listFileNames("META-INF");
assertEquals(3, result.size());
assertTrue(result.contains("MANIFEST.MF"));
assertTrue(result.contains("LICENSE-notice.md"));
assertTrue(result.contains("LICENSE.md"));
}
} }

View File

@ -16,73 +16,94 @@
package cn.hutool.v7.core.lang.range; 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 org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
/** /**
* test for {@link Bound} * test for {@link Bound}
*/ */
@SuppressWarnings("EqualsWithItself") @SuppressWarnings("EqualsWithItself")
public class BoundTest { 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
void isDisjointTest(){
BoundedRange<Integer> range1 = BoundedRange.close(1, 3);
BoundedRange<Integer> range2 = BoundedRange.close(3, 5);
// 点重合相交
assertFalse(range1.isDisjoint(range2));
}
@Test @Test
public void testEquals() { public void testEquals() {
final Bound<Integer> bound = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND); final Bound<Integer> bound = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND);
Assertions.assertEquals(bound, bound); assertEquals(bound, bound);
Assertions.assertEquals(new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND), bound); assertEquals(new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND), bound);
Assertions.assertNotEquals(new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND), bound); assertNotEquals(new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND), bound);
Assertions.assertNotEquals(new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND), bound); assertNotEquals(new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND), bound);
Assertions.assertNotEquals(null, bound); assertNotEquals(null, bound);
} }
@Test @Test
public void testHashCode() { public void testHashCode() {
final int hashCode = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode(); final int hashCode = new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode();
Assertions.assertEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode()); assertEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_UPPER_BOUND).hashCode());
Assertions.assertNotEquals(hashCode, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND).hashCode()); assertNotEquals(hashCode, new FiniteBound<>(2, BoundType.OPEN_UPPER_BOUND).hashCode());
Assertions.assertNotEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND).hashCode()); assertNotEquals(hashCode, new FiniteBound<>(1, BoundType.OPEN_LOWER_BOUND).hashCode());
} }
@Test @Test
public void testNoneLowerBound() { public void testNoneLowerBound() {
final Bound<Integer> bound = Bound.noneLowerBound(); final Bound<Integer> bound = Bound.noneLowerBound();
// negate // negate
Assertions.assertEquals(bound, bound.negate()); assertEquals(bound, bound.negate());
// test // test
Assertions.assertTrue(bound.test(Integer.MAX_VALUE)); assertTrue(bound.test(Integer.MAX_VALUE));
// getType // getType
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertNull(bound.getValue()); assertNull(bound.getValue());
// toString // toString
Assertions.assertEquals("(" + "-∞", bound.descBound()); assertEquals("(" + "-∞", bound.descBound());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(1))); assertEquals(-1, bound.compareTo(Bound.atMost(1)));
Assertions.assertEquals(BoundedRange.all(), bound.toRange()); assertEquals(BoundedRange.all(), bound.toRange());
Assertions.assertEquals("{x | x > -∞}", bound.toString()); assertEquals("{x | x > -∞}", bound.toString());
} }
@Test @Test
public void testNoneUpperBound() { public void testNoneUpperBound() {
final Bound<Integer> bound = Bound.noneUpperBound(); final Bound<Integer> bound = Bound.noneUpperBound();
// negate // negate
Assertions.assertEquals(bound, bound.negate()); assertEquals(bound, bound.negate());
// test // test
Assertions.assertTrue(bound.test(Integer.MAX_VALUE)); assertTrue(bound.test(Integer.MAX_VALUE));
// getType // getType
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertNull(bound.getValue()); assertNull(bound.getValue());
// toString // toString
Assertions.assertEquals("+∞" + ")", bound.descBound()); assertEquals("+∞" + ")", bound.descBound());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(1))); assertEquals(1, bound.compareTo(Bound.atMost(1)));
Assertions.assertEquals(BoundedRange.all(), bound.toRange()); assertEquals(BoundedRange.all(), bound.toRange());
Assertions.assertEquals("{x | x < +∞}", bound.toString()); assertEquals("{x | x < +∞}", bound.toString());
} }
@Test @Test
@ -91,33 +112,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.greaterThan(0); Bound<Integer> bound = Bound.greaterThan(0);
// test // test
Assertions.assertTrue(bound.test(1)); assertTrue(bound.test(1));
Assertions.assertFalse(bound.test(0)); assertFalse(bound.test(0));
Assertions.assertFalse(bound.test(-1)); assertFalse(bound.test(-1));
// getType // getType
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
// toString // toString
Assertions.assertEquals("(0", bound.descBound()); assertEquals("(0", bound.descBound());
Assertions.assertEquals("{x | x > 0}", bound.toString()); assertEquals("{x | x > 0}", bound.toString());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound())); assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.atLeast(-1))); assertEquals(1, bound.compareTo(Bound.atLeast(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atLeast(2))); assertEquals(-1, bound.compareTo(Bound.atLeast(2)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0))); assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(0))); assertEquals(1, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(2))); assertEquals(-1, bound.compareTo(Bound.atMost(2)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound())); assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0} // { x | x >= 0}
bound = bound.negate(); bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType()); assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange()); assertNotNull(bound.toRange());
} }
@Test @Test
@ -126,33 +147,34 @@ public class BoundTest {
Bound<Integer> bound = Bound.atLeast(0); Bound<Integer> bound = Bound.atLeast(0);
// test // test
Assertions.assertTrue(bound.test(1)); assertTrue(bound.test(1));
Assertions.assertTrue(bound.test(0)); assertTrue(bound.test(0));
Assertions.assertFalse(bound.test(-1)); assertFalse(bound.test(-1));
// getType // getType
Assertions.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType()); assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
// toString // toString
Assertions.assertEquals("[0", bound.descBound()); assertEquals("[0", bound.descBound());
Assertions.assertEquals("{x | x >= 0}", bound.toString()); assertEquals("{x | x >= 0}", bound.toString());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound())); assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1))); assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0))); assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0))); assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(0))); // pr#1385@gitee 特殊情况右闭区间与左闭区间在同一点时认为这个边界相等
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(2))); assertEquals(0, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound())); assertEquals(-1, bound.compareTo(Bound.atMost(2)));
assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x < 0} // { x | x < 0}
bound = bound.negate(); bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange()); assertNotNull(bound.toRange());
} }
@Test @Test
@ -161,33 +183,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.lessThan(0); Bound<Integer> bound = Bound.lessThan(0);
// test // test
Assertions.assertFalse(bound.test(1)); assertFalse(bound.test(1));
Assertions.assertFalse(bound.test(0)); assertFalse(bound.test(0));
Assertions.assertTrue(bound.test(-1)); assertTrue(bound.test(-1));
// getType // getType
Assertions.assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_UPPER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
// toString // toString
Assertions.assertEquals("0)", bound.descBound()); assertEquals("0)", bound.descBound());
Assertions.assertEquals("{x | x < 0}", bound.toString()); assertEquals("{x | x < 0}", bound.toString());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound())); assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1))); assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0))); assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(-1))); assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.atMost(0))); assertEquals(-1, bound.compareTo(Bound.atMost(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(-1))); assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound())); assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x >= 0} // { x | x >= 0}
bound = bound.negate(); bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType()); assertEquals(BoundType.CLOSE_LOWER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange()); assertNotNull(bound.toRange());
} }
@Test @Test
@ -196,33 +218,33 @@ public class BoundTest {
Bound<Integer> bound = Bound.atMost(0); Bound<Integer> bound = Bound.atMost(0);
// test // test
Assertions.assertFalse(bound.test(1)); assertFalse(bound.test(1));
Assertions.assertTrue(bound.test(0)); assertTrue(bound.test(0));
Assertions.assertTrue(bound.test(-1)); assertTrue(bound.test(-1));
// getType // getType
Assertions.assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType()); assertEquals(BoundType.CLOSE_UPPER_BOUND, bound.getType());
// getValue // getValue
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
// toString // toString
Assertions.assertEquals("0]", bound.descBound()); assertEquals("0]", bound.descBound());
Assertions.assertEquals("{x | x <= 0}", bound.toString()); assertEquals("{x | x <= 0}", bound.toString());
// compareTo // compareTo
Assertions.assertEquals(0, bound.compareTo(bound)); assertEquals(0, bound.compareTo(bound));
Assertions.assertEquals(-1, bound.compareTo(Bound.noneUpperBound())); assertEquals(-1, bound.compareTo(Bound.noneUpperBound()));
Assertions.assertEquals(1, bound.compareTo(Bound.greaterThan(-1))); assertEquals(1, bound.compareTo(Bound.greaterThan(-1)));
Assertions.assertEquals(-1, bound.compareTo(Bound.greaterThan(0))); assertEquals(-1, bound.compareTo(Bound.greaterThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.atMost(-1))); assertEquals(1, bound.compareTo(Bound.atMost(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(0))); assertEquals(1, bound.compareTo(Bound.lessThan(0)));
Assertions.assertEquals(1, bound.compareTo(Bound.lessThan(-1))); assertEquals(1, bound.compareTo(Bound.lessThan(-1)));
Assertions.assertEquals(1, bound.compareTo(Bound.noneLowerBound())); assertEquals(1, bound.compareTo(Bound.noneLowerBound()));
// { x | x > 0} // { x | x > 0}
bound = bound.negate(); bound = bound.negate();
Assertions.assertEquals((Integer)0, bound.getValue()); assertEquals((Integer)0, bound.getValue());
Assertions.assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType()); assertEquals(BoundType.OPEN_LOWER_BOUND, bound.getType());
Assertions.assertNotNull(bound.toRange()); assertNotNull(bound.toRange());
} }
} }

View File

@ -156,7 +156,7 @@ public class BoundedRangeTest {
// isXXX // isXXX
Assertions.assertTrue(range.isDisjoint(BoundedRange.open(-5, 0))); // (-5, 0) Assertions.assertTrue(range.isDisjoint(BoundedRange.open(-5, 0))); // (-5, 0)
Assertions.assertTrue(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0] Assertions.assertFalse(range.isDisjoint(BoundedRange.close(-5, 0))); // [-5, 0]
Assertions.assertTrue(range.isIntersected(BoundedRange.open(-5, 1))); // [-5, 1] Assertions.assertTrue(range.isIntersected(BoundedRange.open(-5, 1))); // [-5, 1]
Assertions.assertFalse(range.isSubset(BoundedRange.open(0, 5))); // (0, 5) Assertions.assertFalse(range.isSubset(BoundedRange.open(0, 5))); // (0, 5)
Assertions.assertFalse(range.isProperSubset(BoundedRange.open(0, 5))); // (0, 5) Assertions.assertFalse(range.isProperSubset(BoundedRange.open(0, 5))); // (0, 5)

View File

@ -22,13 +22,17 @@ import cn.hutool.v7.core.date.DateTime;
import cn.hutool.v7.core.date.DateUtil; import cn.hutool.v7.core.date.DateUtil;
import cn.hutool.v7.core.text.StrUtil; import cn.hutool.v7.core.text.StrUtil;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.NoSuchElementException; import java.util.NoSuchElementException;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* {@link Range} 单元测试 * {@link Range} 单元测试
* *
@ -36,6 +40,18 @@ import java.util.NoSuchElementException;
*/ */
public class RangeTest { public class RangeTest {
@Test
@DisplayName("测试不包含起始元素的迭代")
void testIteratorWithoutIncludeStart() {
Range<Integer> range = new Range<>(1, 5, (current, end, index) -> current + 1, false, true);
List<Integer> elements = new ArrayList<>();
for (Integer i : range) {
elements.add(i);
}
assertEquals(List.of(2, 3, 4, 5), elements);
}
@Test @Test
public void dateRangeTest() { public void dateRangeTest() {
final DateTime start = DateUtil.parse("2017-01-01"); final DateTime start = DateUtil.parse("2017-01-01");
@ -50,9 +66,9 @@ public class RangeTest {
final Iterator<DateTime> iterator = range.iterator(); final Iterator<DateTime> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2017-01-01"), iterator.next()); assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2017-01-02"), iterator.next()); assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
Assertions.assertFalse(iterator.hasNext()); Assertions.assertFalse(iterator.hasNext());
} }
@ -75,11 +91,11 @@ public class RangeTest {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
DateUtil.rangeConsume(start, end, DateField.DAY_OF_YEAR, a -> sb.append(DateTime.of(a).dayOfMonth()).append("#")); DateUtil.rangeConsume(start, end, DateField.DAY_OF_YEAR, a -> sb.append(DateTime.of(a).dayOfMonth()).append("#"));
Assertions.assertEquals("1#2#3#", sb.toString()); assertEquals("1#2#3#", sb.toString());
final StringBuilder sb2 = new StringBuilder(); final StringBuilder sb2 = new StringBuilder();
DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb2.append(DateTime.of(a).dayOfMonth()).append("#")); DateUtil.rangeConsume(null, null, DateField.DAY_OF_YEAR, a -> sb2.append(DateTime.of(a).dayOfMonth()).append("#"));
Assertions.assertEquals(StrUtil.EMPTY, sb2.toString()); assertEquals(StrUtil.EMPTY, sb2.toString());
} }
@Test @Test
@ -90,11 +106,11 @@ public class RangeTest {
final DateRange range = DateUtil.range(start, end, DateField.MONTH); final DateRange range = DateUtil.range(start, end, DateField.MONTH);
final Iterator<DateTime> iterator = range.iterator(); final Iterator<DateTime> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-01-31"), iterator.next()); assertEquals(DateUtil.parse("2021-01-31"), iterator.next());
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-02-28"), iterator.next()); assertEquals(DateUtil.parse("2021-02-28"), iterator.next());
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(DateUtil.parse("2021-03-31"), iterator.next()); assertEquals(DateUtil.parse("2021-03-31"), iterator.next());
Assertions.assertFalse(iterator.hasNext()); Assertions.assertFalse(iterator.hasNext());
} }
@ -104,7 +120,7 @@ public class RangeTest {
final Iterator<Integer> iterator = range.iterator(); final Iterator<Integer> iterator = range.iterator();
Assertions.assertTrue(iterator.hasNext()); Assertions.assertTrue(iterator.hasNext());
Assertions.assertEquals(Integer.valueOf(1), iterator.next()); assertEquals(Integer.valueOf(1), iterator.next());
Assertions.assertFalse(iterator.hasNext()); Assertions.assertFalse(iterator.hasNext());
} }
@ -116,9 +132,9 @@ public class RangeTest {
// 测试包含开始和结束情况下步进为1的情况 // 测试包含开始和结束情况下步进为1的情况
DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR); DateRange range = DateUtil.range(start, end, DateField.DAY_OF_YEAR);
Iterator<DateTime> iterator = range.iterator(); Iterator<DateTime> iterator = range.iterator();
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-01")); assertEquals(iterator.next(), DateUtil.parse("2017-01-01"));
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-02")); assertEquals(iterator.next(), DateUtil.parse("2017-01-02"));
Assertions.assertEquals(iterator.next(), DateUtil.parse("2017-01-03")); assertEquals(iterator.next(), DateUtil.parse("2017-01-03"));
try { try {
iterator.next(); iterator.next();
Assertions.fail("已超过边界,下一个元素不应该存在!"); Assertions.fail("已超过边界,下一个元素不应该存在!");
@ -128,8 +144,8 @@ public class RangeTest {
// 测试多步进的情况 // 测试多步进的情况
range = new DateRange(start, end, DateField.DAY_OF_YEAR, 2); range = new DateRange(start, end, DateField.DAY_OF_YEAR, 2);
iterator = range.iterator(); iterator = range.iterator();
Assertions.assertEquals(DateUtil.parse("2017-01-01"), iterator.next()); assertEquals(DateUtil.parse("2017-01-01"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-03"), iterator.next()); assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
} }
@Test @Test
@ -140,9 +156,9 @@ public class RangeTest {
// 测试不包含开始结束时间的情况 // 测试不包含开始结束时间的情况
final DateRange range = new DateRange(start, end, DateField.DAY_OF_YEAR, 1, false, false); final DateRange range = new DateRange(start, end, DateField.DAY_OF_YEAR, 1, false, false);
final Iterator<DateTime> iterator = range.iterator(); final Iterator<DateTime> iterator = range.iterator();
Assertions.assertEquals(DateUtil.parse("2017-01-02"), iterator.next()); assertEquals(DateUtil.parse("2017-01-02"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-03"), iterator.next()); assertEquals(DateUtil.parse("2017-01-03"), iterator.next());
Assertions.assertEquals(DateUtil.parse("2017-01-04"), iterator.next()); assertEquals(DateUtil.parse("2017-01-04"), iterator.next());
try { try {
iterator.next(); iterator.next();
Assertions.fail("不包含结束时间情况下,下一个元素不应该存在!"); Assertions.fail("不包含结束时间情况下,下一个元素不应该存在!");
@ -156,8 +172,8 @@ public class RangeTest {
final Date end = DateUtil.parse("2017-01-31"); final Date end = DateUtil.parse("2017-01-31");
final List<DateTime> rangeToList = DateUtil.rangeToList(start, end, DateField.DAY_OF_YEAR); final List<DateTime> rangeToList = DateUtil.rangeToList(start, end, DateField.DAY_OF_YEAR);
Assertions.assertEquals(DateUtil.parse("2017-01-01"), rangeToList.get(0)); assertEquals(DateUtil.parse("2017-01-01"), rangeToList.get(0));
Assertions.assertEquals(DateUtil.parse("2017-01-02"), rangeToList.get(1)); assertEquals(DateUtil.parse("2017-01-02"), rangeToList.get(1));
} }
@ -173,8 +189,8 @@ public class RangeTest {
final DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR); final DateRange endRange = DateUtil.range(start1, end1, DateField.DAY_OF_YEAR);
// 交集 // 交集
final List<DateTime> dateTimes = DateUtil.rangeContains(startRange, endRange); final List<DateTime> dateTimes = DateUtil.rangeContains(startRange, endRange);
Assertions.assertEquals(1, dateTimes.size()); assertEquals(1, dateTimes.size());
Assertions.assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0)); assertEquals(DateUtil.parse("2017-01-31"), dateTimes.get(0));
} }
@Test @Test
@ -190,8 +206,8 @@ public class RangeTest {
// 差集 // 差集
final List<DateTime> dateTimes1 = DateUtil.rangeNotContains(startRange, endRange); final List<DateTime> dateTimes1 = DateUtil.rangeNotContains(startRange, endRange);
Assertions.assertEquals(1, dateTimes1.size()); assertEquals(1, dateTimes1.size());
Assertions.assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0)); assertEquals(DateUtil.parse("2017-01-31"), dateTimes1.get(0));
} }
} }

View File

@ -0,0 +1,31 @@
package cn.hutool.v7.core.lang.ref;
import java.lang.ref.ReferenceQueue;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PhantomObjTest {
private ReferenceQueue<String> queue;
private String testObject;
private PhantomObj<String> phantomObj;
@BeforeEach
void setUp() {
queue = new ReferenceQueue<>();
testObject = "test";
phantomObj = new PhantomObj<>(testObject, queue);
}
@Test
@DisplayName("测试 equals 方法与不同引用对象比较")
void testEqualsWithDifferentReferent() {
String differentObject = "different";
PhantomObj<String> anotherPhantomObj = new PhantomObj<>(differentObject, queue);
assertFalse(phantomObj.equals(anotherPhantomObj));
}
}