KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nfunk > jeptesting > JEPTest


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     /** The parser */
11     JEP myParser;
12     
13     /** Current line position */
14     int lineCount;
15     
16     /**
17      * Creates a new JEPTest instance
18      */

19     public JEPTest(String JavaDoc name) {
20         super(name);
21     }
22     
23     /**
24      * Sets up the parser.
25      */

26     public void setUp() {
27         // Set up the parser
28
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     /**
38      * Runs the test.
39      */

40     public void runTest() {
41         String JavaDoc fileName = "JEPTesterExpressions.txt";
42         testWithFile(fileName);
43         testGetValue();
44         testGetComplexValue();
45     }
46
47     /**
48      *
49      */

50     public static void main(String JavaDoc args[]) {
51         String JavaDoc fileName;
52         
53         // get filename from argument, or use default
54
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         // Create an instance of this class and analyse the file
64
JEPTest jt = new JEPTest("JEPTest");
65         jt.setUp();
66         jt.testWithFile(fileName);
67     }
68     
69     
70     /**
71      * Loads the file specified in fileName. Evaluates the expressions listed
72      * in it and compares the expressions with the results.
73      */

74     public void testWithFile(String JavaDoc fileName) {
75         BufferedReader reader;
76         Complex c1, c2;
77         int compareCount = 0;
78
79         // Load the input file
80
try {
81             reader = new BufferedReader(new FileReader(fileName));
82         } catch (Exception JavaDoc e) {
83             println("File \""+fileName+"\" not found");
84             return;
85         }
86         
87         // reset the line count
88
lineCount = 0;
89         
90         // cycle through the expressions in pairs of two
91
println("Evaluating and comparing expressions...");
92         while (true) {
93             compareCount++;
94
95             // parse the two lines compared
96
c1 = parseNextLine(reader);
97             c2 = parseNextLine(reader);
98
99             if (c1==null || c2==null) break;
100             
101             // TODO: add comparison method that handles all types (Strings...)
102
String JavaDoc 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             // JUnit assertion
121
Assert.assertTrue(errorMsg, c1.equals(c2, 1e-15));
122         }
123         println("Done. " + compareCount*2 + " expressions compared.");
124     }
125     
126     /**
127      * Parses a single line from the reader, and returns the
128      * evaluation of that line.
129      */

130     private Complex parseNextLine(BufferedReader reader) {
131         Complex value;
132         String JavaDoc line, errorStr;
133         
134         // cycle till a valid line is found
135
do {
136             try {
137                 line = reader.readLine();
138                 lineCount++;
139             } catch (Exception JavaDoc e) {
140                 return null;
141             }
142
143             if (line==null) return null;
144
145         } while (line.length()==0 || line.trim().charAt(0)=='#');
146             
147         // parse the expression
148
myParser.parseExpression(line);
149         // did an error occur while parsing?
150
errorStr = myParser.getErrorInfo();
151         if (errorStr != null) {
152             println("Error while parsing line " + lineCount + ": " + errorStr);
153             return null;
154         }
155         
156         // evaluate the expression
157
value = myParser.getComplexValue();
158         // did an error occur while evaluating?
159
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     /**
169      * Test the getValue() method.
170      */

171     public void testGetValue() {
172         // Test whether a normal double value is returned correctly
173
myParser.parseExpression("2.1345");
174         Assert.assertEquals(myParser.getValue(), 2.1345, 0);
175         
176         // Test whether NaN is returned for Somplex numbers
177
myParser.parseExpression("i");
178         Assert.assertTrue(Double.isNaN(myParser.getValue()));
179         
180         // Test whether NaN is returned for String results
181
myParser.parseExpression("\"asdf\"");
182         Assert.assertTrue(Double.isNaN(myParser.getValue()));
183     }
184     
185     /**
186      * Test the getComplexValue() method.
187      */

188     public void testGetComplexValue() {
189         // Test whether a normal double value is returned as a Complex
190
myParser.parseExpression("2.1345");
191         Assert.assertTrue(new Complex(2.1345, 0).equals(
192                             myParser.getComplexValue(), 0));
193         
194         // Test whether (0, 1) is returned for i
195
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         // Test whether NaN is returned for String results
202
myParser.parseExpression("\"asdf\"");
203         Assert.assertTrue(Double.isNaN(myParser.getValue()));
204     }
205
206     /**
207      * Helper function for printing.
208      */

209     private static void print(String JavaDoc str) {
210         System.out.print(str);
211     }
212
213     /**
214      * Helper function for printing lines.
215      */

216     private static void println(String JavaDoc str) {
217         System.out.println(str);
218     }
219 }
220
Popular Tags