2121
2222package at .favre .lib .bytes ;
2323
24- import org .junit .Before ;
2524import org .junit .Test ;
2625
27- import java .nio .ByteBuffer ;
2826import java .nio .ByteOrder ;
2927
30- import static org .junit .Assert .assertArrayEquals ;
3128import static org .junit .Assert .assertEquals ;
29+ import static org .junit .Assert .assertNotEquals ;
3230
33- public class BytesByteOrderTest {
34- @ Before
35- public void setUp () throws Exception {
31+ public class BytesByteOrderTest extends ABytesTest {
32+
33+ @ Test
34+ public void miscInput () throws Exception {
35+ testOrder (Bytes .from (350 ));
36+ testOrder (Bytes .from (172863182736L ));
37+ testOrder (Bytes .from (example_bytes_one ));
38+ testOrder (Bytes .from (example_bytes_four ));
39+ testOrder (Bytes .from (example_bytes_sixteen ));
40+ testOrder (Bytes .from (example_bytes_twentyfour ));
41+ }
42+
43+ private void testOrder (Bytes bytes ) {
44+ Bytes bigEndian = bytes .copy ().byteOrder (ByteOrder .BIG_ENDIAN );
45+ Bytes littleEndian = bytes .copy ().byteOrder (ByteOrder .LITTLE_ENDIAN );
46+ assertEquals (ByteOrder .BIG_ENDIAN , bigEndian .byteOrder ());
47+ assertEquals (ByteOrder .LITTLE_ENDIAN , littleEndian .byteOrder ());
48+ assertNotEquals (bigEndian , littleEndian );
49+
50+ System .out .println ("big endian: " + bigEndian );
51+ System .out .println ("little endian: " + littleEndian );
52+ }
53+
54+ @ Test
55+ public void encodeBinary () throws Exception {
56+ Bytes b = Bytes .from (example_bytes_four );
57+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBinary (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeBinary ());
58+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBinary (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeBinary ());
59+ }
60+
61+ @ Test
62+ public void encodeOct () throws Exception {
63+ Bytes b = Bytes .from (example_bytes_four );
64+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeOctal (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeOctal ());
65+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeOctal (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeOctal ());
3666 }
3767
3868 @ Test
39- public void fromLong () throws Exception {
40- long number = 172863182736L ;
41- System .out .println (Bytes .from (number ).encodeHex ());
42- System .out .println (Bytes .from (number ).toLong ());
43- assertArrayEquals (Bytes .parseHex ("000000283f72d790" ).array (), Bytes .from (number ).array ());
44- assertEquals (number , Bytes .from (number ).toLong ());
69+ public void encodeDec () throws Exception {
70+ Bytes b = Bytes .from (example_bytes_four );
71+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeDec (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeDec ());
72+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeDec (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeDec ());
73+ }
4574
46- byte [] defaultArray = new byte []{(byte ) 0x01 , (byte ) 0x02 , (byte ) 0x03 , (byte ) 0x04 };
75+ @ Test
76+ public void encodeHex () throws Exception {
77+ Bytes b = Bytes .from (example_bytes_two );
78+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeHex (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeHex ());
79+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeHex (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeHex ());
80+ }
4781
48- System .out .println (Bytes .from (ByteBuffer .wrap (defaultArray ).order (ByteOrder .BIG_ENDIAN )).encodeHex ());
49- System .out .println (Bytes .from (ByteBuffer .wrap (defaultArray ).order (ByteOrder .BIG_ENDIAN ).getInt ()).encodeHex ());
50- System .out .println (Bytes .from (ByteBuffer .wrap (defaultArray ).order (ByteOrder .LITTLE_ENDIAN )).encodeHex ());
51- System .out .println (Bytes .from (ByteBuffer .wrap (defaultArray ).order (ByteOrder .LITTLE_ENDIAN ).getInt ()).encodeHex ());
82+ @ Test
83+ public void encodeBase36 () throws Exception {
84+ Bytes b = Bytes .from (example_bytes_four );
85+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBase36 (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeBase36 ());
86+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBase36 (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeBase36 ());
87+ }
88+
89+ @ Test
90+ public void encodeBase64 () throws Exception {
91+ Bytes b = Bytes .from (example_bytes_four );
92+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBase64 (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).encodeBase64 ());
93+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).encodeBase64 (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).reverse ().encodeBase64 ());
5294 }
95+
96+ @ Test
97+ public void toByte () throws Exception {
98+ Bytes b = Bytes .from (example_bytes_one );
99+ assertEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).toByte (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).toByte ());
100+ }
101+
102+ @ Test
103+ public void toChar () throws Exception {
104+ Bytes b = Bytes .from (example_bytes_two );
105+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).toChar (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).toChar ());
106+ }
107+
108+ @ Test
109+ public void toShort () throws Exception {
110+ Bytes b = Bytes .from (example_bytes_two );
111+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).toShort (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).toShort ());
112+ }
113+
114+ @ Test
115+ public void toInt () throws Exception {
116+ Bytes b = Bytes .from (example_bytes_four );
117+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).toInt (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).toInt ());
118+ }
119+
120+ @ Test
121+ public void toLong () throws Exception {
122+ Bytes b = Bytes .from (example_bytes_eight );
123+ assertNotEquals (b .byteOrder (ByteOrder .BIG_ENDIAN ).toLong (), b .byteOrder (ByteOrder .LITTLE_ENDIAN ).toLong ());
124+ }
125+
126+
53127}
0 commit comments