1 /*
   2  * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.hotspot.test;
  24 
  25 import java.io.DataInputStream;
  26 import java.io.InputStream;
  27 import java.nio.ByteBuffer;
  28 import java.util.zip.CRC32;
  29 
  30 import org.junit.Test;
  31 
  32 import org.graalvm.compiler.core.test.GraalCompilerTest;
  33 
  34 /**
  35  * Tests compiled call to {@link CRC32#update(int, int)}.
  36  */
  37 @SuppressWarnings("javadoc")
  38 public class CRC32SubstitutionsTest extends GraalCompilerTest {
  39 
  40     public static long update(byte[] input) {
  41         CRC32 crc = new CRC32();
  42         for (byte b : input) {
  43             crc.update(b);
  44         }
  45         return crc.getValue();
  46     }
  47 
  48     @Test
  49     public void test1() {
  50         test("update", "some string".getBytes());
  51     }
  52 
  53     public static long updateBytes(byte[] input, int offset, int length) {
  54         CRC32 crc = new CRC32();
  55         crc.update(input, offset, length);
  56         return crc.getValue();
  57     }
  58 
  59     @Test
  60     public void test2() {
  61         byte[] buf = "some string".getBytes();
  62         int off = 0;
  63         int len = buf.length;
  64         test("updateBytes", buf, off, len);
  65     }
  66 
  67     @Test
  68     public void test3() throws Throwable {
  69         String classfileName = CRC32SubstitutionsTest.class.getSimpleName().replace('.', '/') + ".class";
  70         InputStream s = CRC32SubstitutionsTest.class.getResourceAsStream(classfileName);
  71         byte[] buf = new byte[s.available()];
  72         new DataInputStream(s).readFully(buf);
  73         test("updateBytes", buf, 0, buf.length);
  74         for (int offset = 1; offset < buf.length; offset++) {
  75             test("updateBytes", buf, offset, buf.length - offset);
  76         }
  77     }
  78 
  79     public static long updateByteBuffer(ByteBuffer buffer) {
  80         CRC32 crc = new CRC32();
  81         buffer.rewind();
  82         crc.update(buffer);
  83         return crc.getValue();
  84     }
  85 
  86     @Test
  87     public void test4() throws Throwable {
  88         String classfileName = CRC32SubstitutionsTest.class.getSimpleName().replace('.', '/') + ".class";
  89         InputStream s = CRC32SubstitutionsTest.class.getResourceAsStream(classfileName);
  90         byte[] buf = new byte[s.available()];
  91         new DataInputStream(s).readFully(buf);
  92 
  93         ByteBuffer directBuf = ByteBuffer.allocateDirect(buf.length);
  94         directBuf.put(buf);
  95         ByteBuffer heapBuf = ByteBuffer.wrap(buf);
  96 
  97         test("updateByteBuffer", directBuf);
  98         test("updateByteBuffer", heapBuf);
  99     }
 100 
 101 }