2 * License : The MIT License
3 * Copyright(c) 2009 olyutorskii
6 package jp.sourceforge.jindolf.parser;
8 import java.util.ArrayList;
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;
16 import static org.junit.Assert.*;
20 public class DecodedContentTest {
22 public DecodedContentTest() {
26 public static void setUpClass() throws Exception{
30 public static void tearDownClass() throws Exception{
38 public void tearDown() {
42 * Test of Constructor, of class DecodedContent.
45 public void testConstructor(){
46 System.out.println("Constructor");
48 DecodedContent content;
50 content = new DecodedContent();
51 assertEquals("", content.toString());
53 content = new DecodedContent("abc");
54 assertEquals("abc", content.toString());
56 content = new DecodedContent(128);
57 assertEquals("", content.toString());
59 content = new DecodedContent(0);
60 assertEquals("", content.toString());
61 content.append("abc");
62 assertEquals("abc", content.toString());
65 Object o = new DecodedContent(-1);
67 }catch(NegativeArraySizeException e){
76 * Test of init method, of class DecodedContent.
79 public void testInit(){
80 System.out.println("init");
82 DecodedContent content;
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());
92 assertEquals("", content.toString());
93 assertEquals(0, content.getDecodeErrorList().size());
96 assertEquals("X", content.toString());
102 * Test of hasDecodeError method, of class DecodedContent.
105 public void testHasDecodeError(){
106 System.out.println("hasDecodeError");
108 DecodedContent content;
110 content = new DecodedContent();
111 assertFalse(content.hasDecodeError());
114 assertFalse(content.hasDecodeError());
116 content.addDecodeError((byte)0xff);
117 assertTrue(content.hasDecodeError());
120 assertTrue(content.hasDecodeError());
123 assertFalse(content.hasDecodeError());
126 assertFalse(content.hasDecodeError());
128 content = new DecodedContent();
129 List<DecodeErrorInfo> list = content.getDecodeErrorList();
130 assertEquals(0, list.size());
131 assertFalse(content.hasDecodeError());
137 * Test of getDecodeErrorList method, of class DecodedContent.
140 public void testGetDecodeErrorList(){
141 System.out.println("getDecodeErrorList");
143 DecodedContent content;
144 List<DecodeErrorInfo> list;
146 content = new DecodedContent();
147 list = content.getDecodeErrorList();
148 assertEquals(0, list.size());
150 content.append("abc");
151 list = content.getDecodeErrorList();
152 assertEquals(0, list.size());
154 content.addDecodeError((byte)0xff);
155 list = content.getDecodeErrorList();
156 assertEquals(1, list.size());
158 content.append("def");
159 list = content.getDecodeErrorList();
160 assertEquals(1, list.size());
162 content.addDecodeError((byte)0x03, (byte)0x04);
163 list = content.getDecodeErrorList();
164 assertEquals(2, list.size());
170 * Test of getRawContent method, of class DecodedContent.
173 public void testGetRawContent(){
174 System.out.println("getRawContent");
176 DecodedContent content;
178 content = new DecodedContent();
179 assertEquals("", content.getRawContent().toString());
182 assertEquals("a", content.getRawContent().toString());
184 content.addDecodeError((byte)0xff);
185 assertEquals("a?", content.getRawContent().toString());
188 assertEquals("a?b", content.getRawContent().toString());
190 assertEquals(content.toString(), content.getRawContent().toString());
196 * Test of charAt method, of class DecodedContent.
199 public void testCharAt(){
200 System.out.println("charAt");
202 DecodedContent content;
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));
213 }catch(IndexOutOfBoundsException e){
221 }catch(IndexOutOfBoundsException e){
230 * Test of length method, of class DecodedContent.
233 public void testLength(){
234 System.out.println("length");
236 DecodedContent content;
238 content = new DecodedContent();
239 assertEquals(0, content.length());
241 content.append("12345");
242 assertEquals(5, content.length());
244 content.addDecodeError((byte)0xff);
245 assertEquals(6, content.length());
248 assertEquals(0, content.length());
254 * Test of subSequence method, of class DecodedContent.
257 public void testSubSequence(){
258 System.out.println("subSequence");
260 DecodedContent content;
262 content = new DecodedContent();
264 content.append("12345");
265 assertEquals("234", content.subSequence(1, 4).toString());
268 content.subSequence(-1, 4);
270 }catch(IndexOutOfBoundsException e){
276 content.subSequence(1, 6);
278 }catch(IndexOutOfBoundsException e){
284 content.subSequence(4, 1);
286 }catch(IndexOutOfBoundsException e){
295 * Test of subContent method, of class DecodedContent.
298 public void testSubContent(){
299 System.out.println("subContent");
301 DecodedContent content;
303 content = new DecodedContent();
305 content.append("12345");
306 assertEquals("234", content.subContent(1, 4).toString());
309 content.subContent(-1, 4);
311 }catch(IndexOutOfBoundsException e){
317 content.subContent(1, 6);
319 }catch(IndexOutOfBoundsException e){
325 content.subContent(4, 1);
327 }catch(IndexOutOfBoundsException e){
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());
339 List<DecodeErrorInfo> list = content.getDecodeErrorList();
340 assertEquals(1, list.size());
341 assertEquals((byte)0x01, list.get(0).getRawByte1st());
347 * Test of append method, of class DecodedContent.
350 public void testAppend_char(){
351 System.out.println("append");
353 DecodedContent content;
355 content = new DecodedContent();
357 assertEquals("a", content.toString());
363 * Test of append method, of class DecodedContent.
366 public void testAppend_CharSequence(){
367 System.out.println("append");
369 DecodedContent content;
371 content = new DecodedContent();
372 CharSequence seq = "abc";
374 assertEquals("abc", content.toString());
380 * Test of append method, of class DecodedContent.
383 public void testAppend_3args_1(){
384 System.out.println("append");
386 DecodedContent content;
388 content = new DecodedContent();
389 content.append("abc");
390 assertEquals("abc", content.toString());
392 CharSequence seq = "12345";
393 content.append(seq, 1, 4);
394 assertEquals("abc234", content.toString());
400 * Test of append method, of class DecodedContent.
403 public void testAppend_3args_2(){
404 System.out.println("append");
406 DecodedContent content;
408 content = new DecodedContent();
409 content.append("abc");
411 DecodedContent other;
412 other = new DecodedContent();
413 other.append("12345");
415 content.append(other, 1, 4);
416 assertEquals("abc234", content.toString());
418 content = new DecodedContent();
419 content.append("abc");
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);
428 content.append(other, 1, 4);
429 assertEquals("abc???", content.toString());
431 List<DecodeErrorInfo> list = content.getDecodeErrorList();
432 assertEquals(3, list.size());
434 DecodeErrorInfo info;
437 assertEquals(3, info.getCharPosition());
438 assertEquals((byte)0x02, info.getRawByte1st());
440 assertEquals(4, info.getCharPosition());
441 assertEquals((byte)0x03, info.getRawByte1st());
443 assertEquals(5, info.getCharPosition());
444 assertEquals((byte)0x04, info.getRawByte1st());
450 * Test of append method, of class DecodedContent.
453 public void testAppend_3args_3(){
454 System.out.println("append");
456 DecodedContent content;
458 content = new DecodedContent();
459 content.append("abc");
460 assertEquals("abc", content.toString());
462 char[] seq = {'1','2','3','4','5',};
463 content.append(seq, 1, 3);
464 assertEquals("abc234", content.toString());
470 * Test of addDecodeError method, of class DecodedContent.
473 public void testAddDecodeError_byte(){
474 System.out.println("addDecodeError");
476 DecodedContent content;
478 content = new DecodedContent();
479 content.append("abc");
480 content.addDecodeError((byte)0xfe);
481 content.append("def");
482 content.addDecodeError((byte)0xff);
484 assertEquals("abc?def?", content.toString());
485 List<DecodeErrorInfo> list = content.getDecodeErrorList();
486 assertEquals(2, list.size());
488 DecodeErrorInfo info;
491 assertEquals(3, list.get(0).getCharPosition());
492 assertFalse(info.has2nd());
493 assertEquals((byte)0xfe, info.getRawByte1st());
496 assertEquals(7, info.getCharPosition());
497 assertFalse(info.has2nd());
498 assertEquals((byte)0xff, info.getRawByte1st());
504 * Test of addDecodeError method, of class DecodedContent.
507 public void testAddDecodeError_byte_byte(){
508 System.out.println("addDecodeError");
510 DecodedContent content;
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);
518 assertEquals("abc?def?", content.toString());
519 List<DecodeErrorInfo> list = content.getDecodeErrorList();
520 assertEquals(2, list.size());
522 DecodeErrorInfo info;
525 assertEquals(3, list.get(0).getCharPosition());
526 assertTrue(info.has2nd());
527 assertEquals((byte)0x01, info.getRawByte1st());
528 assertEquals((byte)0x02, info.getRawByte2nd());
531 assertEquals(7, info.getCharPosition());
532 assertTrue(info.has2nd());
533 assertEquals((byte)0xfe, info.getRawByte1st());
534 assertEquals((byte)0xff, info.getRawByte2nd());
540 * Test of toString method, of class DecodedContent.
543 public void testToString(){
544 System.out.println("toString");
546 DecodedContent content;
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);
554 assertEquals("abc?def?", content.toString());
555 assertEquals(content.getRawContent().toString(), content.toString());
561 * Test of lsearchErrorIndex method, of class DecodedContent.
564 public void testLsearchErrorIndex(){
565 System.out.println("lsearchErrorIndex");
567 List<DecodeErrorInfo> errList;
570 errList = new ArrayList<>();
571 result = DecodedContent.lsearchErrorIndex(errList, 10);
572 assertEquals(0, result);
575 errList.add(new DecodeErrorInfo(5, (byte)0x00));
576 result = DecodedContent.lsearchErrorIndex(errList, 10);
577 assertEquals(1, result);
580 errList.add(new DecodeErrorInfo(10, (byte)0x00));
581 result = DecodedContent.lsearchErrorIndex(errList, 10);
582 assertEquals(0, result);
585 errList.add(new DecodeErrorInfo(15, (byte)0x00));
586 result = DecodedContent.lsearchErrorIndex(errList, 10);
587 assertEquals(0, result);
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);
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);
610 * Test of bsearchErrorIndex method, of class DecodedContent.
613 public void testBsearchErrorIndex(){
614 System.out.println("bsearchErrorIndex");
616 List<DecodeErrorInfo> errList;
619 errList = new ArrayList<>();
620 result = DecodedContent.bsearchErrorIndex(errList, 10);
621 assertEquals(0, result);
624 errList.add(new DecodeErrorInfo(5, (byte)0x00));
625 result = DecodedContent.bsearchErrorIndex(errList, 10);
626 assertEquals(1, result);
629 errList.add(new DecodeErrorInfo(10, (byte)0x00));
630 result = DecodedContent.bsearchErrorIndex(errList, 10);
631 assertEquals(0, result);
634 errList.add(new DecodeErrorInfo(15, (byte)0x00));
635 result = DecodedContent.bsearchErrorIndex(errList, 10);
636 assertEquals(0, result);
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);
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);
663 * Test of searchErrorIndex method, of class DecodedContent.
666 public void testSearchErrorIndex(){
667 System.out.println("searchErrorIndex");
669 List<DecodeErrorInfo> errList;
672 errList = new ArrayList<>();
675 for(int pos = 0; pos <= 1000; pos += 10){
676 errList.add(new DecodeErrorInfo(pos, (byte)0x00));
678 result = DecodedContent.searchErrorIndex(errList, 503);
679 assertEquals(51, result);
682 for(int pos = 0; pos <= 50; pos += 10){
683 errList.add(new DecodeErrorInfo(pos, (byte)0x00));
685 result = DecodedContent.searchErrorIndex(errList, 23);
686 assertEquals(3, result);
692 * Test of appendGappedErrorInfo method, of class DecodedContent.
695 public void testAppendGappedErrorInfo(){
696 System.out.println("appendGappedErrorInfo");
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);
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());
716 * Test of ensureCapacity method, of class DecodedContent.
719 public void testEnsureCapacity(){
720 System.out.println("ensureCapacity");
722 DecodedContent content;
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());
732 content = new DecodedContent();
733 content.ensureCapacity(5);
734 content.append("abc");
735 assertEquals("abc", content.toString());
741 * Test of setCharAt method, of class DecodedContent.
744 public void testSetCharAt(){
745 System.out.println("setCharAt");
747 DecodedContent content;
749 content = new DecodedContent("abc");
750 content.setCharAt(1, 'B');
751 assertEquals("aBc", content.toString());
753 content = new DecodedContent("a");
754 content.addDecodeError((byte)0xff);
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());
763 content = new DecodedContent("abc");
765 content.setCharAt(-1, 'B');
767 }catch(IndexOutOfBoundsException e){
771 content.setCharAt(10, 'B');
773 }catch(IndexOutOfBoundsException e){