-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTester.java
228 lines (205 loc) · 7.38 KB
/
Tester.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import java.util.HashMap;
import java.util.Scanner;
import java.awt.event.KeyEvent;
public class Tester {
public static String generateFuzzTestString(int length) {
char[] chars = {
'x', '1', '+', '-', '*', '/', '(', ')', '^'
};
String str = "";
for (int i=0; i<length; i++) {
int pos = (int) (chars.length * Math.random());
str += chars[pos];
}
return str;
}
public static void fuzzTestParser(String[] args) {
if (args.length < 2) {
System.out.println("ERROR: expecting 2 arguments");
return;
}
int num = Integer.parseInt(args[0]);
int len = Integer.parseInt(args[1]);
for (int i=0; i<num; i++) {
String s = "";
try {
s = generateFuzzTestString(len);
System.out.println(s);
Parser P = new Parser(s);
System.out.println(P.root.toString());
}
catch(Exception e) {
e.printStackTrace();
}
}
}
public static void testParser(String[] args) {
Parser P = new Parser(args[0]);
System.out.println(P.root.toString());
}
public static void testTokenizer(String[] args) {
for (int i=0; i<args.length; i++) {
Tokenizer T = new Tokenizer(args[i]);
while (true) {
Tok t = T.nextToken();
System.out.print(t);
if (t == Tok.VARIABLE || t ==Tok.FUNCTION) System.out.print(", " + T.strVal);
if (t == Tok.NUMBER) System.out.print(", " + T.numVal);
if (t == Tok.EOS) {
System.out.println();
break;
}
System.out.println();
}
}
}
public static void testToString(String[] args) {
Parser P = new Parser(args[0]);
Node tree = P.root;
System.out.println(tree.toString());
}
public static void testPrint(String[] args) {
Parser P = new Parser(args[0]);
Node tree = P.root;
tree.print();
}
public static void testPderiv(String[] args) {
}
public static void testCodeGen(String[] args) {
Parser P = new Parser(args[0]);
System.out.println();
P.root.print();
System.out.println();
Compiler.compileFunction(P);
try {
Class c = Class.forName(Compiler.getClassName(P));
Function tmpfunc = (Function) c.newInstance();
if (args.length == 3) {
System.out.println("func.value(" + args[1] + ", " + args[2] + ") = " +
tmpfunc.value(Double.parseDouble(args[1]),
Double.parseDouble(args[2])));
}
else {
System.out.println("func.value(" + args[1] + ") = " + tmpfunc.value(Double.parseDouble(args[1])));
}
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e3) {
e3.printStackTrace();
} catch (InstantiationException e4) {
e4.printStackTrace();
} catch (SecurityException e5) {
e5.printStackTrace();
} catch (Exception e6) {
e6.printStackTrace();
}
}
public static void testEquals(String[] args) {
Parser P0 = new Parser(args[0]);
Parser P1 = new Parser(args[1]);
P0.root.print();
System.out.println();
P1.root.print();
System.out.println();
System.out.println(P0.root.equals(P1.root));
System.out.println(P1.root.equals(P0.root));
}
public static void testHashCode(String[] args) {
Parser P0 = new Parser(args[0]);
Parser P1 = new Parser(args[1]);
System.out.println(P0.root.toString());
System.out.println(P1.root.toString());
System.out.println();
System.out.println(P0.root.toString().hashCode());
System.out.println(P1.root.toString().hashCode());
}
public static void testIsContinuous(String[] args) {
Parser P = new Parser(args[0]);
NonCompiledFunction func = new NonCompiledFunction(P.root, P.argList);
func.print();
Double val = Double.parseDouble(args[1]);
try {
System.out.println("isContinuous(" + val +
") = " + func.isContinuous(val)
);
}
catch (Exception e) { e.printStackTrace(); }
}
public static void testNonCompiledFunction(String[] args) {
Parser P = new Parser(args[0]);
NonCompiledFunction func = new NonCompiledFunction(P.root, P.argList);
System.out.println();
func.print();
System.out.println();
System.out.println("Bi-Variate: " + func.isBivariate());
System.out.println();
if (func.isBivariate()) {
try {
double x = Double.parseDouble(args[1]);
double y = Double.parseDouble(args[2]);
System.out.println("f(" + x + "," + y + ") = " +
func.value(x, y)
);
}
catch (Exception e) { e.printStackTrace(); }
}
else {
try {
System.out.println("f(" + args[1] + ") = " + func.value(Double.parseDouble(args[1])));
}
catch (Exception e) { e.printStackTrace(); }
}
System.out.println();
}
public static void testNewton(String[] args) {
Parser P = new Parser(args[0]);
Node func = P.root;
try {
Node deriv = P.root.pderiv("x");
HashMap<String, Double> argList = P.argList;
double x0 = Double.parseDouble(args[1]);
double threshold = Double.parseDouble(args[2]);
System.out.println("newton(func, deriv, "+x0+", "+threshold+") = "+
NonCompiledFunction.newton(func, deriv, "x", argList, x0, threshold));
}
catch (Exception e) {
e.printStackTrace();
}
}
public void testGraphCanvas(String[] args) {
GraphCanvas g = new GraphCanvas();
Scanner sc = new Scanner(System.in);
while (sc.hasNextLine()) {
System.out.println("> ");
String text = sc.nextLine();
g.inputbar.setText(text);
g.G.keyPressed( new KeyEvent(g,
KeyEvent.KEY_PRESSED,
System.currentTimeMillis(),
0,
KeyEvent.VK_ENTER,
'\n' ));
try {
Thread.sleep(500);
}
catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) {
//testTokenizer(args);
//testOpCompareTo(args);
//testToString(args);
//testPrint(args);
//testDeriv(args);
//testNewton(args);
testCodeGen(args);
//testEquals(args);
//testHashCode(args);
//testIsContinuous(args);
//testNonCompiledFunction(args);
//testNewton(args);
//testGraphCanvas(args);
}
}