44import com .blankj .utilcode .constant .MemoryConstants ;
55import com .blankj .utilcode .constant .TimeConstants ;
66
7+ import org .junit .Assert ;
78import org .junit .Test ;
89
9- import static com .blankj .utilcode .util .ConvertUtils .*;
10- import static com .google .common .truth .Truth .assertThat ;
10+ import java .util .Arrays ;
1111
1212
1313/**
2020 */
2121public class ConvertUtilsTest {
2222
23- byte [] mBytes = new byte []{0x00 , 0x08 , (byte ) 0xdb , 0x33 , 0x45 , (byte ) 0xab , 0x02 , 0x23 };
24- String hexString = "0008DB3345AB0223" ;
23+ private byte [] mBytes = new byte []{0x00 , 0x08 , (byte ) 0xdb , 0x33 , 0x45 , (byte ) 0xab , 0x02 , 0x23 };
24+ private String hexString = "0008DB3345AB0223" ;
2525
26- @ Test
27- public void testBytes2HexString () throws Exception {
28- assertThat (bytes2HexString (mBytes )).isEqualTo (hexString );
29- }
26+ private char [] mChars1 = new char []{'0' , '1' , '2' };
27+ private byte [] mBytes1 = new byte []{48 , 49 , 50 };
3028
3129 @ Test
32- public void testHexString2Bytes () throws Exception {
33- assertThat (hexString2Bytes (hexString )).isEqualTo (mBytes );
30+ public void bytes2HexString () throws Exception {
31+ Assert .assertEquals (
32+ hexString ,
33+ ConvertUtils .bytes2HexString (mBytes )
34+ );
3435 }
3536
36- char [] mChars1 = new char []{'0' , '1' , '2' };
37- byte [] mBytes1 = new byte []{48 , 49 , 50 };
38-
3937 @ Test
40- public void testChars2Bytes () throws Exception {
41- assertThat (chars2Bytes (mChars1 )).isEqualTo (mBytes1 );
38+ public void hexString2Bytes () throws Exception {
39+ Assert .assertTrue (
40+ Arrays .equals (
41+ mBytes ,
42+ ConvertUtils .hexString2Bytes (hexString )
43+ )
44+ );
4245 }
4346
4447 @ Test
45- public void testBytes2Chars () throws Exception {
46- assertThat (bytes2Chars (mBytes1 )).isEqualTo (mChars1 );
48+ public void chars2Bytes () throws Exception {
49+ Assert .assertTrue (
50+ Arrays .equals (
51+ mBytes1 ,
52+ ConvertUtils .chars2Bytes (mChars1 )
53+ )
54+ );
4755 }
4856
4957 @ Test
50- public void testByte2Unit () throws Exception {
51- assertThat (byte2MemorySize (MemoryConstants .GB , MemoryConstants .MB ) - 1024 ).isWithin (0.001 );
58+ public void bytes2Chars () throws Exception {
59+ Assert .assertTrue (
60+ Arrays .equals (
61+ mChars1 ,
62+ ConvertUtils .bytes2Chars (mBytes1 )
63+ )
64+ );
5265 }
5366
5467 @ Test
55- public void testByte2FitSize () throws Exception {
56- assertThat (byte2FitMemorySize (1024 * 1024 * 3 + 1024 * 100 )).isEqualTo ("3.098MB" );
68+ public void byte2MemorySize () throws Exception {
69+ Assert .assertEquals (
70+ 1024 ,
71+ ConvertUtils .byte2MemorySize (MemoryConstants .GB , MemoryConstants .MB ),
72+ 0.001
73+ );
5774 }
5875
5976 @ Test
60- public void testMillis2FitTimeSpan () throws Exception {
61- long millis = TimeConstants .DAY * 6 + TimeConstants .HOUR * 6
62- + TimeConstants .MIN * 6 + TimeConstants .SEC * 6 + 6 ;
63- System .out .println (millis2FitTimeSpan (millis , 7 ));
64- System .out .println (millis2FitTimeSpan (millis , 4 ));
65- System .out .println (millis2FitTimeSpan (millis , 3 ));
66- System .out .println (millis2FitTimeSpan (millis * 4 , 5 ));
77+ public void byte2FitMemorySize () throws Exception {
78+ Assert .assertEquals (
79+ "3.098MB" ,
80+ ConvertUtils .byte2FitMemorySize (1024 * 1024 * 3 + 1024 * 100 )
81+ );
6782 }
6883
6984 @ Test
70- public void testBytes2Bits () throws Exception {
71- System .out .println (bytes2Bits (new byte []{0x7F , (byte ) 0xFA }));
85+ public void millis2FitTimeSpan () throws Exception {
86+ long millis = 6 * TimeConstants .DAY
87+ + 6 * TimeConstants .HOUR
88+ + 6 * TimeConstants .MIN
89+ + 6 * TimeConstants .SEC
90+ + 6 ;
91+ Assert .assertEquals (
92+ "6天6小时6分钟6秒6毫秒" ,
93+ ConvertUtils .millis2FitTimeSpan (millis , 7 )
94+ );
95+ Assert .assertEquals (
96+ "6天6小时6分钟6秒" ,
97+ ConvertUtils .millis2FitTimeSpan (millis , 4 )
98+ );
99+ Assert .assertEquals (
100+ "6天6小时6分钟" ,
101+ ConvertUtils .millis2FitTimeSpan (millis , 3 )
102+ );
103+ Assert .assertEquals (
104+ "25天24分钟24秒24毫秒" ,
105+ ConvertUtils .millis2FitTimeSpan (millis * 4 , 5 )
106+ );
72107 }
73108
74109 @ Test
75- public void testBits2Bytes () throws Exception {
76- System .out .println (bytes2HexString (bits2Bytes ("111111111111010" )));
110+ public void bytes2Bits_bits2Bytes () throws Exception {
111+ Assert .assertEquals (
112+ "0111111111111010" ,
113+ ConvertUtils .bytes2Bits (new byte []{0x7F , (byte ) 0xFA })
114+ );
115+ Assert .assertEquals (
116+ "0111111111111010" ,
117+ ConvertUtils .bytes2Bits (ConvertUtils .bits2Bytes ("111111111111010" ))
118+ );
77119 }
78120
79121 @ Test
80- public void testInputStream2BytesAndBytes2InputStream () throws Exception {
122+ public void inputStream2Bytes_bytes2InputStream () throws Exception {
81123 String string = "this is test string" ;
82- assertThat (new String (inputStream2Bytes (
83- bytes2InputStream (string .getBytes ("UTF-8" )))))
84- .isEqualTo (string );
124+ Assert .assertEquals (
125+ string .getBytes ("UTF-8" ),
126+ ConvertUtils .inputStream2Bytes (ConvertUtils .bytes2InputStream (string .getBytes ("UTF-8" )))
127+ );
85128 }
86129
87130 @ Test
88- public void testInputStream2StringAndString2InputStream () throws Exception {
131+ public void inputStream2String_string2InputStream () throws Exception {
89132 String string = "this is test string" ;
90- assertThat (inputStream2String (
91- string2InputStream (string , "UTF-8" )
92- , "UTF-8" )).isEqualTo (string );
133+ Assert .assertEquals (
134+ string ,
135+ ConvertUtils .inputStream2String (ConvertUtils .string2InputStream (string , "UTF-8" ), "UTF-8" )
136+ );
93137 }
94138}
0 commit comments