2 * DictZip library test.
4 * Copyright (C) 2016,2019,2022 Hiroshi Miura
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.
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.
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.
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
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.
40 import org.junit.jupiter.api.Assertions;
41 import org.junit.jupiter.api.Test;
42 import tokyo.northside.io.IOUtils2;
44 import java.io.EOFException;
45 import java.io.FileInputStream;
46 import java.io.IOException;
47 import java.util.Arrays;
49 import static org.junit.jupiter.api.Assertions.assertEquals;
50 import static org.junit.jupiter.api.Assertions.assertNotNull;
51 import static org.junit.jupiter.api.Assertions.assertTrue;
54 * Test of DictZipInputStream.
55 * @author Hiroshi Miura
57 public class DictZipInputStreamTest {
59 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
60 private final String dataFile2 = this.getClass().getResource("/test.dsl.dz").getFile();
63 * Test constructor @TestFactory.
64 * @throws Exception when i/o error.
67 public void testConstructorDefaultBufSize() throws Exception {
68 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
74 * Test constructor from filename.
75 * @throws Exception when i/o error.
78 public void testConstructorFromFilename() throws Exception {
79 try (DictZipInputStream din = new DictZipInputStream(dataFile)) {
85 * Test of read method, of class DictZipInputStream.
86 * @throws Exception when i/o error.
89 public void testRead() throws Exception {
91 byte[] buf = new byte[len];
92 byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
93 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
94 din.read(buf, 0, len);
95 assertTrue(Arrays.equals(expResult, buf));
100 * Test of read method with another file, of class DictZipInputStream.
101 * @throws Exception when i/o error.
104 public void testRead2() throws Exception {
106 byte[] buf = new byte[len];
107 byte[] expResult = {(byte) 0xFF, (byte) 0xFE, 35, 0, 78, 0, 65, 0, 77, 0};
108 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile2, "r"))) {
109 din.read(buf, 0, len);
110 assertTrue(Arrays.equals(expResult, buf));
115 * Test mark and reset methods.
116 * @throws Exception when i/o error.
119 public void testMarkReset() throws Exception {
121 byte[] buf1 = new byte[len];
122 byte[] buf2 = new byte[len];
123 byte[] buf3 = new byte[len];
124 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
125 assertTrue(din.markSupported());
126 din.read(buf1, 0, len);
128 din.read(buf2, 0, len);
130 din.read(buf3, 0, len);
131 assertTrue(Arrays.equals(buf2, buf3));
135 * Test of read method, of class DictZipInputStream.
136 * @throws Exception when i/o error.
139 public void testReadWithSeek() throws Exception {
140 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
144 byte[] buf = new byte[len];
146 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
148 din.read(buf, 0, len);
149 assertTrue(Arrays.equals(buf, expResult));
154 * Test of read method, of class DictZipInputStream.
155 * @throws Exception when i/o error.
158 public void testReadNull() throws Exception {
159 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
164 din.read(buf, 0, len);
165 Assertions.fail("Should be throw exception.");
166 } catch (NullPointerException e) {
170 assertTrue(r, "Got NullPointerException when buffer is null");
175 * Test of read method, of class DictZipInputStream.
176 * @throws Exception when i/o error.
179 public void testReadOutOfBound() throws Exception {
180 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
182 byte[] buf = new byte[len];
185 din.read(buf, 0, len + 10);
186 Assertions.fail("Should be throw exception.");
187 } catch (IndexOutOfBoundsException e) {
191 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
196 * Test of read method, of class DictZipInputStream.
197 * @throws Exception when i/o error.
200 public void testReadZeroSize() throws Exception {
201 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
203 byte[] buf = new byte[len];
204 int size = din.read(buf, 0, 0);
205 assertEquals(size, 0);
210 * Test of read method, of class DictZipInputStream.
211 * @throws Exception when i/o error.
214 public void testReadWithSeekLast() throws Exception {
215 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
219 byte[] buf = new byte[len];
220 din.read(buf, 0, len);
221 int result = din.read(buf, 0, len);
222 assertEquals(result, -1);
227 * Test of readFully method, of class DictZipInputStream.
228 * @throws Exception when i/o error.
231 public void testReadFullyByteArr() throws Exception {
232 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
236 byte[] buf = new byte[len];
242 * Test of readFully method, of class DictZipInputStream.
243 * @throws Exception when i/o error.
246 public void testReadFully3args() throws Exception {
247 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
251 byte[] buf = new byte[len];
252 din.readFully(buf, 0, len);
257 * Test of readFully method, of class DictZipInputStream.
258 * @throws IOException when i/o error.
261 public void testReadFullyReadTrailer() throws IOException {
262 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
263 byte[] buf = new byte[512];
266 } catch (EOFException e) {
271 assertEquals(din.getCrc(), 0x024d1f37);
272 assertEquals(din.getLength(), 383783);
277 * Test getComplength method.
278 * @throws IOException when i/o error.
281 public void testGetCompLength() throws IOException {
282 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
283 System.out.println("getCompLength");
284 assertEquals(din.getCompLength(), 136856);
289 * Test getMtime method.
290 * @throws IOException when i/o error.
293 public void testGetMtime() throws IOException {
294 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
295 assertEquals(din.getMtime(), 1193780332);
300 * Test getChunkLength method.
301 * @throws IOException when i/o error.
304 public void testGetChunkLength() throws IOException {
305 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
306 System.out.println("getChunkLength");
307 assertEquals(din.getChunkLength(), 58315);
312 * Test getChunkCount method.
313 * @throws IOException when i/o error.
316 public void testGetChunkCount() throws IOException {
317 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
318 System.out.println("getChunkCount");
319 assertEquals(din.getChunkCount(), 7);
324 * Test getFilename method.
325 * @throws IOException when i/o error.
328 public void testGetFilename() throws IOException {
329 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
330 assertEquals(din.getFilename(), "results.dict");
335 * Test readFully with large seek.
336 * @throws Exception when i/o error.
339 public void testReadFullySeek2() throws Exception {
340 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
345 } catch (IOException ioe) {
346 Assertions.fail("Unexpected IOException");
348 byte[] buf = new byte[len];
351 } catch (EOFException eofe) {
352 Assertions.fail("Unexpected EOF");
358 * Test with large seek.
359 * @throws Exception when i/o error.
362 public void testReadSeek2() throws Exception {
363 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
364 System.out.println("read_seek2");
369 } catch (IOException ioe) {
370 Assertions.fail("Unexpected IOException");
372 byte[] buf = new byte[len];
375 result = din.read(buf, 0, len);
376 } catch (EOFException eofe) {
377 Assertions.fail("Unexpected EOF");
379 assertEquals(result, len);
384 * Test with large seek comparison content.
385 * @throws Exception when i/o error.
388 public void testReadSeek3() throws Exception {
389 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
394 } catch (IOException ioe) {
395 Assertions.fail("Unexpected IOException");
397 FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
399 assertTrue(IOUtils2.contentEquals(din, in2, 0, len));