summaryrefslogtreecommitdiffstats
blob: e47de9c7eaa3f56c2dcebd41e0a25ca6c6746b13 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/*******************************************************************************
 * Copyright (c) 2006, 2008 IBM Corporation.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Thomas Corbat (IFS)
 *******************************************************************************/
package org.eclipse.cdt.internal.pdom.tests;

import junit.framework.Test;

import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.IParameter;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBasicType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.internal.core.CCoreInternals;
import org.eclipse.cdt.internal.core.pdom.PDOM;

/**
 * Tests for verifying whether the PDOM correctly stores information about
 * C++ class member functions.
 */
public class MethodTests extends PDOMTestBase {
	protected ICProject project;
	protected PDOM pdom;

	public static Test suite() {
		return suite(MethodTests.class);
	}

	@Override
	protected void setUp() throws Exception {
		project = createProject("methodTests");
		pdom = (PDOM) CCoreInternals.getPDOMManager().getPDOM(project);
		pdom.acquireReadLock();
	}

	@Override
	protected void tearDown() throws Exception {
		pdom.releaseReadLock();
		if (project != null) {
			CProjectHelper.delete(project);
			project= null;
		}
	}

	public void testMethodParameters() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::normalMethod");
		assertEquals(1, bindings.length);
		ICPPFunction function = (ICPPFunction) bindings[0];
		IParameter[] parameters = function.getParameters();
		assertEquals(IBasicType.t_int, ((ICPPBasicType) parameters[0].getType()).getType());
		assertEquals("p1", parameters[0].getName());
		assertEquals(IBasicType.t_char, ((ICPPBasicType) parameters[1].getType()).getType());
		assertEquals("p2", parameters[1].getName());
		assertEquals(IBasicType.t_float, ((ICPPBasicType) parameters[2].getType()).getType());
		assertEquals("p3", parameters[2].getName());
	}
	
	public void testVirtualMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::inheritedMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.isVirtual());
	}
	
	public void testVirtualMethodType() throws Exception {
		assertType(pdom, "Class1::inheritedMethod", ICPPFunction.class);
	}

	public void testVirtualMethodDeclarations() throws Exception {
		assertDeclarationCount(pdom, "Class1::inheritedMethod", 2);
	}

	public void testVirtualMethodDefinitions() throws Exception {
		assertDefinitionCount(pdom, "Class1::inheritedMethod", 1);
	}

	public void testVirtualMethodReferences() throws Exception {
		assertReferenceCount(pdom, "Class1::inheritedMethod", 3);
	}

	public void testInheritedMethodType() throws Exception {
		assertEquals(0, findQualifiedName(pdom, "Class2::inheritedMethod").length);
	}

	public void testInheritedMethodDeclarations() throws Exception {
		assertDeclarationCount(pdom, "Class2::inheritedMethod", 0);
	}

	public void testInheritedMethodDefinitions() throws Exception {
		assertDefinitionCount(pdom, "Class2::inheritedMethod", 0);
	}

	public void testInheritedMethodReferences() throws Exception {
		assertReferenceCount(pdom, "Class2::inheritedMethod", 0);
	}

	public void testPureVirtualMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::pureVirtualMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.isVirtual());
		assertTrue(method.isPureVirtual());
	}
	
	public void testPureVirtualMethodType() throws Exception {
		assertType(pdom, "Class1::pureVirtualMethod", ICPPFunction.class);
		assertType(pdom, "Class2::pureVirtualMethod", ICPPFunction.class);
	}

	public void testPureVirtualMethodDeclarations() throws Exception {
		assertDeclarationCount(pdom, "Class1::pureVirtualMethod", 1);
		assertDeclarationCount(pdom, "Class2::pureVirtualMethod", 2);
	}

	public void testPureVirtualMethodDefinitions() throws Exception {
		assertDefinitionCount(pdom, "Class1::pureVirtualMethod", 0);
		assertDefinitionCount(pdom, "Class2::pureVirtualMethod", 1);
	}

	public void testPureVirtualMethodReferences() throws Exception {
		assertReferenceCount(pdom, "Class1::pureVirtualMethod", 2);
		assertReferenceCount(pdom, "Class2::pureVirtualMethod", 3);
	}

	public void testOverriddenMethodType() throws Exception {
		assertType(pdom, "Class1::overriddenMethod", ICPPFunction.class);
		assertType(pdom, "Class2::overriddenMethod", ICPPFunction.class);
	}

	public void testOverriddenMethodDeclarations() throws Exception {
		assertDeclarationCount(pdom, "Class1::overriddenMethod", 2);
		assertDeclarationCount(pdom, "Class2::overriddenMethod", 2);
	}

	public void testOverriddenMethodDefinitions() throws Exception {
		assertDefinitionCount(pdom, "Class1::overriddenMethod", 1);
		assertDefinitionCount(pdom, "Class2::overriddenMethod", 1);
	}

	public void testOverriddenMethodReferences() throws Exception {
		assertReferenceCount(pdom, "Class1::overriddenMethod", 3);
		assertReferenceCount(pdom, "Class2::overriddenMethod", 4);
	}

	public void testDestructor() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::~Class1");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.isDestructor());
	}
	
	public void testDefaultPrivateMethod() throws Exception {
		assertVisibility(pdom, "Class3::defaultMethod", ICPPMember.v_private);
	}
	
	public void testPrivateMethod() throws Exception {
		assertVisibility(pdom, "Class3::privateMethod", ICPPMember.v_private);
	}
	
	public void testProtectedMethod() throws Exception {
		assertVisibility(pdom, "Class3::protectedMethod", ICPPMember.v_protected);
	}
	
	public void testPublicMethod() throws Exception {
		assertVisibility(pdom, "Class3::publicMethod", ICPPMember.v_public);
	}
	
	public void testInlineMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::inlineMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.isInline());
	}
	
	public void testStaticMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::staticMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.isStatic());
	}
	
	public void testVarArgsMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::varArgsMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		assertTrue(method.takesVarArgs());
	}
	
	public void testConstMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::constMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertTrue(type.isConst());
	}
	
	public void testVolatileMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::volatileMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertTrue(type.isVolatile());
	}
		
	public void testConstVolatileMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::constVolatileMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertTrue(type.isConst());
		assertTrue(type.isVolatile());
	}
	
	public void testNotConstMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::notConstMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertFalse(type.isConst());
	}
	
	public void testNotVolatileMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::notVolatileMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertFalse(type.isVolatile());
	}
		
	public void testNotConstVolatileMethod() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::notConstVolatileMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		ICPPFunctionType type = method.getType();
		assertFalse(type.isConst());
		assertFalse(type.isVolatile());
	}
	
	public void testNoExceptionSpecification() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::noExceptionSpecMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertNull(exceptionSpec);
	}
	
	public void testEmptyExceptionSpecification() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::emptyExceptionSpecMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertEquals(0, exceptionSpec.length);
	}
	
	public void testNonEmptyExceptionSpecification() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "Class1::nonEmptyExceptionSpecMethod");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertEquals(1, exceptionSpec.length);
		assertEquals(IBasicType.t_int, ((ICPPBasicType) exceptionSpec[0]).getType());
	}

	public void testImplicitCtorExceptionSpec() throws Exception {
		IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::D");
		// get both default ctor + copy ctor
		assertEquals(2, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertNull(exceptionSpec);
	}

	public void testImplicitCopyCtorExceptionSpec() throws Exception {
		IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::D");
		// get both default ctor + copy ctor
		assertEquals(2, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[1];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertEquals(0, exceptionSpec.length);
	}

	public void testImplicitDtorExceptionSpec() throws Exception {
		IBinding[] bindings = findQualifiedPossiblyImplicit(pdom, "D::~D");
		assertEquals(1, bindings.length);
		ICPPMethod method = (ICPPMethod) bindings[0];
		IType[] exceptionSpec = method.getExceptionSpecification();
		assertEquals(2, exceptionSpec.length);
		int t1= ((ICPPBasicType) exceptionSpec[0]).getType();
		int t2= ((ICPPBasicType) exceptionSpec[1]).getType();
		assertEquals(IBasicType.t_int, Math.min(t1, t2));
		assertEquals(IBasicType.t_double, Math.max(t1, t2));
	}

	public void testVirtualMemberFunction() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "E::virtualMemberFunction");
		assertEquals(1, bindings.length);
		assertInstance(bindings[0], ICPPMethod.class);
		ICPPMethod virtMemFun = (ICPPMethod) bindings[0];
		assertFalse(virtMemFun.isOverride());
		assertFalse(virtMemFun.isFinal());
	}

	public void testOverrideVirtualMemberFunction() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "F::virtualMemberFunction");
		assertEquals(1, bindings.length);
		assertInstance(bindings[0], ICPPMethod.class);
		ICPPMethod virtMemFun = (ICPPMethod) bindings[0];
		assertTrue(virtMemFun.isOverride());
		assertFalse(virtMemFun.isFinal());
	}

	public void testOverrideFinalVirtualMemberFunction() throws Exception {
		IBinding[] bindings = findQualifiedName(pdom, "G::virtualMemberFunction");
		assertEquals(1, bindings.length);
		assertInstance(bindings[0], ICPPMethod.class);
		ICPPMethod virtMemFun = (ICPPMethod) bindings[0];
		assertTrue(virtMemFun.isOverride());
		assertTrue(virtMemFun.isFinal());
	}
}