Skip to content

Commit 24793d4

Browse files
More tests
1 parent f11139b commit 24793d4

3 files changed

Lines changed: 231 additions & 5 deletions

File tree

src/test/java/net/marcellperger/mathexpr/CommonData.java

Lines changed: 187 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,185 @@ private static MathSymbol getBigData2_obj() {
162162
);
163163
}
164164

165+
166+
/**
167+
* Generated using the following Python script:
168+
* <pre>{@code
169+
* import random
170+
*
171+
* OPTIONS = [s + 'Operation' for s in ('Add', 'Sub', 'Mul', 'Div', 'Pow')]
172+
*
173+
*
174+
* def happens(prob: float):
175+
* return random.random() < prob
176+
*
177+
*
178+
* def gen_random_double_sym(curr_depth: int, is_arg1: bool):
179+
* indent = ' ' * curr_depth * 4
180+
* return (f'{indent}new BasicDoubleSymbol('
181+
* f'{random.randint(-999, 999) / 10:.1f})'
182+
* + (',' if is_arg1 else ''))
183+
*
184+
*
185+
* def get_leaf_prob(min_depth: int, max_depth: int, depth: int):
186+
* if depth >= max_depth:
187+
* return 1
188+
* if depth < min_depth:
189+
* return 0
190+
* w = max_depth - min_depth
191+
* o = depth - min_depth
192+
* return o / w
193+
*
194+
*
195+
* def gen_random(min_depth: int, max_depth: int,
196+
* curr_depth: int = 0, is_arg1=False) -> str:
197+
* indent = ' ' * curr_depth * 4
198+
* if happens(get_leaf_prob(min_depth, max_depth, curr_depth)):
199+
* return gen_random_double_sym(curr_depth, is_arg1)
200+
* name = random.choice(OPTIONS)
201+
* arg1 = gen_random(min_depth, max_depth, curr_depth + 1, True)
202+
* arg2 = gen_random(min_depth, max_depth, curr_depth + 1, False)
203+
* result = (f'{indent}new {name}(\n'
204+
* f'{arg1}\n'
205+
* f'{arg2}\n'
206+
* f'{indent})' + (',' if is_arg1 else ''))
207+
* return result
208+
*
209+
*
210+
* if __name__ == '__main__':
211+
* print(gen_random(2, 8))
212+
* }</pre>
213+
*/
214+
public static MathSymbol getBigData3Pow_obj() {
215+
return new PowOperation(
216+
new SubOperation(
217+
new DivOperation(
218+
new MulOperation(
219+
new MulOperation(
220+
new SubOperation(
221+
new BasicDoubleSymbol(-19.7),
222+
new BasicDoubleSymbol(17.1)
223+
),
224+
new MulOperation(
225+
new BasicDoubleSymbol(-99.6),
226+
new BasicDoubleSymbol(-49.2)
227+
)
228+
),
229+
new SubOperation(
230+
new PowOperation(
231+
new BasicDoubleSymbol(43.6),
232+
new AddOperation(
233+
new BasicDoubleSymbol(-29.1),
234+
new BasicDoubleSymbol(-68.3)
235+
)
236+
),
237+
new DivOperation(
238+
new SubOperation(
239+
new BasicDoubleSymbol(40.6),
240+
new BasicDoubleSymbol(58.4)
241+
),
242+
new MulOperation(
243+
new AddOperation(
244+
new BasicDoubleSymbol(86.8),
245+
new BasicDoubleSymbol(71.8)
246+
),
247+
new BasicDoubleSymbol(51.8)
248+
)
249+
)
250+
)
251+
),
252+
new MulOperation(
253+
new BasicDoubleSymbol(72.4),
254+
new SubOperation(
255+
new BasicDoubleSymbol(-91.1),
256+
new BasicDoubleSymbol(-3.2)
257+
)
258+
)
259+
),
260+
new AddOperation(
261+
new DivOperation(
262+
new PowOperation(
263+
new DivOperation(
264+
new DivOperation(
265+
new PowOperation(
266+
new BasicDoubleSymbol(-27.1),
267+
new BasicDoubleSymbol(-28.2)
268+
),
269+
new BasicDoubleSymbol(-99.8)
270+
),
271+
new BasicDoubleSymbol(-54.3)
272+
),
273+
new BasicDoubleSymbol(-50.2)
274+
),
275+
new AddOperation(
276+
new AddOperation(
277+
new SubOperation(
278+
new BasicDoubleSymbol(-69.8),
279+
new BasicDoubleSymbol(64.5)
280+
),
281+
new PowOperation(
282+
new BasicDoubleSymbol(63.9),
283+
new BasicDoubleSymbol(-92.9)
284+
)
285+
),
286+
new SubOperation(
287+
new BasicDoubleSymbol(-24.9),
288+
new AddOperation(
289+
new BasicDoubleSymbol(58.1),
290+
new BasicDoubleSymbol(13.3)
291+
)
292+
)
293+
)
294+
),
295+
new AddOperation(
296+
new BasicDoubleSymbol(78.0),
297+
new BasicDoubleSymbol(-34.5)
298+
)
299+
)
300+
),
301+
new PowOperation(
302+
new AddOperation(
303+
new SubOperation(
304+
new BasicDoubleSymbol(12.5),
305+
new BasicDoubleSymbol(52.3)
306+
),
307+
new PowOperation(
308+
new DivOperation(
309+
new DivOperation(
310+
new BasicDoubleSymbol(32.0),
311+
new BasicDoubleSymbol(43.5)
312+
),
313+
new SubOperation(
314+
new BasicDoubleSymbol(37.4),
315+
new BasicDoubleSymbol(-11.4)
316+
)
317+
),
318+
new PowOperation(
319+
new BasicDoubleSymbol(12.2),
320+
new BasicDoubleSymbol(33.0)
321+
)
322+
)
323+
),
324+
new MulOperation(
325+
new PowOperation(
326+
new BasicDoubleSymbol(-19.7),
327+
new DivOperation(
328+
new BasicDoubleSymbol(11.0),
329+
new BasicDoubleSymbol(67.9)
330+
)
331+
),
332+
new MulOperation(
333+
new DivOperation(
334+
new BasicDoubleSymbol(46.2),
335+
new BasicDoubleSymbol(97.6)
336+
),
337+
new BasicDoubleSymbol(-13.9)
338+
)
339+
)
340+
)
341+
);
342+
}
343+
165344
public static ObjStringPair getBigData1_minimumParens() {
166345
return new ObjStringPair(getBigData1_obj(), "(0.2 + 8.1) * (2.7 * -2.1 + 0.1) + (7.9 + -2.3 + (9.9 + 2.3 * -1.1 + 2.1))");
167346
}
@@ -175,4 +354,12 @@ public static ObjStringPair getBigData2_minimumParens() {
175354
public static ObjStringPair getBigData2_groupingParens() {
176355
return new ObjStringPair(getBigData2_obj(), "((68.0 + (93.5 * -85.5) / 41.1) - (42.3 + 66.8) * ((77.8 / 45.3 + -10.7 * 65.6) / (0.4 + (84.5 + -31.1 / 90.6)))) / ((-37.6 + 59.5 * ((-80.9 - -72.2) * (84.1 - -68.0 / -67.8))) * ((((-96.2 + -1.2) / (2.6 + 36.7)) / 40.6) * -57.4))");
177356
}
357+
358+
public static ObjStringPair getBigData3Pow_minimumParens() {
359+
return new ObjStringPair(getBigData3Pow_obj(), "((-19.7 - 17.1) * (-99.6 * -49.2) * (43.6 ** (-29.1 + -68.3) - (40.6 - 58.4) / ((86.8 + 71.8) * 51.8)) / (72.4 * (-91.1 - -3.2)) - ((-27.1 ** -28.2 / -99.8 / -54.3) ** -50.2 / (-69.8 - 64.5 + 63.9 ** -92.9 + (-24.9 - (58.1 + 13.3))) + (78.0 + -34.5))) ** (12.5 - 52.3 + (32.0 / 43.5 / (37.4 - -11.4)) ** 12.2 ** 33.0) ** (-19.7 ** (11.0 / 67.9) * (46.2 / 97.6 * -13.9))");
360+
}
361+
362+
public static ObjStringPair getBigData3Pow_groupingParens() {
363+
return new ObjStringPair(getBigData3Pow_obj(), "(((((-19.7 - 17.1) * (-99.6 * -49.2)) * ((43.6 ** (-29.1 + -68.3)) - ((40.6 - 58.4) / ((86.8 + 71.8) * 51.8)))) / (72.4 * (-91.1 - -3.2))) - ((((((-27.1 ** -28.2) / -99.8) / -54.3) ** -50.2) / (((-69.8 - 64.5) + (63.9 ** -92.9)) + (-24.9 - (58.1 + 13.3)))) + (78.0 + -34.5))) ** (((12.5 - 52.3) + (((32.0 / 43.5) / (37.4 - -11.4)) ** (12.2 ** 33.0))) ** ((-19.7 ** (11.0 / 67.9)) * ((46.2 / 97.6) * -13.9)))");
364+
}
178365
}

