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 
24 package compiler.types.correctness.scenarios;
25 
26 import compiler.types.correctness.hierarchies.TypeHierarchy;
27 
28 /**
29  * Test scenario
30  *
31  * @param <T> parameter type
32  * @param <R> result type
33  */
34 public abstract class Scenario<T extends TypeHierarchy.I, R> {
35 
36     private final String name;
37     public final ProfilingType profilingType;
38     public final TypeHierarchy <? extends T, ? extends T> hierarchy;
39     protected volatile T field;
40 
41     /**
42      * Constructor
43      *
44      * @param name          scenario name
45      * @param profilingType tested profiling type
46      * @param hierarchy     type hierarchy
47      */
Scenario(String name, ProfilingType profilingType, TypeHierarchy<? extends T, ? extends T> hierarchy)48     protected Scenario(String name, ProfilingType profilingType,
49                        TypeHierarchy<? extends T, ? extends T> hierarchy) {
50         this.profilingType = profilingType;
51         this.name = name + " # " + profilingType.name();
52         this.hierarchy = hierarchy;
53     }
54 
55     /**
56      * Returns the object which should be used as a parameter
57      * for the methods used for profile data
58      *
59      * @return profiled type object
60      */
getProfiled()61     public T getProfiled() {
62         return hierarchy.getM();
63     }
64 
65     /**
66      * Returns the object which makes a conflict for a profiled data
67      * when passed instead of {@linkplain Scenario#getProfiled}
68      *
69      * @return incompatible to profiled object
70      */
getConflict()71     public T getConflict() {
72         return hierarchy.getN();
73     }
74 
75     /**
76      * @return scenario name
77      */
getName()78     public String getName() {
79         return name;
80     }
81 
82     /** Is this scenario applicable for a hierarchy it was constructed with */
isApplicable()83     public boolean isApplicable() {
84         return true;
85     }
86 
87     /**
88      * Runs test scenario
89      *
90      * @param t subject of the test
91      * @return  result of the test invocation
92      */
run(T t)93     public abstract R run(T t);
94 
95     /** Used for a return type profiling */
collectReturnType(T t)96     protected final T collectReturnType(T t) {
97         return t;
98     }
99 
100     /**
101      * Checks the result for R and T
102      *
103      * @param r result
104      * @param t original
105      * @throws java.lang.RuntimeException on result mismatch
106      */
check(R r, T t)107     public abstract void check(R r, T t);
108 }
109