OSDN Git Service

2629d8c8be703bbe227da072129ca0caaec4d735
[dictzip-java/dictzip-java.git] / dictzip-lib / src / test / java / org / dict / zip / RandomAccessInputStreamTest.java
1 /*
2  * DictZip library test.
3  *
4  * Copyright (C) 2016,2019,2022 Hiroshi Miura
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * Linking this library statically or dynamically with other modules is
21  * making a combined work based on this library.  Thus, the terms and
22  * conditions of the GNU General Public License cover the whole
23  * combination.
24  *
25  * As a special exception, the copyright holders of this library give you
26  * permission to link this library with independent modules to produce an
27  * executable, regardless of the license terms of these independent
28  * modules, and to copy and distribute the resulting executable under
29  * terms of your choice, provided that you also meet, for each linked
30  * independent module, the terms and conditions of the license of that
31  * module.  An independent module is a module which is not derived from
32  * or based on this library.  If you modify this library, you may extend
33  * this exception to your version of the library, but you are not
34  * obligated to do so.  If you do not wish to do so, delete this
35  * exception statement from your version.
36  */
37
38 package org.dict.zip;
39
40
41 import org.junit.jupiter.api.Assertions;
42 import org.junit.jupiter.api.Test;
43
44 import static org.junit.jupiter.api.Assertions.assertEquals;
45 import static org.junit.jupiter.api.Assertions.assertTrue;
46
47 /**
48  * Test for project.
49  *
50  * @author Hiroshi Miura
51  */
52 public class RandomAccessInputStreamTest {
53
54     private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
55
56     /**
57      * Test of available method, of class RandomAccessInputStream.
58      * @throws Exception when i/o error.
59      */
60     @Test
61     public void testAvailable() throws Exception {
62         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
63         int expResult = 136856;
64         int result = instance.available();
65         assertEquals(result, expResult);
66     }
67
68     /**
69      * Test of close method, of class RandomAccessInputStream.
70      * @throws Exception when error.
71      */
72     @Test
73     public void testClose() throws Exception {
74         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
75         instance.close();
76     }
77
78     /**
79      * Test of getLength method, of class RandomAccessInputStream.
80      * @throws Exception when error.
81      */
82     @Test
83     public void testGetLength() throws Exception {
84         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
85         long expResult = 136856L;
86         long result = instance.length();
87         assertEquals(result, expResult);
88     }
89
90     /**
91      * Test of getPos method, of class RandomAccessInputStream.
92      * @throws Exception when error.
93      */
94     @Test
95     public void testGetPos() throws Exception {
96         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
97         long expResult = 0L;
98         long result = instance.position();
99         assertEquals(result, expResult);
100     }
101
102     /**
103      * Test of mark method, of class RandomAccessInputStream.
104      */
105     @Test
106     public void testMark() {
107         int markpos = 0;
108         try {
109             RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
110             instance.mark(markpos);
111         } catch (Exception ex) {
112             Assertions.fail("get exception.");
113         }
114     }
115
116     /**
117      * Test of markSupported method, of class RandomAccessInputStream.
118      */
119     @Test
120     public void testMarkSupported() {
121         try {
122             RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
123             boolean result = instance.markSupported();
124             assertTrue(result);
125         } catch (Exception ex) {
126             Assertions.fail("get exception.");
127         }
128     }
129
130     /**
131      * Test of read method, of class RandomAccessInputStream.
132      * @throws Exception when error.
133      */
134     @Test
135     public void testRead0args() throws Exception {
136         try {
137             RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
138             int expResult = 31;
139             int result = instance.read();
140             assertEquals(result, expResult);
141         } catch (Exception ex) {
142             Assertions.fail("get exception.");
143         }
144     }
145
146     /**
147      * Test of read method, of class RandomAccessInputStream.
148      * @throws Exception when error.
149      */
150     @Test
151     public void testRead3args() throws Exception {
152         byte[] b = new byte[512];
153         int off = 100;
154         int len = 256;
155         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
156         int expResult = 256;
157         int result = instance.read(b, off, len);
158         assertEquals(result, expResult);
159     }
160
161     /**
162      * Test of readFully method, of class RandomAccessInputStream.
163      * @throws Exception when error.
164      */
165     @Test
166     public void testReadFully() throws Exception {
167         byte[] b = new byte[512];
168         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
169         instance.readFully(b);
170     }
171
172     /**
173      * Test of reset method, of class RandomAccessInputStream.
174      * @throws Exception when error.
175      */
176     @Test
177     public void testReset() throws Exception {
178         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
179         instance.reset();
180     }
181
182     /**
183      * Test of seek method, of class RandomAccessInputStream.
184      * @throws Exception when error.
185      */
186     @Test
187     public void testSeek() throws Exception {
188         long pos = 100L;
189         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
190         instance.seek(pos);
191     }
192
193     /**
194      * Test of skip method, of class RandomAccessInputStream.
195      * @throws Exception when error.
196      */
197     @Test
198     public void testSkip() throws Exception {
199         long n = 100L;
200         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
201         long expResult = 100L;
202         long result = instance.skip(n);
203         assertEquals(result, expResult);
204     }
205
206     @Test
207     public void testLastByte() throws Exception {
208         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
209         instance.seek(136854);
210         int c = instance.read();
211         assertEquals(5, c);
212         c = instance.read();
213         assertEquals(0, c);
214         c = instance.read();
215         assertEquals(-1, c);
216         long pos = instance.position();
217         assertEquals(136856, pos);
218     }
219
220     @Test
221     public void testLastBytes() throws Exception {
222         RandomAccessInputStream instance = new RandomAccessInputStream(dataFile, "r");
223         instance.seek(136848);
224         byte[] buf = new byte[9];
225         int len = instance.read(buf, 0, buf.length);
226         assertEquals(8, len);
227         assertEquals(5, buf[len - 2]);
228         assertEquals(0, buf[len - 1]);
229         long pos = instance.position();
230         assertEquals(136856, pos);
231     }
232 }