2 * License : The MIT License
3 * Copyright(c) 2009 olyutorskii
6 package jp.osdn.jindolf.parser.content;
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 DecodeErrorInfoTest {
22 private static final byte B0 = (byte)0x00;
25 public DecodeErrorInfoTest() {
29 public static void setUpClass() throws Exception{
33 public static void tearDownClass() throws Exception{
41 public void tearDown() {
48 public void testConstructor(){
49 System.out.println("Constructor");
53 info = new DecodeErrorInfo(99, (byte)0xfe);
56 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
59 info = new DecodeErrorInfo(0, (byte)0xfe);
62 info = new DecodeErrorInfo(0, (byte)0x87, (byte)0x40);
66 info = new DecodeErrorInfo(-1, (byte)0xfe);
69 }catch(IndexOutOfBoundsException e){
74 info = new DecodeErrorInfo(-1, (byte)0x87, (byte)0x40);
77 }catch(IndexOutOfBoundsException e){
85 * Test of getCharPosition method, of class DecodeErrorInfo.
88 public void testGetCharPosition(){
89 System.out.println("getCharPosition");
93 info = new DecodeErrorInfo(0, (byte)0xfe);
94 assertEquals(0, info.getCharPosition());
96 info = new DecodeErrorInfo(99, (byte)0xfe);
97 assertEquals(99, info.getCharPosition());
99 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
100 assertEquals(999, info.getCharPosition());
106 * Test of has2nd method, of class DecodeErrorInfo.
109 public void testHas2nd(){
110 System.out.println("has2nd");
112 DecodeErrorInfo info;
114 info = new DecodeErrorInfo(99, (byte)0xfe);
115 assertFalse(info.has2nd());
117 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
118 assertTrue(info.has2nd());
124 * Test of getRawByte1st method, of class DecodeErrorInfo.
127 public void testGetRawByte1st(){
128 System.out.println("getRawByte1st");
130 DecodeErrorInfo info;
132 info = new DecodeErrorInfo(99, (byte)0xfe);
133 assertEquals((byte)0xfe, info.getRawByte1st());
135 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
136 assertEquals((byte)0x87, info.getRawByte1st());
142 * Test of getRawByte2nd method, of class DecodeErrorInfo.
145 public void testGetRawByte2nd(){
146 System.out.println("getRawByte2nd");
148 DecodeErrorInfo info;
150 info = new DecodeErrorInfo(99, (byte)0xfe);
152 info.getRawByte2nd();
154 }catch(IllegalStateException e){
158 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
159 assertEquals((byte)0x40, info.getRawByte2nd());
165 * Test of createGappedClone method, of class DecodeErrorInfo.
168 public void testCreateGappedClone(){
169 System.out.println("createGappedClone");
171 DecodeErrorInfo info;
173 info = new DecodeErrorInfo(99, (byte)0xfe);
174 info = info.createGappedClone(1);
175 assertEquals(98, info.getCharPosition());
177 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
178 info = info.createGappedClone(1);
179 assertEquals(998, info.getCharPosition());
181 info = new DecodeErrorInfo(99, (byte)0xfe);
182 info = info.createGappedClone(-1);
183 assertEquals(100, info.getCharPosition());
185 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
186 info = info.createGappedClone(-1);
187 assertEquals(1000, info.getCharPosition());
189 info = new DecodeErrorInfo(99, (byte)0xfe);
190 info = info.createGappedClone(99);
191 assertEquals(0, info.getCharPosition());
193 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
194 info = info.createGappedClone(999);
195 assertEquals(0, info.getCharPosition());
197 info = new DecodeErrorInfo(99, (byte)0xfe);
199 info = info.createGappedClone(100);
202 }catch(IndexOutOfBoundsException e){
206 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
208 info = info.createGappedClone(1000);
211 }catch(IndexOutOfBoundsException e){
219 * Test of toString method, of class DecodeErrorInfo.
222 public void testToString(){
223 System.out.println("toString");
225 DecodeErrorInfo info;
227 info = new DecodeErrorInfo(99, (byte)0x09);
228 assertEquals("start:99 09", info.toString());
230 info = new DecodeErrorInfo(99, (byte)0xfe);
231 assertEquals("start:99 fe", info.toString());
233 info = new DecodeErrorInfo(999, (byte)0x08, (byte)0x09);
234 assertEquals("start:999 08:09", info.toString());
236 info = new DecodeErrorInfo(999, (byte)0x87, (byte)0x40);
237 assertEquals("start:999 87:40", info.toString());
243 * Test of lsearchErrorIndex method, of class DecodedContent.
246 public void testLsearchErrorIndex(){
247 System.out.println("lsearchErrorIndex");
249 List<DecodeErrorInfo> errList;
252 errList = new ArrayList<>();
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);
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);
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);
302 * Test of bsearchErrorIndex method, of class DecodedContent.
305 public void testBsearchErrorIndex(){
306 System.out.println("bsearchErrorIndex");
308 List<DecodeErrorInfo> errList;
311 errList = new ArrayList<>();
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);
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);
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);
361 * Test of searchErrorIndex method, of class DecodedContent.
364 public void testSearchErrorIndex(){
365 System.out.println("searchErrorIndex");
367 List<DecodeErrorInfo> errList;
370 errList = new ArrayList<>();
373 for(int pos = 0; pos < 150; pos += 10){
374 errList.add(new DecodeErrorInfo(pos, B0));
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);
385 for(int pos = 0; pos < 1500; pos += 10){
386 errList.add(new DecodeErrorInfo(pos, B0));
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);
400 * Test of POS_COMPARATOR.
403 public void testCompare(){
404 System.out.println("POS_COMPARATOR");
406 DecodeErrorInfo info;
407 DecodeErrorInfo other;
409 info = new DecodeErrorInfo(99, (byte)0xf1);
410 other = new DecodeErrorInfo(98, (byte)0xf2);
411 assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) > 0);
413 info = new DecodeErrorInfo(99, (byte)0xf3);
414 other = new DecodeErrorInfo(100, (byte)0xf4);
415 assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) < 0);
417 info = new DecodeErrorInfo(99, (byte)0xf5);
418 other = new DecodeErrorInfo(99, (byte)0xf6);
419 assertTrue(DecodeErrorInfo.POS_COMPARATOR.compare(info, other) == 0);
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);