KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > imagero > uio > buffer > arrays > ArraysTest


1 /*
2  * Copyright (c) Andrey Kuznetsov. All Rights Reserved.
3  *
4  * http://uio.imagero.com
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * o Redistributions of source code must retain the above copyright notice,
10  * this list of conditions and the following disclaimer.
11  *
12  * o Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  *
16  * o Neither the name of imagero Andrei Kouznetsov nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */

32 package com.imagero.uio.buffer.arrays;
33
34 import com.imagero.uio.RandomAccessByteArray;
35 import com.imagero.uio.RandomAccessFactory;
36 import com.imagero.uio.RandomAccessRO;
37 import com.imagero.uio.Sys;
38 import com.imagero.uio.io.BitInputStream;
39 import com.imagero.uio.io.RLE4InputStream;
40 import com.imagero.uio.io.RLE8InputStream;
41 import com.imagero.uio.io.RLEInputStream;
42
43 import java.io.ByteArrayInputStream JavaDoc;
44 import java.io.ByteArrayOutputStream JavaDoc;
45 import java.io.DataInputStream JavaDoc;
46 import java.io.DataOutputStream JavaDoc;
47 import java.io.IOException JavaDoc;
48 import java.util.StringTokenizer JavaDoc;
49
50 /**
51  * @author Andrei Kouznetsov
52  * Date: 14.06.2004
53  * Time: 20:14:34
54  */

