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