comparison truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/profiles/ExactClassValueProfileTest.java @ 22501:a63bda98cfdb

Extract profiles into separate package. Add isProfilingEnabled in TruffleRuntime to disable profiling in the default runtime; Add low overhead profiles for primitives; Add LoopConditionProfile; Profile footprint/threadsafety improvements; Make toString implementations more consistent; Greatly enhanced javadoc documentation for profiles; Deprecate old profiles
author Christian Humer <christian.humer@oracle.com>
date Wed, 16 Dec 2015 16:38:13 +0100
parents truffle/com.oracle.truffle.api.test/src/com/oracle/truffle/api/utilities/ExactClassValueProfileTest.java@1cb72700c10e
children 4ba1aa33fda4
comparison
equal deleted inserted replaced
22500:fbe1eb7b4172 22501:a63bda98cfdb
1 /*
2 * Copyright (c) 2014, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23 package com.oracle.truffle.api.profiles;
24
25 import static org.hamcrest.CoreMatchers.is;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertThat;
30
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.junit.experimental.theories.DataPoint;
34 import org.junit.experimental.theories.Theories;
35 import org.junit.experimental.theories.Theory;
36 import org.junit.runner.RunWith;
37
38 @RunWith(Theories.class)
39 public class ExactClassValueProfileTest {
40
41 @DataPoint public static final String O1 = new String();
42 @DataPoint public static final String O2 = new String();
43 @DataPoint public static final Object O3 = new Object();
44 @DataPoint public static final Integer O4 = new Integer(1);
45 @DataPoint public static final Integer O5 = null;
46 @DataPoint public static final TestBaseClass O6 = new TestBaseClass();
47 @DataPoint public static final TestSubClass O7 = new TestSubClass();
48
49 private ValueProfile.ExactClass profile;
50
51 private static class TestBaseClass {
52 }
53
54 private static class TestSubClass extends TestBaseClass {
55 }
56
57 @Before
58 public void create() {
59 profile = (ValueProfile.ExactClass) ValueProfile.ExactClass.create();
60 }
61
62 @Test
63 public void testInitial() throws Exception {
64 assertThat(isGeneric(profile), is(false));
65 assertThat(isUninitialized(profile), is(true));
66 assertNull(getCachedClass(profile));
67 assertNotNull(profile.toString());
68 }
69
70 @Theory
71 public void testProfileOne(Object value) throws Exception {
72 Object result = profile.profile(value);
73
74 assertThat(result, is(value));
75 assertEquals(expectedClass(value), getCachedClass(profile));
76 assertThat(isUninitialized(profile), is(false));
77 assertNotNull(profile.toString());
78 }
79
80 @Theory
81 public void testProfileTwo(Object value0, Object value1) throws Exception {
82 Object result0 = profile.profile(value0);
83 Object result1 = profile.profile(value1);
84
85 assertThat(result0, is(value0));
86 assertThat(result1, is(value1));
87
88 Object expectedClass = expectedClass(value0) == expectedClass(value1) ? expectedClass(value0) : Object.class;
89
90 assertEquals(expectedClass, getCachedClass(profile));
91 assertThat(isUninitialized(profile), is(false));
92 assertThat(isGeneric(profile), is(expectedClass == Object.class));
93 assertNotNull(profile.toString());
94 }
95
96 @Theory
97 public void testProfileThree(Object value0, Object value1, Object value2) throws Exception {
98 Object result0 = profile.profile(value0);
99 Object result1 = profile.profile(value1);
100 Object result2 = profile.profile(value2);
101
102 assertThat(result0, is(value0));
103 assertThat(result1, is(value1));
104 assertThat(result2, is(value2));
105
106 Object expectedClass = expectedClass(value0) == expectedClass(value1) && expectedClass(value1) == expectedClass(value2) ? expectedClass(value0) : Object.class;
107
108 assertEquals(expectedClass, getCachedClass(profile));
109 assertThat(isUninitialized(profile), is(false));
110 assertThat(isGeneric(profile), is(expectedClass == Object.class));
111 assertNotNull(profile.toString());
112 }
113
114 private static Class<?> expectedClass(Object value) {
115 return value == null ? Object.class : value.getClass();
116 }
117
118 private static Object getCachedClass(ValueProfile.ExactClass profile) throws Exception {
119 return profile.getCachedClass();
120 }
121
122 private static boolean isUninitialized(ValueProfile.ExactClass profile) throws Exception {
123 return profile.isUninitialized();
124 }
125
126 private static boolean isGeneric(ValueProfile.ExactClass profile) throws Exception {
127 return profile.isGeneric();
128 }
129 }