CRC32SubstitutionsTest.java revision 12651:6ef01bd40ce2
11573Srgrimes/*
252867Sjkoshy * Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
31573Srgrimes * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4159503Sakiyama *
5159503Sakiyama * This code is free software; you can redistribute it and/or modify it
6159467Sdelphij * under the terms of the GNU General Public License version 2 only, as
7159467Sdelphij * published by the Free Software Foundation.
81573Srgrimes *
9202216Sed * This code is distributed in the hope that it will be useful, but WITHOUT
10159503Sakiyama * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11185237Sscf * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12221807Sstas * version 2 for more details (a copy is included in the LICENSE file that
13221807Sstas * accompanied this code).
14202216Sed *
15180161Sjhb * You should have received a copy of the GNU General Public License version
16207736Smckusick * 2 along with this work; if not, write to the Free Software Foundation,
17169446Sdes * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1855955Srgrimes *
1964918Sgreen * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20160745Syar * or visit www.oracle.com if you need additional information or have any
21160745Syar * questions.
22160745Syar */
23160745Syarpackage org.graalvm.compiler.hotspot.test;
24160745Syar
25160745Syarimport java.io.DataInputStream;
26160745Syarimport java.io.InputStream;
27160745Syarimport java.nio.ByteBuffer;
28236965Sdesimport java.util.zip.CRC32;
29230016Sghelmer
30230016Sghelmerimport org.junit.Test;
31230016Sghelmer
32230016Sghelmerimport org.graalvm.compiler.core.test.GraalCompilerTest;
33242381Sbapt
34236965Sdes/**
35155804Sdes * Tests compiled call to {@link CRC32#update(int, int)}.
36230016Sghelmer */
37230016Sghelmer@SuppressWarnings("javadoc")
38230016Sghelmerpublic class CRC32SubstitutionsTest extends GraalCompilerTest {
39230016Sghelmer
40230016Sghelmer    public static long update(byte[] input) {
41230016Sghelmer        CRC32 crc = new CRC32();
42230016Sghelmer        for (byte b : input) {
43230016Sghelmer            crc.update(b);
44230016Sghelmer        }
45230016Sghelmer        return crc.getValue();
46230016Sghelmer    }
47230016Sghelmer
48230016Sghelmer    @Test
49230016Sghelmer    public void test1() {
50230016Sghelmer        test("update", "some string".getBytes());
51230016Sghelmer    }
52230016Sghelmer
53230016Sghelmer    public static long updateBytes(byte[] input, int offset, int length) {
54230016Sghelmer        CRC32 crc = new CRC32();
55229943Sghelmer        crc.update(input, offset, length);
56230016Sghelmer        return crc.getValue();
57230016Sghelmer    }
58230016Sghelmer
59230016Sghelmer    @Test
60230016Sghelmer    public void test2() {
61207736Smckusick        byte[] buf = "some string".getBytes();
62230016Sghelmer        int off = 0;
63207736Smckusick        int len = buf.length;
64230016Sghelmer        test("updateBytes", buf, off, len);
65207736Smckusick    }
66207736Smckusick
67230016Sghelmer    @Test
68230016Sghelmer    public void test3() throws Throwable {
69230016Sghelmer        String classfileName = CRC32SubstitutionsTest.class.getSimpleName().replace('.', '/') + ".class";
70242381Sbapt        InputStream s = CRC32SubstitutionsTest.class.getResourceAsStream(classfileName);
71242381Sbapt        byte[] buf = new byte[s.available()];
72242381Sbapt        new DataInputStream(s).readFully(buf);
73242381Sbapt        test("updateBytes", buf, 0, buf.length);
74242381Sbapt        for (int offset = 1; offset < buf.length; offset++) {
75242381Sbapt            test("updateBytes", buf, offset, buf.length - offset);
76242381Sbapt        }
77242381Sbapt    }
78242381Sbapt
79242381Sbapt    public static long updateByteBuffer(ByteBuffer buffer) {
80242381Sbapt        CRC32 crc = new CRC32();
81242381Sbapt        buffer.rewind();
82242381Sbapt        crc.update(buffer);
831573Srgrimes        return crc.getValue();
841573Srgrimes    }
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}
102