4 package jp.sfjp.mikutoga.bin.parser;
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.*;
17 public class CommonParserTest {
19 public CommonParserTest() {
23 public static void setUpClass() {
27 public static void tearDownClass() {
35 public void tearDown() {
39 * Test of getPosition method, of class CommonParser.
43 public void testGetPosition() throws Exception{
44 System.out.println("getPosition");
49 is = new DummyInputStream(new byte[100]);
50 parser = new CommonParser(is);
52 assertEquals(0, parser.getPosition());
55 assertEquals(1, parser.getPosition());
58 assertEquals(5, parser.getPosition());
61 assertEquals(15, parser.getPosition());
67 * Test of hasMore method, of class CommonParser.
71 public void testHasMore() throws Exception {
72 System.out.println("hasMore");
77 is = new DummyInputStream(0x00, 0x01, 0x02);
78 parser = new CommonParser(is);
80 assertTrue(parser.hasMore());
82 assertEquals((byte)0x00, parser.parseByte());
83 assertTrue(parser.hasMore());
85 assertEquals((byte)0x01, parser.parseByte());
86 assertTrue(parser.hasMore());
88 assertEquals((byte)0x02, parser.parseByte());
89 assertFalse(parser.hasMore());
95 * Test of skip method, of class CommonParser.
99 public void testSkip() throws Exception {
100 System.out.println("skip");
105 is = new DummyInputStream(0x00, 0x01, 0x02);
106 parser = new CommonParser(is);
108 assertEquals((byte)0x00, parser.parseByte());
111 assertEquals((byte)0x02, parser.parseByte());
116 }catch(MmdEofException e){
124 * Test of parseByteArray method, of class CommonParser.
128 public void testParseByteArray_3args() throws Exception {
129 System.out.println("parseByteArray");
134 is = new DummyInputStream(0x01, 0x02, 0x03);
135 parser = new CommonParser(is);
138 (byte)0xf1, (byte)0xf2, (byte)0xf3, (byte)0xf4, (byte)0xf5
141 parser.parseByteArray(dst, 1, 2);
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]);
153 * Test of parseByteArray method, of class CommonParser.
157 public void testParseByteArray_byteArr() throws Exception {
158 System.out.println("parseByteArray");
163 is = new DummyInputStream(0x01, 0x02, 0x03);
164 parser = new CommonParser(is);
167 (byte)0xf1, (byte)0xf2
170 parser.parseByteArray(dst);
172 assertEquals((byte)0x01, dst[0]);
173 assertEquals((byte)0x02, dst[1]);
179 * Test of parseByte method, of class CommonParser.
183 public void testParseByte() throws Exception {
184 System.out.println("parseByte");
189 is = new DummyInputStream(0x01, 0x02, 0x03);
190 parser = new CommonParser(is);
192 assertEquals((byte)0x01, parser.parseByte());
193 assertEquals((byte)0x02, parser.parseByte());
194 assertEquals((byte)0x03, parser.parseByte());
199 }catch(MmdEofException e){
207 * Test of parseUByteAsInt method, of class CommonParser.
211 public void testParseUByteAsInt() throws Exception {
212 System.out.println("parseUByteAsInt");
217 is = new DummyInputStream(0x00, 0x01, 0xff);
218 parser = new CommonParser(is);
220 assertEquals(0x00, parser.parseUByteAsInt());
221 assertEquals(0x01, parser.parseUByteAsInt());
222 assertEquals(0xff, parser.parseUByteAsInt());
225 parser.parseUByteAsInt();
227 }catch(MmdEofException e){
235 * Test of parseBoolean method, of class CommonParser.
239 public void testParseBoolean() throws Exception {
240 System.out.println("parseBoolean");
245 is = new DummyInputStream(0x00, 0x01, 0x02, 0xff);
246 parser = new CommonParser(is);
248 assertFalse(parser.parseBoolean());
249 assertTrue(parser.parseBoolean());
250 assertTrue(parser.parseBoolean());
251 assertTrue(parser.parseBoolean());
254 parser.parseBoolean();
256 }catch(MmdEofException e){
264 * Test of parseLeShort method, of class CommonParser.
268 public void testParseLeShort() throws Exception {
269 System.out.println("parseLeShort");
274 is = new DummyInputStream(0xfe, 0xff, 0x01, 0x00, 0x80);
275 parser = new CommonParser(is);
277 assertEquals((short)-2, parser.parseLeShort());
278 assertEquals((short)1, parser.parseLeShort());
281 parser.parseLeShort();
283 }catch(MmdEofException e){
291 * Test of parseLeUShortAsInt method, of class CommonParser.
295 public void testParseLeUShortAsInt() throws Exception {
296 System.out.println("parseLeUShortAsInt");
301 is = new DummyInputStream(0xfe, 0xff, 0x01, 0x00, 0x80);
302 parser = new CommonParser(is);
304 assertEquals(0xfffe, parser.parseLeUShortAsInt());
305 assertEquals(1, parser.parseLeUShortAsInt());
308 parser.parseLeUShortAsInt();
310 }catch(MmdEofException e){
318 * Test of parseLeInt method, of class CommonParser.
322 public void testParseLeInt() throws Exception {
323 System.out.println("parseLeInt");
328 is = new DummyInputStream(
329 0xfe, 0xff, 0xff, 0xff,
330 0x78, 0x56, 0x34, 0x12,
332 parser = new CommonParser(is);
334 assertEquals(-2, parser.parseLeInt());
335 assertEquals(0x12345678, parser.parseLeInt());
340 }catch(MmdEofException e){
348 * Test of parseLeFloat method, of class CommonParser.
352 public void testParseLeFloat() throws Exception {
353 System.out.println("parseLeFloat");
358 is = new DummyInputStream(
359 0x00, 0x00, 0xc0, 0xbf,
360 0x78, 0x56, 0x34, 0x12,
362 parser = new CommonParser(is);
364 assertEquals(-1.5f, parser.parseLeFloat(), 0.0);
366 Float.intBitsToFloat(0x12345678), parser.parseLeFloat(),
370 parser.parseLeFloat();
372 }catch(MmdEofException e){
380 * Test of parseString method, of class CommonParser.
384 public void testParseString() throws Exception {
385 System.out.println("parseString");
391 decoder = new TextDecoder(Charset.forName("Shift_JIS"));
393 is = new DummyInputStream(0x82, 0xa0, 0x82, 0xa2, 0x46);
394 parser = new CommonParser(is);
396 assertEquals("あ", parser.parseString(decoder, 2));
397 assertEquals("い", parser.parseString(decoder, 2));
400 parser.parseString(decoder, 2);
402 }catch(MmdEofException e){
406 is = new DummyInputStream(0x82, 0xa0, 0x82, 0xa2);
407 parser = new CommonParser(is);
409 assertEquals("あい", parser.parseString(decoder, 4));
411 is = new DummyInputStream(0x82, 0xa0, 0x82, 0xff);
412 parser = new CommonParser(is);
415 parser.parseString(decoder, 4);
417 }catch(MmdFormatException e){
418 // assertEquals("unmapped character(position:2)", e.getMessage());
422 is = new DummyInputStream(0x82, 0xa0, 0x82);
423 parser = new CommonParser(is);
426 parser.parseString(decoder, 3);
428 }catch(MmdFormatException e){
429 // assertEquals("illegal character encoding(position:1)", e.getMessage());
433 is = new DummyInputStream(0x41, 0x42, 0x43);
434 parser = new CommonParser(is);
436 assertEquals("A", parser.parseString(decoder, 1));
437 assertEquals("BC", parser.parseString(decoder, 2));