/*******************************************************************************
 * Copyright (c) itemis AG (http://www.itemis.eu) and others.
 * 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
 *
 *******************************************************************************/
package org.eclipse.xtext.parser;

import org.eclipse.xtext.parser.impl.PartialParsingUtil;
import org.eclipse.xtext.parsetree.CompositeNode;
import org.eclipse.xtext.testlanguages.ReferenceGrammarStandaloneSetup;
import org.eclipse.xtext.testlanguages.SimpleExpressionsStandaloneSetup;

/**
 * @author Jan Khnlein - Initial contribution and API
 *
 */
public class PartialParsingPerformanceTest extends AbstractPartialParserTest {

	private static final int NUM_ELEMENTS = 100;
	
	public void testExpression() throws Exception {
		with(SimpleExpressionsStandaloneSetup.class);
		String d = ")+d)\r\n";
		String a_b = "(a+(b*\r\n";
		StringBuffer modelBuffer = new StringBuffer(NUM_ELEMENTS * (a_b.length() + d.length()) + 1);
		for(int i=0; i<NUM_ELEMENTS; ++i) {
			modelBuffer.append(a_b);
		}
		modelBuffer.append("c");
		for(int i=0; i<NUM_ELEMENTS; ++i) {
			modelBuffer.append(d);
		}
		String model = modelBuffer.toString();
		CompositeNode rootNode = getRootNode(model);
		IParseResult reparse = PartialParsingUtil.reparse(getParser(), rootNode, model.indexOf('c'), 1, "Hugo");
		assertTrue(reparse.getParseErrors() == null || reparse.getParseErrors().isEmpty());
	}
	
	public void testReference() throws Exception {
		with(ReferenceGrammarStandaloneSetup.class);
		StringBuffer modelBuffer = new StringBuffer();
		modelBuffer.append("spielplatz 17 {\n");
		for(int i=0; i<NUM_ELEMENTS; ++i) {
			modelBuffer.append("  kind ( Herbert");
			modelBuffer.append(i);
			modelBuffer.append(" 11 )\n");
		}
		for(int i=0; i<NUM_ELEMENTS; ++i) {
			modelBuffer.append("  erwachsener ( Hugo");
			modelBuffer.append(i);
			modelBuffer.append(" 111 )\n");
		}
		modelBuffer.append("  erwachsener ( Sven 112 )\n");
		for(int i=0; i<NUM_ELEMENTS; ++i) {
			modelBuffer.append("  spielzeug ( Schaufel ROT )\n");
		}
		modelBuffer.append("}\n");
		String model = modelBuffer.toString();
		CompositeNode rootNode = getRootNode(model);
		IParseResult reparse = PartialParsingUtil.reparse(getParser(), rootNode, model.indexOf("Sven"), 4, "Peter");
		if(reparse.getParseErrors() != null && !reparse.getParseErrors().isEmpty()) {
			fail("Unexpected parse error " + reparse.getParseErrors().get(0).getMessage()) ;
		}
	}
	
}

@@ -2681,10 +2681,1011 @@
 %09%7D%0A%09%7D%0A%09%0A
+%09public void testReferenceWithErrorAtEnd() throws Exception %7B%0A%09%09with(ReferenceGrammarStandaloneSetup.class);%0A%09%09StringBuffer modelBuffer = new StringBuffer();%0A%09%09modelBuffer.append(%22spielplatz 17 %7B%5Cn%22);%0A%09%09for(int i=0; i%3CNUM_ELEMENTS; ++i) %7B%0A%09%09%09modelBuffer.append(%22  kind ( Herbert%22);%0A%09%09%09modelBuffer.append(i);%0A%09%09%09modelBuffer.append(%22 11 )%5Cn%22);%0A%09%09%7D%0A%09%09for(int i=0; i%3CNUM_ELEMENTS; ++i) %7B%0A%09%09%09modelBuffer.append(%22  erwachsener ( Hugo%22);%0A%09%09%09modelBuffer.append(i);%0A%09%09%09modelBuffer.append(%22 111 )%5Cn%22);%0A%09%09%7D%0A%09%09modelBuffer.append(%22  erwachsener ( Sven 112 )%5Cn%22);%0A%09%09for(int i=0; i%3CNUM_ELEMENTS; ++i) %7B%0A%09%09%09modelBuffer.append(%22  spielzeug ( Schaufel ROT )%5Cn%22);%0A%09%09%7D%0A%09%09modelBuffer.append(%22 kind (Herbert %22 + NUM_ELEMENTS + 1 + %22%5Cn%22);%0A%09%09modelBuffer.append(%22%7D%5Cn%22);%0A%09%09String model = modelBuffer.toString();%0A%09%09CompositeNode rootNode = getRootNode(model);%0A%09%09IParseResult reparse = PartialParsingUtil.reparse(getParser(), rootNode, model.indexOf(%22Sven%22), 4, %22Peter%22);%0A%09%09assertEquals(1, reparse.getParseErrors().size());%0A%09%7D%0A%09%0A
 %7D%0A

patch applied https://bugs.eclipse.org/bugs/show_bug.cgi?id=252967