11package com .ss .rlib .common .test .util ;
22
3+ import static org .junit .jupiter .api .Assertions .*;
34import com .ss .rlib .common .util .NumberUtils ;
4- import com .ss .rlib .common .util .StringUtils ;
5- import org .junit .jupiter .api .Assertions ;
65import org .junit .jupiter .api .Test ;
76
8- import java .time .LocalDate ;
9-
107/**
118 * Test methods in {@link NumberUtils}
129 *
@@ -16,37 +13,164 @@ class NumberUtilsTest {
1613
1714 @ Test
1815 void shouldCheckStringIsLong () {
19- Assertions . assertTrue (NumberUtils .isLong ("1" ));
20- Assertions . assertTrue (NumberUtils .isLong ("123123213123" ));
21- Assertions . assertFalse (NumberUtils .isLong ("notlong" ));
22- Assertions . assertFalse (NumberUtils .isLong (null ));
23- Assertions . assertFalse (NumberUtils .isLong ("2.1234" ));
16+ assertTrue (NumberUtils .isLong ("1" ));
17+ assertTrue (NumberUtils .isLong ("123123213123" ));
18+ assertFalse (NumberUtils .isLong ("notlong" ));
19+ assertFalse (NumberUtils .isLong (null ));
20+ assertFalse (NumberUtils .isLong ("2.1234" ));
2421 }
2522
2623 @ Test
2724 void shouldSafetyConvertStringToLong () {
28- Assertions . assertNotNull (NumberUtils .safeToLong ("1" ));
29- Assertions . assertNotNull (NumberUtils .safeToLong ("123123213123" ));
30- Assertions . assertNull (NumberUtils .safeToLong ("notlong" ));
31- Assertions . assertNull (NumberUtils .safeToLong (null ));
32- Assertions . assertNull (NumberUtils .safeToLong ("2.1234" ));
25+ assertNotNull (NumberUtils .safeToLong ("1" ));
26+ assertNotNull (NumberUtils .safeToLong ("123123213123" ));
27+ assertNull (NumberUtils .safeToLong ("notlong" ));
28+ assertNull (NumberUtils .safeToLong (null ));
29+ assertNull (NumberUtils .safeToLong ("2.1234" ));
3330 }
3431
3532 @ Test
3633 void shouldConvertStringToOptionalLong () {
37- Assertions . assertTrue (NumberUtils .toOptionalLong ("1" ).isPresent ());
38- Assertions . assertTrue (NumberUtils .toOptionalLong ("123123213123" ).isPresent ());
39- Assertions . assertFalse (NumberUtils .toOptionalLong ("notlong" ).isPresent ());
40- Assertions . assertFalse (NumberUtils .toOptionalLong (null ).isPresent ());
41- Assertions . assertFalse (NumberUtils .toOptionalLong ("2.1234" ).isPresent ());
34+ assertTrue (NumberUtils .toOptionalLong ("1" ).isPresent ());
35+ assertTrue (NumberUtils .toOptionalLong ("123123213123" ).isPresent ());
36+ assertFalse (NumberUtils .toOptionalLong ("notlong" ).isPresent ());
37+ assertFalse (NumberUtils .toOptionalLong (null ).isPresent ());
38+ assertFalse (NumberUtils .toOptionalLong ("2.1234" ).isPresent ());
4239 }
4340
4441 @ Test
4542 void shouldSafetyConvertStringToInt () {
46- Assertions .assertNotNull (NumberUtils .safeToInt ("1" ));
47- Assertions .assertNotNull (NumberUtils .safeToInt ("123124" ));
48- Assertions .assertNull (NumberUtils .safeToInt ("notlong" ));
49- Assertions .assertNull (NumberUtils .safeToInt (null ));
50- Assertions .assertNull (NumberUtils .safeToInt ("2.1234" ));
43+ assertNotNull (NumberUtils .safeToInt ("1" ));
44+ assertNotNull (NumberUtils .safeToInt ("123124" ));
45+ assertNull (NumberUtils .safeToInt ("notlong" ));
46+ assertNull (NumberUtils .safeToInt (null ));
47+ assertNull (NumberUtils .safeToInt ("2.1234" ));
48+ }
49+
50+ @ Test
51+ void shouldReadBitsCorrectly () {
52+
53+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00000001, 0 ));
54+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 0 ));
55+
56+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00000010, 1 ));
57+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 1 ));
58+
59+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00000100, 2 ));
60+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 2 ));
61+
62+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00001000, 3 ));
63+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 3 ));
64+
65+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00010000, 4 ));
66+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 4 ));
67+
68+ assertTrue (NumberUtils .isSetBit ((byte ) 0b00100000, 5 ));
69+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 5 ));
70+
71+ assertTrue (NumberUtils .isSetBit ((byte ) 0b01000000, 6 ));
72+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 6 ));
73+
74+ assertTrue (NumberUtils .isSetBit ((byte ) 0b10000000, 7 ));
75+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 7 ));
76+
77+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 0 ));
78+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 1 ));
79+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 2 ));
80+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 3 ));
81+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 4 ));
82+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 5 ));
83+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 6 ));
84+ assertTrue (NumberUtils .isSetBit ((byte ) 0b11111111, 7 ));
85+
86+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 0 ));
87+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 1 ));
88+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 2 ));
89+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 3 ));
90+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 4 ));
91+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 5 ));
92+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 6 ));
93+ assertTrue (NumberUtils .isNotSetBit ((byte ) 0b00000000, 7 ));
94+ }
95+
96+ @ Test
97+ void shouldWriteHighLowBitsCorrectly () {
98+
99+ for (int low = 0 ; low < 16 ; low ++) {
100+ for (int high = 0 ; high < 16 ; high ++) {
101+ var result = NumberUtils .setHighByteBits (low , high );
102+ assertEquals (high , NumberUtils .getHighByteBits (result ));
103+ assertEquals (low , NumberUtils .getLowByteBits (result ));
104+ }
105+ }
106+
107+ for (int low = 0 ; low < 16 ; low ++) {
108+ var result = NumberUtils .setLowByteBits (256 , low );
109+ assertEquals (16 , NumberUtils .getHighByteBits (result ));
110+ assertEquals (low , NumberUtils .getLowByteBits (result ));
111+ }
112+ }
113+
114+ @ Test
115+ void shouldReadHighLowBitsCorrectly () {
116+
117+ for (int i = 0 ; i < 16 ; i ++) {
118+ assertEquals (i , NumberUtils .getHighByteBits (i << 4 ));
119+ }
120+
121+ assertEquals (0b0000_1000, NumberUtils .getHighByteBits (0b1000_0100));
122+ assertEquals (0b0000_0100, NumberUtils .getHighByteBits (0b0100_1000));
123+ assertEquals (0b0000_0010, NumberUtils .getHighByteBits (0b0010_0010));
124+ assertEquals (0b0000_0001, NumberUtils .getHighByteBits (0b0001_0001));
125+ assertEquals (0b0000_0101, NumberUtils .getHighByteBits (0b0101_1000));
126+
127+ for (int i = 0 ; i < 16 ; i ++) {
128+ assertEquals (i , NumberUtils .getLowByteBits (i & 0x0F ));
129+ }
130+
131+ assertEquals (0b0000_1000, NumberUtils .getLowByteBits (0b1000_1000));
132+ assertEquals (0b0000_0100, NumberUtils .getLowByteBits (0b0100_0100));
133+ assertEquals (0b0000_0010, NumberUtils .getLowByteBits (0b0010_0010));
134+ assertEquals (0b0000_0001, NumberUtils .getLowByteBits (0b0001_0001));
135+ assertEquals (0b0000_0101, NumberUtils .getLowByteBits (0b0101_0101));
136+ }
137+
138+ @ Test
139+ void shouldByteToUnsignedByteCorrectly () {
140+ assertEquals (255 , NumberUtils .toUnsignedByte ((byte ) -1 ));
141+ assertEquals (106 , NumberUtils .toUnsignedByte ((byte ) -150 ));
142+ assertEquals (1 , NumberUtils .toUnsignedByte ((byte ) 1 ));
143+ assertEquals (150 , NumberUtils .toUnsignedByte ((byte ) 150 ));
144+ }
145+
146+ @ Test
147+ void shouldChangeBitsInByteCorrectly () {
148+
149+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 0 ), 0 ));
150+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 1 ), 1 ));
151+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 2 ), 2 ));
152+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 3 ), 3 ));
153+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 4 ), 4 ));
154+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 5 ), 5 ));
155+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 6 ), 6 ));
156+ assertTrue (NumberUtils .isSetBit (NumberUtils .setBit (0 , 7 ), 7 ));
157+
158+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 0 ), 0 ));
159+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 1 ), 1 ));
160+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 2 ), 2 ));
161+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 3 ), 3 ));
162+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 4 ), 4 ));
163+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 5 ), 5 ));
164+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 6 ), 6 ));
165+ assertTrue (NumberUtils .isNotSetBit (NumberUtils .unsetBit (255 , 7 ), 7 ));
166+ }
167+
168+ @ Test
169+ void shouldReadDifferentBitsCorrectly () {
170+
171+ assertEquals (0b0000_1000, NumberUtils .getHighByteBits (0b1000_0100));
172+ System .out .println (Integer .toString (NumberUtils .getHighByteBits (0b1000_0100), 2 ));
173+ System .out .println (Integer .toString (NumberUtils .readBits (0b1000_0100, 4 , 1 ), 2 ));
174+ assertEquals (0b0000_1000, NumberUtils .readBits (0b1000_0100, 1 , 4 ));
51175 }
52176}
0 commit comments