comparison graal/com.oracle.truffle.api.test/src/com/oracle/truffle/api/test/utilities/ExactClassValueProfileTest.java @ 17039:1e542561783e

Truffle: new value profiles prototype.
author Christian Humer <christian.humer@gmail.com>
date Wed, 03 Sep 2014 14:58:53 +0200
parents
children
comparison
equal deleted inserted replaced
17038:3b3e768a2b92 17039:1e542561783e
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.test.utilities;
24
25 import static org.hamcrest.CoreMatchers.*;
26 import static org.junit.Assert.*;
27
28 import org.junit.*;
29 import org.junit.experimental.theories.*;
30 import org.junit.runner.*;
31
32 import com.oracle.truffle.api.utilities.*;
33
34 @RunWith(Theories.class)
35 public class ExactClassValueProfileTest {
36
37 @DataPoint public static final String O1 = new String();
38 @DataPoint public static final String O2 = new String();
39 @DataPoint public static final Object O3 = new Object();
40 @DataPoint public static final Integer O4 = new Integer(1);
41 @DataPoint public static final Integer O5 = null;
42
43 private ExactClassValueProfile profile;
44
45 @Before
46 public void create() {
47 profile = (ExactClassValueProfile) ValueProfile.createClassProfile();
48 }
49
50 @Test
51 public void testInitial() {
52 assertThat(profile.isGeneric(), is(false));
53 assertThat(profile.isUninitialized(), is(true));
54 assertNull(profile.getCachedClass());
55 profile.toString(); // test that it is not crashing
56 }
57
58 @Theory
59 public void testProfileOne(Object value) {
60 Object result = profile.profile(value);
61
62 assertThat(result, is(value));
63 assertEquals(profile.getCachedClass(), expectedClass(value));
64 assertThat(profile.isUninitialized(), is(false));
65 profile.toString(); // test that it is not crashing
66 }
67
68 @Theory
69 public void testProfileTwo(Object value0, Object value1) {
70 Object result0 = profile.profile(value0);
71 Object result1 = profile.profile(value1);
72
73 assertThat(result0, is(value0));
74 assertThat(result1, is(value1));
75
76 Object expectedClass = expectedClass(value0) == expectedClass(value1) ? expectedClass(value0) : Object.class;
77
78 assertEquals(profile.getCachedClass(), expectedClass);
79 assertThat(profile.isUninitialized(), is(false));
80 assertThat(profile.isGeneric(), is(expectedClass == Object.class));
81 profile.toString(); // test that it is not crashing
82 }
83
84 @Theory
85 public void testProfileThree(Object value0, Object value1, Object value2) {
86 Object result0 = profile.profile(value0);
87 Object result1 = profile.profile(value1);
88 Object result2 = profile.profile(value2);
89
90 assertThat(result0, is(value0));
91 assertThat(result1, is(value1));
92 assertThat(result2, is(value2));
93
94 Object expectedClass = expectedClass(value0) == expectedClass(value1) && expectedClass(value1) == expectedClass(value2) ? expectedClass(value0) : Object.class;
95
96 assertEquals(profile.getCachedClass(), expectedClass);
97 assertThat(profile.isUninitialized(), is(false));
98 assertThat(profile.isGeneric(), is(expectedClass == Object.class));
99 profile.toString(); // test that it is not crashing
100 }
101
102 private static Class<?> expectedClass(Object value) {
103 return value == null ? Object.class : value.getClass();
104 }
105
106 }