Path: blob/master/test/jdk/tools/jlink/plugins/CompressIndexesTest.java
41149 views
/*1* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @summary Test CompressIndexes26* @author Jean-Francois Denise27* @modules java.base/jdk.internal.jimage.decompressor28* @run main CompressIndexesTest29*/3031import java.io.ByteArrayInputStream;32import java.io.DataInputStream;33import java.io.IOException;34import java.nio.ByteBuffer;35import java.util.ArrayList;36import java.util.List;3738import jdk.internal.jimage.decompressor.CompressIndexes;3940public class CompressIndexesTest {4142public static void main(String[] args) throws IOException {43new CompressIndexesTest().test();44}4546public void test() throws IOException {47int[] data = {48// compressed length 1490x00000000,500x00000001,510x0000000F,520x0000001F,53// compressed length 2540x0000002F,550x00000100,560x00001FFF,57// compressed length 3580x00002FFF,590x00010000,600x001FFFFF,61// compressed length 4620x00200000,630x01000000,64Integer.MAX_VALUE65};66int[] intervals = {4, 7, 10, data.length};67List<byte[]> arrays = new ArrayList<>();68int length = 0;69int begin = 0;70for (int interval : intervals) {71++length;72for (int j = begin; j < interval; ++j) {73arrays.add(check(data[j], length));74}75begin = interval;76}7778int totalLength = arrays.stream().mapToInt(b -> b.length).sum();79ByteBuffer all = ByteBuffer.allocate(totalLength);80arrays.forEach(all::put);81byte[] flow = all.array();82check(flow, arrays);83System.err.println(arrays.size() * 4 + " compressed in " + flow.length84+ " gain of " + (100 - ((flow.length * 100) / (arrays.size() * 4))) + "%");85try (DataInputStream is = new DataInputStream(new ByteArrayInputStream(flow))) {86int index = 0;87while (is.available() > 0) {88int d = CompressIndexes.readInt(is);89if (data[index] != d) {90throw new AssertionError("Expected: " + data[index] + ", got: " + d);91}92++index;93}94}95}9697private void check(byte[] flow, List<byte[]> arrays) {98List<Integer> d = CompressIndexes.decompressFlow(flow);99List<Integer> dd = new ArrayList<>();100for (byte[] b : arrays) {101int i = CompressIndexes.decompress(b, 0);102dd.add(i);103}104if (!d.equals(dd)) {105System.err.println(dd);106System.err.println(d);107throw new AssertionError("Invalid flow " + d);108} else {109System.err.println("OK for flow");110}111}112113private byte[] check(int val, int size) {114byte[] c = CompressIndexes.compress(val);115if (c.length != size) {116throw new AssertionError("Invalid compression size " + c.length);117}118int d = CompressIndexes.decompress(c, 0);119if (val != d) {120throw new AssertionError("Invalid " + d);121} else {122System.err.println("Ok for " + val);123}124return c;125}126}127128129