mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
补充测试用例
This commit is contained in:
@@ -2,8 +2,10 @@ package cn.hutool.core.stream;
|
||||
|
||||
import cn.hutool.core.collection.CollUtil;
|
||||
import cn.hutool.core.collection.ListUtil;
|
||||
import cn.hutool.core.lang.Opt;
|
||||
import cn.hutool.core.map.MapUtil;
|
||||
import cn.hutool.core.math.NumberUtil;
|
||||
import java.math.RoundingMode;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
@@ -85,10 +87,10 @@ public class EasyStreamTest {
|
||||
final List<Integer> orders = Arrays.asList(1, 2, 3, 2);
|
||||
final List<String> list = Arrays.asList("dromara", "guava", "sweet", "hutool");
|
||||
final Map<Integer, String> map = MapUtil.<Integer, String>builder()
|
||||
.put(1, "dromara")
|
||||
.put(2, "hutool")
|
||||
.put(3, "sweet")
|
||||
.build();
|
||||
.put(1, "dromara")
|
||||
.put(2, "hutool")
|
||||
.put(3, "sweet")
|
||||
.build();
|
||||
|
||||
final Map<Integer, String> toZip = EasyStream.of(orders).toZip(list);
|
||||
Assert.assertEquals(map, toZip);
|
||||
@@ -126,15 +128,15 @@ public class EasyStreamTest {
|
||||
final List<Integer> list = Arrays.asList(1, 2, 3);
|
||||
final Map<String, List<Integer>> group = EasyStream.of(list).group(String::valueOf);
|
||||
Assert.assertEquals(
|
||||
new HashMap<String, List<Integer>>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
new HashMap<String, List<Integer>>() {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
{
|
||||
put("1", singletonList(1));
|
||||
put("2", singletonList(2));
|
||||
put("3", singletonList(3));
|
||||
}
|
||||
}, group);
|
||||
{
|
||||
put("1", singletonList(1));
|
||||
put("2", singletonList(2));
|
||||
put("3", singletonList(3));
|
||||
}
|
||||
}, group);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -144,7 +146,7 @@ public class EasyStreamTest {
|
||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
||||
// 并行流时正常
|
||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList());
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel().mapIdx((e, i) -> i + 1 + "." + e).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -185,7 +187,8 @@ public class EasyStreamTest {
|
||||
Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2));
|
||||
|
||||
Assert.assertEquals(
|
||||
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true).distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count()
|
||||
4, EasyStream.of(1, 2, 2, null, 3, null).parallel(true)
|
||||
.distinct(t -> Objects.isNull(t) ? null : t.toString()).sequential().count()
|
||||
);
|
||||
}
|
||||
|
||||
@@ -221,28 +224,28 @@ public class EasyStreamTest {
|
||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"), mapIndex);
|
||||
// 并行流时正常
|
||||
Assert.assertEquals(Arrays.asList("1.dromara", "2.hutool", "3.sweet"),
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel()
|
||||
.flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList());
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel()
|
||||
.flatMapIdx((e, i) -> EasyStream.of(i + 1 + "." + e)).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeek() {
|
||||
EasyStream.of("one", "two", "three", "four")
|
||||
.filter(e -> e.length() == 4)
|
||||
.peek(e -> Assert.assertEquals("four", e))
|
||||
.map(String::toUpperCase)
|
||||
.peek(e -> Assert.assertEquals("FOUR", e))
|
||||
.collect(Collectors.toList());
|
||||
.filter(e -> e.length() == 4)
|
||||
.peek(e -> Assert.assertEquals("four", e))
|
||||
.map(String::toUpperCase)
|
||||
.peek(e -> Assert.assertEquals("FOUR", e))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPeekIdx() {
|
||||
EasyStream.of("one", "two", "three", "four")
|
||||
.filter(e -> e.length() == 4)
|
||||
.peekIdx((e, i) -> Assert.assertEquals("four:0", e + ":" + i))
|
||||
.map(String::toUpperCase)
|
||||
.peekIdx((e, i) -> Assert.assertEquals("FOUR:0", e + ":" + i))
|
||||
.collect(Collectors.toList());
|
||||
.filter(e -> e.length() == 4)
|
||||
.peekIdx((e, i) -> Assert.assertEquals("four:0", e + ":" + i))
|
||||
.map(String::toUpperCase)
|
||||
.peekIdx((e, i) -> Assert.assertEquals("FOUR:0", e + ":" + i))
|
||||
.collect(Collectors.toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -278,7 +281,7 @@ public class EasyStreamTest {
|
||||
Assert.assertEquals(Arrays.asList("dromara", "hutool"), filterIndex);
|
||||
// 并行流时正常
|
||||
Assert.assertEquals(Arrays.asList("dromara", "hutool"),
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList());
|
||||
EasyStream.of("dromara", "hutool", "sweet").parallel().filterIdx((e, i) -> i < 2).toList());
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -384,23 +387,23 @@ public class EasyStreamTest {
|
||||
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
||||
|
||||
final List<String> res1 = EasyStream.of(list)
|
||||
// 舍弃 5
|
||||
.takeWhile(e -> e < 5)
|
||||
// 过滤奇数
|
||||
.filter(e -> (e & 1) == 0)
|
||||
// 反序
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
// 舍弃 5
|
||||
.takeWhile(e -> e < 5)
|
||||
// 过滤奇数
|
||||
.filter(e -> (e & 1) == 0)
|
||||
// 反序
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
Assert.assertEquals(Arrays.asList("4", "2"), res1);
|
||||
|
||||
final List<Integer> res2 = EasyStream.iterate(1, i -> i + 1)
|
||||
.parallel()
|
||||
.takeWhile(e -> e < 5)
|
||||
.map(String::valueOf)
|
||||
.map(Integer::valueOf)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.toList();
|
||||
.parallel()
|
||||
.takeWhile(e -> e < 5)
|
||||
.map(String::valueOf)
|
||||
.map(Integer::valueOf)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.toList();
|
||||
Assert.assertEquals(Arrays.asList(1, 2, 3, 4), res2);
|
||||
}
|
||||
|
||||
@@ -410,25 +413,25 @@ public class EasyStreamTest {
|
||||
final List<Integer> list = EasyStream.iterate(1, i -> i <= 10, i -> i + 1).toList();
|
||||
|
||||
final List<String> res1 = EasyStream.of(list)
|
||||
// 舍弃 5之前的数字
|
||||
.dropWhile(e -> e < 5)
|
||||
// 过滤偶数
|
||||
.filter(e -> (e & 1) == 1)
|
||||
// 反序
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
// 舍弃 5之前的数字
|
||||
.dropWhile(e -> e < 5)
|
||||
// 过滤偶数
|
||||
.filter(e -> (e & 1) == 1)
|
||||
// 反序
|
||||
.sorted(Comparator.reverseOrder())
|
||||
.map(String::valueOf)
|
||||
.toList();
|
||||
Assert.assertEquals(Arrays.asList("9", "7", "5"), res1);
|
||||
|
||||
final List<Integer> res2 = EasyStream.of(list)
|
||||
.parallel()
|
||||
.dropWhile(e -> e < 5)
|
||||
// 过滤偶数
|
||||
.filter(e -> (e & 1) == 1)
|
||||
.map(String::valueOf)
|
||||
.map(Integer::valueOf)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.toList();
|
||||
.parallel()
|
||||
.dropWhile(e -> e < 5)
|
||||
// 过滤偶数
|
||||
.filter(e -> (e & 1) == 1)
|
||||
.map(String::valueOf)
|
||||
.map(Integer::valueOf)
|
||||
.sorted(Comparator.naturalOrder())
|
||||
.toList();
|
||||
Assert.assertEquals(Arrays.asList(5, 7, 9), res2);
|
||||
}
|
||||
|
||||
@@ -440,6 +443,7 @@ public class EasyStreamTest {
|
||||
|
||||
@Test
|
||||
public void testIntSumAndAvg() {
|
||||
//测试int类型的总和
|
||||
int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue);
|
||||
Assert.assertEquals(sum, 55);
|
||||
|
||||
@@ -447,38 +451,102 @@ public class EasyStreamTest {
|
||||
List<Integer> integerList = new ArrayList<>();
|
||||
int emptySum = EasyStream.of(integerList).sum(Integer::intValue);
|
||||
Assert.assertEquals(emptySum, 0);
|
||||
|
||||
//测试平均值
|
||||
OptionalDouble avg = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).avg(Integer::intValue);
|
||||
Assert.assertEquals(avg.getAsDouble(), 5.5, 2);
|
||||
|
||||
//测试元素为空
|
||||
OptionalDouble emptyAvg = EasyStream.of(integerList).avg(Integer::intValue);
|
||||
Assert.assertFalse(emptyAvg.isPresent());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDoubleSumAndAvg() {
|
||||
//测试double类型的sum
|
||||
double doubleSum = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10).sum(Double::doubleValue);
|
||||
Assert.assertEquals(doubleSum, 59.6,2);
|
||||
Assert.assertEquals(doubleSum, 59.6, 2);
|
||||
|
||||
//测试double类型的sum 无元素double
|
||||
List<Double> doubleList = new ArrayList<>();
|
||||
double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue);
|
||||
Assert.assertEquals(emptySum, 0.0,2);
|
||||
Assert.assertEquals(emptySum, 0.0, 2);
|
||||
|
||||
//测试double类型的avg
|
||||
OptionalDouble doubleAvg = EasyStream.of(1.1, 2.2, 3.3, 4.4, 5.5, 6.6, 7.7, 8.8, 9.9, 10.10)
|
||||
.avg(Double::doubleValue);
|
||||
Assert.assertEquals(doubleAvg.getAsDouble(), 5.96, 2);
|
||||
|
||||
//测试double类型的 空元素的avg
|
||||
OptionalDouble emptyDoubleAvg = EasyStream.of(doubleList).avg(Double::doubleValue);
|
||||
Assert.assertFalse(emptyDoubleAvg.isPresent());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testLongSumAndAvg() {
|
||||
//测试long类型的sum
|
||||
long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue);
|
||||
Assert.assertEquals(sum, 55);
|
||||
|
||||
//测试long类型的空元素 sum
|
||||
List<Long> longList = new ArrayList<>();
|
||||
double emptySum = EasyStream.of(longList).sum(Long::longValue);
|
||||
long emptySum = EasyStream.of(longList).sum(Long::longValue);
|
||||
Assert.assertEquals(emptySum, 0L);
|
||||
|
||||
//测试long类型的avg
|
||||
OptionalDouble doubleAvg = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).avg(Long::longValue);
|
||||
Assert.assertEquals(doubleAvg.getAsDouble(), 5.5, 2);
|
||||
|
||||
//测试long类型的avg 空元素
|
||||
OptionalDouble emptyDoubleAvg = EasyStream.of(longList).avg(Long::longValue);
|
||||
Assert.assertFalse(emptyDoubleAvg.isPresent());
|
||||
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testBigDecimalSumAndAvg() {
|
||||
BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity());
|
||||
Assert.assertEquals(sum, 59.6);
|
||||
//测试bigDecimal的sum
|
||||
BigDecimal sum = EasyStream.of(NumberUtil.toBigDecimal(1.1), NumberUtil.toBigDecimal(2.2),
|
||||
NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4), NumberUtil.toBigDecimal(5.5),
|
||||
NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7), NumberUtil.toBigDecimal(8.8),
|
||||
NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10)).sum(Function.identity());
|
||||
Assert.assertEquals(sum, NumberUtil.toBigDecimal(59.6));
|
||||
|
||||
List<BigDecimal> bigDecimalList = new ArrayList<>();
|
||||
BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity());
|
||||
//测试bigDecimal的sum 空元素
|
||||
List<BigDecimal> bigDecimalEmptyList = new ArrayList<>();
|
||||
BigDecimal emptySum = EasyStream.of(bigDecimalEmptyList).sum(Function.identity());
|
||||
Assert.assertEquals(emptySum, BigDecimal.ZERO);
|
||||
|
||||
//测试bigDecimal的avg
|
||||
Opt<BigDecimal> bigDecimalAvgFullParam = EasyStream.of(NumberUtil.toBigDecimal(1.1),
|
||||
NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4),
|
||||
NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7),
|
||||
NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10))
|
||||
.avg(Function.identity(), 2, RoundingMode.HALF_UP);
|
||||
Assert.assertEquals(bigDecimalAvgFullParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||
|
||||
Opt<BigDecimal> bigDecimalAvgOneParam = EasyStream.of(NumberUtil.toBigDecimal(1.1),
|
||||
NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4),
|
||||
NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7),
|
||||
NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10))
|
||||
.avg(Function.identity());
|
||||
Assert.assertEquals(bigDecimalAvgOneParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||
|
||||
Opt<BigDecimal> bigDecimalAvgTwoParam = EasyStream.of(NumberUtil.toBigDecimal(1.1),
|
||||
NumberUtil.toBigDecimal(2.2), NumberUtil.toBigDecimal(3.3), NumberUtil.toBigDecimal(4.4),
|
||||
NumberUtil.toBigDecimal(5.5), NumberUtil.toBigDecimal(6.6), NumberUtil.toBigDecimal(7.7),
|
||||
NumberUtil.toBigDecimal(8.8), NumberUtil.toBigDecimal(9.9), NumberUtil.toBigDecimal(10.10))
|
||||
.avg(Function.identity(), 2);
|
||||
Assert.assertEquals(bigDecimalAvgTwoParam.get(), NumberUtil.toBigDecimal(5.96));
|
||||
|
||||
//测试bigDecimal的avg 空元素
|
||||
Opt<BigDecimal> emptyBigDecimalAvg = EasyStream.of(bigDecimalEmptyList)
|
||||
.avg(Function.identity(), 2, RoundingMode.HALF_UP);
|
||||
Assert.assertFalse(emptyBigDecimalAvg.isPresent());
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user