From 3ee5f868feaf41575e7c41a2fe60fe9bb0147c29 Mon Sep 17 00:00:00 2001
From: Markus Schorn <markus.schorn@windriver.com>
Date: Mon, 2 Jul 2007 13:14:14 +0000
Subject: [PATCH] Test-cases for bug 195127, prefer AST over index in plain C.

---
 .../tests/IndexBindingResolutionTestBase.java | 16 +++-
 .../tests/IndexCBindingResolutionBugs.java    | 92 +++++++++++++++++++
 2 files changed, 106 insertions(+), 2 deletions(-)

diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java
index 1e1196581d2..f0230ffca88 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexBindingResolutionTestBase.java
@@ -396,8 +396,8 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
 
 	/**
 	 * When a test is failing only for the strategy where the test data is split over
-	 * multiple index fragements, we artificially fail the single fragment strategy also.
-	 * This is not ideal, but as both strategies behaviour are typically the same, is 
+	 * multiple index fragments, we artificially fail the single fragment strategy also.
+	 * This is not ideal, but as both strategies behavior are typically the same, is 
 	 * quite rare.
 	 */
 	protected void fakeFailForSingle() {
@@ -405,4 +405,16 @@ public abstract class IndexBindingResolutionTestBase extends BaseTestCase {
 			fail("Artificially failing - see IndexBindingResolutionTestBase.fakeFailForSingle()");
 		}
 	}
+
+	/**
+	 * When a test is failing only for the strategy where the test data is not split over
+	 * multiple index fragments, we artificially fail the single fragment strategy also.
+	 * This is not ideal, but as both strategies behavior are typically the same, is 
+	 * quite rare.
+	 */
+	protected void fakeFailForMultiProject() {
+		if(getName().startsWith("_") && strategy instanceof ReferencedProject) {
+			fail("Artificially failing - see IndexBindingResolutionTestBase.fakeFailForReferenced()");
+		}
+	}
 }
\ No newline at end of file
diff --git a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java
index b2ce4e66ff5..88809ff5c08 100644
--- a/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java
+++ b/core/org.eclipse.cdt.core.tests/parser/org/eclipse/cdt/internal/index/tests/IndexCBindingResolutionBugs.java
@@ -18,9 +18,13 @@ import org.eclipse.cdt.core.dom.ast.IBasicType;
 import org.eclipse.cdt.core.dom.ast.IBinding;
 import org.eclipse.cdt.core.dom.ast.ICompositeType;
 import org.eclipse.cdt.core.dom.ast.IEnumeration;
+import org.eclipse.cdt.core.dom.ast.IEnumerator;
+import org.eclipse.cdt.core.dom.ast.IField;
 import org.eclipse.cdt.core.dom.ast.IFunction;
 import org.eclipse.cdt.core.dom.ast.IParameter;
 import org.eclipse.cdt.core.dom.ast.IType;
+import org.eclipse.cdt.core.dom.ast.ITypedef;
+import org.eclipse.cdt.core.dom.ast.IVariable;
 
 /**
  * For testing PDOM binding resolution
@@ -127,4 +131,92 @@ public class IndexCBindingResolutionBugs extends IndexBindingResolutionTestBase
 //		type= ((ITypedef) type).getType();
 //		assertTrue(type instanceof IEnumeration);
     }
+    
+    // int globalVar;
+
+	// // don't include header
+    // char globalVar;
+    public void _testAstIndexConflictVariable_Bug195127() throws Exception {
+    	fakeFailForMultiProject();
+		IBinding b0 = getBindingFromASTName("globalVar;", 9);
+		assertTrue(b0 instanceof IVariable);
+		IVariable v= (IVariable) b0;
+		IType type= v.getType();
+		assertTrue(type instanceof IBasicType);
+		assertTrue(((IBasicType) type).getType() == IBasicType.t_char);
+    }
+
+    // int globalFunc();
+
+	// // don't include header
+    // char globalFunc();
+    public void _testAstIndexConflictFunction_Bug195127() throws Exception {
+    	fakeFailForMultiProject();
+		IBinding b0 = getBindingFromASTName("globalFunc(", 10);
+		assertTrue(b0 instanceof IFunction);
+		IFunction f= (IFunction) b0;
+		IType type= f.getType().getReturnType();
+		assertTrue(type instanceof IBasicType);
+		assertTrue(((IBasicType) type).getType() == IBasicType.t_char);
+    }
+
+    // struct astruct {
+    //    int member;
+    // };
+
+	// // don't include header
+    // struct astruct {
+    //    char member;
+    //    int additionalMember;
+    // };
+    public void _testAstIndexConflictStruct_Bug195127() throws Exception {
+    	fakeFailForMultiProject();
+		IBinding b0 = getBindingFromASTName("astruct", 7);
+		assertTrue(b0 instanceof ICompositeType);
+		ICompositeType ct= (ICompositeType) b0;
+		IField[] fields= ct.getFields();
+		assertEquals(2, fields.length);
+		IField member= fields[0];
+		IField additionalMember= fields[1];
+		if (member.getName().equals("additionalMember")) {
+			IField h= member; member= additionalMember; additionalMember= h;
+		}
+		assertEquals("member", member.getName());
+		assertEquals("additionalMember", additionalMember.getName());
+		IType type= member.getType();
+		assertTrue(type instanceof IBasicType);
+		assertTrue(((IBasicType) type).getType() == IBasicType.t_char);
+    }
+
+    // enum anenum {
+    //    eItem0
+    // };
+
+	// // don't include header
+    // enum anenum {
+    //    eItem0, eItem1
+    // };
+    public void _testAstIndexConflictEnumerator_Bug195127() throws Exception {
+    	fakeFailForMultiProject();
+		IBinding b0 = getBindingFromASTName("anenum", 7);
+		assertTrue(b0 instanceof IEnumeration);
+		IEnumeration enumeration= (IEnumeration) b0;
+		IEnumerator[] enumerators= enumeration.getEnumerators();
+		assertEquals(2, enumerators.length);
+    }
+
+    // typedef int atypedef;
+
+	// // don't include header
+    // typedef char atypedef;
+    public void testAstIndexConflictTypedef_Bug195127() throws Exception {
+    	fakeFailForMultiProject();
+		IBinding b0 = getBindingFromASTName("atypedef;", 8);
+		assertTrue(b0 instanceof ITypedef);
+		ITypedef t= (ITypedef) b0;
+		IType type= t.getType();
+		assertTrue(type instanceof IBasicType);
+		assertTrue(((IBasicType) type).getType() == IBasicType.t_char);
+    }
+
 }