1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.commons.compress.archivers.zip;
21
22 import static org.junit.jupiter.api.Assertions.assertEquals;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.io.ByteArrayInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29
30 import org.junit.jupiter.api.Test;
31
32 public class BinaryTreeTest {
33
34 @Test
35 public void testDecode() throws IOException {
36 final InputStream in = new ByteArrayInputStream(new byte[] { 0x02, 0x42, 0x01, 0x13 });
37
38 final BinaryTree tree = BinaryTree.decode(in, 8);
39
40 assertNotNull(tree);
41
42 try (BitStream stream = new BitStream(new ByteArrayInputStream(new byte[] { (byte) 0x8D, (byte) 0xC5, (byte) 0x11, 0x00 }))) {
43 assertEquals(0, tree.read(stream));
44 assertEquals(1, tree.read(stream));
45 assertEquals(2, tree.read(stream));
46 assertEquals(3, tree.read(stream));
47 assertEquals(4, tree.read(stream));
48 assertEquals(5, tree.read(stream));
49 assertEquals(6, tree.read(stream));
50 assertEquals(7, tree.read(stream));
51 }
52 }
53
54 @Test
55 public void testExceptions() {
56 final BinaryTree binaryFinary = new BinaryTree(4);
57 binaryFinary.addLeaf(0, 0, 0, 1);
58 assertThrows(IllegalArgumentException.class, () -> binaryFinary.addLeaf(0, 0, 0, 1));
59
60 final InputStream is = new ByteArrayInputStream(new byte[] {});
61 assertThrows(IOException.class, () -> BinaryTree.decode(is, 0));
62 assertThrows(IOException.class, () -> new BinaryTree(4).read(new BitStream(new ByteArrayInputStream(new byte[] { 0 }))));
63 }
64 }