jdk/test/java/lang/invoke/ClassValueTest.java
author alanb
Fri, 10 Feb 2017 09:04:39 +0000
changeset 43712 5dfd0950317c
parent 23010 6dadb192ad81
permissions -rw-r--r--
8173393: Module system implementation refresh (2/2017) Reviewed-by: dfuchs, psandoz, mchung, alanb Contributed-by: alan.bateman@oracle.com, mandy.chung@oracle.com, claes.redestad@oracle.com, alex.buckley@oracle.com, mark.reinhold@oracle.com, john.r.rose@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21596
diff changeset
     2
 * Copyright (c) 2011, 2013, Oracle and/or its affiliates. All rights reserved.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     4
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21596
0e3a39f29dbc 8027696: Incorrect copyright header in the tests
serb
parents: 19388
diff changeset
     7
 * published by the Free Software Foundation.
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     8
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    13
 * accompanied this code).
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    14
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    18
 *
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    21
 * questions.
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    22
 */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    23
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    24
/* @test
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    25
 * @summary tests for class-specific values
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    26
 * @compile ClassValueTest.java
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    27
 * @run testng/othervm test.java.lang.invoke.ClassValueTest
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    28
 */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    29
8822
8145ab9f5f86 7012648: move JSR 292 to package java.lang.invoke and adjust names
jrose
parents: 7562
diff changeset
    30
package test.java.lang.invoke;
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    31
19388
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    32
import org.testng.*;
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    33
import static org.testng.AssertJUnit.*;
188246066dbb 8022749: Convert junit tests to testng in test/java/lang/invoke
henryjen
parents: 14342
diff changeset
    34
import org.testng.annotations.*;
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    35
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    36
/**
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    37
 * @author jrose
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    38
 */
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    39
public class ClassValueTest {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    40
    static String nameForCV1(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    41
        return "CV1:" + type.getName();
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    42
    }
9146
8f574b2bae87 7019529: JSR292: java/dyn/ClassValueTest.java depends on sub-test execution order
jrose
parents: 8822
diff changeset
    43
    int countForCV1;
8f574b2bae87 7019529: JSR292: java/dyn/ClassValueTest.java depends on sub-test execution order
jrose
parents: 8822
diff changeset
    44
    final ClassValue<String> CV1 = new CV1();
8f574b2bae87 7019529: JSR292: java/dyn/ClassValueTest.java depends on sub-test execution order
jrose
parents: 8822
diff changeset
    45
    private class CV1 extends ClassValue<String> {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    46
        protected String computeValue(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    47
            countForCV1++;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    48
            return nameForCV1(type);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    49
        }
7562
a0ad195efe2c 7001424: implement JSR 292 EG adjustments, November 2010
jrose
parents: 7054
diff changeset
    50
    }
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    51
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
    52
    static final Class<?>[] CLASSES = {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    53
        String.class,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    54
        Integer.class,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    55
        int.class,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    56
        boolean[].class,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    57
        char[][].class,
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    58
        ClassValueTest.class
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    59
    };
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    60
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    61
    @Test
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    62
    public void testGet() {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    63
        countForCV1 = 0;
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
    64
        for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    65
            assertEquals(nameForCV1(c), CV1.get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    66
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    67
        assertEquals(CLASSES.length, countForCV1);
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
    68
        for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    69
            assertEquals(nameForCV1(c), CV1.get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    70
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    71
        assertEquals(CLASSES.length, countForCV1);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    72
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    73
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    74
    @Test
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    75
    public void testRemove() {
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
    76
        for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    77
            CV1.get(c);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    78
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    79
        countForCV1 = 0;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    80
        int REMCOUNT = 3;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    81
        for (int i = 0; i < REMCOUNT; i++) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    82
            CV1.remove(CLASSES[i]);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    83
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    84
        assertEquals(0, countForCV1);  // no change
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
    85
        for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    86
            assertEquals(nameForCV1(c), CV1.get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    87
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    88
        assertEquals(REMCOUNT, countForCV1);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    89
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    90
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    91
    static String nameForCVN(Class<?> type, int n) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    92
        return "CV[" + n + "]" + type.getName();
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    93
    }
9146
8f574b2bae87 7019529: JSR292: java/dyn/ClassValueTest.java depends on sub-test execution order
jrose
parents: 8822
diff changeset
    94
    int countForCVN;
8f574b2bae87 7019529: JSR292: java/dyn/ClassValueTest.java depends on sub-test execution order
jrose
parents: 8822
diff changeset
    95
    class CVN extends ClassValue<String> {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    96
        final int n;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    97
        CVN(int n) { this.n = n; }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    98
        protected String computeValue(Class<?> type) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
    99
            countForCVN++;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   100
            return nameForCVN(type, n);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   101
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   102
    };
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   103
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   104
    @Test
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   105
    public void testGetMany() {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   106
        int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   107
        CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   108
        for (int n = 0; n < cvns.length; n++) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   109
            cvns[n] = new CVN(n);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   110
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   111
        countForCVN = 0;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   112
        for (int pass = 0; pass <= 2; pass++) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   113
            for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   114
                eachClass:
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
   115
                for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   116
                    for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   117
                        int n = i1*CVN_COUNT2 + i2;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   118
                        assertEquals(0, countForCVN);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   119
                        assertEquals(nameForCVN(c, n), cvns[n].get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   120
                        cvns[n].get(c);  //get it again
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   121
                        //System.out.println("getting "+n+":"+cvns[n].get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   122
                        boolean doremove = (((i1 + i2) & 3) == 0);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   123
                        switch (pass) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   124
                        case 0:
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   125
                            assertEquals(1, countForCVN);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   126
                            break;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   127
                        case 1:
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   128
                            // remove on middle pass
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   129
                            assertEquals(0, countForCVN);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   130
                            if (doremove) {
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   131
                                //System.out.println("removing "+n+":"+cvns[n].get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   132
                                cvns[n].remove(c);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   133
                                assertEquals(0, countForCVN);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   134
                            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   135
                            break;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   136
                        case 2:
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   137
                            assertEquals(doremove ? 1 : 0, countForCVN);
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   138
                            break;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   139
                        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   140
                        countForCVN = 0;
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   141
                        if (i1 > i2 && i1 < i2+5)  continue eachClass;  // leave diagonal gap
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   142
                    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   143
                }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   144
            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   145
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   146
        assertEquals(countForCVN, 0);
11536
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
   147
        System.out.println("[rechecking values]");
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
   148
        for (int i = 0; i < cvns.length * 10; i++) {
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
   149
            int n = i % cvns.length;
5be4f16d98bc 7030453: JSR 292 ClassValue.get method is too slow
jrose
parents: 11534
diff changeset
   150
            for (Class<?> c : CLASSES) {
7054
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   151
                assertEquals(nameForCVN(c, n), cvns[n].get(c));
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   152
            }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   153
        }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   154
    }
fee2fbaf3293 6982752: dynamic languages need to decorate types with runtime information
jrose
parents:
diff changeset
   155
}