OSDN Git Service

11435c9dd387d919edd6abefe6197b219235ba0e
[mikutoga/TogaGem.git] / src / test / java / jp / sourceforge / mikutoga / parser / MmdInputStreamTest.java
1 /*
2  */
3
4 package jp.sourceforge.mikutoga.parser;
5
6 import java.io.ByteArrayInputStream;
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 MmdInputStreamTest {
18
19     public MmdInputStreamTest() {
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 parseByte method, of class MmdInputStream.
40      */
41     @Test
42     public void testParseByte() throws Exception {
43         System.out.println("parseByte");
44
45         MmdInputStream mis;
46         ByteArrayInputStream bis;
47
48         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02});
49         mis = new MmdInputStream(bis);
50
51         byte result;
52
53         result = mis.parseByte();
54         assertEquals(result, (byte)0x01);
55
56         result = mis.parseByte();
57         assertEquals(result, (byte)0x02);
58
59         try{
60             mis.parseByte();
61             fail();
62         }catch(MmdEofException e){
63             // GOOD
64         }
65
66         assertEquals(-1, mis.read());
67
68         return;
69     }
70
71     /**
72      * Test of parseBoolean method, of class MmdInputStream.
73      */
74     @Test
75     public void testParseBoolean() throws Exception {
76         System.out.println("parseBoolean");
77
78         MmdInputStream mis;
79         ByteArrayInputStream bis;
80
81         bis = new ByteArrayInputStream(new byte[]{0x00, 0x01, 0x02});
82         mis = new MmdInputStream(bis);
83
84         boolean result;
85
86         result = mis.parseBoolean();
87         assertFalse(result);
88
89         result = mis.parseBoolean();
90         assertTrue(result);
91
92         result = mis.parseBoolean();
93         assertTrue(result);
94
95         try{
96             mis.parseBoolean();
97             fail();
98         }catch(MmdEofException e){
99             // GOOD
100         }
101
102         assertEquals(-1, mis.read());
103
104         return;
105     }
106
107     /**
108      * Test of parseBeShort method, of class MmdInputStream.
109      */
110     @Test
111     public void testParseBeShort() throws Exception {
112         System.out.println("parseBeShort");
113
114         MmdInputStream mis;
115         ByteArrayInputStream bis;
116
117         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02, 0x03});
118         mis = new MmdInputStream(bis);
119
120         short result;
121
122         result = mis.parseBeShort();
123         assertEquals((short)0x0102, result);
124
125         try{
126             mis.parseBeShort();
127             fail();
128         }catch(MmdEofException e){
129             // GOOD
130         }
131
132         assertEquals(-1, mis.read());
133
134         return;
135     }
136
137     /**
138      * Test of parseLeShort method, of class MmdInputStream.
139      */
140     @Test
141     public void testParseLeShort() throws Exception {
142         System.out.println("parseLeShort");
143
144         MmdInputStream mis;
145         ByteArrayInputStream bis;
146
147         bis = new ByteArrayInputStream(new byte[]{0x01, 0x02, 0x03});
148         mis = new MmdInputStream(bis);
149
150         short result;
151
152         result = mis.parseLeShort();
153         assertEquals((short)0x0201, result);
154
155         try{
156             mis.parseLeShort();
157             fail();
158         }catch(MmdEofException e){
159             // GOOD
160         }
161
162         assertEquals(-1, mis.read());
163
164         return;
165     }
166
167     /**
168      * Test of parseBeInt method, of class MmdInputStream.
169      */
170     @Test
171     public void testParseBeInt() throws Exception {
172         System.out.println("parseBeInt");
173
174         MmdInputStream mis;
175         ByteArrayInputStream bis;
176
177         bis = new ByteArrayInputStream(
178                 new byte[]{0x01, 0x02, 0x03, 0x04, 0x05});
179         mis = new MmdInputStream(bis);
180
181         int result;
182
183         result = mis.parseBeInt();
184         assertEquals(0x01020304, result);
185
186         try{
187             mis.parseBeInt();
188             fail();
189         }catch(MmdEofException e){
190             // GOOD
191         }
192
193         assertEquals(-1, mis.read());
194
195         return;
196     }
197
198     /**
199      * Test of parseLeInt method, of class MmdInputStream.
200      */
201     @Test
202     public void testParseLeInt() throws Exception {
203         System.out.println("parseLeInt");
204
205         MmdInputStream mis;
206         ByteArrayInputStream bis;
207
208         bis = new ByteArrayInputStream(
209                 new byte[]{0x01, 0x02, 0x03, 0x04, 0x05});
210         mis = new MmdInputStream(bis);
211
212         int result;
213
214         result = mis.parseLeInt();
215         assertEquals(0x04030201, result);
216
217         try{
218             mis.parseLeInt();
219             fail();
220         }catch(MmdEofException e){
221             // GOOD
222         }
223
224         assertEquals(-1, mis.read());
225
226         return;
227     }
228
229     /**
230      * Test of parseBeFloat method, of class MmdInputStream.
231      */
232     @Test
233     public void testParseBeFloat() throws Exception {
234         System.out.println("parseBeFloat");
235
236         MmdInputStream mis;
237         ByteArrayInputStream bis;
238
239         bis = new ByteArrayInputStream(
240                 new byte[]{0x01, 0x02, 0x03, 0x04, 0x05});
241         mis = new MmdInputStream(bis);
242
243         float result;
244
245         result = mis.parseBeFloat();
246         assertEquals(Float.intBitsToFloat(0x01020304), result, 0.0f);
247
248         try{
249             mis.parseBeFloat();
250             fail();
251         }catch(MmdEofException e){
252             // GOOD
253         }
254
255         assertEquals(-1, mis.read());
256
257         return;
258     }
259
260     /**
261      * Test of parseLeFloat method, of class MmdInputStream.
262      */
263     @Test
264     public void testParseLeFloat() throws Exception {
265         System.out.println("parseLeFloat");
266
267         MmdInputStream mis;
268         ByteArrayInputStream bis;
269
270         bis = new ByteArrayInputStream(
271                 new byte[]{0x01, 0x02, 0x03, 0x04, 0x05});
272         mis = new MmdInputStream(bis);
273
274         float result;
275
276         result = mis.parseLeFloat();
277         assertEquals(Float.intBitsToFloat(0x04030201), result, 0.0f);
278
279         try{
280             mis.parseLeFloat();
281             fail();
282         }catch(MmdEofException e){
283             // GOOD
284         }
285
286         assertEquals(-1, mis.read());
287
288         return;
289     }
290
291     /**
292      * Test of skipRepeat method, of class MmdInputStream.
293      */
294     @Test
295     public void testSkipRepeat() throws Exception {
296         System.out.println("skipRepeat");
297
298         MmdInputStream mis;
299         ByteArrayInputStream bis;
300
301         bis = new ByteArrayInputStream(new byte[]{0x11, 0x12, 0x13});
302         mis = new MmdInputStream(bis);
303
304         int result;
305         long skipped;
306
307         result = mis.read();
308         assertEquals(0x11, result);
309
310         skipped = mis.skip(1L);
311         assertEquals(1L, skipped);
312
313         result = mis.read();
314         assertEquals(0x13, result);
315
316         skipped = mis.skip(1L);
317         assertEquals(0L, skipped);
318
319         // TODO: BufferedInputStreamと組み合わせた時の不思議なskip動作
320
321         return;
322     }
323
324 }