package org.agrona;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

import java.nio.ByteBuffer;
import java.util.zip.CRC32;

import static org.agrona.BufferUtil.address;
import static org.agrona.BufferUtil.allocateDirectAligned;
import static org.agrona.Checksums.crc32;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

class ChecksumsTest
{

    @Test
    void crc32DirectByteBufferShouldComputeCorrectCrc32Checksum()
    {
        final CRC32 crc32 = new CRC32();
        final byte[] data = new byte[23];
        for (int i = 0; i < data.length; i++)
        {
            data[i] = (byte)i;
            crc32.update(i);
        }
        final int checksum1 = (int)crc32.getValue();
        crc32.update(new byte[10]);
        final int checksum2 = (int)crc32.getValue();

        final ByteBuffer buffer = allocateDirectAligned(100, 64);
        buffer.position(5);
        buffer.put(data);
        final long address = address(buffer);

        assertEquals(checksum1, crc32(0, address, 5, 23));
        assertEquals(checksum2, crc32(checksum1, address, 28, 10));
        assertNotEquals(checksum2, crc32(0, address, 28, 10));
        assertEquals(checksum1, crc32(0, address, 5, 23));
    }

    @ParameterizedTest
    @CsvSource({ "-1,10", "33, 5", "0,-10", "0,100" })
    void crc32DirectByteBufferProducesGarbageWhenWrongOffsetOrLengthSpecified(final int offset, final int length)
    {
        final ByteBuffer buffer = allocateDirectAligned(32, 32);
        final long address = address(buffer);

        assertNotEquals(0, crc32(0, address, offset, length));
    }
}
@@ -1,8 +1,609 @@
+/*%0A * Copyright 2014-2019 Real Logic Ltd.%0A *%0A * Licensed under the Apache License, Version 2.0 (the %22License%22);%0A * you may not use this file except in compliance with the License.%0A * You may obtain a copy of the License at%0A *%0A * https://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A
 package 
@@ -1105,17 +1105,16 @@
 sTest%0A%7B%0A
-%0A
     @Tes
@@ -1396,16 +1396,17 @@
       %7D%0A
+%0A
         
@@ -1446,24 +1446,24 @@
 getValue();%0A
-
         crc3
@@ -1483,16 +1483,23 @@
 byte%5B10%5D
+, 0, 10
 );%0A     

Fix deprecated warning and apply copyright.