1578 |
08 Nov 05 |
nicklas |
1 |
/* |
1578 |
08 Nov 05 |
nicklas |
$Id$ |
1578 |
08 Nov 05 |
nicklas |
3 |
|
3675 |
16 Aug 07 |
jari |
Copyright (C) 2005 Nicklas Nordborg |
4889 |
06 Apr 09 |
nicklas |
Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg |
3675 |
16 Aug 07 |
jari |
Copyright (C) 2007 Nicklas Nordborg |
1578 |
08 Nov 05 |
nicklas |
7 |
|
2304 |
22 May 06 |
jari |
This file is part of BASE - BioArray Software Environment. |
2304 |
22 May 06 |
jari |
Available at http://base.thep.lu.se/ |
1578 |
08 Nov 05 |
nicklas |
10 |
|
1578 |
08 Nov 05 |
nicklas |
BASE is free software; you can redistribute it and/or |
1578 |
08 Nov 05 |
nicklas |
modify it under the terms of the GNU General Public License |
4480 |
05 Sep 08 |
jari |
as published by the Free Software Foundation; either version 3 |
1578 |
08 Nov 05 |
nicklas |
of the License, or (at your option) any later version. |
1578 |
08 Nov 05 |
nicklas |
15 |
|
1578 |
08 Nov 05 |
nicklas |
BASE is distributed in the hope that it will be useful, |
1578 |
08 Nov 05 |
nicklas |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
1578 |
08 Nov 05 |
nicklas |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
1578 |
08 Nov 05 |
nicklas |
GNU General Public License for more details. |
1578 |
08 Nov 05 |
nicklas |
20 |
|
1578 |
08 Nov 05 |
nicklas |
You should have received a copy of the GNU General Public License |
4514 |
11 Sep 08 |
jari |
along with BASE. If not, see <http://www.gnu.org/licenses/>. |
1578 |
08 Nov 05 |
nicklas |
23 |
*/ |
5319 |
20 Apr 10 |
nicklas |
24 |
import net.sf.basedb.core.BaseException; |
1581 |
09 Nov 05 |
nicklas |
25 |
import net.sf.basedb.core.RawDataTypes; |
1581 |
09 Nov 05 |
nicklas |
26 |
import net.sf.basedb.util.jep.Jep; |
1581 |
09 Nov 05 |
nicklas |
27 |
import net.sf.basedb.util.jep.RawFunction; |
1578 |
08 Nov 05 |
nicklas |
28 |
import net.sf.basedb.core.query.Expression; |
1581 |
09 Nov 05 |
nicklas |
29 |
import net.sf.basedb.core.query.Restriction; |
1578 |
08 Nov 05 |
nicklas |
30 |
|
1578 |
08 Nov 05 |
nicklas |
31 |
public class TestJep |
1578 |
08 Nov 05 |
nicklas |
32 |
{ |
1578 |
08 Nov 05 |
nicklas |
33 |
|
1578 |
08 Nov 05 |
nicklas |
34 |
static boolean ok = true; |
1578 |
08 Nov 05 |
nicklas |
35 |
public static void main(String[] args) |
1578 |
08 Nov 05 |
nicklas |
36 |
{ |
1578 |
08 Nov 05 |
nicklas |
37 |
TestUtil.checkArgs(args); |
1578 |
08 Nov 05 |
nicklas |
38 |
TestUtil.begin(); |
1578 |
08 Nov 05 |
nicklas |
39 |
ok = test_all(); |
1578 |
08 Nov 05 |
nicklas |
40 |
TestUtil.stop(); |
1578 |
08 Nov 05 |
nicklas |
41 |
} |
1578 |
08 Nov 05 |
nicklas |
42 |
|
1578 |
08 Nov 05 |
nicklas |
43 |
static boolean test_all() |
1578 |
08 Nov 05 |
nicklas |
44 |
{ |
1578 |
08 Nov 05 |
nicklas |
45 |
write("++Testing Java Math Expression Parser"); |
1578 |
08 Nov 05 |
nicklas |
46 |
|
1581 |
09 Nov 05 |
nicklas |
47 |
String expression1 = "raw(ch1FgMean) - raw(ch1BgMean)"; |
1581 |
09 Nov 05 |
nicklas |
48 |
String expression2 = "ln((raw(ch1FgMean) - raw(ch1BgMean)) / (raw(ch2FgMean) - raw(ch2BgMean)))"; |
3656 |
13 Aug 07 |
nicklas |
49 |
String bigExpression = "1+sqrt(2)-exp(3)*abs(-4)/log(5)+ln(5.99)+x+if(a<b, 11, -11)"; |
1581 |
09 Nov 05 |
nicklas |
50 |
String restriction1 = expression1 + " > 0"; |
1581 |
09 Nov 05 |
nicklas |
51 |
String restriction2 = expression2 + " > 2"; |
1581 |
09 Nov 05 |
nicklas |
52 |
String restriction3 = restriction1 + " && " + restriction2; |
1581 |
09 Nov 05 |
nicklas |
53 |
String restriction4 = "raw(name) == 'Nicklas'"; |
3604 |
26 Jul 07 |
nicklas |
54 |
String bigRestriction = "1 == 1 && 2 > 1 || 2 >= 1 && 3 < 7.0 && 3 <= 9 && !(5 != 5)"; |
5319 |
20 Apr 10 |
nicklas |
55 |
test_parse("x+2", false); |
5319 |
20 Apr 10 |
nicklas |
56 |
test_parse("2+x", false); |
5319 |
20 Apr 10 |
nicklas |
57 |
test_parse("x(2)", true); |
5319 |
20 Apr 10 |
nicklas |
58 |
test_parse("x(2)+y()", true); |
5319 |
20 Apr 10 |
nicklas |
59 |
|
5319 |
20 Apr 10 |
nicklas |
60 |
test_parse(expression1, false); |
5319 |
20 Apr 10 |
nicklas |
61 |
test_parse(expression2, false); |
5319 |
20 Apr 10 |
nicklas |
62 |
test_parse(bigExpression, false); |
5319 |
20 Apr 10 |
nicklas |
63 |
test_parse(restriction1, false); |
5319 |
20 Apr 10 |
nicklas |
64 |
test_parse(restriction2, false); |
5319 |
20 Apr 10 |
nicklas |
65 |
test_parse(restriction3, false); |
5319 |
20 Apr 10 |
nicklas |
66 |
test_parse(restriction4, false); |
5319 |
20 Apr 10 |
nicklas |
67 |
test_parse(bigRestriction, false); |
1581 |
09 Nov 05 |
nicklas |
68 |
test_expression(expression1); |
1581 |
09 Nov 05 |
nicklas |
69 |
test_expression(expression2); |
3604 |
26 Jul 07 |
nicklas |
70 |
test_expression(bigExpression); |
1581 |
09 Nov 05 |
nicklas |
71 |
test_restriction(restriction1); |
1581 |
09 Nov 05 |
nicklas |
72 |
test_restriction(restriction2); |
1581 |
09 Nov 05 |
nicklas |
73 |
test_restriction(restriction3); |
1581 |
09 Nov 05 |
nicklas |
74 |
test_restriction(restriction4); |
3604 |
26 Jul 07 |
nicklas |
75 |
test_restriction(bigRestriction); |
1581 |
09 Nov 05 |
nicklas |
76 |
|
1578 |
08 Nov 05 |
nicklas |
77 |
write("++Testing Java Math Expression Parser "+(ok ? "OK" : "Failed")+"\n"); |
1578 |
08 Nov 05 |
nicklas |
78 |
return ok; |
1578 |
08 Nov 05 |
nicklas |
79 |
} |
1578 |
08 Nov 05 |
nicklas |
80 |
|
5319 |
20 Apr 10 |
nicklas |
81 |
static void test_parse(String formula, boolean shouldFail) |
1578 |
08 Nov 05 |
nicklas |
82 |
{ |
1578 |
08 Nov 05 |
nicklas |
83 |
try |
1578 |
08 Nov 05 |
nicklas |
84 |
{ |
5319 |
20 Apr 10 |
nicklas |
85 |
Throwable t = null; |
5319 |
20 Apr 10 |
nicklas |
86 |
try |
5319 |
20 Apr 10 |
nicklas |
87 |
{ |
5319 |
20 Apr 10 |
nicklas |
88 |
Jep.newJep(formula, new RawFunction(null, RawDataTypes.getSafeRawDataType("genepix"), true)); |
5319 |
20 Apr 10 |
nicklas |
89 |
} |
5319 |
20 Apr 10 |
nicklas |
90 |
catch (Throwable t1) |
5319 |
20 Apr 10 |
nicklas |
91 |
{ |
5319 |
20 Apr 10 |
nicklas |
92 |
t = t1; |
5319 |
20 Apr 10 |
nicklas |
93 |
} |
5319 |
20 Apr 10 |
nicklas |
94 |
if (shouldFail) |
5319 |
20 Apr 10 |
nicklas |
95 |
{ |
5319 |
20 Apr 10 |
nicklas |
96 |
if (t == null) throw new BaseException("Expected parse failure"); |
5319 |
20 Apr 10 |
nicklas |
97 |
write("--Parse formula: "+formula+" OK [" + t.getMessage() + "]"); |
5319 |
20 Apr 10 |
nicklas |
98 |
} |
5319 |
20 Apr 10 |
nicklas |
99 |
else |
5319 |
20 Apr 10 |
nicklas |
100 |
{ |
5319 |
20 Apr 10 |
nicklas |
101 |
if (t != null) throw t; |
5319 |
20 Apr 10 |
nicklas |
102 |
write("--Parse formula: "+formula+" OK"); |
5319 |
20 Apr 10 |
nicklas |
103 |
} |
1578 |
08 Nov 05 |
nicklas |
104 |
} |
1578 |
08 Nov 05 |
nicklas |
105 |
catch (Throwable ex) |
1578 |
08 Nov 05 |
nicklas |
106 |
{ |
1578 |
08 Nov 05 |
nicklas |
107 |
write("--Parse formula: "+formula+" FAILED"); |
1578 |
08 Nov 05 |
nicklas |
108 |
ex.printStackTrace(); |
1578 |
08 Nov 05 |
nicklas |
109 |
ok = false; |
1578 |
08 Nov 05 |
nicklas |
110 |
} |
1578 |
08 Nov 05 |
nicklas |
111 |
} |
1578 |
08 Nov 05 |
nicklas |
112 |
|
1578 |
08 Nov 05 |
nicklas |
113 |
static void test_expression(String formula) |
1578 |
08 Nov 05 |
nicklas |
114 |
{ |
1578 |
08 Nov 05 |
nicklas |
115 |
try |
1578 |
08 Nov 05 |
nicklas |
116 |
{ |
4124 |
04 Feb 08 |
nicklas |
117 |
Expression e = Jep.formulaToExpression(formula, new RawFunction(null, RawDataTypes.getSafeRawDataType("genepix"), true)); |
5320 |
21 Apr 10 |
nicklas |
118 |
Expression e2 = Jep.formulaToExpression(formula, new RawFunction(null, RawDataTypes.getSafeRawDataType("genepix"), true)); |
5320 |
21 Apr 10 |
nicklas |
119 |
if (!e.equals(e2)) throw new RuntimeException("Identical formulas doens't yield equal expressions"); |
5320 |
21 Apr 10 |
nicklas |
120 |
if (e.hashCode() != e2.hashCode()) throw new RuntimeException("Identical formulas doens't yield equal hash codes"); |
1578 |
08 Nov 05 |
nicklas |
121 |
write(e); |
1578 |
08 Nov 05 |
nicklas |
122 |
write("--Jep to Expression: "+formula+" OK"); |
1578 |
08 Nov 05 |
nicklas |
123 |
} |
1578 |
08 Nov 05 |
nicklas |
124 |
catch (Throwable ex) |
1578 |
08 Nov 05 |
nicklas |
125 |
{ |
1578 |
08 Nov 05 |
nicklas |
126 |
write("--Jep to Expression: "+formula+" FAILED"); |
1578 |
08 Nov 05 |
nicklas |
127 |
ex.printStackTrace(); |
1578 |
08 Nov 05 |
nicklas |
128 |
ok = false; |
1581 |
09 Nov 05 |
nicklas |
129 |
} |
1581 |
09 Nov 05 |
nicklas |
130 |
} |
1578 |
08 Nov 05 |
nicklas |
131 |
|
1581 |
09 Nov 05 |
nicklas |
132 |
static void test_restriction(String formula) |
1581 |
09 Nov 05 |
nicklas |
133 |
{ |
1581 |
09 Nov 05 |
nicklas |
134 |
try |
1581 |
09 Nov 05 |
nicklas |
135 |
{ |
2993 |
01 Dec 06 |
nicklas |
136 |
Restriction r = Jep.formulaToRestriction(formula, |
4124 |
04 Feb 08 |
nicklas |
137 |
new RawFunction(null, RawDataTypes.getSafeRawDataType("genepix"), true)); |
5320 |
21 Apr 10 |
nicklas |
138 |
Restriction r2 = Jep.formulaToRestriction(formula, |
5320 |
21 Apr 10 |
nicklas |
139 |
new RawFunction(null, RawDataTypes.getSafeRawDataType("genepix"), true)); |
5320 |
21 Apr 10 |
nicklas |
140 |
if (!r.equals(r2)) throw new RuntimeException("Identical formulas doens't yield equal restrictions"); |
5320 |
21 Apr 10 |
nicklas |
141 |
if (r.hashCode() != r2.hashCode()) throw new RuntimeException("Identical formulas doens't yield equal hash codes"); |
1581 |
09 Nov 05 |
nicklas |
142 |
write(r); |
1581 |
09 Nov 05 |
nicklas |
143 |
write("--Jep to Restriction: "+formula+" OK"); |
1578 |
08 Nov 05 |
nicklas |
144 |
} |
1581 |
09 Nov 05 |
nicklas |
145 |
catch (Throwable ex) |
1581 |
09 Nov 05 |
nicklas |
146 |
{ |
1581 |
09 Nov 05 |
nicklas |
147 |
write("--Jep to Restriction: "+formula+" FAILED"); |
1581 |
09 Nov 05 |
nicklas |
148 |
ex.printStackTrace(); |
1581 |
09 Nov 05 |
nicklas |
149 |
ok = false; |
1581 |
09 Nov 05 |
nicklas |
150 |
} |
1578 |
08 Nov 05 |
nicklas |
151 |
} |
1578 |
08 Nov 05 |
nicklas |
152 |
|
1578 |
08 Nov 05 |
nicklas |
153 |
static void write(String message) |
1578 |
08 Nov 05 |
nicklas |
154 |
{ |
1578 |
08 Nov 05 |
nicklas |
155 |
System.out.println(message); |
1578 |
08 Nov 05 |
nicklas |
156 |
} |
1578 |
08 Nov 05 |
nicklas |
157 |
|
1578 |
08 Nov 05 |
nicklas |
158 |
static void write(Expression e) |
1578 |
08 Nov 05 |
nicklas |
159 |
{ |
1578 |
08 Nov 05 |
nicklas |
160 |
if (!TestUtil.getSilent()) |
1578 |
08 Nov 05 |
nicklas |
161 |
{ |
5320 |
21 Apr 10 |
nicklas |
162 |
System.out.println("--Expression [" + e.hashCode() + "]: " + e.toString()); |
1578 |
08 Nov 05 |
nicklas |
163 |
} |
1578 |
08 Nov 05 |
nicklas |
164 |
} |
1578 |
08 Nov 05 |
nicklas |
165 |
|
1581 |
09 Nov 05 |
nicklas |
166 |
static void write(Restriction r) |
1581 |
09 Nov 05 |
nicklas |
167 |
{ |
1581 |
09 Nov 05 |
nicklas |
168 |
if (!TestUtil.getSilent()) |
1581 |
09 Nov 05 |
nicklas |
169 |
{ |
5320 |
21 Apr 10 |
nicklas |
170 |
System.out.println("--Restriction [" + r.hashCode() + "]: " + r.toString()); |
1581 |
09 Nov 05 |
nicklas |
171 |
} |
1581 |
09 Nov 05 |
nicklas |
172 |
} |
1581 |
09 Nov 05 |
nicklas |
173 |
|
1578 |
08 Nov 05 |
nicklas |
174 |
} |
1578 |
08 Nov 05 |
nicklas |
175 |
|