OSDN Git Service

add B-search unit test
[jindolf/JinParser.git] / src / test / java / jp / osdn / jindolf / parser / content / DecodeErrorInfoTest.java
1 /*
2  * License : The MIT License
3  * Copyright(c) 2009 olyutorskii
4  */
5
6 package jp.osdn.jindolf.parser.content;
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 DecodeErrorInfoTest {
21
22     private static final byte B0 = (byte)0x00;
23
24
25     public DecodeErrorInfoTest() {
26     }
27
28     @BeforeClass
29     public static void setUpClass() throws Exception{
30     }
31
32     @AfterClass
33     public static void tearDownClass() throws Exception{
34     }
35
36     @Before
37     public void setUp() {
38     }
39
40     @After
41     public void tearDown() {
42     }
43
44     /**
45      * Test of Constructor
46      */
47     @Test
48     public void testConstructor(){
49         System.out.println("Constructor");
50
51         DecodeErrorInfo info;
52
53         info = new DecodeErrorInfo(99, (byte)0xfe);
54         assertNotNull(info);
55
56         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
57         assertNotNull(info);
58
59         info = new DecodeErrorInfo(0, (byte)0xfe);
60         assertNotNull(info);
61
62         info = new DecodeErrorInfo(0, (byte)0x87, (byte)0x40);
63         assertNotNull(info);
64
65         try{
66             info = new DecodeErrorInfo(-1, (byte)0xfe);
67             fail();
68             info.hashCode();
69         }catch(IndexOutOfBoundsException e){
70             // GOOD
71         }
72
73         try{
74             info = new DecodeErrorInfo(-1, (byte)0x87, (byte)0x40);
75             fail();
76             info.hashCode();
77         }catch(IndexOutOfBoundsException e){
78             // GOOD
79         }
80
81         return;
82     }
83
84     /**
85      * Test of getCharPosition method, of class DecodeErrorInfo.
86      */
87     @Test
88     public void testGetCharPosition(){
89         System.out.println("getCharPosition");
90
91         DecodeErrorInfo info;
92
93         info = new DecodeErrorInfo(0, (byte)0xfe);
94         assertEquals(0, info.getCharPosition());
95
96         info = new DecodeErrorInfo(99, (byte)0xfe);
97         assertEquals(99, info.getCharPosition());
98
99         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
100         assertEquals(999, info.getCharPosition());
101
102         return;
103     }
104
105     /**
106      * Test of has2nd method, of class DecodeErrorInfo.
107      */
108     @Test
109     public void testHas2nd(){
110         System.out.println("has2nd");
111
112         DecodeErrorInfo info;
113
114         info = new DecodeErrorInfo(99, (byte)0xfe);
115         assertFalse(info.has2nd());
116
117         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
118         assertTrue(info.has2nd());
119
120         return;
121     }
122
123     /**
124      * Test of getRawByte1st method, of class DecodeErrorInfo.
125      */
126     @Test
127     public void testGetRawByte1st(){
128         System.out.println("getRawByte1st");
129
130         DecodeErrorInfo info;
131
132         info = new DecodeErrorInfo(99, (byte)0xfe);
133         assertEquals((byte)0xfe, info.getRawByte1st());
134
135         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
136         assertEquals((byte)0x87, info.getRawByte1st());
137
138         return;
139     }
140
141     /**
142      * Test of getRawByte2nd method, of class DecodeErrorInfo.
143      */
144     @Test
145     public void testGetRawByte2nd(){
146         System.out.println("getRawByte2nd");
147
148         DecodeErrorInfo info;
149
150         info = new DecodeErrorInfo(99, (byte)0xfe);
151         try{
152             info.getRawByte2nd();
153             fail();
154         }catch(IllegalStateException e){
155             // GOOD
156         }
157
158         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
159         assertEquals((byte)0x40, info.getRawByte2nd());
160
161         return;
162     }
163
164     /**
165      * Test of createGappedClone method, of class DecodeErrorInfo.
166      */
167     @Test
168     public void testCreateGappedClone(){
169         System.out.println("createGappedClone");
170
171         DecodeErrorInfo info;
172
173         info = new DecodeErrorInfo(99, (byte)0xfe);
174         info = info.createGappedClone(1);
175         assertEquals(98, info.getCharPosition());
176
177         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
178         info = info.createGappedClone(1);
179         assertEquals(998, info.getCharPosition());
180
181         info = new DecodeErrorInfo(99, (byte)0xfe);
182         info = info.createGappedClone(-1);
183         assertEquals(100, info.getCharPosition());
184
185         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
186         info = info.createGappedClone(-1);
187         assertEquals(1000, info.getCharPosition());
188
189         info = new DecodeErrorInfo(99, (byte)0xfe);
190         info = info.createGappedClone(99);
191         assertEquals(0, info.getCharPosition());
192
193         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
194         info = info.createGappedClone(999);
195         assertEquals(0, info.getCharPosition());
196
197         info = new DecodeErrorInfo(99, (byte)0xfe);
198         try{
199             info = info.createGappedClone(100);
200             fail();
201             info.hashCode();
202         }catch(IndexOutOfBoundsException e){
203             // GOOD
204         }
205
206         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
207         try{
208             info = info.createGappedClone(1000);
209             fail();
210             info.hashCode();
211         }catch(IndexOutOfBoundsException e){
212             // GOOD
213         }
214
215         return;
216     }
217
218     /**
219      * Test of toString method, of class DecodeErrorInfo.
220      */
221     @Test
222     public void testToString(){
223         System.out.println("toString");
224
225         DecodeErrorInfo info;
226
227         info = new DecodeErrorInfo(99, (byte)0x09);
228         assertEquals("start:99 09", info.toString());
229
230         info = new DecodeErrorInfo(99, (byte)0xfe);
231         assertEquals("start:99 fe", info.toString());
232
233         info = new DecodeErrorInfo(999, (byte)0x08, (byte)0x09);
234         assertEquals("start:999 08:09", info.toString());
235
236         info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
237         assertEquals("start:999 87:40", info.toString());
238
239         return;
240     }
241
242     /**
243      * Test of lsearchErrorIndex method, of class DecodedContent.
244      */
245     @Test
246     public void testLsearchErrorIndex(){
247         System.out.println("lsearchErrorIndex");
248
249         List<DecodeErrorInfo> errList;
250         int result;
251
252         errList = new ArrayList<>();
253
254         errList.clear();
255         result = DecodeErrorInfo.lsearchErrorIndex(errList, -1);
256         assertEquals(0, result);
257         result = DecodeErrorInfo.lsearchErrorIndex(errList, 0);
258         assertEquals(0, result);
259         result = DecodeErrorInfo.lsearchErrorIndex(errList, 10);
260         assertEquals(0, result);
261
262         errList.clear();
263         errList.add(new DecodeErrorInfo(10, B0));
264         result = DecodeErrorInfo.lsearchErrorIndex(errList, 9);
265         assertEquals(0, result);
266         result = DecodeErrorInfo.lsearchErrorIndex(errList, 10);
267         assertEquals(0, result);
268         result = DecodeErrorInfo.lsearchErrorIndex(errList, 11);
269         assertEquals(1, result);
270
271         errList.clear();
272         errList.add(new DecodeErrorInfo(10, B0));
273         errList.add(new DecodeErrorInfo(20, B0));
274         errList.add(new DecodeErrorInfo(30, B0));
275         errList.add(new DecodeErrorInfo(40, B0));
276         errList.add(new DecodeErrorInfo(50, B0));
277         result = DecodeErrorInfo.lsearchErrorIndex(errList, 9);
278         assertEquals(0, result);
279         result = DecodeErrorInfo.lsearchErrorIndex(errList, 10);
280         assertEquals(0, result);
281         result = DecodeErrorInfo.lsearchErrorIndex(errList, 11);
282         assertEquals(1, result);
283         result = DecodeErrorInfo.lsearchErrorIndex(errList, 29);
284         assertEquals(2, result);
285         result = DecodeErrorInfo.lsearchErrorIndex(errList, 30);
286         assertEquals(2, result);
287         result = DecodeErrorInfo.lsearchErrorIndex(errList, 31);
288         assertEquals(3, result);
289         result = DecodeErrorInfo.lsearchErrorIndex(errList, 49);
290         assertEquals(4, result);
291         result = DecodeErrorInfo.lsearchErrorIndex(errList, 50);
292         assertEquals(4, result);
293         result = DecodeErrorInfo.lsearchErrorIndex(errList, 51);
294         assertEquals(5, result);
295         result = DecodeErrorInfo.lsearchErrorIndex(errList, 1000);
296         assertEquals(5, result);
297
298         return;
299      }
300
301     /**
302      * Test of bsearchErrorIndex method, of class DecodedContent.
303      */
304     @Test
305     public void testBsearchErrorIndex(){
306         System.out.println("bsearchErrorIndex");
307
308         List<DecodeErrorInfo> errList;
309         int result;
310
311         errList = new ArrayList<>();
312
313         errList.clear();
314         result = DecodeErrorInfo.bsearchErrorIndex(errList, -1);
315         assertEquals(0, result);
316         result = DecodeErrorInfo.bsearchErrorIndex(errList, 0);
317         assertEquals(0, result);
318         result = DecodeErrorInfo.bsearchErrorIndex(errList, 10);
319         assertEquals(0, result);
320
321         errList.clear();
322         errList.add(new DecodeErrorInfo(10, B0));
323         result = DecodeErrorInfo.bsearchErrorIndex(errList, 9);
324         assertEquals(0, result);
325         result = DecodeErrorInfo.bsearchErrorIndex(errList, 10);
326         assertEquals(0, result);
327         result = DecodeErrorInfo.bsearchErrorIndex(errList, 11);
328         assertEquals(1, result);
329
330         errList.clear();
331         errList.add(new DecodeErrorInfo(10, B0));
332         errList.add(new DecodeErrorInfo(20, B0));
333         errList.add(new DecodeErrorInfo(30, B0));
334         errList.add(new DecodeErrorInfo(40, B0));
335         errList.add(new DecodeErrorInfo(50, B0));
336         result = DecodeErrorInfo.bsearchErrorIndex(errList, 9);
337         assertEquals(0, result);
338         result = DecodeErrorInfo.bsearchErrorIndex(errList, 10);
339         assertEquals(0, result);
340         result = DecodeErrorInfo.bsearchErrorIndex(errList, 11);
341         assertEquals(1, result);
342         result = DecodeErrorInfo.bsearchErrorIndex(errList, 29);
343         assertEquals(2, result);
344         result = DecodeErrorInfo.bsearchErrorIndex(errList, 30);
345         assertEquals(2, result);
346         result = DecodeErrorInfo.bsearchErrorIndex(errList, 31);
347         assertEquals(3, result);
348         result = DecodeErrorInfo.bsearchErrorIndex(errList, 49);
349         assertEquals(4, result);
350         result = DecodeErrorInfo.bsearchErrorIndex(errList, 50);
351         assertEquals(4, result);
352         result = DecodeErrorInfo.bsearchErrorIndex(errList, 51);
353         assertEquals(5, result);
354         result = DecodeErrorInfo.bsearchErrorIndex(errList, 1000);
355         assertEquals(5, result);
356
357         return;
358     }
359
360     /**
361      * Test of searchErrorIndex method, of class DecodedContent.
362      */
363     @Test
364     public void testSearchErrorIndex(){
365         System.out.println("searchErrorIndex");
366
367         List<DecodeErrorInfo> errList;
368         int result;
369
370         errList = new ArrayList<>();
371
372         errList.clear();
373         for(int pos = 0; pos < 150; pos += 10){
374             errList.add(new DecodeErrorInfo(pos, B0));
375         }
376         assertTrue(errList.size() < DecodeErrorInfo.BSEARCH_THRESHOLD);
377         result = DecodeErrorInfo.searchErrorIndex(errList, 89);
378         assertEquals(9, result);
379         result = DecodeErrorInfo.searchErrorIndex(errList, 90);
380         assertEquals(9, result);
381         result = DecodeErrorInfo.searchErrorIndex(errList, 91);
382         assertEquals(10, result);
383
384         errList.clear();
385         for(int pos = 0; pos < 1500; pos += 10){
386             errList.add(new DecodeErrorInfo(pos, B0));
387         }
388         assertTrue(errList.size() >= DecodeErrorInfo.BSEARCH_THRESHOLD);
389         result = DecodeErrorInfo.searchErrorIndex(errList, 899);
390         assertEquals(90, result);
391         result = DecodeErrorInfo.searchErrorIndex(errList, 900);
392         assertEquals(90, result);
393         result = DecodeErrorInfo.searchErrorIndex(errList, 901);
394         assertEquals(91, result);
395
396         return;
397     }
398
399     /**
400      * Test of POS_COMPARATOR.
401      */
402     @Test
403     public void testCompare(){
404         System.out.println("POS_COMPARATOR");
405
406         DecodeErrorInfo info;
407         DecodeErrorInfo other;
408
409         info  = new DecodeErrorInfo(99, (byte)0xf1);
410         other = new DecodeErrorInfo(98, (byte)0xf2);
411         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) > 0);
412
413         info  = new DecodeErrorInfo(99, (byte)0xf3);
414         other = new DecodeErrorInfo(100, (byte)0xf4);
415         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) < 0);
416
417         info  = new DecodeErrorInfo(99, (byte)0xf5);
418         other = new DecodeErrorInfo(99, (byte)0xf6);
419         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) == 0);
420
421         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(null, other) < 0);
422         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, null) > 0);
423         assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(null, null) == 0);
424
425         return;
426     }
427
428 }