Skip to content

Commit b2b16bf

Browse files
Refactor tests
1 parent 7ac3166 commit b2b16bf

File tree

5 files changed

+26
-194
lines changed

5 files changed

+26
-194
lines changed

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

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

165-
public static Pair<MathSymbol, String> getBigData1_minimumParens() {
166-
return new Pair<>(getBigData1_obj(), "(0.2 + 8.1) * (2.7 * -2.1 + 0.1) + (7.9 + -2.3 + (9.9 + 2.3 * -1.1 + 2.1))");
165+
public static ObjStringPair getBigData1_minimumParens() {
166+
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))");
167167
}
168-
public static Pair<MathSymbol, String> getBigData1_groupingParens() {
169-
return new Pair<>(getBigData1_obj(), "(0.2 + 8.1) * (2.7 * -2.1 + 0.1) + ((7.9 + -2.3) + ((9.9 + 2.3 * -1.1) + 2.1))");
168+
public static ObjStringPair getBigData1_groupingParens() {
169+
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))");
170170
}
171171

172-
public static Pair<MathSymbol, String> getBigData2_minimumParens() {
173-
return new Pair<>(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))");
172+
public static ObjStringPair getBigData2_minimumParens() {
173+
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))");
174174
}
175-
public static Pair<MathSymbol, String> getBigData2_groupingParens() {
176-
return new Pair<>(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))");
175+
public static ObjStringPair getBigData2_groupingParens() {
176+
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))");
177177
}
178178
}

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

Lines changed: 10 additions & 178 deletions
Original file line numberDiff line numberDiff line change
@@ -5,190 +5,22 @@
55
import static org.junit.jupiter.api.Assertions.*;
66

