jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 1 | /* |
alanb | 0d05823 | 2012-11-02 15:50:11 +0000 | [diff] [blame] | 2 | * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved. |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 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. Oracle designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Oracle in the LICENSE file that accompanied this code. |
| 10 | * |
| 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | * accompanied this code). |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License version |
| 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | * |
| 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | * or visit www.oracle.com if you need additional information or have any |
| 23 | * questions. |
| 24 | */ |
| 25 | |
| 26 | /* @test |
| 27 | * @summary tests for class-specific values |
| 28 | * @compile ClassValueTest.java |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 29 | * @run testng/othervm test.java.lang.invoke.ClassValueTest |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 30 | */ |
| 31 | |
jrose | ada69fa | 2011-03-23 23:02:31 -0700 | [diff] [blame] | 32 | package test.java.lang.invoke; |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 33 | |
henryjen | f6613f4 | 2013-08-12 12:11:04 -0700 | [diff] [blame] | 34 | import org.testng.*; |
| 35 | import static org.testng.AssertJUnit.*; |
| 36 | import org.testng.annotations.*; |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 37 | |
| 38 | /** |
| 39 | * @author jrose |
| 40 | */ |
| 41 | public class ClassValueTest { |
| 42 | static String nameForCV1(Class<?> type) { |
| 43 | return "CV1:" + type.getName(); |
| 44 | } |
jrose | 691778e | 2011-04-09 21:38:40 -0700 | [diff] [blame] | 45 | int countForCV1; |
| 46 | final ClassValue<String> CV1 = new CV1(); |
| 47 | private class CV1 extends ClassValue<String> { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 48 | protected String computeValue(Class<?> type) { |
| 49 | countForCV1++; |
| 50 | return nameForCV1(type); |
| 51 | } |
jrose | b90d2e7 | 2010-12-16 15:59:27 -0800 | [diff] [blame] | 52 | } |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 53 | |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 54 | static final Class<?>[] CLASSES = { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 55 | String.class, |
| 56 | Integer.class, |
| 57 | int.class, |
| 58 | boolean[].class, |
| 59 | char[][].class, |
| 60 | ClassValueTest.class |
| 61 | }; |
| 62 | |
| 63 | @Test |
| 64 | public void testGet() { |
| 65 | countForCV1 = 0; |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 66 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 67 | assertEquals(nameForCV1(c), CV1.get(c)); |
| 68 | } |
| 69 | assertEquals(CLASSES.length, countForCV1); |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 70 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 71 | assertEquals(nameForCV1(c), CV1.get(c)); |
| 72 | } |
| 73 | assertEquals(CLASSES.length, countForCV1); |
| 74 | } |
| 75 | |
| 76 | @Test |
| 77 | public void testRemove() { |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 78 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 79 | CV1.get(c); |
| 80 | } |
| 81 | countForCV1 = 0; |
| 82 | int REMCOUNT = 3; |
| 83 | for (int i = 0; i < REMCOUNT; i++) { |
| 84 | CV1.remove(CLASSES[i]); |
| 85 | } |
| 86 | assertEquals(0, countForCV1); // no change |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 87 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 88 | assertEquals(nameForCV1(c), CV1.get(c)); |
| 89 | } |
| 90 | assertEquals(REMCOUNT, countForCV1); |
| 91 | } |
| 92 | |
| 93 | static String nameForCVN(Class<?> type, int n) { |
| 94 | return "CV[" + n + "]" + type.getName(); |
| 95 | } |
jrose | 691778e | 2011-04-09 21:38:40 -0700 | [diff] [blame] | 96 | int countForCVN; |
| 97 | class CVN extends ClassValue<String> { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 98 | final int n; |
| 99 | CVN(int n) { this.n = n; } |
| 100 | protected String computeValue(Class<?> type) { |
| 101 | countForCVN++; |
| 102 | return nameForCVN(type, n); |
| 103 | } |
| 104 | }; |
| 105 | |
| 106 | @Test |
| 107 | public void testGetMany() { |
| 108 | int CVN_COUNT1 = 100, CVN_COUNT2 = 100; |
| 109 | CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2]; |
| 110 | for (int n = 0; n < cvns.length; n++) { |
| 111 | cvns[n] = new CVN(n); |
| 112 | } |
| 113 | countForCVN = 0; |
| 114 | for (int pass = 0; pass <= 2; pass++) { |
| 115 | for (int i1 = 0; i1 < CVN_COUNT1; i1++) { |
| 116 | eachClass: |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 117 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 118 | for (int i2 = 0; i2 < CVN_COUNT2; i2++) { |
| 119 | int n = i1*CVN_COUNT2 + i2; |
| 120 | assertEquals(0, countForCVN); |
| 121 | assertEquals(nameForCVN(c, n), cvns[n].get(c)); |
| 122 | cvns[n].get(c); //get it again |
| 123 | //System.out.println("getting "+n+":"+cvns[n].get(c)); |
| 124 | boolean doremove = (((i1 + i2) & 3) == 0); |
| 125 | switch (pass) { |
| 126 | case 0: |
| 127 | assertEquals(1, countForCVN); |
| 128 | break; |
| 129 | case 1: |
| 130 | // remove on middle pass |
| 131 | assertEquals(0, countForCVN); |
| 132 | if (doremove) { |
| 133 | //System.out.println("removing "+n+":"+cvns[n].get(c)); |
| 134 | cvns[n].remove(c); |
| 135 | assertEquals(0, countForCVN); |
| 136 | } |
| 137 | break; |
| 138 | case 2: |
| 139 | assertEquals(doremove ? 1 : 0, countForCVN); |
| 140 | break; |
| 141 | } |
| 142 | countForCVN = 0; |
| 143 | if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap |
| 144 | } |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | assertEquals(countForCVN, 0); |
jrose | 291bec9 | 2012-01-18 17:34:32 -0800 | [diff] [blame] | 149 | System.out.println("[rechecking values]"); |
| 150 | for (int i = 0; i < cvns.length * 10; i++) { |
| 151 | int n = i % cvns.length; |
| 152 | for (Class<?> c : CLASSES) { |
jrose | a64264e | 2010-09-14 01:42:03 -0700 | [diff] [blame] | 153 | assertEquals(nameForCVN(c, n), cvns[n].get(c)); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |