jdk/test/java/util/zip/ChecksumBase.java
author sherman
Fri, 21 Nov 2014 09:28:53 -0800
changeset 27737 0a3fb0529425
child 32649 2ee9017c7597
permissions -rw-r--r--
6321472: Add CRC-32C API Summary: to add CRC-32C api and implementation Reviewed-by: sherman Contributed-by: staffan.friberg@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
27737
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     1
/*
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     2
 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     4
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     8
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    13
 * accompanied this code).
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    14
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    18
 *
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    21
 * questions.
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    22
 */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    23
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    24
/**
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    25
 * Base class for Checksum tests
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    26
 */
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    27
import java.nio.ByteBuffer;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    28
import java.nio.ByteOrder;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    29
import java.nio.charset.StandardCharsets;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    30
import java.util.zip.Checksum;
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    31
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    32
public class ChecksumBase {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    33
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    34
    private final static byte[] BYTES_123456789 = "123456789".getBytes(StandardCharsets.US_ASCII);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    35
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    36
    public static void testAll(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    37
        testBytes(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    38
        testByteArray(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    39
        testWrappedByteBuffer(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    40
        testReadonlyByteBuffer(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    41
        testDirectByteBuffer(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    42
        testByteArrayOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    43
        testDirectByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    44
        testLittleEndianDirectByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    45
        testWrappedByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    46
        testLittleEndianWrappedByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    47
        testReadonlyByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    48
        testLittleEndianReadonlyByteBufferOffset(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    49
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    50
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    51
    private static void testBytes(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    52
        checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    53
        for (byte bits : BYTES_123456789) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    54
            checksum.update(bits);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    55
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    56
        checkChecksum(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    57
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    58
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    59
    private static void testByteArray(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    60
        checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    61
        checksum.update(BYTES_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    62
        checkChecksum(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    63
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    64
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    65
    private static void testWrappedByteBuffer(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    66
        checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    67
        ByteBuffer bb = ByteBuffer.wrap(BYTES_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    68
        checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    69
        checkChecksum(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    70
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    71
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    72
    private static void testReadonlyByteBuffer(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    73
        checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    74
        ByteBuffer bb = ByteBuffer.wrap(BYTES_123456789).asReadOnlyBuffer();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    75
        checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    76
        checkChecksum(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    77
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    78
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    79
    private static void testDirectByteBuffer(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    80
        checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    81
        ByteBuffer bb = ByteBuffer.allocateDirect(BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    82
        bb.put(BYTES_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    83
        bb.rewind();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    84
        checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    85
        checkChecksum(checksum, expected);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    86
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    87
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    88
    private static void checkChecksum(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    89
        if (checksum.getValue() != expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    90
            throw new RuntimeException("Calculated checksum result was invalid."
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    91
                    + " Expected " + Long.toHexString(expected)
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    92
                    + ", but got " + Long.toHexString(checksum.getValue()) + ".");
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    93
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    94
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    95
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    96
    private static void testByteArrayOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    97
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    98
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
    99
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   100
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   101
            checksum.update(unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   102
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   103
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   104
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   105
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   106
    private static void testDirectByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   107
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   108
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   109
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   110
            ByteBuffer bb = ByteBuffer.allocateDirect(unaligned_bytes_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   111
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   112
            bb.put(unaligned_bytes_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   113
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   114
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   115
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   116
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   117
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   118
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   119
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   120
    private static void testLittleEndianDirectByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   121
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   122
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   123
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   124
            ByteBuffer bb = ByteBuffer.allocateDirect(unaligned_bytes_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   125
            bb.order(ByteOrder.LITTLE_ENDIAN);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   126
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   127
            bb.put(unaligned_bytes_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   128
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   129
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   130
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   131
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   132
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   133
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   134
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   135
    private static void testWrappedByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   136
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   137
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   138
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   139
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   140
            ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   141
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   142
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   143
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   144
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   145
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   146
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   147
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   148
    private static void testLittleEndianWrappedByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   149
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   150
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   151
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   152
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   153
            ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   154
            bb.order(ByteOrder.LITTLE_ENDIAN);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   155
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   156
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   157
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   158
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   159
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   160
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   161
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   162
    private static void testReadonlyByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   163
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   164
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   165
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   166
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   167
            ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   168
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   169
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   170
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   171
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   172
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   173
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   174
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   175
    private static void testLittleEndianReadonlyByteBufferOffset(Checksum checksum, long expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   176
        byte[] unaligned_bytes_123456789 = new byte[BYTES_123456789.length + 64];
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   177
        for (int i = 0; i < unaligned_bytes_123456789.length - BYTES_123456789.length; i++) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   178
            checksum.reset();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   179
            System.arraycopy(BYTES_123456789, 0, unaligned_bytes_123456789, i, BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   180
            ByteBuffer bb = ByteBuffer.wrap(unaligned_bytes_123456789).asReadOnlyBuffer();
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   181
            bb.order(ByteOrder.LITTLE_ENDIAN);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   182
            bb.position(i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   183
            bb.limit(i + BYTES_123456789.length);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   184
            checksum.update(bb);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   185
            checkChecksumOffset(checksum, expected, i);
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   186
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   187
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   188
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   189
    private static void checkChecksumOffset(Checksum checksum, long expected, int offset) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   190
        if (checksum.getValue() != expected) {
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   191
            throw new RuntimeException("Calculated CRC32C result was invalid. Array offset "
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   192
                    + offset + ". Expected: " + Long.toHexString(expected) + ", Got: "
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   193
                    + Long.toHexString(checksum.getValue()));
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   194
        }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   195
    }
0a3fb0529425 6321472: Add CRC-32C API
sherman
parents:
diff changeset
   196
}