55 public class ArraysTest extends junit.framework.TestCase {
56
57     public static boolean UNIT_TEST = false;
58
59     public ArraysTest() {
60         super("ArraysTest");
61     }
62
63     public void testInt() throws IOException JavaDoc {
64         if (UNIT_TEST) {
65             Sys.out.println("testInt");
66             int unitSize = 4;
67             int[] a = new int[2];
68             byte[] b = new byte[]{(byte) 0xEE, (byte) 0x0, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77};
69
70             //read ints with standard java method
71
DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
72             for (int i = 0; i < a.length; i++) {
73                 a[i] = dis.readInt();
74             }
75
76             //create manager
77
IntArrayBufferManager iadsm = new IntArrayBufferManager(a, a.length);
78
79             //create and fill byte array with little endian data
80
byte[] bLE = new byte[unitSize * a.length];
81             iadsm.readLE(bLE, 0);
82
83             //read little endian ints
84
RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN);
85             int[] aLE = new int[a.length];
86             r0.readFully(aLE);
87
88             for (int i = 0; i < aLE.length; i++) {
89                 assertEquals(a[i], aLE[i]);
90             }
91
92             //create and fill byte array with big endian data
93
byte[] bBE = new byte[unitSize * a.length];
94             iadsm.readBE(bBE, 0);
95
96             //read big endian ints
97
RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN);
98             int[] aBE = new int[a.length];
99             r1.readFully(aBE);
100
101             for (int i = 0; i < aBE.length; i++) {
102                 assertEquals(a[i], aBE[i]);
103             }
104         }
105     }
106
107     public void testChar() throws IOException JavaDoc {
108         if (UNIT_TEST) {
109             Sys.out.println("testChar");
110             int unitSize = 2;
111             char[] a = new char[4];
112             byte[] b = new byte[]{(byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77};
113
114             //read chars with standard java methode
115
DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
116             for (int i = 0; i < a.length; i++) {
117                 a[i] = dis.readChar();
118             }
119
120             //create manager
121
CharArrayBufferManager iadsm = new CharArrayBufferManager(a, a.length);
122
123             //create and fill byte array with little endian data
124
byte[] bLE = new byte[unitSize * a.length];
125             iadsm.readLE(bLE, 0);
126
127             //read little endian chars
128
RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN);
129             char[] aLE = new char[a.length];
130             r0.readFully(aLE);
131
132             for (int i = 0; i < aLE.length; i++) {
133                 assertEquals(a[i], aLE[i]);
134             }
135
136             //create and fill byte array with big endian data
137
byte[] bBE = new byte[unitSize * a.length];
138             iadsm.readBE(bBE, 0);
139
140             //read big endian chars
141
RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN);
142             char[] aBE = new char[a.length];
143             r1.readFully(aBE);
144
145             for (int i = 0; i < aBE.length; i++) {
146                 assertEquals(a[i], aBE[i]);
147             }
148         }
149     }
150
151     public void testShort() throws IOException JavaDoc {
152         if (UNIT_TEST) {
153             Sys.out.println("testShort");
154             int unitSize = 2;
155             short[] a = new short[4];
156             byte[] b = new byte[]{(byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77};
157
158             //read shorts with standard java methode
159
DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
160             for (int i = 0; i < a.length; i++) {
161                 a[i] = dis.readShort();
162             }
163
164             //create manager
165
ShortArrayBufferManager iadsm = new ShortArrayBufferManager(a, a.length);
166
167             //create and fill byte array with little endian data
168
byte[] bLE = new byte[unitSize * a.length];
169             iadsm.readLE(bLE, 0);
170
171             //read little endian short
172
RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN);
173             short[] aLE = new short[a.length];
174             r0.readFully(aLE);
175
176             for (int i = 0; i < aLE.length; i++) {
177                 assertEquals(a[i], aLE[i]);
178             }
179
180             //create and fill byte array with big endian data
181
byte[] bBE = new byte[unitSize * a.length];
182             iadsm.readBE(bBE, 0);
183
184             //read big endian short
185
RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN);
186             short[] aBE = new short[a.length];
187             r1.readFully(aBE);
188
189             for (int i = 0; i < aBE.length; i++) {
190                 assertEquals(a[i], aBE[i]);
191             }
192         }
193     }
194
195     public void testDouble() throws IOException JavaDoc {
196         if (UNIT_TEST) {
197             Sys.out.println("testDouble");
198             int unitSize = 8;
199             double[] a = new double[]{2.2d, 3.3d};
200             byte[] b;
201
202             final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
203             DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(bout);
204             dos.writeDouble(a[0]);
205             dos.writeDouble(a[1]);
206             dos.flush();
207             dos.close();
208             b = bout.toByteArray();
209
210             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
211             for (int i = 0; i < a.length; i++) {
212                 a[i] = dis.readDouble();
213             }
214
215             DoubleArrayBufferManager iadsm = new DoubleArrayBufferManager(a, a.length);
216
217             byte[] b1 = new byte[unitSize * a.length];
218             iadsm.readBE(b1, 0);
219
220             RandomAccessRO r0 = new RandomAccessByteArray(b1, RandomAccessFactory.BIG_ENDIAN);
221             double[] aBE = new double[a.length];
222             r0.readFully(aBE);
223
224             for (int i = 0; i < aBE.length; i++) {
225                 assertTrue(a[i] == aBE[i]);
226             }
227
228             byte[] b2 = new byte[unitSize * a.length];
229             iadsm.readLE(b2, 0);
230
231             RandomAccessRO r01 = new RandomAccessByteArray(b2, RandomAccessFactory.LITTLE_ENDIAN);
232             double[] aLE = new double[a.length];
233             r01.readFully(aLE);
234
235             for (int i = 0; i < aLE.length; i++) {
236                 assertTrue(a[i] == aLE[i]);
237             }
238         }
239     }
240
241     public void testFloat() throws IOException JavaDoc {
242         if (UNIT_TEST) {
243             Sys.out.println("testFloat");
244             int unitSize = 4;
245             float[] a = new float[]{2.2f, 3.3f};
246             byte[] b;
247
248             final ByteArrayOutputStream JavaDoc bout = new ByteArrayOutputStream JavaDoc();
249             DataOutputStream JavaDoc dos = new DataOutputStream JavaDoc(bout);
250             dos.writeFloat(a[0]);
251             dos.writeFloat(a[1]);
252             dos.flush();
253             dos.close();
254             b = bout.toByteArray();
255
256             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
257             for (int i = 0; i < a.length; i++) {
258                 a[i] = dis.readFloat();
259             }
260
261             FloatArrayBufferManager iadsm = new FloatArrayBufferManager(a, a.length);
262
263             byte[] b1 = new byte[unitSize * a.length];
264             iadsm.readBE(b1, 0);
265
266             RandomAccessRO r0 = new RandomAccessByteArray(b1, RandomAccessFactory.BIG_ENDIAN);
267             float[] aLE = new float[a.length];
268             r0.readFully(aLE);
269
270             for (int i = 0; i < aLE.length; i++) {
271                 assertTrue(a[i] == aLE[i]);
272             }
273
274             byte[] b2 = new byte[unitSize * a.length];
275             iadsm.readLE(b2, 0);
276
277             RandomAccessRO r01 = new RandomAccessByteArray(b2, RandomAccessFactory.LITTLE_ENDIAN);
278             float[] aBE = new float[a.length];
279             r01.readFully(aBE);
280
281             for (int i = 0; i < aBE.length; i++) {
282                 assertTrue(a[i] == aBE[i]);
283             }
284         }
285     }
286
287     public void testLong() throws IOException JavaDoc {
288         if (UNIT_TEST) {
289             Sys.out.println("testLong");
290             int unitSize = 8;
291             long[] a = new long[2];
292             byte[] b = new byte[]{
293                 (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77,
294                 (byte) 0xEE, (byte) 0xDD, (byte) 0xCC, (byte) 0xBB, (byte) 0xAA, (byte) 0x99, (byte) 0x88, 0x77};
295
296             DataInputStream JavaDoc dis = new DataInputStream JavaDoc(new ByteArrayInputStream JavaDoc(b));
297             a[0] = dis.readLong();
298             a[1] = dis.readLong();
299
300             LongArrayBufferManager iadsm = new LongArrayBufferManager(a, a.length);
301
302             byte[] bLE = new byte[unitSize * a.length];
303             iadsm.readLE(bLE, 0);
304
305             RandomAccessRO r0 = new RandomAccessByteArray(bLE, RandomAccessFactory.LITTLE_ENDIAN);
306             long[] aLE = new long[a.length];
307             r0.readFully(aLE);
308
309             for (int i = 0; i < aLE.length; i++) {
310                 assertEquals(a[i], aLE[i]);
311             }
312
313             byte[] bBE = new byte[unitSize * a.length];
314             iadsm.readBE(bBE, 0);
315
316             RandomAccessRO r1 = new RandomAccessByteArray(bBE, RandomAccessFactory.BIG_ENDIAN);
317             long[] aBE = new long[a.length];
318             r1.readFully(aBE);
319
320             for (int i = 0; i < aBE.length; i++) {
321                 assertEquals(a[i], aBE[i]);
322             }
323         }
324     }
325
326     public void testRLE8() {
327         if (UNIT_TEST) {
328             Sys.out.println("testRLE8");
329
330             String JavaDoc c =
331                     "0F FF 00 00 " +
332                     "02 FF 09 00 04 FF 00 00 " +
333                     "04 FF 03 00 03 FF 02 00 03 FF 00 00 " +
334                     "04 FF 03 00 04 FF 02 00 02 FF 00 00 " +
335                     "04 FF 03 00 04 FF 02 00 02 FF 00 00 " +
336                     "04 FF 03 00 04 FF 02 00 02 FF 00 00 " +
337                     "04 FF 03 00 03 FF 02 00 03 FF 00 00 " +
338                     "04 FF 03 00 01 FF 03 00 04 FF 00 00 " +
339                     "04 FF 03 00 01 FF 03 00 04 FF 00 00 " +
340                     "04 FF 03 00 03 FF 02 00 03 FF 00 00 " +
341                     "04 FF 03 00 04 FF 02 00 02 FF 00 00 " +
342                     "04 FF 03 00 04 FF 02 00 02 FF 00 00 " +
343                     "04 FF 03 00 03 FF 03 00 02 FF 00 00 " +
344                     "02 FF 0A 00 03 FF 00 00 " +
345                     "0F FF 00 00 00 01";
346
347             String JavaDoc d =
348                     "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF " +
349                     "FF FF 00 00 00 00 00 00 00 00 00 FF FF FF FF " +
350                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
351                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
352                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
353                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
354                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
355                     "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " +
356                     "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " +
357                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
358                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
359                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
360                     "FF FF FF FF 00 00 00 FF FF FF 00 00 00 FF FF " +
361                     "FF FF 00 00 00 00 00 00 00 00 00 00 FF FF FF " +
362                     "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ";
363
364             ByteArrayOutputStream JavaDoc cout = new ByteArrayOutputStream JavaDoc();
365             StringTokenizer JavaDoc cst = new StringTokenizer JavaDoc(c, " ", false);
366             while (cst.hasMoreElements()) {
367                 String JavaDoc s = cst.nextToken();
368                 cout.write(Integer.parseInt(s, 16));
369             }
370             byte[] cb = cout.toByteArray();
371
372             ByteArrayOutputStream JavaDoc dout = new ByteArrayOutputStream JavaDoc();
373             StringTokenizer JavaDoc dst = new StringTokenizer JavaDoc(d, " ", false);
374             while (dst.hasMoreElements()) {
375                 String JavaDoc s = dst.nextToken();
376                 dout.write(Integer.parseInt(s, 16));
377             }
378             byte[] db = dout.toByteArray();
379
380             ByteArrayOutputStream JavaDoc eout = new ByteArrayOutputStream JavaDoc();
381
382             RLE8InputStream in = new RLE8InputStream(new ByteArrayInputStream JavaDoc(cb));
383             try {
384                 while (in.available() > 0) {
385                     try {
386                         int a = in.read() & 0xFF;
387                         eout.write(a);
388                     }
389                     catch (RLE8InputStream.EndOfLineException ex) {
390                         continue;
391                     }
392                     catch (RLE8InputStream.EndOfBitmapException ex) {
393                         break;
394                     }
395                 }
396             }
397             catch (IOException JavaDoc ex) {
398                 ex.printStackTrace();
399             }
400             byte[] eb = eout.toByteArray();
401
402             for (int i = 0; i < eb.length; i++) {
403                 assertTrue(eb[i] == db[i]);
404             }
405         }
406     }
407
408     public void testRLE4() {
409         if (UNIT_TEST) {
410             Sys.out.println("testRLE8");
411
412             String JavaDoc c =
413                     "0F 11 00 00" +
414                     "02 11 09 00 04 11 00 00 " +
415                     "04 11 03 00 03 11 02 00 03 11 00 00" +
416                     "04 11 03 00 04 11 02 00 02 11 00 00" +
417                     "04 11 03 00 04 11 02 00 02 11 00 00" +
418                     "04 11 03 00 04 11 02 00 02 11 00 00" +
419                     "04 11 03 00 03 11 02 00 03 11 00 00" +
420                     "04 11 03 00 01 11 03 00 04 11 00 00" +
421                     "04 11 03 00 01 11 03 00 04 11 00 00" +
422                     "04 11 03 00 03 11 02 00 03 11 00 00" +
423                     "04 11 03 00 04 11 02 00 02 11 00 00" +
424                     "04 11 03 00 04 11 02 00 02 11 00 00" +
425                     "04 11 03 00 03 11 03 00 02 11 00 00" +
426                     "02 11 0A 00 03 11 00 00" +
427                     "0F 11 00 00 00 01";
428
429             String JavaDoc d =
430                     "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF " +
431                     "FF FF 00 00 00 00 00 00 00 00 00 FF FF FF FF " +
432                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
433                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
434                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
435                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
436                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
437                     "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " +
438                     "FF FF FF FF 00 00 00 FF 00 00 00 FF FF FF FF " +
439                     "FF FF FF FF 00 00 00 FF FF FF 00 00 FF FF FF " +
440                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
441                     "FF FF FF FF 00 00 00 FF FF FF FF 00 00 FF FF " +
442                     "FF FF FF FF 00 00 00 FF FF FF 00 00 00 FF FF " +
443                     "FF FF 00 00 00 00 00 00 00 00 00 00 FF FF FF " +
444                     "FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF ";
445
446             ByteArrayOutputStream JavaDoc cout = new ByteArrayOutputStream JavaDoc();
447             StringTokenizer JavaDoc cst = new StringTokenizer JavaDoc(c, " ", false);
448             while (cst.hasMoreElements()) {
449                 String JavaDoc s = cst.nextToken();
450                 cout.write(Integer.parseInt(s, 16));
451             }
452             byte[] cb = cout.toByteArray();
453
454             ByteArrayOutputStream JavaDoc dout = new ByteArrayOutputStream JavaDoc();
455             StringTokenizer JavaDoc dst = new StringTokenizer JavaDoc(d, " ", false);
456             while (dst.hasMoreElements()) {
457                 String JavaDoc s = dst.nextToken();
458                 dout.write(Integer.parseInt(s, 16));
459             }
460             byte[] db = dout.toByteArray();
461
462             ByteArrayOutputStream JavaDoc eout = new ByteArrayOutputStream JavaDoc();
463
464             RLEInputStream in = new RLE4InputStream(new BitInputStream(new ByteArrayInputStream JavaDoc(cb)));
465             try {
466                 while (in.available() > 0) {
467                     try {
468                         int a = in.read() & 0xFF;
469                         eout.write(a);
470                     }
471                     catch (RLE4InputStream.EndOfLineException ex) {
472                         continue;
473                     }
474                     catch (RLE4InputStream.EndOfBitmapException ex) {
475                         break;
476                     }
477                 }
478             }
479             catch (IOException JavaDoc ex) {
480                 ex.printStackTrace();
481             }
482             byte[] eb = eout.toByteArray();
483
484             for (int i = 0; i < eb.length; i++) {
485                 assertTrue(eb[i] == db[i]);
486             }
487         }
488     }
489 }
490
Popular Tags