Merge branch 'v6-dev' of gitee.com:dromara/hutool into v6-dev

This commit is contained in:
阿超
2022-06-07 15:19:45 +00:00
committed by Gitee
124 changed files with 1404 additions and 2381 deletions

View File

@@ -15,15 +15,17 @@ public class BeanCopyMappingTest {
*/
@Test
public void copyPropertiesTest() {
final CopyOptions copyOptions = CopyOptions.create()
final CopyOptions copyOptions = CopyOptions.of()
.setFieldMapping(MapUtil.of("car", "carNo"));
final B b = B.builder().car("12312312").build();
final A a = A.builder().build();
final C c = C.builder().build();
BeanUtil.copyProperties(b, a, copyOptions);
BeanUtil.copyProperties(a, c);
Assert.assertEquals("12312312", a.getCarNo());
Assert.assertEquals("12312312", c.getCarNo());
}

View File

@@ -71,7 +71,7 @@ public class BeanUtilTest {
return true;
}
}, CopyOptions.create());
}, CopyOptions.of());
Assert.assertEquals("张三", person.getName());
Assert.assertEquals(18, person.getAge());
@@ -116,7 +116,7 @@ public class BeanUtilTest {
// 错误的类型,此处忽略
map.put("age", "aaaaaa");
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreError(true));
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.of().setIgnoreError(true));
Assert.assertEquals("Joe", person.getName());
// 错误的类型不copy这个字段使用对象创建的默认值
Assert.assertEquals(0, person.getAge());
@@ -128,7 +128,7 @@ public class BeanUtilTest {
map.put("Name", "Joe");
map.put("aGe", 12);
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setIgnoreCase(true));
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.of().setIgnoreCase(true));
Assert.assertEquals("Joe", person.getName());
Assert.assertEquals(12, person.getAge());
}
@@ -144,7 +144,7 @@ public class BeanUtilTest {
mapping.put("a_name", "name");
mapping.put("b_age", "age");
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.create().setFieldMapping(mapping));
final Person person = BeanUtil.toBean(map, Person.class, CopyOptions.of().setFieldMapping(mapping));
Assert.assertEquals("Joe", person.getName());
Assert.assertEquals(12, person.getAge());
}
@@ -159,7 +159,7 @@ public class BeanUtilTest {
map.put("age", 12);
// 非空构造也可以实例化成功
final Person2 person = BeanUtil.toBean(map, Person2.class, CopyOptions.create());
final Person2 person = BeanUtil.toBean(map, Person2.class, CopyOptions.of());
Assert.assertEquals("Joe", person.name);
Assert.assertEquals(12, person.age);
}
@@ -229,7 +229,10 @@ public class BeanUtilTest {
person.setSubName("sub名字");
final Map<String, Object> map = BeanUtil.beanToMap(person, new LinkedHashMap<>(),
CopyOptions.create().setFieldValueEditor((key, value) -> key + "_" + value));
CopyOptions.of().setFieldEditor((entry) -> {
entry.setValue(entry.getKey() + "_" + entry.getValue());
return entry;
}));
Assert.assertEquals("subName_sub名字", map.get("subName"));
}
@@ -387,7 +390,7 @@ public class BeanUtilTest {
p2.setName("oldName");
// null值不覆盖目标属性
BeanUtil.copyProperties(p1, p2, CopyOptions.create().ignoreNullValue());
BeanUtil.copyProperties(p1, p2, CopyOptions.of().ignoreNullValue());
Assert.assertEquals("oldName", p2.getName());
// null覆盖目标属性
@@ -578,7 +581,7 @@ public class BeanUtilTest {
info.setBookID("0");
info.setCode("");
final Food newFood = new Food();
final CopyOptions copyOptions = CopyOptions.create().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString()));
final CopyOptions copyOptions = CopyOptions.of().setPropertiesFilter((f, v) -> !(v instanceof CharSequence) || StrUtil.isNotBlank(v.toString()));
BeanUtil.copyProperties(info, newFood, copyOptions);
Assert.assertEquals(info.getBookID(), newFood.getBookID());
Assert.assertNull(newFood.getCode());
@@ -592,7 +595,7 @@ public class BeanUtilTest {
o.setAge(123);
o.setOpenid("asd");
@SuppressWarnings("unchecked") final CopyOptions copyOptions = CopyOptions.create().setIgnoreProperties(Person::getAge,Person::getOpenid);
@SuppressWarnings("unchecked") final CopyOptions copyOptions = CopyOptions.of().setIgnoreProperties(Person::getAge,Person::getOpenid);
final Person n = new Person();
BeanUtil.copyProperties(o, n, copyOptions);
@@ -680,7 +683,12 @@ public class BeanUtilTest {
a,
new LinkedHashMap<>(),
false,
key -> Arrays.asList("id", "name", "code", "sortOrder").contains(key) ? key : null);
entry -> {
if(false == Arrays.asList("id", "name", "code", "sortOrder").contains(entry.getKey())){
entry.setKey(null);
}
return entry;
});
Assert.assertFalse(f.containsKey(null));
}
@@ -749,10 +757,13 @@ public class BeanUtilTest {
childVo1.setChild_father_name("张无忌");
childVo1.setChild_mother_name("赵敏敏");
final CopyOptions copyOptions = CopyOptions.create().
final CopyOptions copyOptions = CopyOptions.of().
//setIgnoreNullValue(true).
//setIgnoreCase(false).
setFieldNameEditor(StrUtil::toCamelCase);
setFieldEditor(entry->{
entry.setKey(StrUtil.toCamelCase(entry.getKey()));
return entry;
});
final ChildVo2 childVo2 = new ChildVo2();
BeanUtil.copyProperties(childVo1, childVo2, copyOptions);
@@ -783,7 +794,7 @@ public class BeanUtilTest {
public void issueI41WKPTest(){
final Test1 t1 = new Test1().setStrList(ListUtil.of("list"));
final Test2 t2_hu = new Test2();
BeanUtil.copyProperties(t1, t2_hu, CopyOptions.create().setIgnoreError(true));
BeanUtil.copyProperties(t1, t2_hu, CopyOptions.of().setIgnoreError(true));
Assert.assertNull(t2_hu.getStrList());
}

View File

@@ -33,7 +33,7 @@ public class Issue1687Test {
sysUserFb.setCustomerId("456");
// 补救别名错位
final CopyOptions copyOptions = CopyOptions.create().setFieldMapping(
final CopyOptions copyOptions = CopyOptions.of().setFieldMapping(
MapUtil.builder("depart", "depId").build()
);
final SysUser sysUser = BeanUtil.toBean(sysUserFb, SysUser.class, copyOptions);

View File

@@ -22,7 +22,10 @@ public class Issue2202Test {
headerMap.put("wechatpay-timestamp", "timestamp");
headerMap.put("wechatpay-signature", "signature");
final ResponseSignVerifyParams case1 = BeanUtil.toBean(headerMap, ResponseSignVerifyParams.class,
CopyOptions.create().setFieldNameEditor(field -> NamingCase.toCamelCase(field, '-')));
CopyOptions.of().setFieldEditor(entry -> {
entry.setKey(NamingCase.toCamelCase(entry.getKey(), '-'));
return entry;
}));
Assert.assertEquals("serial", case1.getWechatpaySerial());
Assert.assertEquals("nonce", case1.getWechatpayNonce());

View File

@@ -12,13 +12,13 @@ public class BeanCopierTest {
public void beanToMapIgnoreNullTest() {
final A a = new A();
HashMap<Object, Object> map = BeanCopier.create(a, new HashMap<>(), CopyOptions.create()).copy();
HashMap<Object, Object> map = BeanCopier.create(a, new HashMap<>(), CopyOptions.of()).copy();
Assert.assertEquals(1, map.size());
Assert.assertTrue(map.containsKey("value"));
Assert.assertNull(map.get("value"));
// 忽略null的情况下空字段不写入map
map = BeanCopier.create(a, new HashMap<>(), CopyOptions.create().ignoreNullValue()).copy();
map = BeanCopier.create(a, new HashMap<>(), CopyOptions.of().ignoreNullValue()).copy();
Assert.assertFalse(map.containsKey("value"));
Assert.assertEquals(0, map.size());
}
@@ -33,7 +33,7 @@ public class BeanCopierTest {
final B b = new B();
b.setValue("abc");
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.create().setOverride(false));
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.of().setOverride(false));
copier.copy();
Assert.assertEquals("abc", b.getValue());
@@ -49,7 +49,7 @@ public class BeanCopierTest {
final B b = new B();
b.setValue("abc");
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.create());
final BeanCopier<B> copier = BeanCopier.create(a, b, CopyOptions.of());
copier.copy();
Assert.assertEquals("123", b.getValue());

View File

@@ -3,7 +3,7 @@ package cn.hutool.core.collection;
import cn.hutool.core.collection.iter.LineIter;
import cn.hutool.core.collection.iter.PartitionIter;
import cn.hutool.core.io.resource.ResourceUtil;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.core.util.ArrayUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -26,7 +26,7 @@ public class PartitionIterTest {
final PartitionIter<Integer> iter = new PartitionIter<>(list.iterator(), 3);
int max = 0;
for (final List<Integer> lines : iter) {
max = NumberUtil.max(max, NumberUtil.max(lines.toArray(new Integer[0])));
max = ArrayUtil.max(max, ArrayUtil.max(lines.toArray(new Integer[0])));
}
Assert.assertEquals(45, max);
}

View File

@@ -39,8 +39,8 @@ public class ConvertToArrayTest {
public void toIntArrayTestIgnoreComponentErrorTest() {
final String[] b = { "a", "1" };
final ArrayConverter arrayConverter = new ArrayConverter(Integer[].class, true);
final Integer[] integerArray = (Integer[]) arrayConverter.convert(b, null);
final ArrayConverter arrayConverter = new ArrayConverter(true);
final Integer[] integerArray = arrayConverter.convert(Integer[].class, b, null);
Assert.assertArrayEquals(integerArray, new Integer[]{null, 1});
}
@@ -89,8 +89,8 @@ public class ConvertToArrayTest {
//字符串转数组
final String arrayStr = "1,2,3,4,5";
//获取Converter类的方法2自己实例化相应Converter对象
final ArrayConverter c3 = new ArrayConverter(int[].class);
final int[] result3 = (int[]) c3.convert(arrayStr, null);
final ArrayConverter c3 = new ArrayConverter();
final int[] result3 = c3.convert(int[].class, arrayStr, null);
Assert.assertArrayEquals(new int[]{1,2,3,4,5}, result3);
}

View File

@@ -41,4 +41,11 @@ public class ConvertToNumberTest {
bigDecimal = Convert.toBigDecimal("1L");
Assert.assertEquals(1L, bigDecimal.longValue());
}
@Test
public void toNumberTest(){
// 直接转换为抽象Number默认使用BigDecimal实现
final Number number = Convert.toNumber("1");
Assert.assertEquals(BigDecimal.class, number.getClass());
}
}

View File

@@ -3,6 +3,8 @@ package cn.hutool.core.convert;
import org.junit.Assert;
import org.junit.Test;
import java.lang.reflect.Type;
/**
* ConverterRegistry 单元测试
* @author Looly
@@ -12,7 +14,7 @@ public class ConverterRegistryTest {
@Test
public void getConverterTest() {
final Converter<Object> converter = ConverterRegistry.getInstance().getConverter(CharSequence.class, false);
final Converter converter = ConverterRegistry.getInstance().getConverter(CharSequence.class, false);
Assert.assertNotNull(converter);
}
@@ -26,14 +28,14 @@ public class ConverterRegistryTest {
//此处做为示例自定义CharSequence转换因为Hutool中已经提供CharSequence转换请尽量不要替换
//替换可能引发关联转换异常例如覆盖CharSequence转换会影响全局
converterRegistry.putCustom(CharSequence.class, CustomConverter.class);
converterRegistry.putCustom(CharSequence.class, new CustomConverter());
result = converterRegistry.convert(CharSequence.class, a);
Assert.assertEquals("Custom: 454553", result);
}
public static class CustomConverter implements Converter<CharSequence>{
public static class CustomConverter implements Converter{
@Override
public CharSequence convert(final Object value, final CharSequence defaultValue) throws IllegalArgumentException {
public Object convert(Type targetType, Object value) throws ConvertException {
return "Custom: " + value.toString();
}
}

View File

@@ -8,15 +8,15 @@ public class NumberConverterTest {
@Test
public void toDoubleTest(){
final NumberConverter numberConverter = new NumberConverter(Double.class);
final Number convert = numberConverter.convert("1,234.55", null);
final NumberConverter numberConverter = new NumberConverter();
final Number convert = numberConverter.convert(Double.class, "1,234.55", null);
Assert.assertEquals(1234.55D, convert);
}
@Test
public void toIntegerTest(){
final NumberConverter numberConverter = new NumberConverter(Integer.class);
final Number convert = numberConverter.convert("1,234.55", null);
final NumberConverter numberConverter = new NumberConverter();
final Number convert = numberConverter.convert(Integer.class, "1,234.55", null);
Assert.assertEquals(1234, convert);
}
}

View File

@@ -0,0 +1,44 @@
package cn.hutool.core.math;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigInteger;
public class MathUtilTest {
@Test
public void factorialTest(){
long factorial = MathUtil.factorial(0);
Assert.assertEquals(1, factorial);
Assert.assertEquals(1L, MathUtil.factorial(1));
Assert.assertEquals(1307674368000L, MathUtil.factorial(15));
Assert.assertEquals(2432902008176640000L, MathUtil.factorial(20));
factorial = MathUtil.factorial(5, 0);
Assert.assertEquals(120, factorial);
factorial = MathUtil.factorial(5, 1);
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, MathUtil.factorial(5, 4));
Assert.assertEquals(2432902008176640000L, MathUtil.factorial(20, 0));
}
@Test
public void factorialTest2(){
long factorial = MathUtil.factorial(new BigInteger("0")).longValue();
Assert.assertEquals(1, factorial);
Assert.assertEquals(1L, MathUtil.factorial(new BigInteger("1")).longValue());
Assert.assertEquals(1307674368000L, MathUtil.factorial(new BigInteger("15")).longValue());
Assert.assertEquals(2432902008176640000L, MathUtil.factorial(20));
factorial = MathUtil.factorial(new BigInteger("5"), new BigInteger("0")).longValue();
Assert.assertEquals(120, factorial);
factorial = MathUtil.factorial(new BigInteger("5"), BigInteger.ONE).longValue();
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, MathUtil.factorial(new BigInteger("5"), new BigInteger("4")).longValue());
Assert.assertEquals(2432902008176640000L, MathUtil.factorial(new BigInteger("20"), BigInteger.ZERO).longValue());
}
}

View File

@@ -167,27 +167,6 @@ public class ArrayUtilTest {
Assert.assertEquals(values[2], cast[2]);
}
@Test
public void rangeTest() {
final int[] range = ArrayUtil.range(0, 10);
Assert.assertEquals(0, range[0]);
Assert.assertEquals(1, range[1]);
Assert.assertEquals(2, range[2]);
Assert.assertEquals(3, range[3]);
Assert.assertEquals(4, range[4]);
Assert.assertEquals(5, range[5]);
Assert.assertEquals(6, range[6]);
Assert.assertEquals(7, range[7]);
Assert.assertEquals(8, range[8]);
Assert.assertEquals(9, range[9]);
}
@Test(expected = NegativeArraySizeException.class)
public void rangeMinTest() {
//noinspection ResultOfMethodCallIgnored
ArrayUtil.range(0, Integer.MIN_VALUE);
}
@Test
public void maxTest() {
final int max = ArrayUtil.max(1, 2, 13, 4, 5);

View File

@@ -1,15 +1,12 @@
package cn.hutool.core.util;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.math.NumberUtil;
import org.junit.Assert;
import org.junit.Test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.Set;
/**
* {@link NumberUtil} 单元测试类
@@ -31,7 +28,7 @@ public class NumberUtilTest {
public void addTest2() {
final double a = 3.15f;
final double b = 4.22;
final double result = NumberUtil.add(a, b);
final double result = NumberUtil.add(a, b).doubleValue();
Assert.assertEquals(7.37, result, 2);
}
@@ -45,7 +42,10 @@ public class NumberUtilTest {
@Test
public void addTest4() {
final BigDecimal result = NumberUtil.add(new BigDecimal("133"), new BigDecimal("331"));
BigDecimal result = NumberUtil.add(new BigDecimal("133"), new BigDecimal("331"));
Assert.assertEquals(new BigDecimal("464"), result);
result = NumberUtil.add(new BigDecimal[]{new BigDecimal("133"), new BigDecimal("331")});
Assert.assertEquals(new BigDecimal("464"), result);
}
@@ -55,6 +55,30 @@ public class NumberUtilTest {
Assert.assertEquals(new BigDecimal("123"), result);
}
@Test
public void subTest() {
BigDecimal result = NumberUtil.sub(new BigDecimal("133"), new BigDecimal("331"));
Assert.assertEquals(new BigDecimal("-198"), result);
result = NumberUtil.sub(new BigDecimal[]{new BigDecimal("133"), new BigDecimal("331")});
Assert.assertEquals(new BigDecimal("-198"), result);
}
@Test
public void mulTest() {
BigDecimal result = NumberUtil.mul(new BigDecimal("133"), new BigDecimal("331"));
Assert.assertEquals(new BigDecimal("44023"), result);
result = NumberUtil.mul(new BigDecimal[]{new BigDecimal("133"), new BigDecimal("331")});
Assert.assertEquals(new BigDecimal("44023"), result);
}
@Test
public void mulNullTest(){
final BigDecimal mul = NumberUtil.mul(new BigDecimal("10"), null);
Assert.assertEquals(BigDecimal.ZERO, mul);
}
@Test
public void isIntegerTest() {
Assert.assertTrue(NumberUtil.isInteger("-12"));
@@ -90,7 +114,7 @@ public class NumberUtilTest {
@Test
public void divTest() {
final double result = NumberUtil.div(0, 1);
final double result = NumberUtil.div(0, 1).doubleValue();
Assert.assertEquals(0.0, result, 0);
}
@@ -177,7 +201,7 @@ public class NumberUtilTest {
public void decimalFormatTest() {
final long c = 299792458;// 光速
final String format = NumberUtil.decimalFormat(",###", c);
final String format = NumberUtil.format(",###", c);
Assert.assertEquals("299,792,458", format);
}
@@ -187,7 +211,7 @@ public class NumberUtilTest {
final Double b = 0D;
final Double c = a / b;
Console.log(NumberUtil.decimalFormat("#%", c));
Console.log(NumberUtil.format("#%", c));
}
@Test(expected = IllegalArgumentException.class)
@@ -195,14 +219,14 @@ public class NumberUtilTest {
final Double a = 0D;
final Double b = 0D;
Console.log(NumberUtil.decimalFormat("#%", a / b));
Console.log(NumberUtil.format("#%", a / b));
}
@Test
public void decimalFormatDoubleTest() {
final Double c = 467.8101;
final String format = NumberUtil.decimalFormat("0.00", c);
final String format = NumberUtil.format("0.00", c);
Assert.assertEquals("467.81", format);
}
@@ -210,11 +234,11 @@ public class NumberUtilTest {
public void decimalFormatMoneyTest() {
final double c = 299792400.543534534;
final String format = NumberUtil.decimalFormatMoney(c);
final String format = NumberUtil.formatMoney(c);
Assert.assertEquals("299,792,400.54", format);
final double value = 0.5;
final String money = NumberUtil.decimalFormatMoney(value);
final String money = NumberUtil.formatMoney(value);
Assert.assertEquals("0.50", money);
}
@@ -237,18 +261,6 @@ public class NumberUtilTest {
Assert.assertEquals("1234.56", bigDecimal.toString());
}
@Test
public void maxTest() {
final int max = NumberUtil.max(5,4,3,6,1);
Assert.assertEquals(6, max);
}
@Test
public void minTest() {
final int min = NumberUtil.min(5,4,3,6,1);
Assert.assertEquals(1, min);
}
@Test
public void parseIntTest() {
int number = NumberUtil.parseInt("0xFF");
@@ -337,49 +349,6 @@ public class NumberUtilTest {
Assert.assertEquals(0, number);
}
@Test
public void factorialTest(){
long factorial = NumberUtil.factorial(0);
Assert.assertEquals(1, factorial);
Assert.assertEquals(1L, NumberUtil.factorial(1));
Assert.assertEquals(1307674368000L, NumberUtil.factorial(15));
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20));
factorial = NumberUtil.factorial(5, 0);
Assert.assertEquals(120, factorial);
factorial = NumberUtil.factorial(5, 1);
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, NumberUtil.factorial(5, 4));
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20, 0));
}
@Test
public void factorialTest2(){
long factorial = NumberUtil.factorial(new BigInteger("0")).longValue();
Assert.assertEquals(1, factorial);
Assert.assertEquals(1L, NumberUtil.factorial(new BigInteger("1")).longValue());
Assert.assertEquals(1307674368000L, NumberUtil.factorial(new BigInteger("15")).longValue());
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(20));
factorial = NumberUtil.factorial(new BigInteger("5"), new BigInteger("0")).longValue();
Assert.assertEquals(120, factorial);
factorial = NumberUtil.factorial(new BigInteger("5"), BigInteger.ONE).longValue();
Assert.assertEquals(120, factorial);
Assert.assertEquals(5, NumberUtil.factorial(new BigInteger("5"), new BigInteger("4")).longValue());
Assert.assertEquals(2432902008176640000L, NumberUtil.factorial(new BigInteger("20"), BigInteger.ZERO).longValue());
}
@Test
public void mulTest(){
final BigDecimal mul = NumberUtil.mul(new BigDecimal("10"), null);
Assert.assertEquals(BigDecimal.ZERO, mul);
}
@Test
public void isPowerOfTwoTest() {
Assert.assertFalse(NumberUtil.isPowerOfTwo(-1));
@@ -389,14 +358,6 @@ public class NumberUtilTest {
Assert.assertFalse(NumberUtil.isPowerOfTwo(17));
}
@Test
public void generateRandomNumberTest(){
final int[] ints = NumberUtil.generateRandomNumber(10, 20, 5);
Assert.assertEquals(5, ints.length);
final Set<?> set = Convert.convert(Set.class, ints);
Assert.assertEquals(5, set.size());
}
@Test
public void toStrTest(){
Assert.assertEquals("1", NumberUtil.toStr(new BigDecimal("1.0000000000")));
@@ -405,15 +366,6 @@ public class NumberUtilTest {
Assert.assertEquals("0", NumberUtil.toStr(new BigDecimal("9600.00000").subtract(new BigDecimal("9600.000000000"))));
}
@Test
public void generateRandomNumberTest2(){
// 检查边界
final int[] ints = NumberUtil.generateRandomNumber(1, 8, 7);
Assert.assertEquals(7, ints.length);
final Set<?> set = Convert.convert(Set.class, ints);
Assert.assertEquals(7, set.size());
}
@Test
public void toPlainNumberTest(){
final String num = "5344.34234e3";
@@ -421,12 +373,6 @@ public class NumberUtilTest {
Assert.assertEquals("5344342.34", s);
}
@Test
public void generateBySetTest(){
final Integer[] integers = NumberUtil.generateBySet(10, 100, 5);
Assert.assertEquals(5, integers.length);
}
@Test
public void isOddOrEvenTest(){
final int[] a = { 0, 32, -32, 123, -123 };
@@ -456,7 +402,7 @@ public class NumberUtilTest {
@Test
public void divIntegerTest(){
final BigDecimal div = NumberUtil.div(100101300, (Number) 100);
final BigDecimal div = NumberUtil.div(100101300, 100);
Assert.assertEquals(1001013, div.intValue());
}
@@ -467,4 +413,25 @@ public class NumberUtilTest {
Assert.assertFalse(NumberUtil.isDouble(" "));
}
@Test
public void rangeTest() {
final int[] range = NumberUtil.range(0, 10);
Assert.assertEquals(0, range[0]);
Assert.assertEquals(1, range[1]);
Assert.assertEquals(2, range[2]);
Assert.assertEquals(3, range[3]);
Assert.assertEquals(4, range[4]);
Assert.assertEquals(5, range[5]);
Assert.assertEquals(6, range[6]);
Assert.assertEquals(7, range[7]);
Assert.assertEquals(8, range[8]);
Assert.assertEquals(9, range[9]);
Assert.assertEquals(10, range[10]);
}
@Test(expected = NegativeArraySizeException.class)
public void rangeMinTest() {
//noinspection ResultOfMethodCallIgnored
NumberUtil.range(0, Integer.MIN_VALUE);
}
}

View File

@@ -1,7 +1,9 @@
package cn.hutool.core.util;
import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.Console;
import cn.hutool.core.math.NumberUtil;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@@ -72,4 +74,12 @@ public class RandomUtilTest {
}
}
}
@Test
public void generateRandomNumberTest(){
final int[] ints = RandomUtil.randomPickInts(5, NumberUtil.range(5, 20));
Assert.assertEquals(5, ints.length);
final Set<?> set = Convert.convert(Set.class, ints);
Assert.assertEquals(5, set.size());
}
}