Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 478fe6bb50fb5e35031167ad1e1a4bb4f224b60e (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2001 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials 
 * are made available under the terms of the Common Public License v0.5 
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *     IBM Corp. - Rational Software - initial implementation
 ******************************************************************************/
package org.eclipse.cdt.core.parser.tests;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.util.NoSuchElementException;
import java.util.StringTokenizer;

import junit.framework.AssertionFailedError;
import junit.framework.Test;

import org.eclipse.cdt.core.parser.IParser;
import org.eclipse.cdt.core.parser.ParserFactory;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.internal.core.dom.DOMBuilder;
import org.eclipse.core.runtime.Path;


/**
 * @author vmozgin
 *
 * Automated parser test framework, to use with GCC testsuites
 */
public class TortureTest extends FractionalAutomatedTest {
	
	static protected boolean isEnabled = false;
	static protected boolean quickParse = true;

	public TortureTest () {
		super();
	}

	public TortureTest (String name) {
		super(name);
	}
	
	protected AutomatedFramework newTest (String name){
		return new TortureTest (name);
	}
	
	protected void loadProperties() throws Exception{
		String resourcePath = org.eclipse.core.runtime.Platform.getPlugin("org.eclipse.cdt.ui.tests").find(new Path("/")).getFile();
		resourcePath += "/parser/org/eclipse/cdt/core/parser/resources";
	
		try {
			FileInputStream propertiesIn = new FileInputStream(resourcePath + "/TortureTest.properties");
			properties.load (propertiesIn);
		
			isEnabled = properties.getProperty("enabled", "false").equalsIgnoreCase("true");
			quickParse = properties.getProperty("quickParse", "true").equalsIgnoreCase("true");
			
			String sourceInfo = properties.getProperty("source", "");
			
			stepSize = Integer.parseInt(properties.getProperty("stepSize", "25000"));
			outputFile = properties.getProperty("outputFile", "");
			timeOut = Integer.parseInt(properties.getProperty("timeOut", "60000"));
			outputDir = properties.getProperty("outDir", "");
			
			if (sourceInfo.equals(""))
				throw new FileNotFoundException();
			else {
				StringTokenizer tokenizer = new StringTokenizer(sourceInfo, ",");
				String str = null, val = null;
				try {
					while (tokenizer.hasMoreTokens()) {
						str = tokenizer.nextToken().trim();
						val = tokenizer.nextToken().trim();
					
						testSources.put(str, val);
					}
				} catch (NoSuchElementException e){
					//only way to get here is to have a missing val, assume cpp for that str
					testSources.put(str, "cpp");
				}
			
			}
		} catch (FileNotFoundException e){
			testSources.put(resourcePath + "/torture", "cpp");
		}
		
		if (!isEnabled) testSources.clear();
	}
	
	
	public static Test suite()
	{
		AutomatedFramework frame = new TortureTest();		
		return frame.createSuite();
	}
	
	
	static protected void reportException (Throwable e, String file, IParser parser){
		String output = null;
		int lineNumber = -1;
		
		try {
			lineNumber = parser.getLineNumberForOffset(parser.getLastErrorOffset());
		} catch (Exception ex) {}
		
		if (e instanceof AssertionFailedError) {
			output = file + ": Parse failed on line ";
			output += lineNumber + "\n";
		} else {
			output = file + ": " + e.getClass().toString();
			output += " on line " + lineNumber + "\n";
		}
		try {
		if (report != null) {
			report.write(output.getBytes());
		}
		} catch (IOException ex) {}

		fail(output);
	}
	
	
	static protected boolean isExpectedToPass (String testCode) 
	{
		// Process some DejaGNU instructions	
		if (testCode.indexOf("{ dg-do run") >= 0) return true;
		if (testCode.indexOf("{ dg-do link") >= 0) return true;
		if (testCode.indexOf("{ dg-error") >= 0) return false;
		if (testCode.indexOf("// ERROR") >= 0) return false;
		if (testCode.indexOf("- ERROR") >= 0) return false;
		if (testCode.indexOf("// XFAIL") >= 0) return false;
		if (testCode.indexOf("{ xfail") >= 0) return false;
		if (testCode.indexOf("{ dg-preprocess") >= 0) return false;
		
		return true;
	}
	
	
	public void doFile() throws Throwable {
		assertNotNull (fileList);
		
		File file = (File)fileList.removeFirst();
		FileInputStream stream = new FileInputStream(file);

		String filePath = file.getCanonicalPath();
		String nature = (String)natures.get(filePath);
	
		StringWriter code = new StringWriter(); 
			
		byte b[] = new byte[stepSize]; 
		int n = stream.read(b);
		while( n != -1 ){
			code.write(new String(b));
			n = stream.read(b);
		}
		
		String testCode = code.toString();
		
		if (isExpectedToPass(testCode)) {
			ParseThread thread = new ParseThread();

			thread.quickParse = quickParse;
			thread.code = testCode;
			thread.cppNature = nature.equalsIgnoreCase("cpp");
			thread.file = filePath;
			
			thread.start();
			thread.join(timeOut);

			if (thread.isAlive()) {
				thread.stop();
				reportHang(testCode, filePath);
			} else if (thread.result != null) {
				reportException(thread.result, filePath, thread.parser);
			}
		} else {
			// gcc probably didn't expect this test to pass.
			// It doesn't mean that it should pass CDT parser,
			// as it is more relaxed
			// Result - 'inconclusive', but we report 'pass'
			assertTrue(true);                                                                         
		}
	}
	
			
	
	static class ParseThread extends Thread {
		public String 		code;
		public boolean 		cppNature;
		public String 		file;
		public Throwable 	result = null;
		public IParser 		parser = null;
		public boolean 		quickParse = true;
	
		public void run(){
			try {           
				DOMBuilder domBuilder = new DOMBuilder(); 
				IParser parser = ParserFactory.createParser( 
					ParserFactory.createScanner( new StringReader( code ), null, null, null, ParserMode.QUICK_PARSE ), nullCallback, ParserMode.QUICK_PARSE);
	
				parser.setCppNature(cppNature);
				parser.mapLineNumbers(true);
	            
				assertTrue(parser.parse());
			} 
			catch( Throwable e )
			{
				result = e;				
			}
		}
	}
}

Back to the top