This commit is contained in:
Looly
2023-03-23 23:50:59 +08:00
parent e81a63c6ad
commit c8309ef5fb
57 changed files with 764 additions and 505 deletions

View File

@@ -1,8 +1,8 @@
package cn.hutool.core.codec;
import cn.hutool.core.codec.binary.Base32;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -25,7 +25,7 @@ public class Base32Test {
@Test
public void hexEncodeAndDecodeTest(){
final String a = "伦家是一个非常长的字符串";
final String encode = Base32.encodeHex(StrUtil.utf8Bytes(a));
final String encode = Base32.encodeHex(ByteUtil.toUtf8Bytes(a));
Assert.assertEquals("SIUADPDEMRJ9HBV4N20E9E5AT6EPTPDON3KPBFV7JA2EBBCNSUMADP5OM8======", encode);
String decodeStr = Base32.decodeStrHex(encode);

View File

@@ -1,9 +1,9 @@
package cn.hutool.core.codec;
import cn.hutool.core.codec.binary.Base64;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -45,7 +45,7 @@ public class Base64Test {
@Test
public void encodeAndDecodeWithoutPaddingTest() {
final String a = "伦家是一个非常长的字符串66";
final String encode = Base64.encodeWithoutPadding(StrUtil.utf8Bytes(a));
final String encode = Base64.encodeWithoutPadding(ByteUtil.toUtf8Bytes(a));
Assert.assertEquals("5Lym5a625piv5LiA5Liq6Z2e5bi46ZW/55qE5a2X56ym5LiyNjY", encode);
final String decodeStr = Base64.decodeStr(encode);

View File

@@ -1,6 +1,6 @@
package cn.hutool.core.codec.hash;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -9,30 +9,30 @@ public class CityHashTest {
@Test
public void hash32Test() {
final CityHash cityHash = CityHash.INSTANCE;
int hv = cityHash.hash32(StrUtil.utf8Bytes(""));
int hv = cityHash.hash32(ByteUtil.toUtf8Bytes(""));
Assert.assertEquals(1290029860, hv);
hv = cityHash.hash32(StrUtil.utf8Bytes("你好"));
hv = cityHash.hash32(ByteUtil.toUtf8Bytes("你好"));
Assert.assertEquals(1374181357, hv);
hv = cityHash.hash32(StrUtil.utf8Bytes("见到你很高兴"));
hv = cityHash.hash32(ByteUtil.toUtf8Bytes("见到你很高兴"));
Assert.assertEquals(1475516842, hv);
hv = cityHash.hash32(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
hv = cityHash.hash32(ByteUtil.toUtf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(0x51020cae, hv);
}
@Test
public void hash64Test() {
final CityHash cityHash = CityHash.INSTANCE;
long hv = cityHash.hash64(StrUtil.utf8Bytes(""));
long hv = cityHash.hash64(ByteUtil.toUtf8Bytes(""));
Assert.assertEquals(-4296898700418225525L, hv);
hv = cityHash.hash64(StrUtil.utf8Bytes("你好"));
hv = cityHash.hash64(ByteUtil.toUtf8Bytes("你好"));
Assert.assertEquals(-4294276205456761303L, hv);
hv = cityHash.hash64(StrUtil.utf8Bytes("见到你很高兴"));
hv = cityHash.hash64(ByteUtil.toUtf8Bytes("见到你很高兴"));
Assert.assertEquals(272351505337503793L, hv);
hv = cityHash.hash64(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
hv = cityHash.hash64(ByteUtil.toUtf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-8234735310919228703L, hv);
}
}

View File

@@ -1,6 +1,6 @@
package cn.hutool.core.codec.hash;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -8,29 +8,29 @@ public class MurmurHashTest {
@Test
public void hash32Test() {
int hv = MurmurHash.INSTANCE.hash32(StrUtil.utf8Bytes(""));
int hv = MurmurHash.INSTANCE.hash32(ByteUtil.toUtf8Bytes(""));
Assert.assertEquals(-1898877446, hv);
hv = MurmurHash.INSTANCE.hash32(StrUtil.utf8Bytes("你好"));
hv = MurmurHash.INSTANCE.hash32(ByteUtil.toUtf8Bytes("你好"));
Assert.assertEquals(337357348, hv);
hv = MurmurHash.INSTANCE.hash32(StrUtil.utf8Bytes("见到你很高兴"));
hv = MurmurHash.INSTANCE.hash32(ByteUtil.toUtf8Bytes("见到你很高兴"));
Assert.assertEquals(1101306141, hv);
hv = MurmurHash.INSTANCE.hash32(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
hv = MurmurHash.INSTANCE.hash32(ByteUtil.toUtf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-785444229, hv);
}
@Test
public void hash64Test() {
long hv = MurmurHash.INSTANCE.hash64(StrUtil.utf8Bytes(""));
long hv = MurmurHash.INSTANCE.hash64(ByteUtil.toUtf8Bytes(""));
Assert.assertEquals(-1349759534971957051L, hv);
hv = MurmurHash.INSTANCE.hash64(StrUtil.utf8Bytes("你好"));
hv = MurmurHash.INSTANCE.hash64(ByteUtil.toUtf8Bytes("你好"));
Assert.assertEquals(-7563732748897304996L, hv);
hv = MurmurHash.INSTANCE.hash64(StrUtil.utf8Bytes("见到你很高兴"));
hv = MurmurHash.INSTANCE.hash64(ByteUtil.toUtf8Bytes("见到你很高兴"));
Assert.assertEquals(-766658210119995316L, hv);
hv = MurmurHash.INSTANCE.hash64(StrUtil.utf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
hv = MurmurHash.INSTANCE.hash64(ByteUtil.toUtf8Bytes("我们将通过生成一个大的文件的方式来检验各种方法的执行效率因为这种方式在结束的时候需要执行文件"));
Assert.assertEquals(-7469283059271653317L, hv);
}
}

View File

@@ -2,7 +2,7 @@ package cn.hutool.core.codec.hash.metro;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.codec.Number128;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -89,7 +89,7 @@ public class MetroHash128Test {
}
static final byte[] ENDIAN =
StrUtil.utf8Bytes("012345678901234567890123456789012345678901234567890123456789012");
ByteUtil.toUtf8Bytes("012345678901234567890123456789012345678901234567890123456789012");
@Test
public void testLittleEndian() {
@@ -106,7 +106,7 @@ public class MetroHash128Test {
}
static String h128(final String input) {
final MetroHash128 mh = MetroHash128.of(0).apply(ByteBuffer.wrap(StrUtil.utf8Bytes(input)));
final MetroHash128 mh = MetroHash128.of(0).apply(ByteBuffer.wrap(ByteUtil.toUtf8Bytes(input)));
final Number128 hash = mh.get();
return hex(hash.getHighValue()) + hex(hash.getLowValue());
}

View File

@@ -1,7 +1,7 @@
package cn.hutool.core.codec.hash.metro;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -92,7 +92,7 @@ public class MetroHash64Test {
}
static final byte[] ENDIAN =
StrUtil.utf8Bytes("012345678901234567890123456789012345678901234567890123456789012");
ByteUtil.toUtf8Bytes("012345678901234567890123456789012345678901234567890123456789012");
@Test
public void testLittleEndian() {
@@ -109,7 +109,7 @@ public class MetroHash64Test {
}
private String h64(final String value) {
return HexUtil.toHex(MetroHash64.of(0).hash64(StrUtil.utf8Bytes(value))).toUpperCase();
return HexUtil.toHex(MetroHash64.of(0).hash64(ByteUtil.toUtf8Bytes(value))).toUpperCase();
}
private static String hex(final byte[] bytes){

View File

@@ -3,7 +3,7 @@ package cn.hutool.core.codec.hash.metro;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.codec.hash.CityHash;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.util.ByteUtil;
import cn.hutool.core.util.CharsetUtil;
import cn.hutool.core.util.RandomUtil;
import org.junit.Assert;
@@ -17,7 +17,7 @@ public class MetroHashTest {
@Test
public void testEmpty() {
Assert.assertEquals("705fb008071e967d", HexUtil.toHex(MetroHash64.of(0).hash64(StrUtil.utf8Bytes(""))));
Assert.assertEquals("705fb008071e967d", HexUtil.toHex(MetroHash64.of(0).hash64(ByteUtil.toUtf8Bytes(""))));
}
@Test
@@ -60,7 +60,7 @@ public class MetroHashTest {
final long startMetro = System.currentTimeMillis();
for (final String s : strArray) {
MetroHash64.of(0).hash64(StrUtil.utf8Bytes(s));
MetroHash64.of(0).hash64(ByteUtil.toUtf8Bytes(s));
}
final long endMetro = System.currentTimeMillis();
@@ -84,7 +84,7 @@ public class MetroHashTest {
final long startMetro = System.currentTimeMillis();
for (final String s : strArray) {
MetroHash128.of(0).hash128(StrUtil.utf8Bytes(s));
MetroHash128.of(0).hash128(ByteUtil.toUtf8Bytes(s));
}
final long endMetro = System.currentTimeMillis();
@@ -103,6 +103,6 @@ public class MetroHashTest {
}
private String h64(final String value) {
return HexUtil.toHex(MetroHash64.of(0).hash64(StrUtil.utf8Bytes(value))).toUpperCase();
return HexUtil.toHex(MetroHash64.of(0).hash64(ByteUtil.toUtf8Bytes(value))).toUpperCase();
}
}

View File

@@ -290,7 +290,7 @@ public class ConvertTest {
public void numberToByteArrayTest(){
// 测试Serializable转换为bytes调用序列化转换
final byte[] bytes = Convert.toPrimitiveByteArray(12L);
Assert.assertArrayEquals(ByteUtil.longToBytes(12L), bytes);
Assert.assertArrayEquals(ByteUtil.toBytes(12L), bytes);
}
@Test

View File

@@ -1,8 +1,8 @@
package cn.hutool.core.io.checksum;
import cn.hutool.core.io.checksum.crc16.CRC16XModem;
import cn.hutool.core.codec.HexUtil;
import cn.hutool.core.text.StrUtil;
import cn.hutool.core.io.checksum.crc16.CRC16XModem;
import cn.hutool.core.util.ByteUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -48,7 +48,7 @@ public class CrcTest {
// I3B3RV@Gitee
final String text = "000123FFFFFF";
final CRC16XModem crc16 = new CRC16XModem();
crc16.update(StrUtil.bytes(text));
crc16.update(ByteUtil.toUtf8Bytes(text));
final String hexValue = crc16.getHexValue(true);
Assert.assertEquals("0e04", hexValue);
}

View File

@@ -158,12 +158,13 @@ public class LambdaFactoryTest {
loop(count, tasks);
}
@SuppressWarnings("unchecked")
@Test
public void testConstructor() {
Constructor<Something> constructor = ((SerSupplier<Constructor<Something>>) Something.class::getConstructor).get();
Supplier<Something> constructorLambda = LambdaFactory.build(Supplier.class, constructor);
final Constructor<Something> constructor = ((SerSupplier<Constructor<Something>>) Something.class::getConstructor).get();
final Supplier<Something> constructorLambda = LambdaFactory.build(Supplier.class, constructor);
// constructorLambda can be cache or transfer
Something something = constructorLambda.get();
final Something something = constructorLambda.get();
Assert.assertEquals(Something.class, something.getClass());
}

View File

@@ -220,4 +220,52 @@ public class CharSequenceUtilTest {
final String str = "A5E6005700000000000000000000000000000000000000090D0100000000000001003830";
Assert.assertEquals("38", StrUtil.subByLength(str,-2,2));
}
@Test
public void commonPrefixTest() {
// -------------------------- None match -----------------------
Assert.assertEquals("", CharSequenceUtil.commonPrefix("", "abc"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix(null, "abc"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("abc", null));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("abc", ""));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("azzzj", "bzzzj"));
Assert.assertEquals("", CharSequenceUtil.commonPrefix("english中文", "french中文"));
// -------------------------- Matched -----------------------
Assert.assertEquals("name_", CharSequenceUtil.commonPrefix("name_abc", "name_efg"));
Assert.assertEquals("zzzj", CharSequenceUtil.commonPrefix("zzzja", "zzzjb"));
Assert.assertEquals("中文", CharSequenceUtil.commonPrefix("中文english", "中文french"));
}
@Test
public void commonSuffixTest() {
// -------------------------- None match -----------------------
Assert.assertEquals("", CharSequenceUtil.commonSuffix("", "abc"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix(null, "abc"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("abc", null));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("abc", ""));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("zzzja", "zzzjb"));
Assert.assertEquals("", CharSequenceUtil.commonSuffix("中文english", "中文Korean"));
// -------------------------- Matched -----------------------
Assert.assertEquals("_name", CharSequenceUtil.commonSuffix("abc_name", "efg_name"));
Assert.assertEquals("zzzj", CharSequenceUtil.commonSuffix("abczzzj", "efgzzzj"));
Assert.assertEquals("中文", CharSequenceUtil.commonSuffix("english中文", "Korean中文"));
}
}

View File

@@ -17,18 +17,18 @@ public class ByteUtilTest {
buffer.putInt(int1);
final byte[] bytesIntFromBuffer = buffer.array();
final byte[] bytesInt = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesInt = ByteUtil.toBytes(int1, ByteOrder.LITTLE_ENDIAN);
Assert.assertArrayEquals(bytesIntFromBuffer, bytesInt);
final int int2 = ByteUtil.bytesToInt(bytesInt, ByteOrder.LITTLE_ENDIAN);
final int int2 = ByteUtil.toInt(bytesInt, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(int1, int2);
final byte[] bytesInt2 = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN);
final int int3 = ByteUtil.bytesToInt(bytesInt2, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesInt2 = ByteUtil.toBytes(int1, ByteOrder.LITTLE_ENDIAN);
final int int3 = ByteUtil.toInt(bytesInt2, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(int1, int3);
final byte[] bytesInt3 = ByteUtil.intToBytes(int1, ByteOrder.LITTLE_ENDIAN);
final int int4 = ByteUtil.bytesToInt(bytesInt3, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesInt3 = ByteUtil.toBytes(int1, ByteOrder.LITTLE_ENDIAN);
final int int4 = ByteUtil.toInt(bytesInt3, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(int1, int4);
}
@@ -41,11 +41,11 @@ public class ByteUtilTest {
buffer.putInt(int2);
final byte[] bytesIntFromBuffer = buffer.array();
final byte[] bytesInt = ByteUtil.intToBytes(int2, ByteOrder.BIG_ENDIAN);
final byte[] bytesInt = ByteUtil.toBytes(int2, ByteOrder.BIG_ENDIAN);
Assert.assertArrayEquals(bytesIntFromBuffer, bytesInt);
// 测试大端序 byte 数组转 int
final int int3 = ByteUtil.bytesToInt(bytesInt, ByteOrder.BIG_ENDIAN);
final int int3 = ByteUtil.toInt(bytesInt, ByteOrder.BIG_ENDIAN);
Assert.assertEquals(int2, int3);
}
@@ -59,18 +59,18 @@ public class ByteUtilTest {
buffer.putLong(long1);
final byte[] bytesLongFromBuffer = buffer.array();
final byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesLong = ByteUtil.toBytes(long1, ByteOrder.LITTLE_ENDIAN);
Assert.assertArrayEquals(bytesLongFromBuffer, bytesLong);
final long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.LITTLE_ENDIAN);
final long long2 = ByteUtil.toLong(bytesLong, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(long1, long2);
final byte[] bytesLong2 = ByteUtil.longToBytes(long1);
final long long3 = ByteUtil.bytesToLong(bytesLong2, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesLong2 = ByteUtil.toBytes(long1);
final long long3 = ByteUtil.toLong(bytesLong2, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(long1, long3);
final byte[] bytesLong3 = ByteUtil.longToBytes(long1, ByteOrder.LITTLE_ENDIAN);
final long long4 = ByteUtil.bytesToLong(bytesLong3);
final byte[] bytesLong3 = ByteUtil.toBytes(long1, ByteOrder.LITTLE_ENDIAN);
final long long4 = ByteUtil.toLong(bytesLong3);
Assert.assertEquals(long1, long4);
}
@@ -83,10 +83,10 @@ public class ByteUtilTest {
buffer.putLong(long1);
final byte[] bytesLongFromBuffer = buffer.array();
final byte[] bytesLong = ByteUtil.longToBytes(long1, ByteOrder.BIG_ENDIAN);
final byte[] bytesLong = ByteUtil.toBytes(long1, ByteOrder.BIG_ENDIAN);
Assert.assertArrayEquals(bytesLongFromBuffer, bytesLong);
final long long2 = ByteUtil.bytesToLong(bytesLong, ByteOrder.BIG_ENDIAN);
final long long2 = ByteUtil.toLong(bytesLong, ByteOrder.BIG_ENDIAN);
Assert.assertEquals(long1, long2);
}
@@ -95,8 +95,8 @@ public class ByteUtilTest {
// 测试 long 转 byte 数组
final float f1 = (float) RandomUtil.randomDouble();
final byte[] bytesLong = ByteUtil.floatToBytes(f1, ByteOrder.LITTLE_ENDIAN);
final float f2 = ByteUtil.bytesToFloat(bytesLong, ByteOrder.LITTLE_ENDIAN);
final byte[] bytesLong = ByteUtil.toBytes(f1, ByteOrder.LITTLE_ENDIAN);
final float f2 = ByteUtil.toFloat(bytesLong, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(f1, f2, 0);
}
@@ -105,8 +105,8 @@ public class ByteUtilTest {
// 测试大端序 long 转 byte 数组
final float f1 = (float) RandomUtil.randomDouble();
final byte[] bytesLong = ByteUtil.floatToBytes(f1, ByteOrder.BIG_ENDIAN);
final float f2 = ByteUtil.bytesToFloat(bytesLong, ByteOrder.BIG_ENDIAN);
final byte[] bytesLong = ByteUtil.toBytes(f1, ByteOrder.BIG_ENDIAN);
final float f2 = ByteUtil.toFloat(bytesLong, ByteOrder.BIG_ENDIAN);
Assert.assertEquals(f1, f2, 0);
}
@@ -115,24 +115,24 @@ public class ByteUtilTest {
public void shortAndBytesLittleEndianTest() {
final short short1 = (short) RandomUtil.randomInt();
final byte[] bytes = ByteUtil.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN);
final short short2 = ByteUtil.bytesToShort(bytes, ByteOrder.LITTLE_ENDIAN);
final byte[] bytes = ByteUtil.toBytes(short1, ByteOrder.LITTLE_ENDIAN);
final short short2 = ByteUtil.toShort(bytes, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(short2, short1);
final byte[] bytes2 = ByteUtil.shortToBytes(short1);
final short short3 = ByteUtil.bytesToShort(bytes2, ByteOrder.LITTLE_ENDIAN);
final byte[] bytes2 = ByteUtil.toBytes(short1);
final short short3 = ByteUtil.toShort(bytes2, ByteOrder.LITTLE_ENDIAN);
Assert.assertEquals(short3, short1);
final byte[] bytes3 = ByteUtil.shortToBytes(short1, ByteOrder.LITTLE_ENDIAN);
final short short4 = ByteUtil.bytesToShort(bytes3);
final byte[] bytes3 = ByteUtil.toBytes(short1, ByteOrder.LITTLE_ENDIAN);
final short short4 = ByteUtil.toShort(bytes3);
Assert.assertEquals(short4, short1);
}
@Test
public void shortAndBytesBigEndianTest() {
final short short1 = 122;
final byte[] bytes = ByteUtil.shortToBytes(short1, ByteOrder.BIG_ENDIAN);
final short short2 = ByteUtil.bytesToShort(bytes, ByteOrder.BIG_ENDIAN);
final byte[] bytes = ByteUtil.toBytes(short1, ByteOrder.BIG_ENDIAN);
final short short2 = ByteUtil.toShort(bytes, ByteOrder.BIG_ENDIAN);
Assert.assertEquals(short2, short1);
}
@@ -140,12 +140,12 @@ public class ByteUtilTest {
@Test
public void bytesToLongTest(){
final long a = RandomUtil.randomLong(0, Long.MAX_VALUE);
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.longToBytes(a));
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.toBytes(a));
wrap.order(ByteOrder.LITTLE_ENDIAN);
long aLong = wrap.getLong();
Assert.assertEquals(a, aLong);
wrap = ByteBuffer.wrap(ByteUtil.longToBytes(a, ByteOrder.BIG_ENDIAN));
wrap = ByteBuffer.wrap(ByteUtil.toBytes(a, ByteOrder.BIG_ENDIAN));
wrap.order(ByteOrder.BIG_ENDIAN);
aLong = wrap.getLong();
Assert.assertEquals(a, aLong);
@@ -154,12 +154,12 @@ public class ByteUtilTest {
@Test
public void bytesToIntTest(){
final int a = RandomUtil.randomInt(0, Integer.MAX_VALUE);
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.intToBytes(a));
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.toBytes(a));
wrap.order(ByteOrder.LITTLE_ENDIAN);
int aInt = wrap.getInt();
Assert.assertEquals(a, aInt);
wrap = ByteBuffer.wrap(ByteUtil.intToBytes(a, ByteOrder.BIG_ENDIAN));
wrap = ByteBuffer.wrap(ByteUtil.toBytes(a, ByteOrder.BIG_ENDIAN));
wrap.order(ByteOrder.BIG_ENDIAN);
aInt = wrap.getInt();
Assert.assertEquals(a, aInt);
@@ -169,12 +169,12 @@ public class ByteUtilTest {
public void bytesToShortTest(){
final short a = (short) RandomUtil.randomInt(0, Short.MAX_VALUE);
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.shortToBytes(a));
ByteBuffer wrap = ByteBuffer.wrap(ByteUtil.toBytes(a));
wrap.order(ByteOrder.LITTLE_ENDIAN);
short aShort = wrap.getShort();
Assert.assertEquals(a, aShort);
wrap = ByteBuffer.wrap(ByteUtil.shortToBytes(a, ByteOrder.BIG_ENDIAN));
wrap = ByteBuffer.wrap(ByteUtil.toBytes(a, ByteOrder.BIG_ENDIAN));
wrap.order(ByteOrder.BIG_ENDIAN);
aShort = wrap.getShort();
Assert.assertEquals(a, aShort);

View File

@@ -1,7 +1,6 @@
package cn.hutool.core.util;
import cn.hutool.core.codec.hash.HashUtil;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -10,7 +9,7 @@ public class HashUtilTest {
@Test
public void cityHash128Test(){
final String s="Google发布的Hash计算算法CityHash64 与 CityHash128";
final long[] hash = HashUtil.cityHash128(StrUtil.utf8Bytes(s));
final long[] hash = HashUtil.cityHash128(ByteUtil.toUtf8Bytes(s));
Assert.assertEquals(0x5944f1e788a18db0L, hash[0]);
Assert.assertEquals(0xc2f68d8b2bf4a5cfL, hash[1]);
}
@@ -18,14 +17,14 @@ public class HashUtilTest {
@Test
public void cityHash64Test(){
final String s="Google发布的Hash计算算法CityHash64 与 CityHash128";
final long hash = HashUtil.cityHash64(StrUtil.utf8Bytes(s));
final long hash = HashUtil.cityHash64(ByteUtil.toUtf8Bytes(s));
Assert.assertEquals(0x1d408f2bbf967e2aL, hash);
}
@Test
public void cityHash32Test(){
final String s="Google发布的Hash计算算法CityHash64 与 CityHash128";
final int hash = HashUtil.cityHash32(StrUtil.utf8Bytes(s));
final int hash = HashUtil.cityHash32(ByteUtil.toUtf8Bytes(s));
Assert.assertEquals(0xa8944fbe, hash);
}
}

View File

@@ -2,6 +2,7 @@ package cn.hutool.core.util;
import cn.hutool.core.lang.Console;
import cn.hutool.core.math.NumberUtil;
import cn.hutool.core.text.StrUtil;
import org.junit.Assert;
import org.junit.Test;
@@ -10,6 +11,10 @@ import java.math.RoundingMode;
import java.text.NumberFormat;
import java.text.ParseException;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
/**
* {@link NumberUtil} 单元测试类
*
@@ -412,6 +417,23 @@ public class NumberUtilTest {
NumberUtil.parseInt(numberStr);
}
@Test
public void parseIntTest5() {
// -------------------------- Parse failed -----------------------
assertThat(NumberUtil.parseInt("abc", null), nullValue());
assertThat(NumberUtil.parseInt("abc", 456), equalTo(456));
// -------------------------- Parse success -----------------------
assertThat(NumberUtil.parseInt("123.abc", 789), equalTo(123));
assertThat(NumberUtil.parseInt("123.3", null), equalTo(123));
}
@Test
public void parseNumberTest() {
// from 5.4.8 issue#I23ORQ@Gitee
@@ -432,6 +454,29 @@ public class NumberUtilTest {
Assert.assertTrue(number instanceof BigDecimal);
}
@Test
public void parseNumberTest3(){
// -------------------------- Parse failed -----------------------
assertThat(NumberUtil.parseNumber("abc", null), nullValue());
assertThat(NumberUtil.parseNumber(StrUtil.EMPTY, null), nullValue());
assertThat(NumberUtil.parseNumber(StrUtil.repeat(StrUtil.SPACE, 10), null), nullValue());
assertThat(NumberUtil.parseNumber("abc", 456).intValue(), equalTo(456));
// -------------------------- Parse success -----------------------
assertThat(NumberUtil.parseNumber("123.abc", 789).intValue(), equalTo(123));
assertThat(NumberUtil.parseNumber("123.3", null).doubleValue(), equalTo(123.3D));
assertThat(NumberUtil.parseNumber("0.123.3", null).doubleValue(), equalTo(0.123D));
}
@Test
public void parseHexNumberTest() {
// 千位分隔符去掉
@@ -470,6 +515,27 @@ public class NumberUtilTest {
Assert.assertEquals(0, number);
}
@Test
public void parseLongTest2() {
// -------------------------- Parse failed -----------------------
final Long v1 = NumberUtil.parseLong(null, null);
assertThat(v1, nullValue());
final Long v2 = NumberUtil.parseLong(StrUtil.EMPTY, null);
assertThat(v2, nullValue());
final Long v3 = NumberUtil.parseLong("L3221", 1233L);
assertThat(v3, equalTo(1233L));
// -------------------------- Parse success -----------------------
final Long v4 = NumberUtil.parseLong("1233L", null);
assertThat(v4, equalTo(1233L));
}
@Test
public void isPowerOfTwoTest() {
Assert.assertFalse(NumberUtil.isPowerOfTwo(-1));
@@ -585,4 +651,46 @@ public class NumberUtilTest {
Assert.assertFalse(NumberUtil.isPrime(296733));
Assert.assertFalse(NumberUtil.isPrime(20_4123_2399));
}
@Test
public void parseFloatTest() {
// -------------------------- Parse failed -----------------------
assertThat(NumberUtil.parseFloat("abc", null), nullValue());
assertThat(NumberUtil.parseFloat("a123.33", null), nullValue());
assertThat(NumberUtil.parseFloat("..123", null), nullValue());
assertThat(NumberUtil.parseFloat(StrUtil.EMPTY, 1233F), equalTo(1233F));
// -------------------------- Parse success -----------------------
assertThat(NumberUtil.parseFloat("123.33a", null), equalTo(123.33F));
assertThat(NumberUtil.parseFloat(".123", null), equalTo(0.123F));
}
@Test
public void parseDoubleTest() {
// -------------------------- Parse failed -----------------------
assertThat(NumberUtil.parseDouble("abc", null), nullValue());
assertThat(NumberUtil.parseDouble("a123.33", null), nullValue());
assertThat(NumberUtil.parseDouble("..123", null), nullValue());
assertThat(NumberUtil.parseDouble(StrUtil.EMPTY, 1233D), equalTo(1233D));
// -------------------------- Parse success -----------------------
assertThat(NumberUtil.parseDouble("123.33a", null), equalTo(123.33D));
assertThat(NumberUtil.parseDouble(".123", null), equalTo(0.123D));
}
}

View File

@@ -120,7 +120,7 @@ public class ZipUtilTest {
@Test
public void gzipTest() {
final String data = "我是一个需要压缩的很长很长的字符串";
final byte[] bytes = StrUtil.utf8Bytes(data);
final byte[] bytes = ByteUtil.toUtf8Bytes(data);
final byte[] gzip = ZipUtil.gzip(bytes);
//保证gzip长度正常
@@ -134,7 +134,7 @@ public class ZipUtilTest {
@Test
public void zlibTest() {
final String data = "我是一个需要压缩的很长很长的字符串";
final byte[] bytes = StrUtil.utf8Bytes(data);
final byte[] bytes = ByteUtil.toUtf8Bytes(data);
byte[] gzip = ZipUtil.zlib(bytes, 0);
//保证zlib长度正常
@@ -203,12 +203,12 @@ public class ZipUtilTest {
@Test
@Ignore
public void sizeUnzip() throws IOException {
String zipPath = "F:\\BaiduNetdiskDownload\\demo.zip";
String outPath = "F:\\BaiduNetdiskDownload\\test";
ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"));
File file = new File(outPath);
final String zipPath = "F:\\BaiduNetdiskDownload\\demo.zip";
final String outPath = "F:\\BaiduNetdiskDownload\\test";
final ZipFile zipFile = new ZipFile(zipPath, Charset.forName("GBK"));
final File file = new File(outPath);
// 限制解压文件大小为637KB
long size = 637*1024L;
final long size = 637*1024L;
// 限制解压文件大小为636KB
// long size = 636*1024L;