1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-08-13 19:25:38 +02:00

Fixed test infrastructure (use common code C test utils to read files from java comments)

This commit is contained in:
Alena Laskavaia 2010-05-11 02:08:44 +00:00
parent fdb501da55
commit 5f256a6889
7 changed files with 341 additions and 356 deletions

View file

@ -148,11 +148,17 @@ public class ControlFlowGraphTest extends CodanTestCase {
return false;
}
protected void buildAndCheck(String code) {
buildAndCheck(code, false);
}
protected void buildAndCheck_cpp(String code) {
buildAndCheck(code, true);
}
/**
* @param file
*/
protected void buildAndCheck(String file) {
load(file);
protected void buildAndCheck(String code, boolean cpp) {
loadcode(code, cpp);
buildCfg();
checkCfg();
}
@ -195,30 +201,27 @@ public class ControlFlowGraphTest extends CodanTestCase {
return null;
}
/*-
<code file="test1.c">
main() {
int a;
a=1;
}
</code>
*/
public void test_basic() {
buildAndCheck("test1.c");
@Override
public boolean isCpp() {
return true;
}
/*-
<code file="test2.c">
main() {
int a=10;
while (a--) {
a=a-2;
}
}
</code>
*/
// main() {
// int a;
// a=1;
// }
public void test_basic() {
buildAndCheck(getAboveComment());
}
// main() {
// int a=10;
// while (a--) {
// a=a-2;
// }
// }
public void test_while() {
buildAndCheck("test2.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IConnectorNode conn = (IConnectorNode) decl.getOutgoing();
@ -233,18 +236,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode ret = (IExitNode) ((IConnectorNode) m1).getOutgoing();
}
/*-
<code file="test3.c">
main() {
int a=10;
if (a--) {
a=a-2;
}
}
</code>
*/
// main() {
// int a=10;
// if (a--) {
// a=a-2;
// }
// }
public void test_if() {
buildAndCheck("test3.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -257,18 +256,14 @@ public class ControlFlowGraphTest extends CodanTestCase {
assertSame(m1, m2);
}
/*-
<code file="test4.c">
main() {
int a=10;
if (a--) {
return;
}
}
</code>
*/
// main() {
// int a=10;
// if (a--) {
// return;
// }
// }
public void test_if_ret() {
buildAndCheck("test4.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -277,19 +272,16 @@ public class ControlFlowGraphTest extends CodanTestCase {
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse);
}
/*-
<code file="test5.c">
main() {
int a=10;
if (a--) {
return;
a++;
}
}
</code>
*/
// main() {
// int a=10;
// if (a--) {
// return;
// a++;
// }
// }
public void test_if_dead() {
buildAndCheck("test5.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -297,21 +289,18 @@ public class ControlFlowGraphTest extends CodanTestCase {
IExitNode bThen = (IExitNode) branchEnd(des, IBranchNode.THEN);
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m1 = jumpEnd(bElse);
assertEquals(1,graph.getUnconnectedNodeSize());
assertEquals(1, graph.getUnconnectedNodeSize());
}
/*-
<code file="test_ifif.c">
foo() {
int a=10, x=5;
if (a--) {
if (x<0)
a++;
}
}
</code>
*/
// foo() {
// int a=10, x=5;
// if (a--) {
// if (x<0)
// a++;
// }
// }
public void test_ifif() {
buildAndCheck("test_ifif.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
IPlainNode decl = (IPlainNode) startNode.getOutgoing();
IDecisionNode des = (IDecisionNode) decl.getOutgoing();
@ -319,41 +308,33 @@ public class ControlFlowGraphTest extends CodanTestCase {
IDecisionNode bThen = (IDecisionNode) branchEnd(des, IBranchNode.THEN);
assertEquals("x<0", data(bThen));
IBasicBlock bElse = branchEnd(des, IBranchNode.ELSE);
IBasicBlock m2 = jumpEnd(branchEnd(bThen,IBranchNode.THEN));
IBasicBlock m2 = jumpEnd(branchEnd(bThen, IBranchNode.THEN));
IBasicBlock m1 = jumpEnd(bElse);
IBasicBlock m3 = jumpEnd(m2);
assertSame(m1, m3);
}
/*-
<code file="test_throw.cc">
int foo() {
throw 5;
}
</code>
*/
// int foo() {
// throw 5;
// }
public void test_throw() {
buildAndCheck("test_throw.cc");
buildAndCheck_cpp(getAboveComment());
IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize());
Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next();
assertEquals("throw 5;", data(exit));
}
/*-
<code file="test_exit.c">
int foo() {
exit(0);
}
</code>
*/
// int foo() {
// exit(0);
// }
public void test_exit() {
buildAndCheck("test_exit.c");
buildAndCheck(getAboveComment());
IStartNode startNode = graph.getStartNode();
assertEquals(1, graph.getExitNodeSize());
Iterator<IExitNode> exitNodeIterator = graph.getExitNodeIterator();
IExitNode exit = exitNodeIterator.next();
assertEquals("exit(0);", data(exit));
}
}

View file

@ -18,95 +18,71 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class ExpressionRequiredInReturnCheckerTest extends CheckerTestCase {
/*-
<code file="test1.c">
dummy() {
return; // error here on line 2
}
</code>
*/
// dummy() {
// return; // error here on line 2
// }
public void testDummyFunction() {
load("test1.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors(); // because return type if not defined, usually people don't care
}
/*-
<code file="test2.c">
void void_function(void) {
return; // no error here
}
</code>
*/
// void void_function(void) {
// return; // no error here
// }
public void testVoidFunction() {
load("test2.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test3.c">
int integer_return_function(void) {
if (global) {
if (global == 100) {
return; // error here on line 4
}
}
}
</code>
*/
// int integer_return_function(void) {
// if (global) {
// if (global == 100) {
// return; // error here on line 4
// }
// }
// }
public void testBasicTypeFunction() {
load("test3.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
/*-
<code file="test4.c">
struct My_Struct {
int a;
};
struct My_Struct struct_return_function(void) {
return; // error here on line 6
}
</code>
*/
//
// struct My_Struct {
// int a;
// };
//
// struct My_Struct struct_return_function(void) {
// return; // error here on line 6
// }
public void testUserDefinedFunction() {
load("test4.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(6);
}
/*-
<code file="test5.c">
typedef unsigned int uint8_t;
uint8_t return_typedef(void) {
return; // error here on line 4
}
</code>
*/
// typedef unsigned int uint8_t;
//
// uint8_t return_typedef(void) {
// return; // error here on line 4
// }
public void testTypedefReturnFunction() {
load("test5.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
/*-
<code file="test6.c">
typedef unsigned int uint8_t;
uint8_t (*return_fp_no_typedef(void))(void)
{
return; // error here on line 5
}
</code>
*/
// typedef unsigned int uint8_t;
//
// uint8_t (*return_fp_no_typedef(void))(void)
// {
// return; // error here on line 5
// }
public void testFunctionPointerReturnFunction() {
load("test6.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(5);
}

View file

@ -10,6 +10,9 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.internal.checkers;
import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.codan.core.test.CheckerTestCase;
/**
@ -17,84 +20,89 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class StatementHasNoEffectCheckerTest extends CheckerTestCase {
/*-
<code file="test1.c">
main() {
int a;
+a; // error here on line 3
}
</code>
*/
public void testUnaryExpression() {
load("test1.c");
runOnFile();
// main() {
// int a;
// +a; // error here on line 3
// }
public void testUnaryExpression() throws IOException {
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
/*-
<code file="test2.c">
main() {
int a,b;
b+a; // error here on line 4
}
</code>
*/
// main() {
// int a,b;
//
// b+a; // error here on line 4
// }
public void testBinaryExpression() {
load("test2.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
/*-
<code file="test3.c">
main() {
int a,b;
a=b+a; // no error here
}
</code>
*/
// main() {
// int a,b;
//
// a=b+a; // no error here
// }
public void testNormalAssignment() {
load("test3.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test4.c">
main() {
int a,b;
(a=b); // no errors here
a+=b;
a<<=b;
a-=b;
a++;
b--;
--a;
++b;
a%=2;
a>>=2;
}
</code>
*/
// main() {
// int a,b;
//
// (a=b); // no errors here
// a+=b;
// a<<=b;
// a-=b;
// a++;
// b--;
// --a;
// ++b;
// a%=2;
// a>>=2;
// }
public void testFalsePositives() {
load("test4.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test5.c">
main() {
int a;
a; // error here on line 3
}
</code>
*/
// main() {
// int a;
// a; // error here on line 3
// }
public void testIdExpression() {
load("test5.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
// main() {
// int a=({foo();a;}); // no error here on line 2
// }
public void testGNUExpressionCompoundStmt() {
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/* first file */
// main() {
// int a;
// +a; // error here on line 3
// }
/* second file */
// foo() {
// int a;
//
// +a; // error here on line 4
// }
/* this test is using two files */
public void test2FilesUnaryExpression() throws IOException {
StringBuffer[] code = getContents(2);
File f1 = loadcode(code[0].toString());
File f2 = loadcode(code[1].toString());
runOnProject();
checkErrorLine(f1, 3);
checkErrorLine(f2, 4);
}
}

View file

@ -17,87 +17,68 @@ import org.eclipse.cdt.codan.core.test.CheckerTestCase;
*
*/
public class SuggestedParenthesisCheckerTest extends CheckerTestCase {
/*-
<code file="test1.c">
main() {
int a=1,b=3;
if (!a<10) b=4; // error here on line 3
}
</code>
*/
// main() {
// int a=1,b=3;
// if (!a<10) b=4; // error here on line 3
// }
public void test1() {
load("test1.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
/*-
<code file="test2.c">
main() {
int a=1,b=3;
if (b+a && a>b || b-a) b--; // error here on line 4
}
</code>
*/
// main() {
// int a=1,b=3;
//
// if (b+a && a>b || b-a) b--; // error here on line 4
// }
public void test2() {
load("test2.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(4);
}
/*-
<code file="test3.c">
main() {
int a=1,b=3;
if (!(a<10)) b=4; // no error here on line 3
}
</code>
*/
// main() {
// int a=1,b=3;
// if (!(a<10)) b=4; // no error here on line 3
// }
public void test3() {
load("test3.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test4.c">
main() {
int a=1,b=3;
if (a && !b) b=4; // no error here on line 3
}
</code>
*/
// main() {
// int a=1,b=3;
// if (a && !b) b=4; // no error here on line 3
// }
public void test_lastnot() {
load("test4.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test5.c">
main() {
int a=1,b=3;
if ((!a) && 10) b=4; // no error here on line 3
}
</code>
*/
// main() {
// int a=1,b=3;
// if ((!a) && 10) b=4; // no error here on line 3
// }
public void test_fixed() {
load("test5.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkNoErrors();
}
/*-
<code file="test6.c">
main() {
int a=1,b=3;
if (a && b & a) b=4; // error here on line 3
}
</code>
*/
// main() {
// int a=1,b=3;
// if (a && b & a) b=4; // error here on line 3
// }
public void test_mixedbin() {
load("test6.c");
runOnFile();
loadCodeAndRun(getAboveComment());
checkErrorLine(3);
}
}

View file

@ -10,6 +10,7 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.test;
import java.io.File;
import java.io.IOException;
import org.eclipse.cdt.codan.core.CodanRuntime;
@ -24,12 +25,14 @@ import org.eclipse.core.runtime.CoreException;
public class CheckerTestCase extends CodanTestCase {
private IMarker[] markers;
public void checkErrorLine(int i) {
checkErrorLine(currentFile, i);
}
/**
* @param i
* - line
*/
public void checkErrorLine(int i) {
public void checkErrorLine(File file, int i) {
assertTrue(markers != null);
assertTrue(markers.length > 0);
boolean found = false;
@ -48,8 +51,13 @@ public class CheckerTestCase extends CodanTestCase {
} catch (IOException e) {
fail(e.getMessage());
}
String mfile = m.getResource().getName();
if (line.equals(i)) {
found = true;
if (file!=null && !file.getName().equals(mfile))
found = false;
else
break;
}
}
assertTrue("Error on line " + i + " not found ", found);
@ -63,12 +71,27 @@ public class CheckerTestCase extends CodanTestCase {
/**
*
*/
public void runOnFile() {
public void runOnProject() {
try {
loadFiles();
indexFiles();
} catch (CoreException e) {
fail(e.getMessage());
}
runCodan();
}
public void loadCodeAndRun(String code) {
loadcode(code);
runOnProject();
}
public void loadCodeAndRunCpp(String code) {
loadcode(code, true);
runOnProject();
}
/**
*
*/
protected void runCodan() {
CodanRuntime.getInstance().getBuilder().processResource(
cproject.getProject(), NPM);
try {

View file

@ -10,17 +10,20 @@
*******************************************************************************/
package org.eclipse.cdt.codan.core.test;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import junit.framework.AssertionFailedError;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.util.BaseTestCase;
import org.eclipse.cdt.core.testplugin.util.TestSourceReader;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
@ -54,7 +57,7 @@ public class CodanTestCase extends BaseTestCase {
}
/**
* Override for c++
* Override for c++ (i.e. at least one c++ test)
*
* @return is c++ tests
*/
@ -69,32 +72,6 @@ public class CodanTestCase extends BaseTestCase {
tmpDir = cproject.getProject().getLocation().makeAbsolute().toFile();
}
public File load(String file) {
Class clazz = getClass();
InputStream st = null;
try {
st = TestUtils.getJavaFileText(clazz);
} catch (IOException e) {
fail("Cannot find java file: " + clazz + ": " + e.getMessage());
}
try {
File testFile = new File(tmpDir, file);
tempFiles.add(testFile);
TestUtils.saveFile(st, testFile);
st.close();
currentFile = testFile;
try {
cproject.getProject().refreshLocal(1, null);
} catch (CoreException e) {
// hmm
}
return testFile;
} catch (IOException e) {
fail("Cannot save test: " + file + ": " + e.getMessage());
return null;
}
}
public void tearDown() throws CoreException {
if (cproject != null) {
try {
@ -102,7 +79,6 @@ public class CodanTestCase extends BaseTestCase {
IResource.FORCE
| IResource.ALWAYS_DELETE_PROJECT_CONTENT,
new NullProgressMonitor());
} catch (CoreException e) {
throw e;
}
@ -156,7 +132,7 @@ public class CodanTestCase extends BaseTestCase {
return cprojects[0];
}
protected void loadFiles() throws CoreException {
protected void indexFiles() throws CoreException {
final IWorkspace workspace = ResourcesPlugin.getWorkspace();
workspace.run(new IWorkspaceRunnable() {
public void run(IProgressMonitor monitor) throws CoreException {
@ -167,7 +143,7 @@ public class CodanTestCase extends BaseTestCase {
CCorePlugin.getIndexManager().setIndexerId(cproject,
IPDOMManager.ID_FAST_INDEXER);
// wait until the indexer is done
assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000*60, // 1 min
assertTrue(CCorePlugin.getIndexManager().joinIndexer(1000 * 60, // 1 min
new NullProgressMonitor()));
return;
}
@ -190,10 +166,75 @@ public class CodanTestCase extends BaseTestCase {
return line;
cur++;
}
;
} finally {
st.close();
}
return 0;
}
protected String getAboveComment() {
return getContents(1)[0].toString();
}
protected StringBuffer[] getContents(int sections) {
try {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
if (plugin == null)
throw new AssertionFailedError(
"This test must be run as a JUnit plugin test");
return TestSourceReader.getContentsForTest(plugin.getBundle(),
"src", getClass(), getName(), sections);
} catch (IOException e) {
fail(e.getMessage());
return null;
}
}
public File loadcode(String code, boolean cpp) {
String ext = cpp ? ".cc" : ".c"; //$NON-NLS-1$ //$NON-NLS-2$
File testFile = null;
try {
testFile = File.createTempFile("test", ext, tmpDir); //$NON-NLS-1$
} catch (IOException e1) {
fail(e1.getMessage());
return null;
}
return loadcode(code, testFile);
}
public File loadcode(String code, String filename) {
File testFile = new File(tmpDir, filename);
return loadcode(code, testFile);
}
private File loadcode(String code, File testFile) {
try {
tempFiles.add(testFile);
TestUtils.saveFile(
new ByteArrayInputStream(code.trim().getBytes()), testFile);
currentFile = testFile;
try {
cproject.getProject().refreshLocal(1, null);
} catch (CoreException e) {
// hmm
fail(e.getMessage());
}
return testFile;
} catch (IOException e) {
fail("Cannot save test: " + testFile + ": " + e.getMessage()); //$NON-NLS-1$ //$NON-NLS-2$
return null;
}
}
public File loadcode_c(String code) {
return loadcode(code, true);
}
public File loadcode_cpp(String code) {
return loadcode(code, false);
}
public File loadcode(String code) {
return loadcode(code, isCpp());
}
}

View file

@ -7,8 +7,7 @@
*
* Contributors:
* Alena Laskavaia - initial API and implementation
*******************************************************************************/
*******************************************************************************/
package org.eclipse.cdt.codan.core.test;
import java.io.BufferedReader;
@ -19,42 +18,19 @@ import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* TODO: add description
*/
public class TestUtils {
static final Pattern filePattern = Pattern.compile("file=\"(.*)\"");
/**
* @param st
* @param testFile
* @return
* @throws FileNotFoundException
* @throws IOException
*/
public static File saveFile(InputStream st, File testFile)
throws FileNotFoundException, IOException {
BufferedReader r = new BufferedReader(new InputStreamReader(st));
String line;
PrintStream wr = new PrintStream(testFile);
try {
boolean print = false;
while ((line = r.readLine()) != null) {
if (line.contains("<code ")) {
Matcher m = filePattern.matcher(line);
if (m.find()) {
String userFile = m.group(1);
if (userFile.equals(testFile.getName())) {
print = true;
}
}
} else if (line.contains("</code>")) {
print = false;
} else if (print) {
wr.println(line);
}
wr.println(line);
}
} finally {
wr.close();
@ -67,20 +43,19 @@ public class TestUtils {
* @return
* @throws IOException
*/
public static InputStream getJavaFileText(Class clazz) throws IOException {
public static InputStream getJavaFileText(String srcRoot, Class clazz)
throws IOException {
CodanCoreTestActivator plugin = CodanCoreTestActivator.getDefault();
String classFile = clazz.getName().replaceAll("\\.", "/");
classFile += ".java";
InputStream st = null;
if (plugin != null) {
URL resource = plugin.getBundle().getResource(
"src/" + classFile);
st = resource.openStream();
} else {
st = clazz.getResourceAsStream(classFile);
}
if (plugin != null) {
URL resource = plugin.getBundle().getResource(
srcRoot + "/" + classFile);
st = resource.openStream();
} else {
st = clazz.getResourceAsStream(classFile);
}
return st;
}
}