1 package org.nfunk.jeptesting; 2 3 import java.io.*; 4 import org.nfunk.jep.*; 5 import org.nfunk.jep.type.*; 6 import junit.framework.*; 7 8 public class JEPTest extends TestCase { 9 10 11 JEP myParser; 12 13 14 int lineCount; 15 16 19 public JEPTest(String name) { 20 super(name); 21 } 22 23 26 public void setUp() { 27 myParser = new JEP(); 29 myParser.setImplicitMul(true); 30 myParser.addStandardFunctions(); 31 myParser.addStandardConstants(); 32 myParser.addComplex(); 33 myParser.setTraverse(false); 34 lineCount = 0; 35 } 36 37 40 public void runTest() { 41 String fileName = "JEPTesterExpressions.txt"; 42 testWithFile(fileName); 43 testGetValue(); 44 testGetComplexValue(); 45 } 46 47 50 public static void main(String args[]) { 51 String fileName; 52 53 if (args!=null && args.length>0) { 55 fileName = args[0]; 56 } else { 57 fileName = "JEPTesterExpressions.txt"; 58 println("Using default input file: " + fileName); 59 println("Start with \"java org.nfunk.jepexamples."+ 60 "JEPTester <filename>\" to load a different input file."); 61 } 62 63 JEPTest jt = new JEPTest("JEPTest"); 65 jt.setUp(); 66 jt.testWithFile(fileName); 67 } 68 69 70 74 public void testWithFile(String fileName) { 75 BufferedReader reader; 76 Complex c1, c2; 77 int compareCount = 0; 78 79 try { 81 reader = new BufferedReader(new FileReader(fileName)); 82 } catch (Exception e) { 83 println("File \""+fileName+"\" not found"); 84 return; 85 } 86 87 lineCount = 0; 89 90 println("Evaluating and comparing expressions..."); 92 while (true) { 93 compareCount++; 94 95 c1 = parseNextLine(reader); 97 c2 = parseNextLine(reader); 98 99 if (c1==null || c2==null) break; 100 101 String errorMsg = ""; 103 104 if (!c1.equals(c2,1e-15)) { 105 errorMsg += "Line: " + lineCount + ": "; 106 if (c1.im() == 0) 107 errorMsg += c1.re(); 108 else 109 errorMsg += c1; 110 111 errorMsg += " != "; 112 113 if (c2.im() == 0) 114 errorMsg += c2.re(); 115 else 116 errorMsg += c2; 117 118 println(errorMsg); 119 } 120 Assert.assertTrue(errorMsg, c1.equals(c2, 1e-15)); 122 } 123 println("Done. " + compareCount*2 + " expressions compared."); 124 } 125 126 130 private Complex parseNextLine(BufferedReader reader) { 131 Complex value; 132 String line, errorStr; 133 134 do { 136 try { 137 line = reader.readLine(); 138 lineCount++; 139 } catch (Exception e) { 140 return null; 141 } 142 143 if (line==null) return null; 144 145 } while (line.length()==0 || line.trim().charAt(0)=='#'); 146 147 myParser.parseExpression(line); 149 errorStr = myParser.getErrorInfo(); 151 if (errorStr != null) { 152 println("Error while parsing line " + lineCount + ": " + errorStr); 153 return null; 154 } 155 156 value = myParser.getComplexValue(); 158 errorStr = myParser.getErrorInfo(); 160 if ((value == null) || (errorStr != null)) { 161 println("Error while evaluating line " + lineCount + ": " + errorStr); 162 return null; 163 } 164 165 return value; 166 } 167 168 171 public void testGetValue() { 172 myParser.parseExpression("2.1345"); 174 Assert.assertEquals(myParser.getValue(), 2.1345, 0); 175 176 myParser.parseExpression("i"); 178 Assert.assertTrue(Double.isNaN(myParser.getValue())); 179 180 myParser.parseExpression("\"asdf\""); 182 Assert.assertTrue(Double.isNaN(myParser.getValue())); 183 } 184 185 188 public void testGetComplexValue() { 189 myParser.parseExpression("2.1345"); 191 Assert.assertTrue(new Complex(2.1345, 0).equals( 192 myParser.getComplexValue(), 0)); 193 194 myParser.parseExpression("i"); 196 Complex z = myParser.getComplexValue(); 197 Assert.assertTrue(z != null); 198 Assert.assertTrue(z.re() == 0); 199 Assert.assertTrue(z.im() == 1); 200 201 myParser.parseExpression("\"asdf\""); 203 Assert.assertTrue(Double.isNaN(myParser.getValue())); 204 } 205 206 209 private static void print(String str) { 210 System.out.print(str); 211 } 212 213 216 private static void println(String str) { 217 System.out.println(str); 218 } 219 } 220 | Popular Tags |