1 9 10 package org.nfunk.jepexamples; 11 12 import java.io.*; 13 14 import org.nfunk.jep.JEP; 15 import org.nfunk.jep.type.Complex; 16 17 27 public class JEPTester { 28 29 30 JEP myParser; 31 32 33 int lineCount; 34 35 38 public JEPTester() { 39 myParser = new JEP(); 41 myParser.setImplicitMul(true); 42 myParser.addStandardFunctions(); 43 myParser.addStandardConstants(); 44 myParser.addComplex(); 45 myParser.setTraverse(false); 46 lineCount = 0; 47 } 48 49 53 public static void main(String args[]) { 54 String fileName; 55 56 if (args!=null && args.length>0) { 58 fileName = args[0]; 59 } else { 60 fileName = "JEPTesterExpressions.txt"; 61 println("Using default input file: " + fileName); 62 println("Start with \"java org.nfunk.jepexamples."+ 63 "JEPTester <filename>\" to load a different input file."); 64 } 65 66 JEPTester jt = new JEPTester(); 68 jt.run(fileName); 69 } 70 71 75 public void run(String fileName) { 76 BufferedReader reader; 77 Complex c1, c2; 78 boolean hasError = false; 79 80 try { 82 reader = new BufferedReader(new FileReader(fileName)); 83 } catch (Exception e) { 84 println("File \""+fileName+"\" not found"); 85 return; 86 } 87 88 lineCount = 0; 90 91 println("Evaluating and comparing expressions..."); 93 while (true) { 94 c1 = parseNextLine(reader); 96 c2 = parseNextLine(reader); 97 98 if (c1==null || c2==null) break; 99 100 if (!c1.equals(c2,1e-15)) { 102 hasError = true; 103 print("Line: " + lineCount + ": "); 104 if (c1.im() == 0) 105 print("" + c1.re() + " != "); 106 else 107 print("" + c1 + " != "); 108 109 if (c2.im() == 0) 110 println("" + c2.re()); 111 else 112 println("" + c2); 113 } 114 } 115 116 if (!hasError) { 117 print("\n" + lineCount + " lines processed. No errors were found.\n\n"); 118 } 119 } 120 121 125 private Complex parseNextLine(BufferedReader reader) { 126 Complex value; 127 String line, errorStr; 128 129 do { 131 try { 132 line = reader.readLine(); 133 lineCount++; 134 } catch (Exception e) { 135 return null; 136 } 137 138 if (line==null) return null; 139 140 } while (line.length()==0 || line.trim().charAt(0)=='#'); 141 142 myParser.parseExpression(line); 144 errorStr = myParser.getErrorInfo(); 146 if (errorStr != null) { 147 println("Error while parsing line " + lineCount + ": " + errorStr); 148 return null; 149 } 150 151 value = myParser.getComplexValue(); 153 errorStr = myParser.getErrorInfo(); 155 if ((value == null) || (errorStr != null)) { 156 println("Error while evaluating line " + lineCount + ": " + errorStr); 157 return null; 158 } 159 160 return value; 161 } 162 163 166 private static void print(String str) { 167 System.out.print(str); 168 } 169 170 173 private static void println(String str) { 174 System.out.println(str); 175 } 176 } 177 | Popular Tags |