|
1 /* |
|
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. |
|
8 * |
|
9 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 * version 2 for more details (a copy is included in the LICENSE file that |
|
13 * accompanied this code). |
|
14 * |
|
15 * You should have received a copy of the GNU General Public License version |
|
16 * 2 along with this work; if not, write to the Free Software Foundation, |
|
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 * |
|
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 * or visit www.oracle.com if you need additional information or have any |
|
21 * questions. |
|
22 */ |
|
23 |
|
24 /* |
|
25 * @test |
|
26 * @summary Test CompressIndexes |
|
27 * @author Jean-Francois Denise |
|
28 * @modules java.base/jdk.internal.jimage.decompressor |
|
29 * @run main CompressIndexesTest |
|
30 */ |
|
31 |
|
32 import java.io.ByteArrayInputStream; |
|
33 import java.io.DataInputStream; |
|
34 import java.io.IOException; |
|
35 import java.nio.ByteBuffer; |
|
36 import java.util.ArrayList; |
|
37 import java.util.List; |
|
38 |
|
39 import jdk.internal.jimage.decompressor.CompressIndexes; |
|
40 |
|
41 public class CompressIndexesTest { |
|
42 |
|
43 public static void main(String[] args) throws IOException { |
|
44 new CompressIndexesTest().test(); |
|
45 } |
|
46 |
|
47 public void test() throws IOException { |
|
48 int[] data = { |
|
49 // compressed length 1 |
|
50 0x00000000, |
|
51 0x00000001, |
|
52 0x0000000F, |
|
53 0x0000001F, |
|
54 // compressed length 2 |
|
55 0x0000002F, |
|
56 0x00000100, |
|
57 0x00001FFF, |
|
58 // compressed length 3 |
|
59 0x00002FFF, |
|
60 0x00010000, |
|
61 0x001FFFFF, |
|
62 // compressed length 4 |
|
63 0x00200000, |
|
64 0x01000000, |
|
65 Integer.MAX_VALUE |
|
66 }; |
|
67 int[] intervals = {4, 7, 10, data.length}; |
|
68 List<byte[]> arrays = new ArrayList<>(); |
|
69 int length = 0; |
|
70 int begin = 0; |
|
71 for (int interval : intervals) { |
|
72 ++length; |
|
73 for (int j = begin; j < interval; ++j) { |
|
74 arrays.add(check(data[j], length)); |
|
75 } |
|
76 begin = interval; |
|
77 } |
|
78 |
|
79 int totalLength = arrays.stream().mapToInt(b -> b.length).sum(); |
|
80 ByteBuffer all = ByteBuffer.allocate(totalLength); |
|
81 arrays.forEach(all::put); |
|
82 byte[] flow = all.array(); |
|
83 check(flow, arrays); |
|
84 System.err.println(arrays.size() * 4 + " compressed in " + flow.length |
|
85 + " gain of " + (100 - ((flow.length * 100) / (arrays.size() * 4))) + "%"); |
|
86 try (DataInputStream is = new DataInputStream(new ByteArrayInputStream(flow))) { |
|
87 int index = 0; |
|
88 while (is.available() > 0) { |
|
89 int d = CompressIndexes.readInt(is); |
|
90 if (data[index] != d) { |
|
91 throw new AssertionError("Expected: " + data[index] + ", got: " + d); |
|
92 } |
|
93 ++index; |
|
94 } |
|
95 } |
|
96 } |
|
97 |
|
98 private void check(byte[] flow, List<byte[]> arrays) { |
|
99 List<Integer> d = CompressIndexes.decompressFlow(flow); |
|
100 List<Integer> dd = new ArrayList<>(); |
|
101 for (byte[] b : arrays) { |
|
102 int i = CompressIndexes.decompress(b, 0); |
|
103 dd.add(i); |
|
104 } |
|
105 if (!d.equals(dd)) { |
|
106 System.err.println(dd); |
|
107 System.err.println(d); |
|
108 throw new AssertionError("Invalid flow " + d); |
|
109 } else { |
|
110 System.err.println("OK for flow"); |
|
111 } |
|
112 } |
|
113 |
|
114 private byte[] check(int val, int size) { |
|
115 byte[] c = CompressIndexes.compress(val); |
|
116 if (c.length != size) { |
|
117 throw new AssertionError("Invalid compression size " + c.length); |
|
118 } |
|
119 int d = CompressIndexes.decompress(c, 0); |
|
120 if (val != d) { |
|
121 throw new AssertionError("Invalid " + d); |
|
122 } else { |
|
123 System.err.println("Ok for " + val); |
|
124 } |
|
125 return c; |
|
126 } |
|
127 } |