OSDN Git Service

パッケージ変更。テスト整備。
[mikutoga/TogaGem.git] / src / test / java / jp / sfjp / mikutoga / bin / parser / CommonParserTest.java
1 /*
2  */
3
4 package jp.sfjp.mikutoga.bin.parser;
5
6 import java.nio.charset.Charset;
7 import org.junit.After;
8 import org.junit.AfterClass;
9 import org.junit.Before;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import static org.junit.Assert.*;
13
14 /**
15  *
16  */
17 public class CommonParserTest {
18
19     public CommonParserTest() {
20     }
21
22     @BeforeClass
23     public static void setUpClass() {
24     }
25
26     @AfterClass
27     public static void tearDownClass() {
28     }
29
30     @Before
31     public void setUp() {
32     }
33
34     @After
35     public void tearDown() {
36     }
37
38     /**
39      * Test of getPosition method, of class CommonParser.
40      * @throws Exception
41      */
42     @Test
43     public void testGetPosition() throws Exception{
44         System.out.println("getPosition");
45
46         CommonParser parser;
47         DummyInputStream is;
48
49         is = new DummyInputStream(new byte[100]);
50         parser = new CommonParser(is);
51
52         assertEquals(0, parser.getPosition());
53
54         parser.parseByte();
55         assertEquals(1, parser.getPosition());
56
57         parser.parseLeInt();
58         assertEquals(5, parser.getPosition());
59
60         parser.skip(10);
61         assertEquals(15, parser.getPosition());
62
63         return;
64     }
65
66     /**
67      * Test of hasMore method, of class CommonParser.
68      * @throws Exception
69      */
70     @Test
71     public void testHasMore() throws Exception {
72         System.out.println("hasMore");
73
74         CommonParser parser;
75         DummyInputStream is;
76
77         is = new DummyInputStream(0x00, 0x01, 0x02);
78         parser = new CommonParser(is);
79
80         assertTrue(parser.hasMore());
81
82         assertEquals((byte)0x00, parser.parseByte());
83         assertTrue(parser.hasMore());
84
85         assertEquals((byte)0x01, parser.parseByte());
86         assertTrue(parser.hasMore());
87
88         assertEquals((byte)0x02, parser.parseByte());
89         assertFalse(parser.hasMore());
90
91         return;
92     }
93
94     /**
95      * Test of skip method, of class CommonParser.
96      * @throws Exception
97      */
98     @Test
99     public void testSkip() throws Exception {
100         System.out.println("skip");
101
102         CommonParser parser;
103         DummyInputStream is;
104
105         is = new DummyInputStream(0x00, 0x01, 0x02);
106         parser = new CommonParser(is);
107
108         assertEquals((byte)0x00, parser.parseByte());
109
110         parser.skip(1L);
111         assertEquals((byte)0x02, parser.parseByte());
112
113         try{
114             parser.skip(1L);
115             fail();
116         }catch(MmdEofException e){
117             // GOOD
118         }
119
120         return;
121     }
122
123     /**
124      * Test of parseByteArray method, of class CommonParser.
125      * @throws Exception
126      */
127     @Test
128     public void testParseByteArray_3args() throws Exception {
129         System.out.println("parseByteArray");
130
131         CommonParser parser;
132         DummyInputStream is;
133
134         is = new DummyInputStream(0x01, 0x02, 0x03);
135         parser = new CommonParser(is);
136
137         byte[] dst = {
138             (byte)0xf1, (byte)0xf2, (byte)0xf3, (byte)0xf4, (byte)0xf5
139         };
140
141         parser.parseByteArray(dst, 1, 2);
142
143         assertEquals((byte)0xf1, dst[0]);
144         assertEquals((byte)0x01, dst[1]);
145         assertEquals((byte)0x02, dst[2]);
146         assertEquals((byte)0xf4, dst[3]);
147         assertEquals((byte)0xf5, dst[4]);
148
149         return;
150     }
151
152     /**
153      * Test of parseByteArray method, of class CommonParser.
154      * @throws Exception
155      */
156     @Test
157     public void testParseByteArray_byteArr() throws Exception {
158         System.out.println("parseByteArray");
159
160         CommonParser parser;
161         DummyInputStream is;
162
163         is = new DummyInputStream(0x01, 0x02, 0x03);
164         parser = new CommonParser(is);
165
166         byte[] dst = {
167             (byte)0xf1, (byte)0xf2
168         };
169
170         parser.parseByteArray(dst);
171
172         assertEquals((byte)0x01, dst[0]);
173         assertEquals((byte)0x02, dst[1]);
174
175         return;
176     }
177
178     /**
179      * Test of parseByte method, of class CommonParser.
180      * @throws Exception
181      */
182     @Test
183     public void testParseByte() throws Exception {
184         System.out.println("parseByte");
185
186         CommonParser parser;
187         DummyInputStream is;
188
189         is = new DummyInputStream(0x01, 0x02, 0x03);
190         parser = new CommonParser(is);
191
192         assertEquals((byte)0x01, parser.parseByte());
193         assertEquals((byte)0x02, parser.parseByte());
194         assertEquals((byte)0x03, parser.parseByte());
195
196         try{
197             parser.parseByte();
198             fail();
199         }catch(MmdEofException e){
200             // GOOD
201         }
202
203         return;
204     }
205
206     /**
207      * Test of parseUByteAsInt method, of class CommonParser.
208      * @throws Exception
209      */
210     @Test
211     public void testParseUByteAsInt() throws Exception {
212         System.out.println("parseUByteAsInt");
213
214         CommonParser parser;
215         DummyInputStream is;
216
217         is = new DummyInputStream(0x00, 0x01, 0xff);
218         parser = new CommonParser(is);
219
220         assertEquals(0x00, parser.parseUByteAsInt());
221         assertEquals(0x01, parser.parseUByteAsInt());
222         assertEquals(0xff, parser.parseUByteAsInt());
223
224         try{
225             parser.parseUByteAsInt();
226             fail();
227         }catch(MmdEofException e){
228             // GOOD
229         }
230
231         return;
232     }
233
234     /**
235      * Test of parseBoolean method, of class CommonParser.
236      * @throws Exception
237      */
238     @Test
239     public void testParseBoolean() throws Exception {
240         System.out.println("parseBoolean");
241
242         CommonParser parser;
243         DummyInputStream is;
244
245         is = new DummyInputStream(0x00, 0x01, 0x02, 0xff);
246         parser = new CommonParser(is);
247
248         assertFalse(parser.parseBoolean());
249         assertTrue(parser.parseBoolean());
250         assertTrue(parser.parseBoolean());
251         assertTrue(parser.parseBoolean());
252
253         try{
254             parser.parseBoolean();
255             fail();
256         }catch(MmdEofException e){
257             // GOOD
258         }
259
260         return;
261     }
262
263     /**
264      * Test of parseLeShort method, of class CommonParser.
265      * @throws Exception
266      */
267     @Test
268     public void testParseLeShort() throws Exception {
269         System.out.println("parseLeShort");
270
271         CommonParser parser;
272         DummyInputStream is;
273
274         is = new DummyInputStream(0xfe, 0xff, 0x01, 0x00, 0x80);
275         parser = new CommonParser(is);
276
277         assertEquals((short)-2, parser.parseLeShort());
278         assertEquals((short)1, parser.parseLeShort());
279
280         try{
281             parser.parseLeShort();
282             fail();
283         }catch(MmdEofException e){
284             // GOOD
285         }
286
287         return;
288     }
289
290     /**
291      * Test of parseLeUShortAsInt method, of class CommonParser.
292      * @throws Exception
293      */
294     @Test
295     public void testParseLeUShortAsInt() throws Exception {
296         System.out.println("parseLeUShortAsInt");
297
298         CommonParser parser;
299         DummyInputStream is;
300
301         is = new DummyInputStream(0xfe, 0xff, 0x01, 0x00, 0x80);
302         parser = new CommonParser(is);
303
304         assertEquals(0xfffe, parser.parseLeUShortAsInt());
305         assertEquals(1, parser.parseLeUShortAsInt());
306
307         try{
308             parser.parseLeUShortAsInt();
309             fail();
310         }catch(MmdEofException e){
311             // GOOD
312         }
313
314         return;
315     }
316
317     /**
318      * Test of parseLeInt method, of class CommonParser.
319      * @throws Exception
320      */
321     @Test
322     public void testParseLeInt() throws Exception {
323         System.out.println("parseLeInt");
324
325         CommonParser parser;
326         DummyInputStream is;
327
328         is = new DummyInputStream(
329                 0xfe, 0xff, 0xff, 0xff,
330                 0x78, 0x56, 0x34, 0x12,
331                 0x7f );
332         parser = new CommonParser(is);
333
334         assertEquals(-2, parser.parseLeInt());
335         assertEquals(0x12345678, parser.parseLeInt());
336
337         try{
338             parser.parseLeInt();
339             fail();
340         }catch(MmdEofException e){
341             // GOOD
342         }
343
344         return;
345     }
346
347     /**
348      * Test of parseLeFloat method, of class CommonParser.
349      * @throws Exception
350      */
351     @Test
352     public void testParseLeFloat() throws Exception {
353         System.out.println("parseLeFloat");
354
355         CommonParser parser;
356         DummyInputStream is;
357
358         is = new DummyInputStream(
359                 0x00, 0x00, 0xc0, 0xbf,
360                 0x78, 0x56, 0x34, 0x12,
361                 0x7f );
362         parser = new CommonParser(is);
363
364         assertEquals(-1.5f, parser.parseLeFloat(), 0.0);
365         assertEquals(
366                 Float.intBitsToFloat(0x12345678), parser.parseLeFloat(),
367                 0.0 );
368
369         try{
370             parser.parseLeFloat();
371             fail();
372         }catch(MmdEofException e){
373             // GOOD
374         }
375
376         return;
377     }
378
379     /**
380      * Test of parseString method, of class CommonParser.
381      * @throws Exception
382      */
383     @Test
384     public void testParseString() throws Exception {
385         System.out.println("parseString");
386
387         CommonParser parser;
388         DummyInputStream is;
389         TextDecoder decoder;
390
391         decoder = new TextDecoder(Charset.forName("Shift_JIS"));
392
393         is = new DummyInputStream(0x82, 0xa0, 0x82, 0xa2, 0x46);
394         parser = new CommonParser(is);
395
396         assertEquals("あ", parser.parseString(decoder, 2));
397         assertEquals("い", parser.parseString(decoder, 2));
398
399         try{
400             parser.parseString(decoder, 2);
401             fail();
402         }catch(MmdEofException e){
403             // GOOD
404         }
405
406         is = new DummyInputStream(0x82, 0xa0, 0x82, 0xa2);
407         parser = new CommonParser(is);
408
409         assertEquals("あい", parser.parseString(decoder, 4));
410
411         is = new DummyInputStream(0x82, 0xa0, 0x82, 0xff);
412         parser = new CommonParser(is);
413
414         try{
415             parser.parseString(decoder, 4);
416             fail();
417         }catch(MmdFormatException e){
418 //          assertEquals("unmapped character(position:2)", e.getMessage());
419             // GOOD
420         }
421
422         is = new DummyInputStream(0x82, 0xa0, 0x82);
423         parser = new CommonParser(is);
424
425         try{
426             parser.parseString(decoder, 3);
427             fail();
428         }catch(MmdFormatException e){
429 //          assertEquals("illegal character encoding(position:1)", e.getMessage());
430             // GOOD
431         }
432
433         is = new DummyInputStream(0x41, 0x42, 0x43);
434         parser = new CommonParser(is);
435
436         assertEquals("A", parser.parseString(decoder, 1));
437         assertEquals("BC", parser.parseString(decoder, 2));
438
439         return;
440     }
441
442 }