blob: cf7bcff82d6d0da509890c0981092f4fd75c4250 [file] [log] [blame]
jrosea64264e2010-09-14 01:42:03 -07001/*
2 * Copyright (c) 2010, 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. 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
29 * @run junit/othervm test.java.dyn.ClassValueTest
30 */
31
32/*
33 Manually:
34 $ $JAVA7X_HOME/bin/javac -d foo -cp $JUNIT4_JAR test/java/dyn/ClassValueTest.java
35 $ $JAVA7X_HOME/bin/java -cp foo:$JUNIT4_JAR org.junit.runner.JUnitCore test.java.dyn.ClassValueTest
36 Output: .testAdd => 1000 : Integer
37 */
38
39package test.java.dyn;
40
41import java.util.*;
42
43import java.dyn.*;
44
45import org.junit.*;
46import static org.junit.Assert.*;
47
48/**
49 * @author jrose
50 */
51public class ClassValueTest {
52 static String nameForCV1(Class<?> type) {
53 return "CV1:" + type.getName();
54 }
55 static int countForCV1;
56 static final ClassValue<String> CV1 = new ClassValue<String>() {
57 protected String computeValue(Class<?> type) {
58 countForCV1++;
59 return nameForCV1(type);
60 }
61 };
62
63 static final Class[] CLASSES = {
64 String.class,
65 Integer.class,
66 int.class,
67 boolean[].class,
68 char[][].class,
69 ClassValueTest.class
70 };
71
72 @Test
73 public void testGet() {
74 countForCV1 = 0;
75 for (Class c : CLASSES) {
76 assertEquals(nameForCV1(c), CV1.get(c));
77 }
78 assertEquals(CLASSES.length, countForCV1);
79 for (Class c : CLASSES) {
80 assertEquals(nameForCV1(c), CV1.get(c));
81 }
82 assertEquals(CLASSES.length, countForCV1);
83 }
84
85 @Test
86 public void testRemove() {
87 for (Class c : CLASSES) {
88 CV1.get(c);
89 }
90 countForCV1 = 0;
91 int REMCOUNT = 3;
92 for (int i = 0; i < REMCOUNT; i++) {
93 CV1.remove(CLASSES[i]);
94 }
95 assertEquals(0, countForCV1); // no change
96 for (Class c : CLASSES) {
97 assertEquals(nameForCV1(c), CV1.get(c));
98 }
99 assertEquals(REMCOUNT, countForCV1);
100 }
101
102 static String nameForCVN(Class<?> type, int n) {
103 return "CV[" + n + "]" + type.getName();
104 }
105 static int countForCVN;
106 static class CVN extends ClassValue<String> {
107 final int n;
108 CVN(int n) { this.n = n; }
109 protected String computeValue(Class<?> type) {
110 countForCVN++;
111 return nameForCVN(type, n);
112 }
113 };
114
115 @Test
116 public void testGetMany() {
117 int CVN_COUNT1 = 100, CVN_COUNT2 = 100;
118 CVN cvns[] = new CVN[CVN_COUNT1 * CVN_COUNT2];
119 for (int n = 0; n < cvns.length; n++) {
120 cvns[n] = new CVN(n);
121 }
122 countForCVN = 0;
123 for (int pass = 0; pass <= 2; pass++) {
124 for (int i1 = 0; i1 < CVN_COUNT1; i1++) {
125 eachClass:
126 for (Class c : CLASSES) {
127 for (int i2 = 0; i2 < CVN_COUNT2; i2++) {
128 int n = i1*CVN_COUNT2 + i2;
129 assertEquals(0, countForCVN);
130 assertEquals(nameForCVN(c, n), cvns[n].get(c));
131 cvns[n].get(c); //get it again
132 //System.out.println("getting "+n+":"+cvns[n].get(c));
133 boolean doremove = (((i1 + i2) & 3) == 0);
134 switch (pass) {
135 case 0:
136 assertEquals(1, countForCVN);
137 break;
138 case 1:
139 // remove on middle pass
140 assertEquals(0, countForCVN);
141 if (doremove) {
142 //System.out.println("removing "+n+":"+cvns[n].get(c));
143 cvns[n].remove(c);
144 assertEquals(0, countForCVN);
145 }
146 break;
147 case 2:
148 assertEquals(doremove ? 1 : 0, countForCVN);
149 break;
150 }
151 countForCVN = 0;
152 if (i1 > i2 && i1 < i2+5) continue eachClass; // leave diagonal gap
153 }
154 }
155 }
156 }
157 assertEquals(countForCVN, 0);
158 for (int n = 0; n < cvns.length; n++) {
159 for (Class c : CLASSES) {
160 assertEquals(nameForCVN(c, n), cvns[n].get(c));
161 }
162 }
163 }
164}