src/test/java/net/marcellperger/mathexpr/MathSymbolTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ void test_fmt__pow() {
2626
"(0.2 ** 5.5) ** 3.3"));
2727
assertExprFmt(new ObjStringPair(new PowOperation(new BasicDoubleSymbol(0.2), new PowOperation(new BasicDoubleSymbol(5.5), new BasicDoubleSymbol(3.3))),
2828
"0.2 ** 5.5 ** 3.3"));
29+
assertExprFmt(CommonData.getBigData3Pow_minimumParens());
2930
}
3031

3132
@Test

src/test/java/net/marcellperger/mathexpr/parser/ParserTest.java

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -135,16 +135,54 @@ void parsePrecedenceLevel_pow() {
135135
new PowOperation(new BasicDoubleSymbol(1.2), new PowOperation(new BasicDoubleSymbol(9.1), new BasicDoubleSymbol(.3))));
136136
assertInfixParsesTo("1.2**9.1+.3", ADD_PREC,
137137
new AddOperation(new PowOperation(new BasicDoubleSymbol(1.2), new BasicDoubleSymbol(9.1)), new BasicDoubleSymbol(.3)));
138+
assertInfixParsesTo(CommonData.getBigData3Pow_minimumParens(), ADD_PREC);
139+
assertInfixParsesTo(CommonData.getBigData3Pow_groupingParens(), ADD_PREC);
140+
}
141+
142+
@Test
143+
void parse_pow() {
144+
assertParsesTo("1.2**9.1",
145+
new PowOperation(new BasicDoubleSymbol(1.2), new BasicDoubleSymbol(9.1)));
146+
assertParsesTo("1.2**9.1**.3",
147+
new PowOperation(new BasicDoubleSymbol(1.2), new PowOperation(new BasicDoubleSymbol(9.1), new BasicDoubleSymbol(.3))));
148+
assertParsesTo("1.2**9.1+.3",
149+
new AddOperation(new PowOperation(new BasicDoubleSymbol(1.2), new BasicDoubleSymbol(9.1)), new BasicDoubleSymbol(.3)));
150+
assertParsesTo(CommonData.getBigData3Pow_minimumParens());
151+
assertParsesTo(CommonData.getBigData3Pow_groupingParens());
152+
}
153+
154+
@Test // TODO can we do parametrized tests?
155+
void parsePrecedenceLevel_pow_nocache() {
156+
try(var ignored = new WithNocache(this)) {
157+
parsePrecedenceLevel_pow();
158+
}
159+
}
160+
161+
@Test // TODO can we do parametrized tests?
162+
void parse_pow_nocache() {
163+
try(var ignored = new WithNocache(this)) {
164+
parse_pow();
165+
}
138166
}
139167

140168
@Test
141169
void parsePrecedenceLevel_nocache() {
142-
boolean origNocache = nocache;
143-
nocache = true;
144-
try {
170+
try(var ignored = new WithNocache(this)) {
145171
parsePrecedenceLevel();
146-
} finally {
147-
nocache = origNocache;
172+
}
173+
}
174+
175+
static class WithNocache implements AutoCloseable {
176+
boolean origNocache;
177+
ParserTest inst;
178+
179+
WithNocache(ParserTest inst_) {
180+
inst = inst_;
181+
origNocache = inst.nocache;
182+
}
183+
184+
public void close() {
185+
inst.nocache = origNocache;
148186
}
149187
}
150188

0 commit comments

Comments
 (0)