OSDN Git Service

7babd0744e0dea9317e8393d5660c106cbf68219
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / binio / BinaryExporterTest.java
1 /*
2  */
3
4 package jp.sourceforge.mikutoga.binio;
5
6 import java.io.ByteArrayOutputStream;
7 import java.io.FilterOutputStream;
8 import java.io.IOException;
9 import org.junit.After;
10 import org.junit.AfterClass;
11 import org.junit.Before;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import static org.junit.Assert.*;
15
16 /**
17  *
18  */
19 public class BinaryExporterTest {
20
21     public BinaryExporterTest() {
22     }
23
24     @BeforeClass
25     public static void setUpClass() {
26     }
27
28     @AfterClass
29     public static void tearDownClass() {
30     }
31
32     @Before
33     public void setUp() {
34     }
35
36     @After
37     public void tearDown() {
38     }
39
40     /**
41      * Test of close method, of class BinaryExporter.
42      */
43     @Test
44     public void testClose() throws Exception {
45         System.out.println("close");
46
47         TestOutputStream os = new TestOutputStream();
48         BinaryExporter bex;
49         bex = new BinaryExporter(os);
50
51         assertFalse(os.closed);
52         bex.close();
53         assertTrue(os.closed);
54
55         return;
56     }
57
58     /**
59      * Test of flush method, of class BinaryExporter.
60      */
61     @Test
62     public void testFlush() throws Exception {
63         System.out.println("flush");
64
65         TestOutputStream os = new TestOutputStream();
66         BinaryExporter bex;
67         bex = new BinaryExporter(os);
68
69         assertFalse(os.flushed);
70         bex.flush();
71         assertTrue(os.flushed);
72
73         return;
74     }
75
76     /**
77      * Test of dumpByte method, of class BinaryExporter.
78      */
79     @Test
80     public void testDumpByte_byte() throws Exception {
81         System.out.println("dumpByte");
82
83         ByteArrayOutputStream bos;
84         BinaryExporter bex;
85         byte[] barr;
86
87         bos = new ByteArrayOutputStream();
88         bex = new BinaryExporter(bos);
89
90         bex.dumpByte((byte)10);
91         bex.flush();
92
93         barr = bos.toByteArray();
94         assertEquals(1, barr.length);
95         assertEquals((byte)10, barr[0]);
96
97         return;
98     }
99
100     /**
101      * Test of dumpByte method, of class BinaryExporter.
102      */
103     @Test
104     public void testDumpByte_int() throws Exception {
105         System.out.println("dumpByte");
106
107         ByteArrayOutputStream bos;
108         BinaryExporter bex;
109         byte[] barr;
110
111         bos = new ByteArrayOutputStream();
112         bex = new BinaryExporter(bos);
113
114         bex.dumpByte(10);
115         bex.flush();
116         barr = bos.toByteArray();
117         assertEquals(1, barr.length);
118         assertEquals((byte)10, barr[0]);
119
120         bos.reset();
121         bex.dumpByte(257);
122         bex.flush();
123         barr = bos.toByteArray();
124         assertEquals(1, barr.length);
125         assertEquals((byte)1, barr[0]);
126
127
128         return;
129     }
130
131     /**
132      * Test of dumpByteArray method, of class BinaryExporter.
133      */
134     @Test
135     public void testDumpByteArray_byteArr() throws Exception {
136         System.out.println("dumpByteArray");
137
138         ByteArrayOutputStream bos;
139         BinaryExporter bex;
140         byte[] barr;
141
142         bos = new ByteArrayOutputStream();
143         bex = new BinaryExporter(bos);
144
145         bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03});
146         bex.flush();
147
148         barr = bos.toByteArray();
149         assertEquals(3, barr.length);
150         assertEquals((byte)0x01, barr[0]);
151         assertEquals((byte)0x02, barr[1]);
152         assertEquals((byte)0x03, barr[2]);
153
154         return;
155     }
156
157     /**
158      * Test of dumpByteArray method, of class BinaryExporter.
159      */
160     @Test
161     public void testDumpByteArray_3args() throws Exception {
162         System.out.println("dumpByteArray");
163
164         ByteArrayOutputStream bos;
165         BinaryExporter bex;
166         byte[] barr;
167
168         bos = new ByteArrayOutputStream();
169         bex = new BinaryExporter(bos);
170
171         bex.dumpByteArray(new byte[]{0x01, 0x02, 0x03, 0x04, 0x05},
172                           1, 3);
173         bex.flush();
174
175         barr = bos.toByteArray();
176         assertEquals(3, barr.length);
177         assertEquals((byte)0x02, barr[0]);
178         assertEquals((byte)0x03, barr[1]);
179         assertEquals((byte)0x04, barr[2]);
180
181         return;
182     }
183
184     /**
185      * Test of dumpLeShort method, of class BinaryExporter.
186      */
187     @Test
188     public void testDumpLeShort_short() throws Exception {
189         System.out.println("dumpLeShort");
190
191         ByteArrayOutputStream bos;
192         BinaryExporter bex;
193         byte[] barr;
194
195         bos = new ByteArrayOutputStream();
196         bex = new BinaryExporter(bos);
197
198         bos.reset();
199         bex.dumpLeShort((short)( 256 * 2 + 1));
200         bex.flush();
201         barr = bos.toByteArray();
202         assertEquals(2, barr.length);
203         assertEquals((byte)1, barr[0]);
204         assertEquals((byte)2, barr[1]);
205
206         bos.reset();
207         bex.dumpLeShort((short)-2);
208         bex.flush();
209         barr = bos.toByteArray();
210         assertEquals(2, barr.length);
211         assertEquals((byte)0xfe, barr[0]);
212         assertEquals((byte)0xff, barr[1]);
213
214         return;
215     }
216
217     /**
218      * Test of dumpLeShort method, of class BinaryExporter.
219      */
220     @Test
221     public void testDumpLeShort_int() throws Exception {
222         System.out.println("dumpLeShort");
223
224         ByteArrayOutputStream bos;
225         BinaryExporter bex;
226         byte[] barr;
227
228         bos = new ByteArrayOutputStream();
229         bex = new BinaryExporter(bos);
230
231         bos.reset();
232         bex.dumpLeShort(256 * 2 + 1);
233         bex.flush();
234         barr = bos.toByteArray();
235         assertEquals(2, barr.length);
236         assertEquals((byte)1, barr[0]);
237         assertEquals((byte)2, barr[1]);
238
239         bos.reset();
240         bex.dumpLeShort(0xff1234);
241         bex.flush();
242         barr = bos.toByteArray();
243         assertEquals(2, barr.length);
244         assertEquals((byte)0x34, barr[0]);
245         assertEquals((byte)0x12, barr[1]);
246
247         bos.reset();
248         bex.dumpLeShort(-2);
249         bex.flush();
250         barr = bos.toByteArray();
251         assertEquals(2, barr.length);
252         assertEquals((byte)0xfe, barr[0]);
253         assertEquals((byte)0xff, barr[1]);
254
255         return;
256     }
257
258     /**
259      * Test of dumpLeInt method, of class BinaryExporter.
260      */
261     @Test
262     public void testDumpLeInt() throws Exception {
263         System.out.println("dumpLeInt");
264
265         ByteArrayOutputStream bos;
266         BinaryExporter bex;
267         byte[] barr;
268
269         bos = new ByteArrayOutputStream();
270         bex = new BinaryExporter(bos);
271
272         bos.reset();
273         bex.dumpLeInt(0x12345678);
274         bex.flush();
275         barr = bos.toByteArray();
276         assertEquals(4, barr.length);
277         assertEquals((byte)0x78, barr[0]);
278         assertEquals((byte)0x56, barr[1]);
279         assertEquals((byte)0x34, barr[2]);
280         assertEquals((byte)0x12, barr[3]);
281
282         bos.reset();
283         bex.dumpLeInt(-2);
284         bex.flush();
285         barr = bos.toByteArray();
286         assertEquals(4, barr.length);
287         assertEquals((byte)0xfe, barr[0]);
288         assertEquals((byte)0xff, barr[1]);
289         assertEquals((byte)0xff, barr[2]);
290         assertEquals((byte)0xff, barr[3]);
291
292         return;
293     }
294
295     /**
296      * Test of dumpLeLong method, of class BinaryExporter.
297      */
298     @Test
299     public void testDumpLeLong() throws Exception {
300         System.out.println("dumpLeLong");
301
302         ByteArrayOutputStream bos;
303         BinaryExporter bex;
304         byte[] barr;
305
306         bos = new ByteArrayOutputStream();
307         bex = new BinaryExporter(bos);
308
309         bos.reset();
310         bex.dumpLeLong(0x12345678abcdef00L);
311         bex.flush();
312         barr = bos.toByteArray();
313         assertEquals(8, barr.length);
314         assertEquals((byte)0x00, barr[0]);
315         assertEquals((byte)0xef, barr[1]);
316         assertEquals((byte)0xcd, barr[2]);
317         assertEquals((byte)0xab, barr[3]);
318         assertEquals((byte)0x78, barr[4]);
319         assertEquals((byte)0x56, barr[5]);
320         assertEquals((byte)0x34, barr[6]);
321         assertEquals((byte)0x12, barr[7]);
322
323         bos.reset();
324         bex.dumpLeLong(-2L);
325         bex.flush();
326         barr = bos.toByteArray();
327         assertEquals(8, barr.length);
328         assertEquals((byte)0xfe, barr[0]);
329         assertEquals((byte)0xff, barr[1]);
330         assertEquals((byte)0xff, barr[2]);
331         assertEquals((byte)0xff, barr[3]);
332         assertEquals((byte)0xff, barr[4]);
333         assertEquals((byte)0xff, barr[5]);
334         assertEquals((byte)0xff, barr[6]);
335         assertEquals((byte)0xff, barr[7]);
336
337         return;
338     }
339
340     /**
341      * Test of dumpLeFloat method, of class BinaryExporter.
342      */
343     @Test
344     public void testDumpLeFloat() throws Exception {
345         System.out.println("dumpLeFloat");
346
347         ByteArrayOutputStream bos;
348         BinaryExporter bex;
349         byte[] barr;
350
351         bos = new ByteArrayOutputStream();
352         bex = new BinaryExporter(bos);
353
354         bos.reset();
355         bex.dumpLeFloat(Float.intBitsToFloat(0x12345678));
356         bex.flush();
357         barr = bos.toByteArray();
358         assertEquals(4, barr.length);
359         assertEquals((byte)0x78, barr[0]);
360         assertEquals((byte)0x56, barr[1]);
361         assertEquals((byte)0x34, barr[2]);
362         assertEquals((byte)0x12, barr[3]);
363
364         bos.reset();
365         bex.dumpLeFloat(-1.5f);
366         bex.flush();
367         barr = bos.toByteArray();
368         assertEquals(4, barr.length);
369         assertEquals((byte)0x00, barr[0]);
370         assertEquals((byte)0x00, barr[1]);
371         assertEquals((byte)0xc0, barr[2]);
372         assertEquals((byte)0xbf, barr[3]);
373
374         return;
375     }
376
377     /**
378      * Test of dumpLeDouble method, of class BinaryExporter.
379      */
380     @Test
381     public void testDumpLeDouble() throws Exception {
382         System.out.println("dumpLeDouble");
383
384         ByteArrayOutputStream bos;
385         BinaryExporter bex;
386         byte[] barr;
387
388         bos = new ByteArrayOutputStream();
389         bex = new BinaryExporter(bos);
390
391         bos.reset();
392         bex.dumpLeDouble(Double.longBitsToDouble(0x12345678abcdef00L));
393         bex.flush();
394         barr = bos.toByteArray();
395         assertEquals(8, barr.length);
396         assertEquals((byte)0x00, barr[0]);
397         assertEquals((byte)0xef, barr[1]);
398         assertEquals((byte)0xcd, barr[2]);
399         assertEquals((byte)0xab, barr[3]);
400         assertEquals((byte)0x78, barr[4]);
401         assertEquals((byte)0x56, barr[5]);
402         assertEquals((byte)0x34, barr[6]);
403         assertEquals((byte)0x12, barr[7]);
404
405         bos.reset();
406         bex.dumpLeDouble(-1.5);
407         bex.flush();
408         barr = bos.toByteArray();
409         assertEquals(8, barr.length);
410         assertEquals((byte)0x00, barr[0]);
411         assertEquals((byte)0x00, barr[1]);
412         assertEquals((byte)0x00, barr[2]);
413         assertEquals((byte)0x00, barr[3]);
414         assertEquals((byte)0x00, barr[4]);
415         assertEquals((byte)0x00, barr[5]);
416         assertEquals((byte)0xf8, barr[6]);
417         assertEquals((byte)0xbf, barr[7]);
418
419         return;
420     }
421
422     /**
423      * Test of dumpFiller method, of class BinaryExporter.
424      */
425     @Test
426     public void testDumpFiller() throws Exception {
427         System.out.println("dumpFiller");
428
429         ByteArrayOutputStream bos;
430         BinaryExporter bex;
431         byte[] barr;
432
433         bos = new ByteArrayOutputStream();
434         bex = new BinaryExporter(bos);
435
436         bex.dumpFiller(new byte[]{}, 1);
437         bex.flush();
438         barr = bos.toByteArray();
439         assertEquals(0, barr.length);
440
441         bos.reset();
442         bex.dumpFiller(new byte[]{0x01}, 3);
443         bex.flush();
444         barr = bos.toByteArray();
445         assertEquals(3, barr.length);
446         assertEquals((byte)0x01, barr[0]);
447         assertEquals((byte)0x01, barr[1]);
448         assertEquals((byte)0x01, barr[2]);
449
450         bos.reset();
451         bex.dumpFiller(new byte[]{0x01, 0x02}, 3);
452         bex.flush();
453         barr = bos.toByteArray();
454         assertEquals(3, barr.length);
455         assertEquals((byte)0x01, barr[0]);
456         assertEquals((byte)0x02, barr[1]);
457         assertEquals((byte)0x02, barr[2]);
458
459         bos.reset();
460         bex.dumpFiller(new byte[]{0x01, 0x02, 0x03}, 2);
461         bex.flush();
462         barr = bos.toByteArray();
463         assertEquals(2, barr.length);
464         assertEquals((byte)0x01, barr[0]);
465         assertEquals((byte)0x02, barr[1]);
466
467         return;
468     }
469
470     /**
471      * Test of dumpFixedW31j method, of class BinaryExporter.
472      */
473     @Test
474     public void testDumpFixedW31j() throws Exception {
475         System.out.println("dumpFixedW31j");
476
477         ByteArrayOutputStream bos;
478         BinaryExporter bex;
479         byte[] barr;
480
481         bos = new ByteArrayOutputStream();
482         bex = new BinaryExporter(bos);
483
484         bos.reset();
485         bex.dumpFixedW31j("あい", 7, new byte[]{0x01, 0x02});
486         bex.flush();
487         barr = bos.toByteArray();
488         assertEquals(7, barr.length);
489         assertEquals((byte)0x82, barr[0]);
490         assertEquals((byte)0xA0, barr[1]);
491         assertEquals((byte)0x82, barr[2]);
492         assertEquals((byte)0xA2, barr[3]);
493         assertEquals((byte)0x01, barr[4]);
494         assertEquals((byte)0x02, barr[5]);
495         assertEquals((byte)0x02, barr[6]);
496
497         bos.reset();
498         try{
499             bex.dumpFixedW31j("あい", 3, new byte[]{0x00});
500             fail();
501         }catch(IllegalTextExportException e){
502             // GOOD
503         }
504
505         bos.reset();
506         try{
507             bex.dumpFixedW31j("¤", 10, new byte[]{0x00});
508             fail();
509         }catch(IllegalTextExportException e){
510             // GOOD
511         }
512
513         return;
514     }
515
516     /**
517      * Test of dumpHollerithUtf16LE method, of class BinaryExporter.
518      */
519     @Test
520     public void testDumpHollerithUtf16LE() throws Exception {
521         System.out.println("dumpHollerithUtf16LE");
522
523         ByteArrayOutputStream bos;
524         BinaryExporter bex;
525         byte[] barr;
526
527         bos = new ByteArrayOutputStream();
528         bex = new BinaryExporter(bos);
529
530         bex.dumpHollerithUtf16LE("あい");
531         bex.flush();
532         barr = bos.toByteArray();
533         assertEquals(8, barr.length);
534         assertEquals((byte)0x04, barr[0]);
535         assertEquals((byte)0x00, barr[1]);
536         assertEquals((byte)0x00, barr[2]);
537         assertEquals((byte)0x00, barr[3]);
538         assertEquals((byte)0x42, barr[4]);
539         assertEquals((byte)0x30, barr[5]);
540         assertEquals((byte)0x44, barr[6]);
541         assertEquals((byte)0x30, barr[7]);
542
543         bos.reset();
544         bex.dumpHollerithUtf16LE("");
545         bex.flush();
546         barr = bos.toByteArray();
547         assertEquals(4, barr.length);
548         assertEquals((byte)0x00, barr[0]);
549         assertEquals((byte)0x00, barr[1]);
550         assertEquals((byte)0x00, barr[2]);
551         assertEquals((byte)0x00, barr[3]);
552
553         return;
554     }
555
556     private static class TestOutputStream extends FilterOutputStream{
557         public boolean closed = false;
558         public boolean flushed = false;
559
560         TestOutputStream(){
561             super(new ByteArrayOutputStream());
562             return;
563         }
564
565         @Override
566         public void flush() throws IOException {
567             super.flush();
568             this.flushed = true;
569             return;
570         }
571
572         @Override
573         public void close() throws IOException {
574             super.close();
575             this.closed = true;
576             return;
577         }
578
579     }
580
581 }