001/*
002 * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.hotspot.test;
024
025import java.io.*;
026import java.nio.*;
027import java.util.zip.*;
028
029import org.junit.*;
030
031import com.oracle.graal.compiler.test.*;
032
033/**
034 * Tests compiled call to {@link CRC32#update(int, int)}.
035 */
036@SuppressWarnings("javadoc")
037public class CRC32SubstitutionsTest extends GraalCompilerTest {
038
039    public static long update(byte[] input) {
040        CRC32 crc = new CRC32();
041        for (byte b : input) {
042            crc.update(b);
043        }
044        return crc.getValue();
045    }
046
047    @Test
048    public void test1() {
049        test("update", "some string".getBytes());
050    }
051
052    public static long updateBytes(byte[] input, int offset, int length) {
053        CRC32 crc = new CRC32();
054        crc.update(input, offset, length);
055        return crc.getValue();
056    }
057
058    @Test
059    public void test2() {
060        byte[] buf = "some string".getBytes();
061        int off = 0;
062        int len = buf.length;
063        test("updateBytes", buf, off, len);
064    }
065
066    @Test
067    public void test3() throws Throwable {
068        String classfileName = CRC32SubstitutionsTest.class.getSimpleName().replace('.', '/') + ".class";
069        InputStream s = CRC32SubstitutionsTest.class.getResourceAsStream(classfileName);
070        byte[] buf = new byte[s.available()];
071        new DataInputStream(s).readFully(buf);
072        test("updateBytes", buf, 0, buf.length);
073        for (int offset = 1; offset < buf.length; offset++) {
074            test("updateBytes", buf, offset, buf.length - offset);
075        }
076    }
077
078    public static long updateByteBuffer(ByteBuffer buffer) {
079        CRC32 crc = new CRC32();
080        buffer.rewind();
081        crc.update(buffer);
082        return crc.getValue();
083    }
084
085    @Test
086    public void test4() throws Throwable {
087        String classfileName = CRC32SubstitutionsTest.class.getSimpleName().replace('.', '/') + ".class";
088        InputStream s = CRC32SubstitutionsTest.class.getResourceAsStream(classfileName);
089        byte[] buf = new byte[s.available()];
090        new DataInputStream(s).readFully(buf);
091
092        ByteBuffer directBuf = ByteBuffer.allocateDirect(buf.length);
093        directBuf.put(buf);
094        ByteBuffer heapBuf = ByteBuffer.wrap(buf);
095
096        test("updateByteBuffer", directBuf);
097        test("updateByteBuffer", heapBuf);
098    }
099
100}