OSDN Git Service

topic/jiocema とマージ
[jindolf/JinParser.git] / src / test / java / jp / sourceforge / jindolf / parser / DecodedContentTest.java
1 /*
2  * License : The MIT License
3  * Copyright(c) 2009 olyutorskii
4  */
5
6 package jp.sourceforge.jindolf.parser;
7
8 import java.util.ArrayList;
9 import java.util.List;
10 import org.junit.After;
11 import org.junit.AfterClass;
12 import org.junit.Before;
13 import org.junit.BeforeClass;
14 import org.junit.Test;
15
16 import static org.junit.Assert.*;
17
18 /**
19  */
20 public class DecodedContentTest {
21
22     public DecodedContentTest() {
23     }
24
25     @BeforeClass
26     public static void setUpClass() throws Exception{
27     }
28
29     @AfterClass
30     public static void tearDownClass() throws Exception{
31     }
32
33     @Before
34     public void setUp() {
35     }
36
37     @After
38     public void tearDown() {
39     }
40
41     /**
42      * Test of Constructor, of class DecodedContent.
43      */
44     @Test
45     public void testConstructor(){
46         System.out.println("Constructor");
47
48         DecodedContent content;
49
50         content = new DecodedContent();
51         assertEquals("", content.toString());
52
53         content = new DecodedContent("abc");
54         assertEquals("abc", content.toString());
55
56         content = new DecodedContent(128);
57         assertEquals("", content.toString());
58
59         content = new DecodedContent(0);
60         assertEquals("", content.toString());
61         content.append("abc");
62         assertEquals("abc", content.toString());
63
64         try{
65             Object o = new DecodedContent(-1);
66             fail();
67         }catch(NegativeArraySizeException e){
68         }catch(Throwable e){
69             fail();
70         }
71
72         return;
73     }
74
75     /**
76      * Test of init method, of class DecodedContent.
77      */
78     @Test
79     public void testInit(){
80         System.out.println("init");
81
82         DecodedContent content;
83
84         content = new DecodedContent();
85         content.append("abc");
86         content.addDecodeError((byte)0xff);
87         content.append("def");
88         assertEquals("abc?def", content.toString());
89         assertEquals(1, content.getDecodeErrorList().size());
90
91         content.init();
92         assertEquals("", content.toString());
93         assertEquals(0, content.getDecodeErrorList().size());
94
95         content.append('X');
96         assertEquals("X", content.toString());
97
98         return;
99     }
100
101     /**
102      * Test of hasDecodeError method, of class DecodedContent.
103      */
104     @Test
105     public void testHasDecodeError(){
106         System.out.println("hasDecodeError");
107
108         DecodedContent content;
109
110         content = new DecodedContent();
111         assertFalse(content.hasDecodeError());
112
113         content.append("a");
114         assertFalse(content.hasDecodeError());
115
116         content.addDecodeError((byte)0xff);
117         assertTrue(content.hasDecodeError());
118
119         content.append("b");
120         assertTrue(content.hasDecodeError());
121
122         content.init();
123         assertFalse(content.hasDecodeError());
124
125         content.append("c");
126         assertFalse(content.hasDecodeError());
127
128         content = new DecodedContent();
129         List<DecodeErrorInfo> list = content.getDecodeErrorList();
130         assertEquals(0, list.size());
131         assertFalse(content.hasDecodeError());
132
133         return;
134     }
135
136     /**
137      * Test of getDecodeErrorList method, of class DecodedContent.
138      */
139     @Test
140     public void testGetDecodeErrorList(){
141         System.out.println("getDecodeErrorList");
142
143         DecodedContent content;
144         List<DecodeErrorInfo> list;
145
146         content = new DecodedContent();
147         list = content.getDecodeErrorList();
148         assertEquals(0, list.size());
149
150         content.append("abc");
151         list = content.getDecodeErrorList();
152         assertEquals(0, list.size());
153
154         content.addDecodeError((byte)0xff);
155         list = content.getDecodeErrorList();
156         assertEquals(1, list.size());
157
158         content.append("def");
159         list = content.getDecodeErrorList();
160         assertEquals(1, list.size());
161
162         content.addDecodeError((byte)0x03, (byte)0x04);
163         list = content.getDecodeErrorList();
164         assertEquals(2, list.size());
165
166         return;
167     }
168
169     /**
170      * Test of getRawContent method, of class DecodedContent.
171      */
172     @Test
173     public void testGetRawContent(){
174         System.out.println("getRawContent");
175
176         DecodedContent content;
177
178         content = new DecodedContent();
179         assertEquals("", content.getRawContent().toString());
180
181         content.append("a");
182         assertEquals("a", content.getRawContent().toString());
183
184         content.addDecodeError((byte)0xff);
185         assertEquals("a?", content.getRawContent().toString());
186
187         content.append("b");
188         assertEquals("a?b", content.getRawContent().toString());
189
190         assertEquals(content.toString(), content.getRawContent().toString());
191
192         return;
193     }
194
195     /**
196      * Test of charAt method, of class DecodedContent.
197      */
198     @Test
199     public void testCharAt(){
200         System.out.println("charAt");
201
202         DecodedContent content;
203
204         content = new DecodedContent();
205         content.append("12345");
206         assertEquals('1', content.charAt(0));
207         assertEquals('3', content.charAt(2));
208         assertEquals('5', content.charAt(4));
209
210         try{
211             content.charAt(-1);
212             fail();
213         }catch(IndexOutOfBoundsException e){
214         }catch(Throwable e){
215             fail();
216         }
217
218         try{
219             content.charAt(5);
220             fail();
221         }catch(IndexOutOfBoundsException e){
222         }catch(Throwable e){
223             fail();
224         }
225
226         return;
227     }
228
229     /**
230      * Test of length method, of class DecodedContent.
231      */
232     @Test
233     public void testLength(){
234         System.out.println("length");
235
236         DecodedContent content;
237
238         content = new DecodedContent();
239         assertEquals(0, content.length());
240
241         content.append("12345");
242         assertEquals(5, content.length());
243
244         content.addDecodeError((byte)0xff);
245         assertEquals(6, content.length());
246
247         content.init();
248         assertEquals(0, content.length());
249
250         return;
251     }
252
253     /**
254      * Test of subSequence method, of class DecodedContent.
255      */
256     @Test
257     public void testSubSequence(){
258         System.out.println("subSequence");
259
260         DecodedContent content;
261
262         content = new DecodedContent();
263
264         content.append("12345");
265         assertEquals("234", content.subSequence(1, 4).toString());
266
267         try{
268             content.subSequence(-1, 4);
269             fail();
270         }catch(IndexOutOfBoundsException e){
271         }catch(Throwable e){
272             fail();
273         }
274
275         try{
276             content.subSequence(1, 6);
277             fail();
278         }catch(IndexOutOfBoundsException e){
279         }catch(Throwable e){
280             fail();
281         }
282
283         try{
284             content.subSequence(4, 1);
285             fail();
286         }catch(IndexOutOfBoundsException e){
287         }catch(Throwable e){
288             fail();
289         }
290
291         return;
292     }
293
294     /**
295      * Test of subContent method, of class DecodedContent.
296      */
297     @Test
298     public void testSubContent(){
299         System.out.println("subContent");
300
301         DecodedContent content;
302
303         content = new DecodedContent();
304
305         content.append("12345");
306         assertEquals("234", content.subContent(1, 4).toString());
307
308         try{
309             content.subContent(-1, 4);
310             fail();
311         }catch(IndexOutOfBoundsException e){
312         }catch(Throwable e){
313             fail();
314         }
315
316         try{
317             content.subContent(1, 6);
318             fail();
319         }catch(IndexOutOfBoundsException e){
320         }catch(Throwable e){
321             fail();
322         }
323
324         try{
325             content.subContent(4, 1);
326             fail();
327         }catch(IndexOutOfBoundsException e){
328         }catch(Throwable e){
329             fail();
330         }
331
332         content = new DecodedContent();
333         content.append("ab");
334         content.addDecodeError((byte)0x01);
335         content.append("de");
336         content = content.subContent(1,4);
337         assertEquals("b?d", content.toString());
338
339         List<DecodeErrorInfo> list = content.getDecodeErrorList();
340         assertEquals(1, list.size());
341         assertEquals((byte)0x01, list.get(0).getRawByte1st());
342
343         return;
344     }
345
346     /**
347      * Test of append method, of class DecodedContent.
348      */
349     @Test
350     public void testAppend_char(){
351         System.out.println("append");
352
353         DecodedContent content;
354
355         content = new DecodedContent();
356         content.append('a');
357         assertEquals("a", content.toString());
358
359         return;
360     }
361
362     /**
363      * Test of append method, of class DecodedContent.
364      */
365     @Test
366     public void testAppend_CharSequence(){
367         System.out.println("append");
368
369         DecodedContent content;
370
371         content = new DecodedContent();
372         CharSequence seq = "abc";
373         content.append(seq);
374         assertEquals("abc", content.toString());
375
376         return;
377     }
378
379     /**
380      * Test of append method, of class DecodedContent.
381      */
382     @Test
383     public void testAppend_3args_1(){
384         System.out.println("append");
385
386         DecodedContent content;
387
388         content = new DecodedContent();
389         content.append("abc");
390         assertEquals("abc", content.toString());
391
392         CharSequence seq = "12345";
393         content.append(seq, 1, 4);
394         assertEquals("abc234", content.toString());
395
396         return;
397     }
398
399     /**
400      * Test of append method, of class DecodedContent.
401      */
402     @Test
403     public void testAppend_3args_2(){
404         System.out.println("append");
405
406         DecodedContent content;
407
408         content = new DecodedContent();
409         content.append("abc");
410
411         DecodedContent other;
412         other = new DecodedContent();
413         other.append("12345");
414
415         content.append(other, 1, 4);
416         assertEquals("abc234", content.toString());
417
418         content = new DecodedContent();
419         content.append("abc");
420
421         other = new DecodedContent();
422         other.addDecodeError((byte)0x01);
423         other.addDecodeError((byte)0x02);
424         other.addDecodeError((byte)0x03);
425         other.addDecodeError((byte)0x04);
426         other.addDecodeError((byte)0x05);
427
428         content.append(other, 1, 4);
429         assertEquals("abc???", content.toString());
430
431         List<DecodeErrorInfo> list = content.getDecodeErrorList();
432         assertEquals(3, list.size());
433
434         DecodeErrorInfo info;
435
436         info = list.get(0);
437         assertEquals(3, info.getCharPosition());
438         assertEquals((byte)0x02, info.getRawByte1st());
439         info = list.get(1);
440         assertEquals(4, info.getCharPosition());
441         assertEquals((byte)0x03, info.getRawByte1st());
442         info = list.get(2);
443         assertEquals(5, info.getCharPosition());
444         assertEquals((byte)0x04, info.getRawByte1st());
445
446         return;
447     }
448
449     /**
450      * Test of append method, of class DecodedContent.
451      */
452     @Test
453     public void testAppend_3args_3(){
454         System.out.println("append");
455
456         DecodedContent content;
457
458         content = new DecodedContent();
459         content.append("abc");
460         assertEquals("abc", content.toString());
461
462         char[] seq = {'1','2','3','4','5',};
463         content.append(seq, 1, 3);
464         assertEquals("abc234", content.toString());
465
466         return;
467     }
468
469     /**
470      * Test of addDecodeError method, of class DecodedContent.
471      */
472     @Test
473     public void testAddDecodeError_byte(){
474         System.out.println("addDecodeError");
475
476         DecodedContent content;
477
478         content = new DecodedContent();
479         content.append("abc");
480         content.addDecodeError((byte)0xfe);
481         content.append("def");
482         content.addDecodeError((byte)0xff);
483
484         assertEquals("abc?def?", content.toString());
485         List<DecodeErrorInfo> list = content.getDecodeErrorList();
486         assertEquals(2, list.size());
487
488         DecodeErrorInfo info;
489
490         info = list.get(0);
491         assertEquals(3, list.get(0).getCharPosition());
492         assertFalse(info.has2nd());
493         assertEquals((byte)0xfe, info.getRawByte1st());
494
495         info = list.get(1);
496         assertEquals(7, info.getCharPosition());
497         assertFalse(info.has2nd());
498         assertEquals((byte)0xff, info.getRawByte1st());
499
500         return;
501     }
502
503     /**
504      * Test of addDecodeError method, of class DecodedContent.
505      */
506     @Test
507     public void testAddDecodeError_byte_byte(){
508         System.out.println("addDecodeError");
509
510         DecodedContent content;
511
512         content = new DecodedContent();
513         content.append("abc");
514         content.addDecodeError((byte)0x01, (byte)0x02);
515         content.append("def");
516         content.addDecodeError((byte)0xfe, (byte)0xff);
517
518         assertEquals("abc?def?", content.toString());
519         List<DecodeErrorInfo> list = content.getDecodeErrorList();
520         assertEquals(2, list.size());
521
522         DecodeErrorInfo info;
523
524         info = list.get(0);
525         assertEquals(3, list.get(0).getCharPosition());
526         assertTrue(info.has2nd());
527         assertEquals((byte)0x01, info.getRawByte1st());
528         assertEquals((byte)0x02, info.getRawByte2nd());
529
530         info = list.get(1);
531         assertEquals(7, info.getCharPosition());
532         assertTrue(info.has2nd());
533         assertEquals((byte)0xfe, info.getRawByte1st());
534         assertEquals((byte)0xff, info.getRawByte2nd());
535
536         return;
537     }
538
539     /**
540      * Test of toString method, of class DecodedContent.
541      */
542     @Test
543     public void testToString(){
544         System.out.println("toString");
545
546         DecodedContent content;
547
548         content = new DecodedContent();
549         content.append("abc");
550         content.addDecodeError((byte)0x01, (byte)0x02);
551         content.append("def");
552         content.addDecodeError((byte)0xfe, (byte)0xff);
553
554         assertEquals("abc?def?", content.toString());
555         assertEquals(content.getRawContent().toString(), content.toString());
556
557         return;
558     }
559
560     /**
561      * Test of lsearchErrorIndex method, of class DecodedContent.
562      */
563     @Test
564     public void testLsearchErrorIndex(){
565         System.out.println("lsearchErrorIndex");
566
567         List<DecodeErrorInfo> errList;
568         int result;
569
570         errList = new ArrayList<>();
571         result = DecodedContent.lsearchErrorIndex(errList, 10);
572         assertEquals(0, result);
573
574         errList.clear();
575         errList.add(new DecodeErrorInfo(5, (byte)0x00));
576         result = DecodedContent.lsearchErrorIndex(errList, 10);
577         assertEquals(1, result);
578
579         errList.clear();
580         errList.add(new DecodeErrorInfo(10, (byte)0x00));
581         result = DecodedContent.lsearchErrorIndex(errList, 10);
582         assertEquals(0, result);
583
584         errList.clear();
585         errList.add(new DecodeErrorInfo(15, (byte)0x00));
586         result = DecodedContent.lsearchErrorIndex(errList, 10);
587         assertEquals(0, result);
588
589         errList.clear();
590         errList.add(new DecodeErrorInfo(4, (byte)0x00));
591         errList.add(new DecodeErrorInfo(5, (byte)0x00));
592         errList.add(new DecodeErrorInfo(14, (byte)0x00));
593         errList.add(new DecodeErrorInfo(15, (byte)0x00));
594         result = DecodedContent.lsearchErrorIndex(errList, 10);
595         assertEquals(2, result);
596
597         errList.clear();
598         errList.add(new DecodeErrorInfo(4, (byte)0x00));
599         errList.add(new DecodeErrorInfo(5, (byte)0x00));
600         errList.add(new DecodeErrorInfo(10, (byte)0x00));
601         errList.add(new DecodeErrorInfo(14, (byte)0x00));
602         errList.add(new DecodeErrorInfo(15, (byte)0x00));
603         result = DecodedContent.lsearchErrorIndex(errList, 10);
604         assertEquals(2, result);
605
606         return;
607      }
608
609     /**
610      * Test of bsearchErrorIndex method, of class DecodedContent.
611      */
612     @Test
613     public void testBsearchErrorIndex(){
614         System.out.println("bsearchErrorIndex");
615
616         List<DecodeErrorInfo> errList;
617         int result;
618
619         errList = new ArrayList<>();
620         result = DecodedContent.bsearchErrorIndex(errList, 10);
621         assertEquals(0, result);
622
623         errList.clear();
624         errList.add(new DecodeErrorInfo(5, (byte)0x00));
625         result = DecodedContent.bsearchErrorIndex(errList, 10);
626         assertEquals(1, result);
627
628         errList.clear();
629         errList.add(new DecodeErrorInfo(10, (byte)0x00));
630         result = DecodedContent.bsearchErrorIndex(errList, 10);
631         assertEquals(0, result);
632
633         errList.clear();
634         errList.add(new DecodeErrorInfo(15, (byte)0x00));
635         result = DecodedContent.bsearchErrorIndex(errList, 10);
636         assertEquals(0, result);
637
638         errList.clear();
639         errList.add(new DecodeErrorInfo(4, (byte)0x00));
640         errList.add(new DecodeErrorInfo(5, (byte)0x00));
641         errList.add(new DecodeErrorInfo(14, (byte)0x00));
642         errList.add(new DecodeErrorInfo(15, (byte)0x00));
643         result = DecodedContent.bsearchErrorIndex(errList, 10);
644         assertEquals(2, result);
645
646         errList.clear();
647         errList.add(new DecodeErrorInfo(4, (byte)0x00));
648         errList.add(new DecodeErrorInfo(5, (byte)0x00));
649         errList.add(new DecodeErrorInfo(10, (byte)0x00));
650         errList.add(new DecodeErrorInfo(14, (byte)0x00));
651         errList.add(new DecodeErrorInfo(15, (byte)0x00));
652         result = DecodedContent.bsearchErrorIndex(errList, 10);
653         assertEquals(2, result);
654         result = DecodedContent.bsearchErrorIndex(errList, 9);
655         assertEquals(2, result);
656         result = DecodedContent.bsearchErrorIndex(errList, 11);
657         assertEquals(3, result);
658
659         return;
660     }
661
662     /**
663      * Test of searchErrorIndex method, of class DecodedContent.
664      */
665     @Test
666     public void testSearchErrorIndex(){
667         System.out.println("searchErrorIndex");
668
669         List<DecodeErrorInfo> errList;
670         int result;
671
672         errList = new ArrayList<>();
673
674         errList.clear();
675         for(int pos = 0; pos <= 1000; pos += 10){
676             errList.add(new DecodeErrorInfo(pos, (byte)0x00));
677         }
678         result = DecodedContent.searchErrorIndex(errList, 503);
679         assertEquals(51, result);
680
681         errList.clear();
682         for(int pos = 0; pos <= 50; pos += 10){
683             errList.add(new DecodeErrorInfo(pos, (byte)0x00));
684         }
685         result = DecodedContent.searchErrorIndex(errList, 23);
686         assertEquals(3, result);
687
688         return;
689     }
690
691     /**
692      * Test of appendGappedErrorInfo method, of class DecodedContent.
693      */
694     @Test
695     public void testAppendGappedErrorInfo(){
696         System.out.println("appendGappedErrorInfo");
697
698         DecodedContent sourceContent;
699         sourceContent = new DecodedContent();
700         for(int pos = 0; pos <= 50; pos += 10){
701             sourceContent.append("123456789");
702             sourceContent.addDecodeError((byte)0x00);
703         }
704
705         List<DecodeErrorInfo> result;
706         result = DecodedContent.appendGappedErrorInfo(sourceContent, 15, 35, null, -100);
707         assertNotNull(result);
708         assertEquals(2, result.size());
709         assertEquals(119, result.get(0).getCharPosition());
710         assertEquals(129, result.get(1).getCharPosition());
711
712         return;
713     }
714
715     /**
716      * Test of ensureCapacity method, of class DecodedContent.
717      */
718     @Test
719     public void testEnsureCapacity(){
720         System.out.println("ensureCapacity");
721
722         DecodedContent content;
723
724         content = new DecodedContent("abc");
725         content.ensureCapacity(-1);
726         content.ensureCapacity(0);
727         content.ensureCapacity(1);
728         content.ensureCapacity(5);
729         content.append("def");
730         assertEquals("abcdef", content.toString());
731
732         content = new DecodedContent();
733         content.ensureCapacity(5);
734         content.append("abc");
735         assertEquals("abc", content.toString());
736
737         return;
738     }
739
740     /**
741      * Test of setCharAt method, of class DecodedContent.
742      */
743     @Test
744     public void testSetCharAt(){
745         System.out.println("setCharAt");
746
747         DecodedContent content;
748
749         content = new DecodedContent("abc");
750         content.setCharAt(1, 'B');
751         assertEquals("aBc", content.toString());
752
753         content = new DecodedContent("a");
754         content.addDecodeError((byte)0xff);
755         content.append('c');
756         assertEquals("a?c", content.toString());
757         content.setCharAt(1, 'B');
758         assertEquals("aBc", content.toString());
759         assertEquals(1, content.getDecodeErrorList().size());
760         assertEquals(1, content.getDecodeErrorList().get(0).getCharPosition());
761         assertEquals((byte)0xff, content.getDecodeErrorList().get(0).getRawByte1st());
762
763         content = new DecodedContent("abc");
764         try{
765             content.setCharAt(-1, 'B');
766             fail();
767         }catch(IndexOutOfBoundsException e){
768             // NOTHING
769         }
770         try{
771             content.setCharAt(10, 'B');
772             fail();
773         }catch(IndexOutOfBoundsException e){
774             // NOTHING
775         }
776
777         return;
778     }
779
780 }