1
/*******************************************************************************
2
* Copyright (c) 2006, 2008 IBM Corporation.
3
* All rights reserved. This program and the accompanying materials
4
* are made available under the terms of the Eclipse Public License v1.0
5
* which accompanies this distribution, and is available at
6
* http://www.eclipse.org/legal/epl-v10.html
9
* IBM Corporation - initial API and implementation
10
*******************************************************************************/
11
package org.eclipse.cdt.internal.pdom.tests;
13
import junit.framework.Test;
15
import org.eclipse.cdt.core.dom.ast.IBasicType;
16
import org.eclipse.cdt.core.dom.ast.IBinding;
17
import org.eclipse.cdt.core.dom.ast.IParameter;
18
import org.eclipse.cdt.core.dom.ast.IType;
19
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
20
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
21
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
22
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
23
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
24
import org.eclipse.cdt.core.model.ICProject;
25
import org.eclipse.cdt.core.testplugin.CProjectHelper;
26
import org.eclipse.cdt.internal.core.CCoreInternals;
27
import org.eclipse.cdt.internal.core.pdom.PDOM;
30
* Tests for verifying whether the PDOM correctly stores information about
31
* C++ class member functions.
33
public class MethodTests extends PDOMTestBase {
34
protected ICProject project;
37
public static Test suite() {
38
return suite(MethodTests.class);
42
protected void setUp() throws Exception {
43
project = createProject("methodTests");
44
pdom = (PDOM) CCoreInternals.getPDOMManager().getPDOM(project);
45
pdom.acquireReadLock();
49
protected void tearDown() throws Exception {
50
pdom.releaseReadLock();
51
if (project != null) {
52
CProjectHelper.delete(project);
57
public void testMethodParameters() throws Exception {
58
IBinding[] bindings = findQualifiedName(pdom, "Class1::normalMethod");
59
assertEquals(1, bindings.length);
60
ICPPFunction function = (ICPPFunction) bindings[0];
61
IParameter[] parameters = function.getParameters();
62
assertEquals(IBasicType.t_int, ((ICPPBasicType) parameters[0].getType()).getType());
63
assertEquals("p1", parameters[0].getName());
64
assertEquals(IBasicType.t_char, ((ICPPBasicType) parameters[1].getType()).getType());
65
assertEquals("p2", parameters[1].getName());
66
assertEquals(IBasicType.t_float, ((ICPPBasicType) parameters[2].getType()).getType());
67
assertEquals("p3", parameters[2].getName());
70
public void testVirtualMethod() throws Exception {
71
IBinding[] bindings = findQualifiedName(pdom, "Class1::inheritedMethod");
72
assertEquals(1, bindings.length);
73
ICPPMethod method = (ICPPMethod) bindings[0];
74
assertTrue(method.isVirtual());
77
public void testVirtualMethodType() throws Exception {
78
assertType(pdom, "Class1::inheritedMethod", ICPPFunction.class);
81
public void testVirtualMethodDeclarations() throws Exception {
82
assertDeclarationCount(pdom, "Class1::inheritedMethod", 2);
85
public void testVirtualMethodDefinitions() throws Exception {
86
assertDefinitionCount(pdom, "Class1::inheritedMethod", 1);
89
public void testVirtualMethodReferences() throws Exception {
90
assertReferenceCount(pdom, "Class1::inheritedMethod", 3);
93
public void testInheritedMethodType() throws Exception {
94
assertEquals(0, findQualifiedName(pdom, "Class2::inheritedMethod").length);
97
public void testInheritedMethodDeclarations() throws Exception {
98
assertDeclarationCount(pdom, "Class2::inheritedMethod", 0);
101
public void testInheritedMethodDefinitions() throws Exception {
102
assertDefinitionCount(pdom, "Class2::inheritedMethod", 0);
105
public void testInheritedMethodReferences() throws Exception {
106
assertReferenceCount(pdom, "Class2::inheritedMethod", 0);
109
public void testPureVirtualMethod() throws Exception {
110
IBinding[] bindings = findQualifiedName(pdom, "Class1::pureVirtualMethod");
111
assertEquals(1, bindings.length);
112
ICPPMethod method = (ICPPMethod) bindings[0];
113
assertTrue(method.isVirtual());
114
assertTrue(method.isPureVirtual());
117
public void testPureVirtualMethodType() throws Exception {
118
assertType(pdom, "Class1::pureVirtualMethod", ICPPFunction.class);
119
assertType(pdom, "Class2::pureVirtualMethod", ICPPFunction.class);
122
public void testPureVirtualMethodDeclarations() throws Exception {
123
assertDeclarationCount(pdom, "Class1::pureVirtualMethod", 1);
124
assertDeclarationCount(pdom, "Class2::pureVirtualMethod", 2);
127
public void testPureVirtualMethodDefinitions() throws Exception {
128
assertDefinitionCount(pdom, "Class1::pureVirtualMethod", 0);
129
assertDefinitionCount(pdom, "Class2::pureVirtualMethod", 1);
132
public void testPureVirtualMethodReferences() throws Exception {
133
assertReferenceCount(pdom, "Class1::pureVirtualMethod", 2);
134
assertReferenceCount(pdom, "Class2::pureVirtualMethod", 3);
137
public void testOverriddenMethodType() throws Exception {
138
assertType(pdom, "Class1::overriddenMethod", ICPPFunction.class);
139
assertType(pdom, "Class2::overriddenMethod", ICPPFunction.class);
142
public void testOverriddenMethodDeclarations() throws Exception {
143
assertDeclarationCount(pdom, "Class1::overriddenMethod", 2);
144
assertDeclarationCount(pdom, "Class2::overriddenMethod", 2);
147
public void testOverriddenMethodDefinitions() throws Exception {
148
assertDefinitionCount(pdom, "Class1::overriddenMethod", 1);
149
assertDefinitionCount(pdom, "Class2::overriddenMethod", 1);
152
public void testOverriddenMethodReferences() throws Exception {
153
assertReferenceCount(pdom, "Class1::overriddenMethod", 3);
154
assertReferenceCount(pdom, "Class2::overriddenMethod", 4);
157
public void testDestructor() throws Exception {
158
IBinding[] bindings = findQualifiedName(pdom, "Class1::~Class1");
159
assertEquals(1, bindings.length);
160
ICPPMethod method = (ICPPMethod) bindings[0];
161
assertTrue(method.isDestructor());
164
public void testDefaultPrivateMethod() throws Exception {
165
assertVisibility(pdom, "Class3::defaultMethod", ICPPMember.v_private);
168
public void testPrivateMethod() throws Exception {
169
assertVisibility(pdom, "Class3::privateMethod", ICPPMember.v_private);
172
public void testProtectedMethod() throws Exception {
173
assertVisibility(pdom, "Class3::protectedMethod", ICPPMember.v_protected);
176
public void testPublicMethod() throws Exception {
177
assertVisibility(pdom, "Class3::publicMethod", ICPPMember.v_public);
180
public void testInlineMethod() throws Exception {
181
IBinding[] bindings = findQualifiedName(pdom, "Class1::inlineMethod");
182
assertEquals(1, bindings.length);
183
ICPPMethod method = (ICPPMethod) bindings[0];
184
assertTrue(method.isInline());
187
public void testStaticMethod() throws Exception {
188
IBinding[] bindings = findQualifiedName(pdom, "Class1::staticMethod");
189
assertEquals(1, bindings.length);
190
ICPPMethod method = (ICPPMethod) bindings[0];
191
assertTrue(method.isStatic());
194
public void testVarArgsMethod() throws Exception {
195
IBinding[] bindings = findQualifiedName(pdom, "Class1::varArgsMethod");
196
assertEquals(1, bindings.length);
197
ICPPMethod method = (ICPPMethod) bindings[0];
198
assertTrue(method.takesVarArgs());
201
public void testConstMethod() throws Exception {
202
IBinding[] bindings = findQualifiedName(pdom, "Class1::constMethod");
203
assertEquals(1, bindings.length);
204
ICPPMethod method = (ICPPMethod) bindings[0];
205
ICPPFunctionType type = method.getType();
206
assertTrue(type.isConst());
209
public void testVolatileMethod() throws Exception {
210
IBinding[] bindings = findQualifiedName(pdom, "Class1::volatileMethod");
211
assertEquals(1, bindings.length);
212
ICPPMethod method = (ICPPMethod) bindings[0];
213
ICPPFunctionType type = method.getType();
214
assertTrue(type.isVolatile());
217
public void testConstVolatileMethod() throws Exception {
218
IBinding[] bindings = findQualifiedName(pdom, "Class1::constVolatileMethod");
219
assertEquals(1, bindings.length);
220
ICPPMethod method = (ICPPMethod) bindings[0];
221
ICPPFunctionType type = method.getType();
222
assertTrue(type.isConst());
223
assertTrue(type.isVolatile());
226
public void testNotConstMethod() throws Exception {
227
IBinding[] bindings = findQualifiedName(pdom, "Class1::notConstMethod");
228
assertEquals(1, bindings.length);
229
ICPPMethod method = (ICPPMethod) bindings[0];
230
ICPPFunctionType type = method.getType();
231
assertFalse(type.isConst());
234
public void testNotVolatileMethod() throws Exception {
235
IBinding[] bindings = findQualifiedName(pdom, "Class1::notVolatileMethod");
236
assertEquals(1, bindings.length);
237
ICPPMethod method = (ICPPMethod) bindings[0];
238
ICPPFunctionType type = method.getType();
239
assertFalse(type.isVolatile());
242
public void testNotConstVolatileMethod() throws Exception {
243
IBinding[] bindings = findQualifiedName(pdom, "Class1::notConstVolatileMethod");
244
assertEquals(1, bindings.length);
245
ICPPMethod method = (ICPPMethod) bindings[0];
246
ICPPFunctionType type = method.getType();
247
assertFalse(type.isConst());
248
assertFalse(type.isVolatile());
251
public void testNoExceptionSpecification() throws Exception {
252
IBinding[] bindings = findQualifiedName(pdom, "Class1::noExceptionSpecMethod");
253
assertEquals(1, bindings.length);
254
ICPPMethod method = (ICPPMethod) bindings[0];
255
IType[] exceptionSpec = method.getExceptionSpecification();
256
assertNull(exceptionSpec);
259
public void testEmptyExceptionSpecification() throws Exception {
260
IBinding[] bindings = findQualifiedName(pdom, "Class1::emptyExceptionSpecMethod");
261
assertEquals(1, bindings.length);
262
ICPPMethod method = (ICPPMethod) bindings[0];
263
IType[] exceptionSpec = method.getExceptionSpecification();
264
assertEquals(0, exceptionSpec.length);
267
public void testNonEmptyExceptionSpecification() throws Exception {
268
IBinding[] bindings = findQualifiedName(pdom, "Class1::nonEmptyExceptionSpecMethod");
269
assertEquals(1, bindings.length);
270
ICPPMethod method = (ICPPMethod) bindings[0];
271
IType[] exceptionSpec = method.getExceptionSpecification();
272
assertEquals(1, exceptionSpec.length);
273
assertEquals(IBasicType.t_int, ((ICPPBasicType) exceptionSpec[0]).getType());
276
public void testImplicitCtorExceptionSpec() throws Exception {
277
IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::D");
278
// get both default ctor + copy ctor
279
assertEquals(2, bindings.length);
280
ICPPMethod method = (ICPPMethod) bindings[0];
281
IType[] exceptionSpec = method.getExceptionSpecification();
282
assertNull(exceptionSpec);
285
public void testImplicitCopyCtorExceptionSpec() throws Exception {
286
IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::D");
287
// get both default ctor + copy ctor
288
assertEquals(2, bindings.length);
289
ICPPMethod method = (ICPPMethod) bindings[1];
290
IType[] exceptionSpec = method.getExceptionSpecification();
291
assertEquals(0, exceptionSpec.length);
294
public void testImplicitDtorExceptionSpec() throws Exception {
295
IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::~D");
296
assertEquals(1, bindings.length);
297
ICPPMethod method = (ICPPMethod) bindings[0];
298
IType[] exceptionSpec = method.getExceptionSpecification();
299
assertEquals(2, exceptionSpec.length);
300
int t1= ((ICPPBasicType) exceptionSpec[0]).getType();
301
int t2= ((ICPPBasicType) exceptionSpec[1]).getType();
302
assertEquals(IBasicType.t_int, Math.min(t1, t2));
303
assertEquals(IBasicType.t_double, Math.max(t1, t2));