/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.CompPad;
/**
*
* @author trule
*/
import com.CompPad.model.ComplexAmount;
import com.CompPad.model.Expression;
import com.CompPad.model.GroovyEngine;
import com.CompPad.model.Listener;
import com.CompPad.model.Operators;
import com.CompPad.model.Parser;
import java.io.IOException;
import java.util.logging.LogRecord;
import junit.framework.TestCase;
import com.CompPad.shell.Shell;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
* Not exactly a unit test, but testing basic integrated functionality of
* computational model.
* @author trule
*/
public class ParserTest extends TestCase{
public ParserTest(String testName) throws Exception {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
}
public void testTest() throws Exception{
System.out.println("testComplex");
// Parser must refer to a GroovyEngine
GroovyEngine gEngine = new GroovyEngine();
Parser.setGroovyEngine(gEngine);
/* get real and imaginary components */
// String s = "round(a + b,c)";
String[] ss = {
"(3)-2", "%pi - 2", /* test parsing of minus sign after parentheses*/
"{sin(1)}","{round(3,2)}",
"round(1,2)+round(3,4)",
"round a,b",
"round a b ", /* This doesn't work because it's not delimited with
* parentheses or comma */
"round (10^2,3)",
"round a b,c",
"round a ,b round c ,d ",
"round {a} b ",
"round ( round ( a, b), (c))",
"x := %pi over 10 (0 dotslow 20)",
"( A . / B ) . / C", /* Test parsing of . */
"matrix {a # b ## c # d }",
"(a,b)",
"Re (a b)",
"a + b cdot c",
"a + b c ",
"a b + c",
"round {{a b} c}",
"round {a b} c d",
"round (a b) round( (c d) e )",
"0.001 %pi","0.0001 %pi",
"100000 %pi",
"plotxy ( x_2 / [m], y_2 / [kN m] )",
"1.1 + 2.3", "-65.7 x", "sin^-1 ax", "A^.v", "A ^.v","A.B",
"\u03BB_1:= 2",
"color red { %lambda_1 := 1 }",
"v_2:=matrix{1##2##3}" /* did not parse correctly without spaces */
};
for (String s:ss){
System.out.println("0: "+s);
LinkedList list1 = com.CompPad.model.Parser.listTokenize(s);
System.out.println("1: "+list1);
Parser.preprocessTokenizedList(list1);
System.out.println("2: "+list1);
/* Why did I use Linkedlist for one, arraylist for the other? */
ArrayList<String> list2 = Parser.tokensToPostfix(list1);
System.out.println("3: "+list2);
System.out.println("4:"+Parser.postfixToGroovyString(list2));
}
}
public void testFunctionDef() throws Exception{
System.out.println("testFunctionDef");
// Parser must refer to a GroovyEngine
GroovyEngine gEngine = new GroovyEngine();
Parser.setGroovyEngine(gEngine);
// define a closure in the groovy shell
gEngine.evaluate("f = {x,y-> x + y}");
String ss[] ={
"z := f(1,2)","z+1",
"f",
/* treat function as variable if it is followed by an end of line or a
closing parentheses or an operator. That way it can be
assigned, or used as an argument in a function. */
"g(x,y):= x + y"
/* Should parse to groovystring:
* g = { x,y -> ops.plus(x,y) }*/
};
for (String s:ss){
System.out.println("0: "+s);
LinkedList list1 = com.CompPad.model.Parser.listTokenize(s);
System.out.println("1: "+list1);
Parser.preprocessTokenizedList(list1);
System.out.println("2: "+list1);
/* Why did I use Linkedlist for one, arraylist for the other? */
ArrayList<String> list2 = Parser.tokensToPostfix(list1);
System.out.println("3: "+list2);
System.out.println("4:"+Parser.postfixToGroovyString(list2));
}
}
private class myHandler extends Handler{
@Override
public void publish(LogRecord record) {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void flush() {
throw new UnsupportedOperationException("Not supported yet.");
}
@Override
public void close() throws SecurityException {
throw new UnsupportedOperationException("Not supported yet.");
}
}
}