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;
43 import java.io.EOFException;
44 import java.io.FileInputStream;
45 import java.io.IOException;
46 import java.util.Arrays;
48 import static org.junit.jupiter.api.Assertions.assertEquals;
49 import static org.junit.jupiter.api.Assertions.assertTrue;
50 import static tokyo.northside.io.IOUtils2.contentEquals;
53 * Test of DictZipInputStream.
54 * @author Hiroshi Miura
56 public class DictZipInputStreamTest {
58 private final String dataFile = this.getClass().getResource("/test.dict.dz").getFile();
61 * Test constructor @TestFactory.
62 * @throws Exception when i/o error.
65 public void testConstructorDefaultBufSize() throws Exception {
66 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"))) {
72 * Test constructor from filename.
73 * @throws Exception when i/o error.
76 public void testConstructorFromFilename() throws Exception {
77 try (DictZipInputStream din = new DictZipInputStream(dataFile)) {
83 * Test of read method, of class DictZipInputStream.
84 * @throws Exception when i/o error.
87 public void testRead() throws Exception {
89 byte[] buf = new byte[len];
90 byte[] expResult = {0x70, 0x72, (byte) 0xc3, (byte) 0xa9, 0x70, 0x2e, 0x20, 0x3a, 0x20, 0x2b};
91 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
93 din.read(buf, 0, len);
94 assertTrue(Arrays.equals(expResult, buf));
99 * Test of read method, of class DictZipInputStream.
100 * @throws Exception when i/o error.
103 public void testReadWithSeek() throws Exception {
104 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
108 byte[] buf = new byte[len];
110 0x61, 0x70, 0x72, (byte) 0xc3, (byte) 0xa8, 0x73, 0x20, 0x75, 0x6e, 0x20
112 din.read(buf, 0, len);
113 assertTrue(Arrays.equals(buf, expResult));
118 * Test of read method, of class DictZipInputStream.
119 * @throws Exception when i/o error.
122 public void testReadNull() throws Exception {
123 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
128 din.read(buf, 0, len);
129 Assertions.fail("Should be throw exception.");
130 } catch (NullPointerException e) {
134 assertTrue(r, "Got NullPointerException when buffer is null");
139 * Test of read method, of class DictZipInputStream.
140 * @throws Exception when i/o error.
143 public void testReadOutOfBound() throws Exception {
144 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
146 byte[] buf = new byte[len];
149 din.read(buf, 0, len + 10);
150 Assertions.fail("Should be throw exception.");
151 } catch (IndexOutOfBoundsException e) {
155 assertTrue(r, "Got IndexOutOfBoundException when size is over the buffer size");
160 * Test of read method, of class DictZipInputStream.
161 * @throws Exception when i/o error.
164 public void testReadZeroSize() throws Exception {
165 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
167 byte[] buf = new byte[len];
168 int size = din.read(buf, 0, 0);
169 assertEquals(size, 0);
174 * Test of read method, of class DictZipInputStream.
175 * @throws Exception when i/o error.
178 public void testReadWithSeekLast() throws Exception {
179 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
183 byte[] buf = new byte[len];
184 din.read(buf, 0, len);
185 int result = din.read(buf, 0, len);
186 assertEquals(result, -1);
191 * Test of readFully method, of class DictZipInputStream.
192 * @throws Exception when i/o error.
195 public void testReadFullyByteArr() throws Exception {
196 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
200 byte[] buf = new byte[len];
206 * Test of readFully method, of class DictZipInputStream.
207 * @throws Exception when i/o error.
210 public void testReadFully3args() throws Exception {
211 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
215 byte[] buf = new byte[len];
216 din.readFully(buf, 0, len);
221 * Test of readFully method, of class DictZipInputStream.
222 * @throws IOException when i/o error.
225 public void testReadFullyReadTrailer() throws IOException {
226 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
227 byte[] buf = new byte[512];
230 } catch (EOFException e) {
235 assertEquals(din.getCrc(), 0x024d1f37);
236 assertEquals(din.getLength(), 383783);
241 * Test getComplength method.
242 * @throws IOException when i/o error.
245 public void testGetCompLength() throws IOException {
246 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
247 System.out.println("getCompLength");
248 assertEquals(din.getCompLength(), 136856);
253 * Test getMtime method.
254 * @throws IOException when i/o error.
257 public void testGetMtime() throws IOException {
258 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
259 assertEquals(din.getMtime(), 1193780332);
264 * Test getChunkLength method.
265 * @throws IOException when i/o error.
268 public void testGetChunkLength() throws IOException {
269 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
270 System.out.println("getChunkLength");
271 assertEquals(din.getChunkLength(), 58315);
276 * Test getChunkCount method.
277 * @throws IOException when i/o error.
280 public void testGetChunkCount() throws IOException {
281 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
282 System.out.println("getChunkCount");
283 assertEquals(din.getChunkCount(), 7);
288 * Test getFilename method.
289 * @throws IOException when i/o error.
292 public void testGetFilename() throws IOException {
293 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
294 assertEquals(din.getFilename(), "results.dict");
299 * Test readFully with large seek.
300 * @throws Exception when i/o error.
303 public void testReadFullySeek2() throws Exception {
304 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
309 } catch (IOException ioe) {
310 Assertions.fail("Unexpected IOException");
312 byte[] buf = new byte[len];
315 } catch (EOFException eofe) {
316 Assertions.fail("Unexpected EOF");
322 * Test with large seek.
323 * @throws Exception when i/o error.
326 public void testReadSeek2() throws Exception {
327 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
328 System.out.println("read_seek2");
333 } catch (IOException ioe) {
334 Assertions.fail("Unexpected IOException");
336 byte[] buf = new byte[len];
339 result = din.read(buf, 0, len);
340 } catch (EOFException eofe) {
341 Assertions.fail("Unexpected EOF");
343 assertEquals(result, len);
348 * Test with large seek comparison content.
349 * @throws Exception when i/o error.
352 public void testReadSeek3() throws Exception {
353 try (DictZipInputStream din = new DictZipInputStream(new RandomAccessInputStream(dataFile, "r"), 65534)) {
358 } catch (IOException ioe) {
359 Assertions.fail("Unexpected IOException");
361 FileInputStream in2 = new FileInputStream(this.getClass().getResource("/test.dict.expected").getFile());
363 assertTrue(contentEquals(din, in2, 0, len));