KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > io > EndianUtilsTest


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.io;
17
18 import junit.framework.TestCase;
19
20 import java.io.IOException JavaDoc;
21 import java.io.ByteArrayInputStream JavaDoc;
22 import java.io.ByteArrayOutputStream JavaDoc;
23
24 /**
25  * @author Henri Yandell (bayard at apache dot org)
26  * @version $Revision: 1.13 $ $Date: 2004/02/29 21:47:07 $
27  */

28 public class EndianUtilsTest extends TestCase {
29
30     public EndianUtilsTest(String JavaDoc 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      * Tests all swapXxxx methods for symmetry when going from one endian
74      * to another and back again.
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 JavaDoc {
87         byte[] bytes = new byte[] { 0x02, 0x01 };
88         assertEquals( 0x0102, EndianUtils.readSwappedShort( bytes, 0 ) );
89
90         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
91         assertEquals( 0x0102, EndianUtils.readSwappedShort( input ) );
92     }
93
94     public void testWriteSwappedShort() throws IOException JavaDoc {
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc(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 JavaDoc {
108         byte[] bytes = new byte[] { 0x02, 0x01 };
109         assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( bytes, 0 ) );
110
111         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
112         assertEquals( 0x00000102, EndianUtils.readSwappedUnsignedShort( input ) );
113     }
114
115     public void testReadSwappedInteger() throws IOException JavaDoc {
116         byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
117         assertEquals( 0x01020304, EndianUtils.readSwappedInteger( bytes, 0 ) );
118
119         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
120         assertEquals( 0x01020304, EndianUtils.readSwappedInteger( input ) );
121     }
122
123     public void testWriteSwappedInteger() throws IOException JavaDoc {
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc(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 JavaDoc {
141         byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
142         assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( bytes, 0 ) );
143
144         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
145         assertEquals( 0x0000000001020304L, EndianUtils.readSwappedUnsignedInteger( input ) );
146     }
147
148     public void testReadSwappedLong() throws IOException JavaDoc {
149         byte[] bytes = new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01 };
150         assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( bytes, 0 ) );
151
152         ByteArrayInputStream JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
153         assertEquals( 0x0102030405060708L, EndianUtils.readSwappedLong( input ) );
154     }
155
156     public void testWriteSwappedLong() throws IOException JavaDoc {
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc(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 JavaDoc {
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 JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
188         assertEquals( f1, EndianUtils.readSwappedFloat( input ), 0.0 );
189     }
190
191     public void testWriteSwappedFloat() throws IOException JavaDoc {
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc(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 JavaDoc {
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 JavaDoc input = new ByteArrayInputStream JavaDoc(bytes);
216         assertEquals( d1, EndianUtils.readSwappedDouble( input ), 0.0 );
217     }
218
219     public void testWriteSwappedDouble() throws IOException JavaDoc {
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 JavaDoc baos = new ByteArrayOutputStream JavaDoc(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
Popular Tags