补充测试用例

This commit is contained in:
LoveHuahua 2023-02-13 21:48:23 +08:00
parent 835e7bf66d
commit a079a80908
2 changed files with 186 additions and 96 deletions

View File

@ -197,7 +197,8 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* @param next 用上一个元素作为参数执行并返回一个新的元素 * @param next 用上一个元素作为参数执行并返回一个新的元素
* @return 无限有序流 * @return 无限有序流
*/ */
public static <T> EasyStream<T> iterate(final T seed, final Predicate<? super T> hasNext, final UnaryOperator<T> next) { public static <T> EasyStream<T> iterate(final T seed, final Predicate<? super T> hasNext,
final UnaryOperator<T> next) {
Objects.requireNonNull(next); Objects.requireNonNull(next);
Objects.requireNonNull(hasNext); Objects.requireNonNull(hasNext);
return new EasyStream<>(StreamUtil.iterate(seed, hasNext, next)); return new EasyStream<>(StreamUtil.iterate(seed, hasNext, next));
@ -240,7 +241,8 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
* @return 拆分后元素组成的流 * @return 拆分后元素组成的流
*/ */
public static EasyStream<String> split(final CharSequence str, final String regex) { public static EasyStream<String> split(final CharSequence str, final String regex) {
return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of).orElseGet(EasyStream::empty); return Opt.ofBlankAble(str).map(CharSequence::toString).map(s -> s.split(regex)).map(EasyStream::of)
.orElseGet(EasyStream::empty);
} }
// --------------------------------------------------------------- Static method end // --------------------------------------------------------------- Static method end
@ -272,8 +274,6 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
} }
/** /**
* 计算int类型总和 * 计算int类型总和
* *
@ -315,8 +315,32 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add); return stream.map(mapper).reduce(BigDecimal.ZERO, BigDecimal::add);
} }
/**
* 计算bigDecimal平均值 并以四舍五入的方式保留2位精度
*
* @param mapper 映射
* @return 如果元素的长度为0 那么会返回为空的opt
*/
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper) {
return avg(mapper, 2);
}
/**
* 计算bigDecimal平均值 并以四舍五入的方式保留scale的进度
*
* @param mapper 映射
* @param scale 精度
* @return 如果元素的长度为0 那么会返回为空的opt
*/
public Opt<BigDecimal> avg(final Function<? super T, BigDecimal> mapper, int scale) {
return avg(mapper, scale, RoundingMode.HALF_UP);
}
/** /**
* 计算bigDecimal平均值 * 计算bigDecimal平均值
*
* @param mapper 映射 * @param mapper 映射
* @param scale 精度 * @param scale 精度
* @param roundingMode 舍入模式 * @param roundingMode 舍入模式
@ -329,12 +353,11 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
return Opt.ofNullable(null); return Opt.ofNullable(null);
} }
return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add).divide(NumberUtil.toBigDecimal(bigDecimalList.size()),scale, roundingMode)); return Opt.of(EasyStream.of(bigDecimalList).reduce(BigDecimal.ZERO, BigDecimal::add)
.divide(NumberUtil.toBigDecimal(bigDecimalList.size()), scale, roundingMode));
} }
/** /**
* 计算int平均值 * 计算int平均值
* *
@ -367,7 +390,6 @@ public class EasyStream<T> extends AbstractEnhancedWrappedStream<T, EasyStream<T
} }
/** /**
* 建造者 * 建造者
* *

View File

@ -2,8 +2,10 @@ package cn.hutool.core.stream;
import cn.hutool.core.collection.CollUtil; import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil; import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.lang.Opt;
import cn.hutool.core.map.MapUtil; import cn.hutool.core.map.MapUtil;
import cn.hutool.core.math.NumberUtil; import cn.hutool.core.math.NumberUtil;
import java.math.RoundingMode;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Test; import org.junit.Test;
@ -185,7 +187,8 @@ public class EasyStreamTest {
Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2)); Assert.assertEquals(ListUtil.sort(collect2), ListUtil.sort(distinctBy2));
Assert.assertEquals( 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()
); );
} }
@ -440,6 +443,7 @@ public class EasyStreamTest {
@Test @Test
public void testIntSumAndAvg() { public void testIntSumAndAvg() {
//测试int类型的总和
int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue); int sum = EasyStream.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10).sum(Integer::intValue);
Assert.assertEquals(sum, 55); Assert.assertEquals(sum, 55);
@ -447,38 +451,102 @@ public class EasyStreamTest {
List<Integer> integerList = new ArrayList<>(); List<Integer> integerList = new ArrayList<>();
int emptySum = EasyStream.of(integerList).sum(Integer::intValue); int emptySum = EasyStream.of(integerList).sum(Integer::intValue);
Assert.assertEquals(emptySum, 0); 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 @Test
public void testDoubleSumAndAvg() { 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); 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<>(); List<Double> doubleList = new ArrayList<>();
double emptySum = EasyStream.of(doubleList).sum(Double::doubleValue); 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 @Test
public void testLongSumAndAvg() { public void testLongSumAndAvg() {
//测试long类型的sum
long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue); long sum = EasyStream.of(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L).sum(Long::longValue);
Assert.assertEquals(sum, 55); Assert.assertEquals(sum, 55);
//测试long类型的空元素 sum
List<Long> longList = new ArrayList<>(); 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); 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 @Test
public void testBigDecimalSumAndAvg() { 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()); //测试bigDecimal的sum
Assert.assertEquals(sum, 59.6); 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的sum 空元素
BigDecimal emptySum = EasyStream.of(bigDecimalList).sum(Function.identity()); List<BigDecimal> bigDecimalEmptyList = new ArrayList<>();
BigDecimal emptySum = EasyStream.of(bigDecimalEmptyList).sum(Function.identity());
Assert.assertEquals(emptySum, BigDecimal.ZERO); 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());
} }
} }