1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.io;
18
19 import junit.framework.TestCase;
20
21 import java.io.IOException;
22 import java.io.ByteArrayInputStream;
23 import java.io.ByteArrayOutputStream;
24
25
26
27
28 public class EndianUtilsTest extends TestCase {
29
30 public EndianUtilsTest(String name) {
31 super(name);
32 }
33
34 public void testSwapShort() {
35 assertEquals( (short) 0, EndianUtils.swapShort( (short) 0 ) );
36 assertEquals( (short) 0x0201, EndianUtils.swapShort( (short) 0x0102 ) );
37 assertEquals( (short) 0xffff, EndianUtils.swapShort( (short) 0xffff ) );
38 assertEquals( (short) 0x0102, EndianUtils.swapShort( (short) 0x0201 ) );
39 }
40
41 public void testSwapInteger() {
42 assertEquals( 0, EndianUtils.swapInteger( 0 ) );
43 assertEquals( 0x04030201, EndianUtils.swapInteger( 0x01020304 ) );
44 assertEquals( 0x01000000, EndianUtils.swapInteger( 0x00000001 ) );
45 assertEquals( 0x00000001, EndianUtils.swapInteger( 0x01000000 ) );
46 assertEquals( 0x11111111, EndianUtils.swapInteger( 0x11111111 ) );
47 assertEquals( 0xabcdef10, EndianUtils.swapInteger( 0x10efcdab ) );
48 assertEquals( 0xab, EndianUtils.swapInteger( 0xab000000 ) );
49 }
50
51 public void testSwapLong() {
52 assertEquals( 0, EndianUtils.swapLong( 0 ) );
53 assertEquals( 0x0807060504030201L, EndianUtils.swapLong( 0x0102030405060708L ) );
54 assertEquals( 0xffffffffffffffffL, EndianUtils.swapLong( 0xffffffffffffffffL ) );
55 assertEquals( 0xab, EndianUtils.swapLong( 0xab00000000000000L ) );
56 }
57
58 public void testSwapFloat() {
59 assertEquals( 0.0f, EndianUtils.swapFloat( 0.0f ), 0.0 );
60 float f1 = Float.intBitsToFloat( 0x01020304 );
61 float f2 = Float.intBitsToFloat( 0x04030201 );
62 assertEquals( f2, EndianUtils.swapFloat( f1 ), 0.0 );
63 }
64
65 public void testSwapDouble() {
66 assertEquals( 0.0, EndianUtils.swapDouble( 0.0 ), 0.0 );
67 double d1 = Double.longBitsToDouble( 0x0102030405060708L );
68 double d2 = Double.longBitsToDouble( 0x0807060504030201L );
69 assertEquals( d2, EndianUtils.swapDouble( d1 ), 0.0 );
70 }
71
72
73
74
75
76 public void testSymmetry() {
77 assertEquals( (short) 0x0102, EndianUtils.swapShort( EndianUtils.swapShort( (short) 0x0102 ) ) );
78 assertEquals( 0x01020304, EndianUtils.swapInteger( EndianUtils.swapInteger( 0x01020304 ) ) );
79 assertEquals( 0x0102030405060708L, EndianUtils.swapLong( EndianUtils.swapLong( 0x0102030405060708L ) ) );
80 float f1 = Float.intBitsToFloat( 0x01020304 );
81 assertEquals( f1, EndianUtils.swapFloat( EndianUtils.swapFloat( f1 ) ), 0.0 );
82 double d1 = Double.longBitsToDouble( 0x0102030405060708L );
83 assertEquals( d1, EndianUtils.swapDouble( EndianUtils.swapDouble( d1 ) ), 0.0 );
84 }
85
86 public void testReadSwappedShort() throws IOException {
87 byte[] bytes = new byte[] { 0x02, 0x01 };
88 assertEquals( 0x0102, EndianUtils.readSwappedShort( bytes, 0 ) );
89
90 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
91 assertEquals( 0x0102, EndianUtils.readSwappedShort( input ) );
92 }
93
94 public void testWriteSwappedShort() throws IOException {
95 byte[] bytes = new byte[2];
96 EndianUtils.writeSwappedShort( bytes, 0, (short) 0x0102 );
97 assertEquals( 0x02, bytes[0] );
98 assertEquals( 0x01, bytes[1] );
99
100 ByteArrayOutputStream baos = new ByteArrayOutputStream(2);
101 EndianUtils.writeSwappedShort( baos, (short) 0x0102 );
102 bytes = baos.toByteArray();
103 assertEquals( 0x02, bytes[0] );
104 assertEquals( 0x01, bytes[1] );
105 }
106
107 public void testReadSwappedUnsignedShort() throws IOException {
108 byte[] bytes = new byte[] { 0x02, 0x01 };
109 assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( bytes, 0 ) );
110
111 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
112 assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( input ) );
113 }
114
115 public void testReadSwappedInteger() throws IOException {
116 byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
117 assertEquals( 0x01020304, EndianUtils.readSwappedInteger( bytes, 0 ) );
118
119 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
120 assertEquals( 0x01020304, EndianUtils.readSwappedInteger( input ) );
121 }
122
123 public void testWriteSwappedInteger() throws IOException {
124 byte[] bytes = new byte[4];
125 EndianUtils.writeSwappedInteger( bytes, 0, 0x01020304 );
126 assertEquals( 0x04, bytes[0] );
127 assertEquals( 0x03, bytes[1] );
128 assertEquals( 0x02, bytes[2] );
129 assertEquals( 0x01, bytes[3] );
130
131 ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
132 EndianUtils.writeSwappedInteger( baos, 0x01020304 );
133 bytes = baos.toByteArray();
134 assertEquals( 0x04, bytes[0] );
135 assertEquals( 0x03, bytes[1] );
136 assertEquals( 0x02, bytes[2] );
137 assertEquals( 0x01, bytes[3] );
138 }
139
140 public void testReadSwappedUnsignedInteger() throws IOException {
141 byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
142 assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( bytes, 0 ) );
143
144 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
145 assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( input ) );
146 }
147
148 public void testReadSwappedLong() throws IOException {
149 byte[] bytes = new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
150 assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( bytes, 0 ) );
151
152 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
153 assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( input ) );
154 }
155
156 public void testWriteSwappedLong() throws IOException {
157 byte[] bytes = new byte[8];
158 EndianUtils.writeSwappedLong( bytes, 0, 0x0102030405060708L );
159 assertEquals( 0x08, bytes[0] );
160 assertEquals( 0x07, bytes[1] );
161 assertEquals( 0x06, bytes[2] );
162 assertEquals( 0x05, bytes[3] );
163 assertEquals( 0x04, bytes[4] );
164 assertEquals( 0x03, bytes[5] );
165 assertEquals( 0x02, bytes[6] );
166 assertEquals( 0x01, bytes[7] );
167
168 ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
169 EndianUtils.writeSwappedLong( baos, 0x0102030405060708L );
170 bytes = baos.toByteArray();
171 assertEquals( 0x08, bytes[0] );
172 assertEquals( 0x07, bytes[1] );
173 assertEquals( 0x06, bytes[2] );
174 assertEquals( 0x05, bytes[3] );
175 assertEquals( 0x04, bytes[4] );
176 assertEquals( 0x03, bytes[5] );
177 assertEquals( 0x02, bytes[6] );
178 assertEquals( 0x01, bytes[7] );
179 }
180
181 public void testReadSwappedFloat() throws IOException {
182 byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
183 float f1 = Float.intBitsToFloat( 0x01020304 );
184 float f2 = EndianUtils.readSwappedFloat( bytes, 0 );
185 assertEquals( f1, f2, 0.0 );
186
187 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
188 assertEquals( f1, EndianUtils.readSwappedFloat( input ), 0.0 );
189 }
190
191 public void testWriteSwappedFloat() throws IOException {
192 byte[] bytes = new byte[4];
193 float f1 = Float.intBitsToFloat( 0x01020304 );
194 EndianUtils.writeSwappedFloat( bytes, 0, f1 );
195 assertEquals( 0x04, bytes[0] );
196 assertEquals( 0x03, bytes[1] );
197 assertEquals( 0x02, bytes[2] );
198 assertEquals( 0x01, bytes[3] );
199
200 ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
201 EndianUtils.writeSwappedFloat( baos, f1 );
202 bytes = baos.toByteArray();
203 assertEquals( 0x04, bytes[0] );
204 assertEquals( 0x03, bytes[1] );
205 assertEquals( 0x02, bytes[2] );
206 assertEquals( 0x01, bytes[3] );
207 }
208
209 public void testReadSwappedDouble() throws IOException {
210 byte[] bytes = new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
211 double d1 = Double.longBitsToDouble( 0x0102030405060708L );
212 double d2 = EndianUtils.readSwappedDouble( bytes, 0 );
213 assertEquals( d1, d2, 0.0 );
214
215 ByteArrayInputStream input = new ByteArrayInputStream(bytes);
216 assertEquals( d1, EndianUtils.readSwappedDouble( input ), 0.0 );
217 }
218
219 public void testWriteSwappedDouble() throws IOException {
220 byte[] bytes = new byte[8];
221 double d1 = Double.longBitsToDouble( 0x0102030405060708L );
222 EndianUtils.writeSwappedDouble( bytes, 0, d1 );
223 assertEquals( 0x08, bytes[0] );
224 assertEquals( 0x07, bytes[1] );
225 assertEquals( 0x06, bytes[2] );
226 assertEquals( 0x05, bytes[3] );
227 assertEquals( 0x04, bytes[4] );
228 assertEquals( 0x03, bytes[5] );
229 assertEquals( 0x02, bytes[6] );
230 assertEquals( 0x01, bytes[7] );
231
232 ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
233 EndianUtils.writeSwappedDouble( baos, d1 );
234 bytes = baos.toByteArray();
235 assertEquals( 0x08, bytes[0] );
236 assertEquals( 0x07, bytes[1] );
237 assertEquals( 0x06, bytes[2] );
238 assertEquals( 0x05, bytes[3] );
239 assertEquals( 0x04, bytes[4] );
240 assertEquals( 0x03, bytes[5] );
241 assertEquals( 0x02, bytes[6] );
242 assertEquals( 0x01, bytes[7] );
243 }
244
245
246 public void testSymmetryOfLong() throws IOException {
247
248 double[] tests = new double[] {34.345, -345.5645, 545.12, 10.043, 7.123456789123};
249 for (int i = 0; i< tests.length ;i++) {
250
251
252 byte[] buffer = new byte[8];
253 long ln1 = Double.doubleToLongBits( tests[i] );
254 EndianUtils.writeSwappedLong(buffer, 0, ln1);
255 long ln2 = EndianUtils.readSwappedLong(buffer, 0);
256 assertEquals( ln1, ln2 );
257
258
259 buffer = new byte[8];
260 EndianUtils.writeSwappedDouble(buffer, 0, tests[i]);
261 double val = EndianUtils.readSwappedDouble(buffer, 0);
262 assertEquals( tests[i], val, 0 );
263 }
264 }
265
266
267 public void testUnsignedOverrun() throws Exception {
268 byte[] target = new byte[] { 0, 0, 0, (byte)0x80 };
269 long expected = 0x80000000L;
270
271 long actual = EndianUtils.readSwappedUnsignedInteger(target, 0);
272 assertEquals("readSwappedUnsignedInteger(byte[], int) was incorrect", expected, actual);
273
274 ByteArrayInputStream in = new ByteArrayInputStream(target);
275 actual = EndianUtils.readSwappedUnsignedInteger(in);
276 assertEquals("readSwappedUnsignedInteger(InputStream) was incorrect", expected, actual);
277 }
278
279 }