001/*
002 * Copyright (c) 2013, 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 */
023
024package com.oracle.graal.hotspot.test;
025
026import java.util.*;
027import java.util.concurrent.atomic.*;
028
029import jdk.internal.jvmci.hotspot.*;
030import jdk.internal.jvmci.meta.*;
031
032import org.junit.*;
033
034import com.oracle.graal.compiler.test.*;
035
036/**
037 * The following tests perform object/array equality and assignments in various ways. The selected
038 * cases have been the problematic ones while implementing the Compressed Oops support.
039 */
040public class CompressedOopTest extends GraalCompilerTest {
041
042    private HotSpotInstalledCode getInstalledCode(String name, Class<?>... parameterTypes) throws Exception {
043        final ResolvedJavaMethod javaMethod = getResolvedJavaMethod(getClass(), name, parameterTypes);
044        final HotSpotInstalledCode installedBenchmarkCode = (HotSpotInstalledCode) getCode(javaMethod);
045        return installedBenchmarkCode;
046    }
047
048    @Test
049    public void test() throws Exception {
050        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("fieldTest", Object.class);
051        Container c1 = new Container();
052        Assert.assertEquals(c1.b, installedBenchmarkCode.executeVarargs(c1));
053    }
054
055    public static Object fieldTest(Object c1) {
056        ((Container) c1).a = ((Container) c1).b;
057        return ((Container) c1).a;
058    }
059
060    @Test
061    public void test1() throws Exception {
062        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("arrayTest", Object.class, Object.class, Object.class);
063        ArrayContainer ac = new ArrayContainer();
064        Assert.assertEquals(ac.a[9], installedBenchmarkCode.executeVarargs(ac.a, 0, 9));
065        Assert.assertEquals(ac.a[8], installedBenchmarkCode.executeVarargs(ac.a, 1, 8));
066        Assert.assertEquals(ac.a[7], installedBenchmarkCode.executeVarargs(ac.a, 2, 7));
067        Assert.assertEquals(ac.a[6], installedBenchmarkCode.executeVarargs(ac.a, 3, 6));
068        Assert.assertEquals(ac.a[5], installedBenchmarkCode.executeVarargs(ac.a, 4, 5));
069        Assert.assertEquals(ac.a[4], installedBenchmarkCode.executeVarargs(ac.a, 5, 4));
070        Assert.assertEquals(ac.a[3], installedBenchmarkCode.executeVarargs(ac.a, 6, 3));
071        Assert.assertEquals(ac.a[2], installedBenchmarkCode.executeVarargs(ac.a, 7, 2));
072        Assert.assertEquals(ac.a[1], installedBenchmarkCode.executeVarargs(ac.a, 8, 1));
073        Assert.assertEquals(ac.a[0], installedBenchmarkCode.executeVarargs(ac.a, 9, 0));
074    }
075
076    public static Object arrayTest(Object c1, Object c2, Object c3) {
077        Object[] array = (Object[]) c1;
078        int initialIndex = ((Integer) c2).intValue();
079        int replacingIndex = ((Integer) c3).intValue();
080        array[initialIndex] = array[replacingIndex];
081        return array[initialIndex];
082    }
083
084    @Test
085    public void test2() throws Exception {
086        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("arrayCopyTest", Object.class, Object.class);
087        ArrayContainer source = new ArrayContainer();
088        ArrayContainer destination = new ArrayContainer();
089        Assert.assertEquals(source.a.length, destination.a.length);
090        Assert.assertFalse(Arrays.equals(source.a, destination.a));
091        installedBenchmarkCode.executeVarargs(source.a, destination.a);
092        Assert.assertArrayEquals(source.a, destination.a);
093    }
094
095    public static void arrayCopyTest(Object c1, Object c2) {
096        Object[] source = (Object[]) c1;
097        Object[] destination = (Object[]) c2;
098        System.arraycopy(source, 0, destination, 0, source.length);
099    }
100
101    @Test
102    public void test3() throws Exception {
103        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("compareAndSwapTest", Object.class, Object.class, Object.class);
104        Object initial = new Object();
105        Object replacement = new Object();
106        AtomicReference<Object> cas = new AtomicReference<>();
107        Assert.assertEquals(cas.get(), null);
108        installedBenchmarkCode.executeVarargs(cas, null, initial);
109        Assert.assertEquals(cas.get(), initial);
110        installedBenchmarkCode.executeVarargs(cas, initial, replacement);
111        Assert.assertEquals(cas.get(), replacement);
112    }
113
114    @SuppressWarnings("unchecked")
115    public static void compareAndSwapTest(Object c1, Object c2, Object c3) throws ClassCastException {
116        AtomicReference<Object> cas = (AtomicReference<Object>) c1;
117        cas.compareAndSet(c2, c3);
118    }
119
120    @Test
121    public void test4() throws Exception {
122        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("charArrayCopyTest", Object.class, Object.class, Object.class);
123        StringContainer1 source1 = new StringContainer1();
124        StringContainer2 source2 = new StringContainer2();
125        char[] result = new char[source1.value.length + source2.value.length];
126        installedBenchmarkCode.executeVarargs(source1.value, source2.value, result);
127        Assert.assertArrayEquals(new char[]{'T', 'e', 's', 't', ' ', 'S', 't', 'r', 'i', 'n', 'g'}, result);
128    }
129
130    public static char[] charArrayCopyTest(Object c1, Object c2, Object c3) {
131        char[] source1 = (char[]) c1;
132        char[] source2 = (char[]) c2;
133        char[] result = (char[]) c3;
134        for (int i = 0; i < source1.length; i++) {
135            result[i] = source1[i];
136        }
137
138        for (int i = 0; i < source2.length; i++) {
139            result[source1.length + i] = source2[i];
140        }
141        return result;
142    }
143
144    @Test
145    public void test5() throws Exception {
146        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("charContainerArrayCopyTest", Object.class, Object.class, Object.class);
147        StringContainer1 source1 = new StringContainer1();
148        StringContainer2 source2 = new StringContainer2();
149        char[] result = new char[source1.value.length + source2.value.length];
150        installedBenchmarkCode.executeVarargs(source1, source2, result);
151        Assert.assertArrayEquals(new char[]{'T', 'e', 's', 't', ' ', 'S', 't', 'r', 'i', 'n', 'g'}, result);
152    }
153
154    public static char[] charContainerArrayCopyTest(Object c1, Object c2, Object c3) {
155        char[] source1 = ((StringContainer1) c1).value;
156        char[] source2 = ((StringContainer2) c2).value;
157        char[] result = (char[]) c3;
158        for (int i = 0; i < source1.length; i++) {
159            result[i] = source1[i];
160        }
161        for (int i = 0; i < source2.length; i++) {
162            result[source1.length + i] = source2[i];
163        }
164        return result;
165    }
166
167    @Test
168    public void test6() throws Exception {
169        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringCopyTest", Object.class, Object.class);
170        String a = new String("Test ");
171        String b = new String("String");
172        String c = (String) installedBenchmarkCode.executeVarargs(a, b);
173        Assert.assertTrue(c.equals("Test String"));
174    }
175
176    public static String stringCopyTest(Object c1, Object c2) {
177        String source = (String) c1;
178        String destination = (String) c2;
179        return source + destination;
180    }
181
182    @Test
183    public void test7() throws Exception {
184        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("queueTest", Object.class, Object.class);
185        ArrayDeque<Object> q = new ArrayDeque<>();
186        Object[] objects = new Object[512];
187        for (int i = 0; i < objects.length; i++) {
188            objects[i] = new Object();
189        }
190        int j = 0;
191        while (j < objects.length) {
192            if (!installedBenchmarkCode.isValid()) {
193                // This can get invalidated due to lack of MDO update
194                installedBenchmarkCode = getInstalledCode("queueTest", Object.class, Object.class);
195            }
196            installedBenchmarkCode.executeVarargs(q, objects[j]);
197            j++;
198        }
199
200        System.gc();
201        Assert.assertTrue(q.size() == objects.length);
202        Assert.assertTrue(!q.isEmpty());
203        j = 0;
204        while (j < objects.length) {
205            Assert.assertTrue(objects[j] == q.remove());
206            j++;
207        }
208
209        Assert.assertTrue(q.size() == 0);
210        Assert.assertTrue(q.isEmpty());
211    }
212
213    @SuppressWarnings("unchecked")
214    public static void queueTest(Object c1, Object c2) {
215        ArrayDeque<Object> queue = (ArrayDeque<Object>) c1;
216        queue.add(c2);
217    }
218
219    @Test
220    public void test8() throws Exception {
221        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("unmodListTest", Object.class);
222        List<Object> list = new ArrayList<>();
223        for (int i = 0; i < 512; i++) {
224            list.add(new Object());
225        }
226        Object[] array = (Object[]) installedBenchmarkCode.executeVarargs(list);
227        Assert.assertTrue(list.size() == array.length);
228        int i = 0;
229        for (Object obj : list) {
230            Assert.assertTrue(obj == array[i]);
231            i++;
232        }
233    }
234
235    @SuppressWarnings("unchecked")
236    public static Object[] unmodListTest(Object c1) {
237        List<Object> queue = (ArrayList<Object>) c1;
238        Object[] result = Collections.unmodifiableCollection(queue).toArray(new Object[queue.size()]);
239        return result;
240    }
241
242    @Test
243    public void test9() throws Exception {
244        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("unmodListTest", Object.class);
245        List<Object> list = new ArrayList<>();
246        Object[] array = (Object[]) installedBenchmarkCode.executeVarargs(list);
247        Assert.assertTrue(list.size() == array.length);
248    }
249
250    public void test10() throws Exception {
251        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("constantTest", Object.class);
252        Container c = new Container();
253        Assert.assertFalse((boolean) installedBenchmarkCode.executeVarargs(c));
254    }
255
256    public static Boolean constantTest(Object c1) {
257        ConstantContainer container = (ConstantContainer) c1;
258        return container.a.equals(container.b);
259    }
260
261    @Test
262    public void test11() throws Exception {
263        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringEqualsTest", Object.class, Object.class);
264        String s1 = new String("Test");
265        String s2 = new String("Test");
266        boolean result = ((Boolean) (installedBenchmarkCode.executeVarargs(s1, s2))).booleanValue();
267        Assert.assertTrue(result);
268    }
269
270    public static Boolean stringEqualsTest(Object c1, Object c2) {
271        return ((String) c1).equals(c2);
272    }
273
274    @Test
275    public void test12() throws Exception {
276        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringConstantEqualsTest", Object.class);
277        String s1 = new String("Test");
278        boolean result = ((Boolean) (installedBenchmarkCode.executeVarargs(s1))).booleanValue();
279        Assert.assertTrue(result);
280    }
281
282    public static Boolean stringConstantEqualsTest(Object c1) {
283        return "Test".equals(c1);
284    }
285
286    @SuppressWarnings("unchecked")
287    public static Object[] unmodListTestByte(Object c1) {
288        List<Byte> queue = (ArrayList<Byte>) c1;
289        Byte[] result = Collections.unmodifiableCollection(queue).toArray(new Byte[queue.size()]);
290        return result;
291    }
292
293    @Test
294    public void test13() throws Exception {
295        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("unmodListTestByte", Object.class);
296        List<Byte> list = new ArrayList<>();
297        Byte[] array = (Byte[]) installedBenchmarkCode.executeVarargs(list);
298        Assert.assertTrue(list.size() == array.length);
299    }
300
301    @Test
302    public void test14() throws Exception {
303        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringBuilderTest", Object.class, Object.class);
304        StringBuilder buffer = new StringBuilder("TestTestTestTestTestTestTest");
305        Assert.assertTrue(buffer.length() == 28);
306        String a = new String("TestTestTestTestTestTestTest");
307        installedBenchmarkCode.executeVarargs(buffer, a.toCharArray());
308        Assert.assertTrue(buffer.length() == 56);
309        Assert.assertTrue(buffer.toString().equals("TestTestTestTestTestTestTestTestTestTestTestTestTestTest"));
310    }
311
312    public static void stringBuilderTest(Object c1, Object c2) {
313        StringBuilder source = (StringBuilder) c1;
314        char[] add = (char[]) c2;
315        for (int i = 0; i < add.length; i++) {
316            source.append(add[i]);
317        }
318    }
319
320    @Test
321    public void test15() throws Exception {
322        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringBuilderTestIn");
323        installedBenchmarkCode.executeVarargs();
324    }
325
326    public static void stringBuilderTestIn() {
327        StringBuilder buffer = new StringBuilder("TestTestTestTestTestTestTest");
328        Assert.assertTrue(buffer.length() == 28);
329        String a = new String("TestTestTestTestTestTestTest");
330        char[] add = a.toCharArray();
331        for (int i = 0; i < add.length; i++) {
332            buffer.append(add[i]);
333        }
334        Assert.assertTrue(buffer.length() == 56);
335        Assert.assertTrue(buffer.toString().equals("TestTestTestTestTestTestTestTestTestTestTestTestTestTest"));
336    }
337
338    @Test
339    public void test16() throws Exception {
340        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringBuilderArrayCopy");
341        installedBenchmarkCode.executeVarargs();
342    }
343
344    public static void stringBuilderArrayCopy() {
345        StringBuilder buffer = new StringBuilder("TestTestTestTestTestTestTest");
346        Assert.assertTrue(buffer.length() == 28);
347        String a = new String("TestTestTestTestTestTestTest");
348        char[] dst = new char[buffer.length() * 2];
349        System.arraycopy(buffer.toString().toCharArray(), 0, dst, 0, buffer.length());
350        System.arraycopy(a.toCharArray(), 0, dst, buffer.length(), buffer.length());
351        Assert.assertTrue(dst.length == 56);
352        Assert.assertTrue(new String(dst).equals("TestTestTestTestTestTestTestTestTestTestTestTestTestTest"));
353    }
354
355    @Test
356    public void test17() throws Exception {
357        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringFormat");
358        installedBenchmarkCode.executeVarargs();
359    }
360
361    public static void stringFormat() {
362        String.format("Hello %d", 0);
363        String.format("Hello %d", -11);
364        String.format("Hello %d", -2147483648);
365    }
366
367    @Test
368    public void test18() throws Exception {
369        HotSpotInstalledCode installedBenchmarkCode = getInstalledCode("stringBuilder");
370        StringBuilder b = (StringBuilder) installedBenchmarkCode.executeVarargs();
371        Assert.assertTrue(b.capacity() == 16);
372        Assert.assertTrue(b.length() == 0);
373    }
374
375    public static Object stringBuilder() {
376        return new StringBuilder();
377    }
378
379    static class Container {
380
381        public Object a = new Object();
382        public Object b = new Object();
383    }
384
385    static class ArrayContainer {
386
387        public Object[] a = new Object[10];
388
389        public ArrayContainer() {
390            for (int i = 0; i < 10; i++) {
391                a[i] = new Object();
392            }
393        }
394    }
395
396    static class HashMapContainer {
397
398        public HashMap<Object, Object> a = new HashMap<>();
399
400        public HashMapContainer() {
401            for (int i = 0; i < 10; i++) {
402                a.put(new Object(), new Object());
403            }
404        }
405    }
406
407    static class StringContainer1 {
408
409        public char[] value = new char[5];
410
411        public StringContainer1() {
412            value[0] = 'T';
413            value[1] = 'e';
414            value[2] = 's';
415            value[3] = 't';
416            value[4] = ' ';
417
418        }
419    }
420
421    static class StringContainer2 {
422
423        public char[] value = new char[6];
424
425        public StringContainer2() {
426            value[0] = 'S';
427            value[1] = 't';
428            value[2] = 'r';
429            value[3] = 'i';
430            value[4] = 'n';
431            value[5] = 'g';
432        }
433    }
434
435    static class ConstantContainer {
436
437        public final Object a = new Object();
438        public final Object b = new Object();
439
440        public ConstantContainer() {
441
442        }
443    }
444}