77
class MathSymbolTest {
8-
@Test
9-
void test_fmt() {
10-
MathSymbol sym = getBigExpression();
11-
assertEquals("(0.2 + 8.1) * (2.7 * -2.1 + 0.1) + (7.9 + -2.3 + (9.9 + 2.3 * -1.1 + 2.1))", sym.fmt());
8+
void assertExprFmt(ObjStringPair testcase) {
9+
assertEquals(/*expected*/testcase.str(), testcase.obj().fmt());
1210
}
13-
14-
@Test
15-
void test_calculateValue() {
16-
MathSymbol sym = getBigExpression();
17-
assertEquals(-31.161, sym.calculateValue());
11+
void assertExprValue(double expected, ObjStringPair testcase) {
12+
assertEquals(expected, testcase.obj().calculateValue());
1813
}
1914

2015
@Test
21-
void test_fmtBigMixed2() {
22-
MathSymbol sym = getBigMixedExpr2();
23-
assertEquals("(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))",
24-
sym.fmt());
16+
void test_fmt() {
17+
assertExprFmt(CommonData.getBigData1_minimumParens());
18+
assertExprFmt(CommonData.getBigData2_minimumParens());
2519
}
2620

2721
@Test
28-
void test_calculateValueBigMixed2() {
29-
MathSymbol sym = getBigMixedExpr2();
30-
assertEquals(-0.0051502812181513195, sym.calculateValue());
31-
}
32-
33-
MathSymbol getBigExpression() {
34-
return new AddOperation(
35-
new MulOperation(
36-
new AddOperation(
37-
new BasicDoubleSymbol(0.2),
38-
new BasicDoubleSymbol(8.1)),
39-
new AddOperation(
40-
new MulOperation(
41-
new BasicDoubleSymbol(2.7),
42-
new BasicDoubleSymbol(-2.1)
43-
),
44-
new BasicDoubleSymbol(0.1)
45-
)
46-
),
47-
new AddOperation(
48-
new AddOperation(
49-
new BasicDoubleSymbol(7.9),
50-
new BasicDoubleSymbol(-2.3)),
51-
new AddOperation(
52-
new AddOperation(
53-
new BasicDoubleSymbol(9.9),
54-
new MulOperation(
55-
new BasicDoubleSymbol(2.3),
56-
new BasicDoubleSymbol(-1.1))),
57-
new BasicDoubleSymbol(2.1)
58-
)
59-
)
60-
);
61-
}
62-
63-
64-
/**
65-
* Tests involving this are more like snapshot tests - this just returns this randomly generated MathSymbol
66-
* (small python script). You could call this a form of fuzzing.
67-
* <pre>{@code
68-
* import random
69-
*
70-
* OPTIONS = [s + 'Operation' for s in ('Add', 'Sub', 'Mul', 'Div')]
71-
*
72-
*
73-
* def happens(prob: float):
74-
* return random.random() < prob
75-
*
76-
*
77-
* def gen_random_double_sym(curr_depth: int, is_arg1: bool):
78-
* indent = ' ' * curr_depth * 4
79-
* return (f'{indent}new BasicDoubleSymbol('
80-
* f'{random.randint(-999, 999) / 10:.1f})'
81-
* + (',' if is_arg1 else ''))
82-
*
83-
*
84-
* def get_leaf_prob(min_depth: int, max_depth: int, depth: int):
85-
* if depth >= max_depth:
86-
* return 1
87-
* if depth < min_depth:
88-
* return 0
89-
* w = max_depth - min_depth
90-
* o = depth - min_depth
91-
* return o / w
92-
*
93-
*
94-
* def gen_random(min_depth: int, max_depth: int,
95-
* curr_depth: int = 0, is_arg1=False) -> str:
96-
* indent = ' ' * curr_depth * 4
97-
* if happens(get_leaf_prob(min_depth, max_depth, curr_depth)):
98-
* return gen_random_double_sym(curr_depth, is_arg1)
99-
* name = random.choice(OPTIONS)
100-
* arg1 = gen_random(min_depth, max_depth, curr_depth + 1, True)
101-
* arg2 = gen_random(min_depth, max_depth, curr_depth + 1, False)
102-
* result = (f'{indent}new {name}(\n'
103-
* f'{arg1}\n'
104-
* f'{arg2}\n'
105-
* f'{indent})' + (',' if is_arg1 else ''))
106-
* return result
107-
*
108-
*
109-
* if __name__ == '__main__':
110-
* print(gen_random(2, 7))
111-
* }</pre>
112-
*/
113-
MathSymbol getBigMixedExpr2() {
114-
return new DivOperation(
115-
new SubOperation(
116-
new AddOperation(
117-
new BasicDoubleSymbol(68.0),
118-
new DivOperation(
119-
new MulOperation(
120-
new BasicDoubleSymbol(93.5),
121-
new BasicDoubleSymbol(-85.5)
122-
),
123-
new BasicDoubleSymbol(41.1)
124-
)
125-
),
126-
new MulOperation(
127-
new AddOperation(
128-
new BasicDoubleSymbol(42.3),
129-
new BasicDoubleSymbol(66.8)
130-
),
131-
new DivOperation(
132-
new AddOperation(
133-
new DivOperation(
134-
new BasicDoubleSymbol(77.8),
135-
new BasicDoubleSymbol(45.3)
136-
),
137-
new MulOperation(
138-
new BasicDoubleSymbol(-10.7),
139-
new BasicDoubleSymbol(65.6)
140-
)
141-
),
142-
new AddOperation(
143-
new BasicDoubleSymbol(0.4),
144-
new AddOperation(
145-
new BasicDoubleSymbol(84.5),
146-
new DivOperation(
147-
new BasicDoubleSymbol(-31.1),
148-
new BasicDoubleSymbol(90.6)
149-
)
150-
)
151-
)
152-
)
153-
)
154-
),
155-
new MulOperation(
156-
new AddOperation(
157-
new BasicDoubleSymbol(-37.6),
158-
new MulOperation(
159-
new BasicDoubleSymbol(59.5),
160-
new MulOperation(
161-
new SubOperation(
162-
new BasicDoubleSymbol(-80.9),
163-
new BasicDoubleSymbol(-72.2)
164-
),
165-
new SubOperation(
166-
new BasicDoubleSymbol(84.1),
167-
new DivOperation(
168-
new BasicDoubleSymbol(-68.0),
169-
new BasicDoubleSymbol(-67.8)
170-
)
171-
)
172-
)
173-
)
174-
),
175-
new MulOperation(
176-
new DivOperation(
177-
new DivOperation(
178-
new AddOperation(
179-
new BasicDoubleSymbol(-96.2),
180-
new BasicDoubleSymbol(-1.2)
181-
),
182-
new AddOperation(
183-
new BasicDoubleSymbol(2.6),
184-
new BasicDoubleSymbol(36.7)
185-
)
186-
),
187-
new BasicDoubleSymbol(40.6)
188-
),
189-
new BasicDoubleSymbol(-57.4)
190-
)
191-
)
192-
);
22+
void test_calculateValue() {
23+
assertExprValue(-31.161, CommonData.getBigData1_minimumParens());
24+
assertExprValue(-0.0051502812181513195, CommonData.getBigData2_minimumParens());
19325
}
19426
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
package net.marcellperger.mathexpr;
2+
3+
public record ObjStringPair(MathSymbol obj, String str) {
4+
}

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

Lines changed: 0 additions & 4 deletions
This file was deleted.

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ else try (WithSuppressingCache ignored = WithSuppressingCache.start()) {
2525
}
2626
}
2727

28-
void assertInfixParsesTo(Pair<MathSymbol, String> exprPair, int level) {
29-
assertInfixParsesTo(exprPair.right(), level, exprPair.left());
28+
void assertInfixParsesTo(ObjStringPair exprPair, int level) {
29+
assertInfixParsesTo(exprPair.str(), level, exprPair.obj());
3030
}
3131

3232
void assertParsesToInner(String src, MathSymbol expected) {
@@ -45,8 +45,8 @@ else try (WithSuppressingCache ignored = WithSuppressingCache.start()) {
4545
}
4646
}
4747

48-
void assertParsesTo(Pair<MathSymbol, String> exprPair) {
49-
assertParsesTo(exprPair.right(), exprPair.left());
48+
void assertParsesTo(ObjStringPair exprPair) {
49+
assertParsesTo(exprPair.str(), exprPair.obj());
5050
}
5151

5252
void assertInfixParsesToInner(String src, int level, MathSymbol expected) {

0 commit comments

Comments
 (0)