Source Code Cross Referenced for EndianUtilsTest.java in  » Library » apache-common-IO » org » apache » commons » io » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Library » apache common IO » org.apache.commons.io 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * Licensed to the Apache Software Foundation (ASF) under one or more
003:         * contributor license agreements.  See the NOTICE file distributed with
004:         * this work for additional information regarding copyright ownership.
005:         * The ASF licenses this file to You under the Apache License, Version 2.0
006:         * (the "License"); you may not use this file except in compliance with
007:         * the License.  You may obtain a copy of the License at
008:         * 
009:         *      http://www.apache.org/licenses/LICENSE-2.0
010:         * 
011:         * Unless required by applicable law or agreed to in writing, software
012:         * distributed under the License is distributed on an "AS IS" BASIS,
013:         * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014:         * See the License for the specific language governing permissions and
015:         * limitations under the License.
016:         */
017:        package org.apache.commons.io;
018:
019:        import junit.framework.TestCase;
020:
021:        import java.io.IOException;
022:        import java.io.ByteArrayInputStream;
023:        import java.io.ByteArrayOutputStream;
024:
025:        /**
026:         * @version $Revision: 539638 $ $Date: 2007-05-19 01:44:30 +0200 (Sa, 19 Mai 2007) $
027:         */
028:        public class EndianUtilsTest extends TestCase {
029:
030:            public EndianUtilsTest(String name) {
031:                super (name);
032:            }
033:
034:            public void testSwapShort() {
035:                assertEquals((short) 0, EndianUtils.swapShort((short) 0));
036:                assertEquals((short) 0x0201, EndianUtils
037:                        .swapShort((short) 0x0102));
038:                assertEquals((short) 0xffff, EndianUtils
039:                        .swapShort((short) 0xffff));
040:                assertEquals((short) 0x0102, EndianUtils
041:                        .swapShort((short) 0x0201));
042:            }
043:
044:            public void testSwapInteger() {
045:                assertEquals(0, EndianUtils.swapInteger(0));
046:                assertEquals(0x04030201, EndianUtils.swapInteger(0x01020304));
047:                assertEquals(0x01000000, EndianUtils.swapInteger(0x00000001));
048:                assertEquals(0x00000001, EndianUtils.swapInteger(0x01000000));
049:                assertEquals(0x11111111, EndianUtils.swapInteger(0x11111111));
050:                assertEquals(0xabcdef10, EndianUtils.swapInteger(0x10efcdab));
051:                assertEquals(0xab, EndianUtils.swapInteger(0xab000000));
052:            }
053:
054:            public void testSwapLong() {
055:                assertEquals(0, EndianUtils.swapLong(0));
056:                assertEquals(0x0807060504030201L, EndianUtils
057:                        .swapLong(0x0102030405060708L));
058:                assertEquals(0xffffffffffffffffL, EndianUtils
059:                        .swapLong(0xffffffffffffffffL));
060:                assertEquals(0xab, EndianUtils.swapLong(0xab00000000000000L));
061:            }
062:
063:            public void testSwapFloat() {
064:                assertEquals(0.0f, EndianUtils.swapFloat(0.0f), 0.0);
065:                float f1 = Float.intBitsToFloat(0x01020304);
066:                float f2 = Float.intBitsToFloat(0x04030201);
067:                assertEquals(f2, EndianUtils.swapFloat(f1), 0.0);
068:            }
069:
070:            public void testSwapDouble() {
071:                assertEquals(0.0, EndianUtils.swapDouble(0.0), 0.0);
072:                double d1 = Double.longBitsToDouble(0x0102030405060708L);
073:                double d2 = Double.longBitsToDouble(0x0807060504030201L);
074:                assertEquals(d2, EndianUtils.swapDouble(d1), 0.0);
075:            }
076:
077:            /**
078:             * Tests all swapXxxx methods for symmetry when going from one endian 
079:             * to another and back again. 
080:             */
081:            public void testSymmetry() {
082:                assertEquals((short) 0x0102, EndianUtils.swapShort(EndianUtils
083:                        .swapShort((short) 0x0102)));
084:                assertEquals(0x01020304, EndianUtils.swapInteger(EndianUtils
085:                        .swapInteger(0x01020304)));
086:                assertEquals(0x0102030405060708L, EndianUtils
087:                        .swapLong(EndianUtils.swapLong(0x0102030405060708L)));
088:                float f1 = Float.intBitsToFloat(0x01020304);
089:                assertEquals(f1, EndianUtils.swapFloat(EndianUtils
090:                        .swapFloat(f1)), 0.0);
091:                double d1 = Double.longBitsToDouble(0x0102030405060708L);
092:                assertEquals(d1, EndianUtils.swapDouble(EndianUtils
093:                        .swapDouble(d1)), 0.0);
094:            }
095:
096:            public void testReadSwappedShort() throws IOException {
097:                byte[] bytes = new byte[] { 0x02, 0x01 };
098:                assertEquals(0x0102, EndianUtils.readSwappedShort(bytes, 0));
099:
100:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
101:                assertEquals(0x0102, EndianUtils.readSwappedShort(input));
102:            }
103:
104:            public void testWriteSwappedShort() throws IOException {
105:                byte[] bytes = new byte[2];
106:                EndianUtils.writeSwappedShort(bytes, 0, (short) 0x0102);
107:                assertEquals(0x02, bytes[0]);
108:                assertEquals(0x01, bytes[1]);
109:
110:                ByteArrayOutputStream baos = new ByteArrayOutputStream(2);
111:                EndianUtils.writeSwappedShort(baos, (short) 0x0102);
112:                bytes = baos.toByteArray();
113:                assertEquals(0x02, bytes[0]);
114:                assertEquals(0x01, bytes[1]);
115:            }
116:
117:            public void testReadSwappedUnsignedShort() throws IOException {
118:                byte[] bytes = new byte[] { 0x02, 0x01 };
119:                assertEquals(0x00000102, EndianUtils.readSwappedUnsignedShort(
120:                        bytes, 0));
121:
122:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
123:                assertEquals(0x00000102, EndianUtils
124:                        .readSwappedUnsignedShort(input));
125:            }
126:
127:            public void testReadSwappedInteger() throws IOException {
128:                byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
129:                assertEquals(0x01020304, EndianUtils.readSwappedInteger(bytes,
130:                        0));
131:
132:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
133:                assertEquals(0x01020304, EndianUtils.readSwappedInteger(input));
134:            }
135:
136:            public void testWriteSwappedInteger() throws IOException {
137:                byte[] bytes = new byte[4];
138:                EndianUtils.writeSwappedInteger(bytes, 0, 0x01020304);
139:                assertEquals(0x04, bytes[0]);
140:                assertEquals(0x03, bytes[1]);
141:                assertEquals(0x02, bytes[2]);
142:                assertEquals(0x01, bytes[3]);
143:
144:                ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
145:                EndianUtils.writeSwappedInteger(baos, 0x01020304);
146:                bytes = baos.toByteArray();
147:                assertEquals(0x04, bytes[0]);
148:                assertEquals(0x03, bytes[1]);
149:                assertEquals(0x02, bytes[2]);
150:                assertEquals(0x01, bytes[3]);
151:            }
152:
153:            public void testReadSwappedUnsignedInteger() throws IOException {
154:                byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
155:                assertEquals(0x0000000001020304L, EndianUtils
156:                        .readSwappedUnsignedInteger(bytes, 0));
157:
158:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
159:                assertEquals(0x0000000001020304L, EndianUtils
160:                        .readSwappedUnsignedInteger(input));
161:            }
162:
163:            public void testReadSwappedLong() throws IOException {
164:                byte[] bytes = new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
165:                        0x02, 0x01 };
166:                assertEquals(0x0102030405060708L, EndianUtils.readSwappedLong(
167:                        bytes, 0));
168:
169:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
170:                assertEquals(0x0102030405060708L, EndianUtils
171:                        .readSwappedLong(input));
172:            }
173:
174:            public void testWriteSwappedLong() throws IOException {
175:                byte[] bytes = new byte[8];
176:                EndianUtils.writeSwappedLong(bytes, 0, 0x0102030405060708L);
177:                assertEquals(0x08, bytes[0]);
178:                assertEquals(0x07, bytes[1]);
179:                assertEquals(0x06, bytes[2]);
180:                assertEquals(0x05, bytes[3]);
181:                assertEquals(0x04, bytes[4]);
182:                assertEquals(0x03, bytes[5]);
183:                assertEquals(0x02, bytes[6]);
184:                assertEquals(0x01, bytes[7]);
185:
186:                ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
187:                EndianUtils.writeSwappedLong(baos, 0x0102030405060708L);
188:                bytes = baos.toByteArray();
189:                assertEquals(0x08, bytes[0]);
190:                assertEquals(0x07, bytes[1]);
191:                assertEquals(0x06, bytes[2]);
192:                assertEquals(0x05, bytes[3]);
193:                assertEquals(0x04, bytes[4]);
194:                assertEquals(0x03, bytes[5]);
195:                assertEquals(0x02, bytes[6]);
196:                assertEquals(0x01, bytes[7]);
197:            }
198:
199:            public void testReadSwappedFloat() throws IOException {
200:                byte[] bytes = new byte[] { 0x04, 0x03, 0x02, 0x01 };
201:                float f1 = Float.intBitsToFloat(0x01020304);
202:                float f2 = EndianUtils.readSwappedFloat(bytes, 0);
203:                assertEquals(f1, f2, 0.0);
204:
205:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
206:                assertEquals(f1, EndianUtils.readSwappedFloat(input), 0.0);
207:            }
208:
209:            public void testWriteSwappedFloat() throws IOException {
210:                byte[] bytes = new byte[4];
211:                float f1 = Float.intBitsToFloat(0x01020304);
212:                EndianUtils.writeSwappedFloat(bytes, 0, f1);
213:                assertEquals(0x04, bytes[0]);
214:                assertEquals(0x03, bytes[1]);
215:                assertEquals(0x02, bytes[2]);
216:                assertEquals(0x01, bytes[3]);
217:
218:                ByteArrayOutputStream baos = new ByteArrayOutputStream(4);
219:                EndianUtils.writeSwappedFloat(baos, f1);
220:                bytes = baos.toByteArray();
221:                assertEquals(0x04, bytes[0]);
222:                assertEquals(0x03, bytes[1]);
223:                assertEquals(0x02, bytes[2]);
224:                assertEquals(0x01, bytes[3]);
225:            }
226:
227:            public void testReadSwappedDouble() throws IOException {
228:                byte[] bytes = new byte[] { 0x08, 0x07, 0x06, 0x05, 0x04, 0x03,
229:                        0x02, 0x01 };
230:                double d1 = Double.longBitsToDouble(0x0102030405060708L);
231:                double d2 = EndianUtils.readSwappedDouble(bytes, 0);
232:                assertEquals(d1, d2, 0.0);
233:
234:                ByteArrayInputStream input = new ByteArrayInputStream(bytes);
235:                assertEquals(d1, EndianUtils.readSwappedDouble(input), 0.0);
236:            }
237:
238:            public void testWriteSwappedDouble() throws IOException {
239:                byte[] bytes = new byte[8];
240:                double d1 = Double.longBitsToDouble(0x0102030405060708L);
241:                EndianUtils.writeSwappedDouble(bytes, 0, d1);
242:                assertEquals(0x08, bytes[0]);
243:                assertEquals(0x07, bytes[1]);
244:                assertEquals(0x06, bytes[2]);
245:                assertEquals(0x05, bytes[3]);
246:                assertEquals(0x04, bytes[4]);
247:                assertEquals(0x03, bytes[5]);
248:                assertEquals(0x02, bytes[6]);
249:                assertEquals(0x01, bytes[7]);
250:
251:                ByteArrayOutputStream baos = new ByteArrayOutputStream(8);
252:                EndianUtils.writeSwappedDouble(baos, d1);
253:                bytes = baos.toByteArray();
254:                assertEquals(0x08, bytes[0]);
255:                assertEquals(0x07, bytes[1]);
256:                assertEquals(0x06, bytes[2]);
257:                assertEquals(0x05, bytes[3]);
258:                assertEquals(0x04, bytes[4]);
259:                assertEquals(0x03, bytes[5]);
260:                assertEquals(0x02, bytes[6]);
261:                assertEquals(0x01, bytes[7]);
262:            }
263:
264:            // tests #IO-101
265:            public void testSymmetryOfLong() throws IOException {
266:
267:                double[] tests = new double[] { 34.345, -345.5645, 545.12,
268:                        10.043, 7.123456789123 };
269:                for (int i = 0; i < tests.length; i++) {
270:
271:                    // testing the real problem
272:                    byte[] buffer = new byte[8];
273:                    long ln1 = Double.doubleToLongBits(tests[i]);
274:                    EndianUtils.writeSwappedLong(buffer, 0, ln1);
275:                    long ln2 = EndianUtils.readSwappedLong(buffer, 0);
276:                    assertEquals(ln1, ln2);
277:
278:                    // testing the bug report
279:                    buffer = new byte[8];
280:                    EndianUtils.writeSwappedDouble(buffer, 0, tests[i]);
281:                    double val = EndianUtils.readSwappedDouble(buffer, 0);
282:                    assertEquals(tests[i], val, 0);
283:                }
284:            }
285:
286:            // tests #IO-117
287:            public void testUnsignedOverrun() throws Exception {
288:                byte[] target = new byte[] { 0, 0, 0, (byte) 0x80 };
289:                long expected = 0x80000000L;
290:
291:                long actual = EndianUtils.readSwappedUnsignedInteger(target, 0);
292:                assertEquals(
293:                        "readSwappedUnsignedInteger(byte[], int) was incorrect",
294:                        expected, actual);
295:
296:                ByteArrayInputStream in = new ByteArrayInputStream(target);
297:                actual = EndianUtils.readSwappedUnsignedInteger(in);
298:                assertEquals(
299:                        "readSwappedUnsignedInteger(InputStream) was incorrect",
300:                        expected, actual);
301:            }
302:
303:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.